-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid_to_bitmap_map.go
159 lines (131 loc) · 3.41 KB
/
id_to_bitmap_map.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package dm
import (
"git.100tal.com/wangxiao_xesbiz_operation/gently-utils/container/dm/croaring"
)
type IToBMap map[uint16]map[uint16]croaring.Bitmap
type IdToBitmap struct {
lenFields int
fields []IToBMap
stats IToBStats
}
func NewIdToBitmap(invertNum ...int) *IdToBitmap {
num := 0
if len(invertNum) > 0 {
num = invertNum[0]
}
itb := &IdToBitmap{
lenFields: num,
fields: make([]IToBMap, num, num),
}
for index := range itb.fields {
itb.fields[index] = make(IToBMap)
}
return itb
}
// AddToBitmaps 添加bitmap列表是直接将id添加到对应bitmap当中
func (itb *IdToBitmap) AddToBitmaps(id uint32, bitmaps ...croaring.Bitmap) {
itb.add(id, bitmaps, func(bitmap croaring.Bitmap) {
bitmap.Add(id)
})
}
func (itb *IdToBitmap) Add(id uint32, bitmaps ...croaring.Bitmap) {
itb.add(id, bitmaps, nil)
}
func (itb *IdToBitmap) add(id uint32, bitmaps []croaring.Bitmap, cb func(bitmap croaring.Bitmap)) {
h, l := itb.splitId(id)
if diff := len(bitmaps) - itb.lenFields; diff > 0 {
for i := 0; i < diff; i++ {
itb.lenFields++
itb.fields = append(itb.fields, make(IToBMap))
}
}
for index, bitmap := range bitmaps {
if cb != nil {
cb(bitmap)
}
m, has := itb.fields[index][h]
if !has {
m = make(map[uint16]croaring.Bitmap)
itb.fields[index][h] = m
}
m[l] = bitmap
itb.stats.Count++
}
}
// DelFromBitmaps 删除id时,直接从bitmap中将该id删除
func (itb *IdToBitmap) DelFromBitmaps(id uint32) {
itb.del(id, func(bitmap croaring.Bitmap) {
bitmap.Remove(id)
if bitmap.IsEmpty() {
itb.stats.Empty++
}
})
}
func (itb *IdToBitmap) Del(id uint32) {
itb.del(id, nil)
}
func (itb *IdToBitmap) del(id uint32, cb func(bitmap croaring.Bitmap)) {
h, l := itb.splitId(id)
for _, field := range itb.fields {
m, hasH := field[h]
if !hasH {
break
}
bitmap, hasL := m[l]
if !hasL {
break
}
if cb != nil {
cb(bitmap)
}
delete(m, l)
if len(m) <= 0 {
delete(field, h)
}
itb.stats.Count--
}
}
func (itb *IdToBitmap) Replace(id uint32, bitmaps ...croaring.Bitmap) {
itb.Del(id)
itb.Add(id, bitmaps...)
}
func (itb *IdToBitmap) ToArray(id uint32) (bitmaps []croaring.Bitmap) {
itb.Iterate(id, func(bitmap croaring.Bitmap) bool {
bitmaps = append(bitmaps, bitmap)
return true
})
return
}
func (itb *IdToBitmap) Iterate(id uint32, cb func(bitmap croaring.Bitmap) bool) {
h, l := itb.splitId(id)
for _, field := range itb.fields {
m, hasH := field[h]
if !hasH {
break
}
bitmap, hasL := m[l]
if !hasL {
break
}
if !cb(bitmap) {
break
}
}
}
func (itb *IdToBitmap) splitId(id uint32) (h uint16, l uint16) {
h = uint16((id >> 16) & 0xFFFF)
l = uint16(id & 0xFFFF)
return
}
func (itb *IdToBitmap) GetEmptyNum() uint64 {
return itb.stats.Empty
}
func (itb *IdToBitmap) GetBitmapNum() uint64 {
return itb.stats.Count
}
func (itb *IdToBitmap) GetStats() IToBStats {
return itb.stats
}
func (itb *IdToBitmap) ResetEmptyNum() {
itb.stats.Empty = 0
}