-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathroaring64.go
More file actions
152 lines (123 loc) · 2.75 KB
/
roaring64.go
File metadata and controls
152 lines (123 loc) · 2.75 KB
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
package cardinality
import (
"github.com/RoaringBitmap/roaring/v2/roaring64"
)
type bitmap64Iterator struct {
iterator roaring64.IntPeekable64
}
func (s bitmap64Iterator) HasNext() bool {
return s.iterator.HasNext()
}
func (s bitmap64Iterator) Next() uint64 {
return s.iterator.Next()
}
type bitmap64 struct {
bitmap *roaring64.Bitmap
}
func NewBitmap64() Duplex[uint64] {
return bitmap64{
bitmap: roaring64.New(),
}
}
func NewBitmap64Provider() Provider[uint64] {
return NewBitmap64()
}
func NewBitmap64With(values ...uint64) Duplex[uint64] {
duplex := NewBitmap64()
duplex.Add(values...)
return duplex
}
func (s bitmap64) Clear() {
s.bitmap.Clear()
}
func (s bitmap64) Each(delegate func(nextValue uint64) bool) {
for itr := s.bitmap.Iterator(); itr.HasNext(); {
if ok := delegate(itr.Next()); !ok {
break
}
}
}
func (s bitmap64) Iterator() Iterator[uint64] {
return bitmap64Iterator{
iterator: s.bitmap.Iterator(),
}
}
func (s bitmap64) Slice() []uint64 {
return s.bitmap.ToArray()
}
func (s bitmap64) Contains(value uint64) bool {
return s.bitmap.Contains(value)
}
func (s bitmap64) CheckedAdd(value uint64) bool {
return s.bitmap.CheckedAdd(value)
}
func (s bitmap64) Add(values ...uint64) {
switch len(values) {
case 0:
case 1:
s.bitmap.Add(values[0])
default:
s.bitmap.AddMany(values)
}
}
func (s bitmap64) Remove(value uint64) {
s.bitmap.Remove(value)
}
func (s bitmap64) Xor(provider Provider[uint64]) {
switch typedProvider := provider.(type) {
case bitmap64:
s.bitmap.Xor(typedProvider.bitmap)
case Duplex[uint64]:
providerCopy := roaring64.New()
typedProvider.Each(func(value uint64) bool {
providerCopy.Add(value)
return true
})
s.bitmap.Xor(providerCopy)
}
}
func (s bitmap64) And(provider Provider[uint64]) {
switch typedProvider := provider.(type) {
case bitmap64:
s.bitmap.And(typedProvider.bitmap)
case Duplex[uint64]:
s.Each(func(nextValue uint64) bool {
if !typedProvider.Contains(nextValue) {
s.Remove(nextValue)
}
return true
})
}
}
func (s bitmap64) Or(provider Provider[uint64]) {
switch typedProvider := provider.(type) {
case bitmap64:
s.bitmap.Or(typedProvider.bitmap)
case Duplex[uint64]:
typedProvider.Each(func(nextValue uint64) bool {
s.Add(nextValue)
return true
})
}
}
func (s bitmap64) Cardinality() uint64 {
return s.bitmap.GetCardinality()
}
func (s bitmap64) Clone() Duplex[uint64] {
return bitmap64{
bitmap: s.bitmap.Clone(),
}
}
func (s bitmap64) AndNot(provider Provider[uint64]) {
switch typedProvider := provider.(type) {
case bitmap64:
s.bitmap.AndNot(typedProvider.bitmap)
case Duplex[uint64]:
s.Each(func(nextValue uint64) bool {
if typedProvider.Contains(nextValue) {
s.Remove(nextValue)
}
return true
})
}
}