-
Notifications
You must be signed in to change notification settings - Fork 834
/
bitarray.go
314 lines (259 loc) · 7.08 KB
/
bitarray.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
Copyright 2014 Workiva, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Package bitarray implements a bit array. Useful for tracking bool type values in a space
efficient way. This is *NOT* a threadsafe package.
*/
package bitarray
// bitArray is a struct that maintains state of a bit array.
type bitArray struct {
blocks []block
lowest uint64
highest uint64
anyset bool
}
func getIndexAndRemainder(k uint64) (uint64, uint64) {
return k / s, k % s
}
func (ba *bitArray) setLowest() {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
if ba.blocks[i] == 0 {
continue
}
pos := ba.blocks[i].findRightPosition()
ba.lowest = (i * s) + pos
ba.anyset = true
return
}
ba.anyset = false
ba.lowest = 0
ba.highest = 0
}
func (ba *bitArray) setHighest() {
for i := len(ba.blocks) - 1; i >= 0; i-- {
if ba.blocks[i] == 0 {
continue
}
pos := ba.blocks[i].findLeftPosition()
ba.highest = (uint64(i) * s) + pos
ba.anyset = true
return
}
ba.anyset = false
ba.highest = 0
ba.lowest = 0
}
// capacity returns the total capacity of the bit array.
func (ba *bitArray) Capacity() uint64 {
return uint64(len(ba.blocks)) * s
}
// ToNums converts this bitarray to a list of numbers contained within it.
func (ba *bitArray) ToNums() []uint64 {
nums := make([]uint64, 0, ba.highest-ba.lowest/4)
for i, block := range ba.blocks {
block.toNums(uint64(i)*s, &nums)
}
return nums
}
// SetBit sets a bit at the given index to true.
func (ba *bitArray) SetBit(k uint64) error {
if k >= ba.Capacity() {
return OutOfRangeError(k)
}
if !ba.anyset {
ba.lowest = k
ba.highest = k
ba.anyset = true
} else {
if k < ba.lowest {
ba.lowest = k
} else if k > ba.highest {
ba.highest = k
}
}
i, pos := getIndexAndRemainder(k)
ba.blocks[i] = ba.blocks[i].insert(pos)
return nil
}
// GetBit returns a bool indicating if the value at the given
// index has been set.
func (ba *bitArray) GetBit(k uint64) (bool, error) {
if k >= ba.Capacity() {
return false, OutOfRangeError(k)
}
i, pos := getIndexAndRemainder(k)
result := ba.blocks[i]&block(1<<pos) != 0
return result, nil
}
//ClearBit will unset a bit at the given index if it is set.
func (ba *bitArray) ClearBit(k uint64) error {
if k >= ba.Capacity() {
return OutOfRangeError(k)
}
if !ba.anyset { // nothing is set, might as well bail
return nil
}
i, pos := getIndexAndRemainder(k)
ba.blocks[i] &^= block(1 << pos)
if k == ba.highest {
ba.setHighest()
} else if k == ba.lowest {
ba.setLowest()
}
return nil
}
// Or will bitwise or two bit arrays and return a new bit array
// representing the result.
func (ba *bitArray) Or(other BitArray) BitArray {
if dba, ok := other.(*bitArray); ok {
return orDenseWithDenseBitArray(ba, dba)
}
return orSparseWithDenseBitArray(other.(*sparseBitArray), ba)
}
// And will bitwise and two bit arrays and return a new bit array
// representing the result.
func (ba *bitArray) And(other BitArray) BitArray {
if dba, ok := other.(*bitArray); ok {
return andDenseWithDenseBitArray(ba, dba)
}
return andSparseWithDenseBitArray(other.(*sparseBitArray), ba)
}
// Nand will return the result of doing a bitwise and not of the bit array
// with the other bit array on each block.
func (ba *bitArray) Nand(other BitArray) BitArray {
if dba, ok := other.(*bitArray); ok {
return nandDenseWithDenseBitArray(ba, dba)
}
return nandDenseWithSparseBitArray(ba, other.(*sparseBitArray))
}
// Reset clears out the bit array.
func (ba *bitArray) Reset() {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
ba.blocks[i] &= block(0)
}
ba.anyset = false
}
// Equals returns a bool indicating if these two bit arrays are equal.
func (ba *bitArray) Equals(other BitArray) bool {
if other.Capacity() == 0 && ba.highest > 0 {
return false
}
if other.Capacity() == 0 && !ba.anyset {
return true
}
var selfIndex uint64
for iter := other.Blocks(); iter.Next(); {
toIndex, otherBlock := iter.Value()
if toIndex > selfIndex {
for i := selfIndex; i < toIndex; i++ {
if ba.blocks[i] > 0 {
return false
}
}
}
selfIndex = toIndex
if !ba.blocks[selfIndex].equals(otherBlock) {
return false
}
selfIndex++
}
lastIndex, _ := getIndexAndRemainder(ba.highest)
if lastIndex >= selfIndex {
return false
}
return true
}
// Intersects returns a bool indicating if the supplied bitarray intersects
// this bitarray. This will check for intersection up to the length of the supplied
// bitarray. If the supplied bitarray is longer than this bitarray, this
// function returns false.
func (ba *bitArray) Intersects(other BitArray) bool {
if other.Capacity() > ba.Capacity() {
return false
}
if sba, ok := other.(*sparseBitArray); ok {
return ba.intersectsSparseBitArray(sba)
}
return ba.intersectsDenseBitArray(other.(*bitArray))
}
// Blocks will return an iterator over this bit array.
func (ba *bitArray) Blocks() Iterator {
return newBitArrayIterator(ba)
}
func (ba *bitArray) IsEmpty() bool {
return !ba.anyset
}
// complement flips all bits in this array.
func (ba *bitArray) complement() {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
ba.blocks[i] = ^ba.blocks[i]
}
ba.setLowest()
if ba.anyset {
ba.setHighest()
}
}
func (ba *bitArray) intersectsSparseBitArray(other *sparseBitArray) bool {
for i, index := range other.indices {
if !ba.blocks[index].intersects(other.blocks[i]) {
return false
}
}
return true
}
func (ba *bitArray) intersectsDenseBitArray(other *bitArray) bool {
for i, block := range other.blocks {
if !ba.blocks[i].intersects(block) {
return false
}
}
return true
}
func (ba *bitArray) copy() BitArray {
blocks := make(blocks, len(ba.blocks))
copy(blocks, ba.blocks)
return &bitArray{
blocks: blocks,
lowest: ba.lowest,
highest: ba.highest,
anyset: ba.anyset,
}
}
// newBitArray returns a new dense BitArray at the specified size. This is a
// separate private constructor so unit tests don't have to constantly cast the
// BitArray interface to the concrete type.
func newBitArray(size uint64, args ...bool) *bitArray {
i, r := getIndexAndRemainder(size)
if r > 0 {
i++
}
ba := &bitArray{
blocks: make([]block, i),
anyset: false,
}
if len(args) > 0 && args[0] == true {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
ba.blocks[i] = maximumBlock
}
ba.lowest = 0
ba.highest = i*s - 1
ba.anyset = true
}
return ba
}
// NewBitArray returns a new BitArray at the specified size. The
// optional arg denotes whether this bitarray should be set to the
// bitwise complement of the empty array, ie. sets all bits.
func NewBitArray(size uint64, args ...bool) BitArray {
return newBitArray(size, args...)
}