-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseDict.go
108 lines (98 loc) · 1.92 KB
/
BaseDict.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package util
//为map[int]int对像绑定的一些方法
type BaseDcit struct {
Data map[int]int
}
//Clone 复制一个BaseData
func (this BaseDcit) Clone() *BaseDcit {
result := new(BaseDcit)
result.Data = make(map[int]int)
for k, v := range this.Data {
result.Data[k] = v
}
return result
}
//NewBaseDictString 用字符串初始化一个数据
func NewBaseDictString(str *String) *BaseDcit {
result := new(BaseDcit)
result.Data = make(map[int]int)
if str == nil {
return result
}
str = str.Replace("|", ";")
d := str.ToString()
if d == "" || d == "0" {
return result
}
arr := StringToIntArray(d, ";")
for i := 0; i < len(arr); i += 2 {
result.Data[arr[i]] = arr[i+1]
}
return result
}
//UpData 更新指定数据
func (this *BaseDcit) UpData(key, num int) {
v, _ := this.Data[key]
if num+v > 0 {
this.Data[key] = v + num
} else {
this.Data[key] = v + num
}
}
//UpDataBc批量用别的数据,更新本数据
func (this *BaseDcit) UpDataBc(addbc, delbc *BaseDcit) {
if delbc != nil {
for k, n := range delbc.Data {
this.UpData(k, -n)
}
}
if addbc != nil {
for k, n := range addbc.Data {
this.UpData(k, n)
}
}
}
//GetNumByKey指定数据的值
func (this *BaseDcit) GetNumByKey(key int) int {
v, ok := this.Data[key]
if !ok {
return 0
}
return v
}
//ToString 字符串化
func (this *BaseDcit) ToString() string {
sb := NewStringBuilder()
t := 0
for k, v := range this.Data {
if t == 0 {
t++
} else {
sb.Append(";")
}
sb.AppendInt(k)
sb.Append(";")
sb.AppendInt(v)
}
return sb.ToString()
}
//Count 总数量
func (this *BaseDcit) Count() (result int) {
for _, n := range this.Data {
result += n
}
return result
}
//MaxItem 最大数值的KEY,value
func (this *BaseDcit) MaxItem() (key, num int) {
for k, n := range this.Data {
if n > num {
key, num = k, n
}
}
return key, num
}
//Clear清数据
func (this *BaseDcit) Clear() {
this.Data = make(map[int]int)
}