-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathids.go
132 lines (105 loc) · 2.5 KB
/
ids.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
package dm
import (
"sync"
"github.com/RoaringBitmap/roaring"
"git.100tal.com/wangxiao_xesbiz_operation/gently-utils/convert"
)
// IdsManager 维护roaring.Bitmap的并发安全
type IdsManager struct {
lock sync.RWMutex
ids *roaring.Bitmap
}
func NewIdsManager() *IdsManager {
m := &IdsManager {
ids: roaring.NewBitmap(),
}
return m
}
func (receiver *IdsManager) IsEmpty() bool {
if receiver.ids == nil {
return false
}
receiver.lock.RLock()
defer receiver.lock.RUnlock()
return receiver.ids.IsEmpty()
}
func (receiver *IdsManager) Clear() {
if receiver.ids == nil {
return
}
receiver.lock.Lock()
defer receiver.lock.Unlock()
receiver.ids.Clear()
}
func (receiver *IdsManager) Add(x uint32) {
if receiver.ids == nil {
return
}
receiver.lock.Lock()
defer receiver.lock.Unlock()
receiver.ids.Add(x)
}
func (receiver *IdsManager) AddMany(dat []uint32) {
if receiver.ids == nil {
return
}
receiver.lock.Lock()
defer receiver.lock.Unlock()
receiver.ids.AddMany(dat)
}
func (receiver *IdsManager) AddX(x interface{}) {
receiver.Add(convert.Uint32(x))
}
func (receiver *IdsManager) AddInt(x int) {
receiver.Add(uint32(x))
}
func (receiver *IdsManager) AddManyInt(dat []int) {
if len(dat) == 0 {
return
}
dat32 := make([]uint32, 0, len(dat))
for _, d := range dat {
dat32 = append(dat32, uint32(d))
}
receiver.AddMany(dat32)
}
func (receiver *IdsManager) Remove(x uint32) {
if receiver.ids == nil {
return
}
receiver.lock.Lock()
defer receiver.lock.Unlock()
receiver.ids.Remove(x)
}
func (receiver *IdsManager) Contains(x uint32) bool {
if receiver.ids == nil {
return false
}
receiver.lock.RLock()
defer receiver.lock.RUnlock()
return receiver.ids.Contains(x)
}
func (receiver *IdsManager) ContainsInt(x int) bool {
return receiver.Contains(uint32(x))
}
func (receiver *IdsManager) ToArray() []uint32 {
if receiver.ids == nil {
return nil
}
receiver.lock.RLock()
defer receiver.lock.RUnlock()
return receiver.ids.ToArray()
}
func (receiver *IdsManager) ToArrayInt() []int {
if receiver.ids == nil {
return nil
}
receiver.lock.RLock()
defer receiver.lock.RUnlock()
ids := receiver.ids.ToArray()
nids := make([]int, 0, len(ids))
for _, id := range ids {
nids = append(nids, int(id))
}
return nids
}