forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roaringarray.go
453 lines (386 loc) · 10.8 KB
/
roaringarray.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package roaring
import (
"encoding/binary"
"io"
)
type container interface {
clone() container
and(container) container
iand(container) container // i stands for inplace
andNot(container) container
iandNot(container) container // i stands for inplace
getCardinality() int
rank(uint16) int
add(uint16) container
//addRange(start, final int) container // range is [firstOfRange,lastOfRange) (unused)
iaddRange(start, final int) container // i stands for inplace, range is [firstOfRange,lastOfRange)
remove(uint16) container
not(start, final int) container // range is [firstOfRange,lastOfRange)
inot(firstOfRange, lastOfRange int) container // i stands for inplace, range is [firstOfRange,lastOfRange)
xor(r container) container
getShortIterator() shortIterable
contains(i uint16) bool
equals(i interface{}) bool
fillLeastSignificant16bits(array []uint32, i int, mask uint32)
or(r container) container
ior(r container) container // i stands for inplace
intersects(r container) bool // whether the two containers intersect
lazyOR(r container) container
lazyIOR(r container) container
getSizeInBytes() int
//removeRange(start, final int) container // range is [firstOfRange,lastOfRange) (unused)
iremoveRange(start, final int) container // i stands for inplace, range is [firstOfRange,lastOfRange)
selectInt(uint16) int
serializedSizeInBytes() int
readFrom(io.Reader) (int, error)
writeTo(io.Writer) (int, error)
}
// careful: range is [firstOfRange,lastOfRange]
func rangeOfOnes(start, last int) container {
if (last - start + 1) > arrayDefaultMaxSize {
return newBitmapContainerwithRange(start, last)
}
return newArrayContainerRange(start, last)
}
type roaringArray struct {
keys []uint16
containers []container
needCopyOnWrite []bool
}
func newRoaringArray() *roaringArray {
ra := &roaringArray{}
ra.clear()
return ra
}
func (ra *roaringArray) appendContainer(key uint16, value container, mustCopyOnWrite bool) {
ra.keys = append(ra.keys, key)
ra.containers = append(ra.containers, value)
ra.needCopyOnWrite = append(ra.needCopyOnWrite, mustCopyOnWrite)
}
func (ra *roaringArray) appendWithoutCopy(sa roaringArray, startingindex int) {
ra.appendContainer(sa.keys[startingindex], sa.containers[startingindex], false)
}
func (ra *roaringArray) appendCopy(sa roaringArray, startingindex int) {
ra.appendContainer(sa.keys[startingindex], sa.containers[startingindex], true)
sa.setNeedsCopyOnWrite(startingindex)
}
func (ra *roaringArray) appendWithoutCopyMany(sa roaringArray, startingindex, end int) {
for i := startingindex; i < end; i++ {
ra.appendWithoutCopy(sa, i)
}
}
func (ra *roaringArray) appendCopyMany(sa roaringArray, startingindex, end int) {
for i := startingindex; i < end; i++ {
ra.appendCopy(sa, i)
}
}
func (ra *roaringArray) appendCopiesUntil(sa roaringArray, stoppingKey uint16) {
for i := 0; i < sa.size(); i++ {
if sa.keys[i] >= stoppingKey {
break
}
ra.appendContainer(sa.keys[i], sa.containers[i], true)
sa.setNeedsCopyOnWrite(i)
}
}
func (ra *roaringArray) appendCopiesAfter(sa roaringArray, beforeStart uint16) {
startLocation := sa.getIndex(beforeStart)
if startLocation >= 0 {
startLocation++
} else {
startLocation = -startLocation - 1
}
for i := startLocation; i < sa.size(); i++ {
ra.appendContainer(sa.keys[i], sa.containers[i], true)
sa.setNeedsCopyOnWrite(i)
}
}
func (ra *roaringArray) removeIndexRange(begin, end int) {
if end <= begin {
return
}
r := end - begin
copy(ra.keys[begin:], ra.keys[end:])
copy(ra.containers[begin:], ra.containers[end:])
copy(ra.needCopyOnWrite[begin:], ra.needCopyOnWrite[end:])
ra.resize(len(ra.keys) - r)
}
func (ra *roaringArray) resize(newsize int) {
for k := newsize; k < len(ra.containers); k++ {
ra.containers[k] = nil
}
ra.keys = ra.keys[:newsize]
ra.containers = ra.containers[:newsize]
ra.needCopyOnWrite = ra.needCopyOnWrite[:newsize]
}
func (ra *roaringArray) clear() {
ra.keys = make([]uint16, 0)
ra.containers = make([]container, 0)
ra.needCopyOnWrite = make([]bool, 0)
}
func (ra *roaringArray) clone() *roaringArray {
sa := new(roaringArray)
sa.keys = make([]uint16, len(ra.keys))
sa.containers = make([]container, len(ra.containers))
sa.needCopyOnWrite = make([]bool, len(ra.keys))
copy(sa.keys, ra.keys)
copy(sa.containers, ra.containers)
sa.markAllAsNeedingCopyOnWrite()
ra.markAllAsNeedingCopyOnWrite()
return sa
}
func (ra *roaringArray) containsKey(x uint16) bool {
return (ra.binarySearch(0, len(ra.keys), x) >= 0)
}
func (ra *roaringArray) getContainer(x uint16) container {
i := ra.binarySearch(0, len(ra.keys), x)
if i < 0 {
return nil
}
return ra.containers[i]
}
func (ra *roaringArray) getWritableContainerContainer(x uint16) container {
i := ra.binarySearch(0, len(ra.keys), x)
if i < 0 {
return nil
}
if ra.needCopyOnWrite[i] {
ra.containers[i] = ra.containers[i].clone()
ra.needCopyOnWrite[i] = false
}
return ra.containers[i]
}
func (ra *roaringArray) getContainerAtIndex(i int) container {
return ra.containers[i]
}
func (ra *roaringArray) getWritableContainerAtIndex(i int) container {
if ra.needCopyOnWrite[i] {
ra.containers[i] = ra.containers[i].clone()
ra.needCopyOnWrite[i] = false
}
return ra.containers[i]
}
func (ra *roaringArray) getIndex(x uint16) int {
// before the binary search, we optimize for frequent cases
size := len(ra.keys)
if (size == 0) || (ra.keys[size-1] == x) {
return size - 1
}
return ra.binarySearch(0, size, x)
}
func (ra *roaringArray) getKeyAtIndex(i int) uint16 {
return ra.keys[i]
}
func (ra *roaringArray) insertNewKeyValueAt(i int, key uint16, value container) {
ra.keys = append(ra.keys, 0)
ra.containers = append(ra.containers, nil)
copy(ra.keys[i+1:], ra.keys[i:])
copy(ra.containers[i+1:], ra.containers[i:])
ra.keys[i] = key
ra.containers[i] = value
ra.needCopyOnWrite = append(ra.needCopyOnWrite, false)
copy(ra.needCopyOnWrite[i+1:], ra.needCopyOnWrite[i:])
ra.needCopyOnWrite[i] = false
}
func (ra *roaringArray) remove(key uint16) bool {
i := ra.binarySearch(0, len(ra.keys), key)
if i >= 0 { // if a new key
ra.removeAtIndex(i)
return true
}
return false
}
func (ra *roaringArray) removeAtIndex(i int) {
copy(ra.keys[i:], ra.keys[i+1:])
copy(ra.containers[i:], ra.containers[i+1:])
copy(ra.needCopyOnWrite[i:], ra.needCopyOnWrite[i+1:])
ra.resize(len(ra.keys) - 1)
}
func (ra *roaringArray) setContainerAtIndex(i int, c container) {
ra.containers[i] = c
}
func (ra *roaringArray) replaceKeyAndContainerAtIndex(i int, key uint16, c container, mustCopyOnWrite bool) {
ra.keys[i] = key
ra.containers[i] = c
ra.needCopyOnWrite[i] = mustCopyOnWrite
}
func (ra *roaringArray) size() int {
return len(ra.keys)
}
func (ra *roaringArray) binarySearch(begin, end int, ikey uint16) int {
low := begin
high := end - 1
for low+16 <= high {
middleIndex := int(uint((low + high)) >> 1)
middleValue := ra.keys[middleIndex]
if middleValue < ikey {
low = middleIndex + 1
} else if middleValue > ikey {
high = middleIndex - 1
} else {
return middleIndex
}
}
for ; low <= high; low++ {
val := ra.keys[low]
if val >= ikey {
if val == ikey {
return low
}
break
}
}
return -(low + 1)
}
func (ra *roaringArray) equals(o interface{}) bool {
srb, ok := o.(roaringArray)
if ok {
if srb.size() != ra.size() {
return false
}
for i, k := range ra.keys {
if k != srb.keys[i] {
return false
}
}
for i, c := range ra.containers {
if !c.equals(srb.containers[i]) {
return false
}
}
return true
}
return false
}
func (ra *roaringArray) serializedSizeInBytes() uint64 {
count := uint64(4 + 4)
for _, c := range ra.containers {
count = count + 4 + 4
count = count + uint64(c.serializedSizeInBytes())
}
return count
}
func (ra *roaringArray) writeTo(stream io.Writer) (int64, error) {
preambleSize := 4 + 4 + 4*len(ra.keys)
buf := make([]byte, preambleSize+4*len(ra.keys))
binary.LittleEndian.PutUint32(buf[0:], uint32(serialCookie))
binary.LittleEndian.PutUint32(buf[4:], uint32(len(ra.keys)))
for i, key := range ra.keys {
off := 8 + i*4
binary.LittleEndian.PutUint16(buf[off:], uint16(key))
c := ra.containers[i]
binary.LittleEndian.PutUint16(buf[off+2:], uint16(c.getCardinality()-1))
}
startOffset := int64(preambleSize + 4*len(ra.keys))
for i, c := range ra.containers {
binary.LittleEndian.PutUint32(buf[preambleSize+i*4:], uint32(startOffset))
startOffset += int64(getSizeInBytesFromCardinality(c.getCardinality()))
}
_, err := stream.Write(buf)
if err != nil {
return 0, err
}
for _, c := range ra.containers {
_, err := c.writeTo(stream)
if err != nil {
return 0, err
}
}
return startOffset, nil
}
func (ra *roaringArray) readFrom(stream io.Reader) (int64, error) {
var cookie uint32
err := binary.Read(stream, binary.LittleEndian, &cookie)
if err != nil {
return 0, err
}
if cookie != serialCookie {
return 0, err
}
var size uint32
err = binary.Read(stream, binary.LittleEndian, &size)
if err != nil {
return 0, err
}
keycard := make([]uint16, 2*size, 2*size)
err = binary.Read(stream, binary.LittleEndian, keycard)
if err != nil {
return 0, err
}
offsets := make([]uint32, size, size)
err = binary.Read(stream, binary.LittleEndian, offsets)
if err != nil {
return 0, err
}
offset := int64(4 + 4 + 8*size)
for i := uint32(0); i < size; i++ {
c := int(keycard[2*i+1]) + 1
offset += int64(getSizeInBytesFromCardinality(c))
if c > arrayDefaultMaxSize {
nb := newBitmapContainer()
nb.readFrom(stream)
nb.cardinality = int(c)
ra.appendContainer(keycard[2*i], nb, false)
} else {
nb := newArrayContainerSize(int(c))
nb.readFrom(stream)
ra.appendContainer(keycard[2*i], nb, false)
}
}
return offset, nil
}
func (ra *roaringArray) advanceUntil(min uint16, pos int) int {
lower := pos + 1
if lower >= len(ra.keys) || ra.keys[lower] >= min {
return lower
}
spansize := 1
for lower+spansize < len(ra.keys) && ra.keys[lower+spansize] < min {
spansize *= 2
}
var upper int
if lower+spansize < len(ra.keys) {
upper = lower + spansize
} else {
upper = len(ra.keys) - 1
}
if ra.keys[upper] == min {
return upper
}
if ra.keys[upper] < min {
// means
// array
// has no
// item
// >= min
// pos = array.length;
return len(ra.keys)
}
// we know that the next-smallest span was too small
lower += (spansize / 2)
mid := 0
for lower+1 != upper {
mid = (lower + upper) / 2
if ra.keys[mid] == min {
return mid
} else if ra.keys[mid] < min {
lower = mid
} else {
upper = mid
}
}
return upper
}
func (ra *roaringArray) markAllAsNeedingCopyOnWrite() {
needCopyOnWrite := make([]bool, len(ra.keys))
for i := range needCopyOnWrite {
needCopyOnWrite[i] = true
}
ra.needCopyOnWrite = needCopyOnWrite
}
func (ra *roaringArray) needsCopyOnWrite(i int) bool {
return ra.needCopyOnWrite[i]
}
func (ra *roaringArray) setNeedsCopyOnWrite(i int) {
ra.needCopyOnWrite[i] = true
}