-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap.go
179 lines (151 loc) · 3.01 KB
/
bitmap.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Package bitmap provides a []bool/bitmap implementation with standardized
// iteration. Bitmaps are the equivalent of []bool, with improved compression
// for runs of similar values, and faster operations on ranges and the like.
package bitmap
import (
"math"
"github.com/RoaringBitmap/roaring"
)
const MaxInt = -1
// Bitmaps store the existence of values in [0,math.MaxUint32] more
// efficiently than []bool. The empty value starts with no bits set.
type Bitmap struct {
rb *roaring.Bitmap
}
// The number of set bits in the bitmap. Also known as cardinality.
func (me *Bitmap) Len() int {
if me.rb == nil {
return 0
}
return int(me.rb.GetCardinality())
}
func (me Bitmap) ToSortedSlice() (ret []int) {
if me.rb == nil {
return
}
for _, ui32 := range me.rb.ToArray() {
ret = append(ret, int(int32(ui32)))
}
return
}
func (me *Bitmap) lazyRB() *roaring.Bitmap {
if me.rb == nil {
me.rb = roaring.NewBitmap()
}
return me.rb
}
func (me *Bitmap) Iter(f func(interface{}) bool) {
me.IterTyped(func(i int) bool {
return f(i)
})
}
func (me Bitmap) IterTyped(f func(int) bool) bool {
if me.rb == nil {
return true
}
it := me.rb.Iterator()
for it.HasNext() {
if !f(int(it.Next())) {
return false
}
}
return true
}
func checkInt(i int) {
if i < math.MinInt32 || i > math.MaxInt32 {
panic("out of bounds")
}
}
func (me *Bitmap) Add(is ...int) {
rb := me.lazyRB()
for _, i := range is {
checkInt(i)
rb.AddInt(i)
}
}
func (me *Bitmap) AddRange(begin, end int) {
if begin >= end {
return
}
me.lazyRB().AddRange(uint32(begin), uint32(end))
}
func (me *Bitmap) Remove(i int) {
if me.rb == nil {
return
}
me.rb.Remove(uint32(i))
}
func (me *Bitmap) Union(other *Bitmap) {
me.lazyRB().Or(other.lazyRB())
}
func (me *Bitmap) Contains(i int) bool {
if me.rb == nil {
return false
}
return me.rb.Contains(uint32(i))
}
type Iter struct {
ii roaring.IntIterable
}
func (me *Iter) Next() bool {
if me == nil {
return false
}
return me.ii.HasNext()
}
func (me *Iter) Value() interface{} {
return me.ValueInt()
}
func (me *Iter) ValueInt() int {
return int(me.ii.Next())
}
func (me *Iter) Stop() {}
func Sub(left, right *Bitmap) *Bitmap {
return &Bitmap{
rb: roaring.AndNot(left.lazyRB(), right.lazyRB()),
}
}
func (me *Bitmap) Sub(other *Bitmap) {
if other.rb == nil {
return
}
if me.rb == nil {
return
}
me.rb.AndNot(other.rb)
}
func (me *Bitmap) Clear() {
if me.rb == nil {
return
}
me.rb.Clear()
}
func (me Bitmap) Copy() (ret Bitmap) {
ret = me
if ret.rb != nil {
ret.rb = ret.rb.Clone()
}
return
}
func (me *Bitmap) FlipRange(begin, end int) {
me.lazyRB().FlipInt(begin, end)
}
func (me *Bitmap) Get(bit int) bool {
return me.rb != nil && me.rb.ContainsInt(bit)
}
func (me *Bitmap) Set(bit int, value bool) {
if value {
me.lazyRB().AddInt(bit)
} else {
if me.rb != nil {
me.rb.Remove(uint32(bit))
}
}
}
func (me *Bitmap) RemoveRange(begin, end int) *Bitmap {
if me.rb == nil {
return me
}
me.rb.RemoveRange(uint32(begin), uint32(end))
return me
}