/
vec.go
327 lines (267 loc) · 6.65 KB
/
vec.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
// Package ranksel provides a bit vector
// that can answer rank and select queries.
package ranksel
import (
"bytes"
"encoding/gob"
"fmt"
"unsafe"
"github.com/robskie/bit"
)
// Options determines the size of the rank and
// select sampling block. Lower values translates
// to faster operations but results in larger size.
type Options struct {
// Sr is the rank sampling block size.
// This represents the number of bits in
// each rank sampling block. Default is 1024.
Sr int
// Ss is the select sampling block size.
// This represents the number of 1s in each
// select sampling block. Default is 8192.
Ss int
}
// NewOptions creates an Options
// object with default values.
func NewOptions() *Options {
return &Options{1024, 8192}
}
// BitVector is a bitmap with added data structure described by G. Navarro and
// E. Providel's `A Structure for Plain Bitmaps: Combined Sampling` in "Fast,
// Small, Simple Rank/Select on Bitmaps" with some minor modifications.
//
// See http://dcc.uchile.cl/~gnavarro/ps/sea12.1.pdf for more details.
type BitVector struct {
bits *bit.Array
// ranks[i] is the number of 1s
// from 0 to index (i*sr)-1
ranks []int
// indices[i] points to the
// beginning of the uint64 (LSB)
// that contains the (i*ss)+1th
// set bit.
indices []int
popcount int
opts *Options
}
// NewBitVector creates a new BitVector.
func NewBitVector(opts *Options) *BitVector {
if opts == nil {
opts = NewOptions()
}
b := bit.NewArray(0)
rs := make([]int, 1)
idx := make([]int, 1)
return &BitVector{
bits: b,
ranks: rs,
indices: idx,
opts: opts,
}
}
// Add appends the bits given its size to the vector.
func (v *BitVector) Add(bits uint64, size int) {
if size <= 0 || size > 64 {
panic("ranksel: bit size must be in range [1,64]")
}
// Add bits
v.bits.Add(bits, size)
vlength := v.bits.Len()
// Increment popcount
popcnt := bit.PopCount(bits)
v.popcount += popcnt
// Update rank sampling
lenranks := len(v.ranks)
overflow := vlength - (lenranks * v.opts.Sr)
if overflow > 0 {
v.ranks = append(v.ranks, 0)
rank := bit.Rank(bits, size-overflow-1)
v.ranks[lenranks] = v.popcount - popcnt + rank
}
// Update select sampling
lenidx := len(v.indices)
overflow = v.popcount - (lenidx * v.opts.Ss)
if overflow > 0 {
v.indices = append(v.indices, 0)
sel := bit.Select(bits, popcnt-overflow+1)
v.indices[lenidx] = (vlength - size + sel) & ^0x3F
}
}
// Get returns the uint64 representation of
// bits starting from index idx given the bit size.
func (v *BitVector) Get(idx, size int) uint64 {
return v.bits.Get(idx, size)
}
// Bit returns the bit value at index i.
func (v *BitVector) Bit(i int) uint {
if i >= v.bits.Len() {
panic("ranksel: index out of range")
}
vbits := v.bits.Bits()
if vbits[i>>6]&(1<<uint(i&63)) != 0 {
return 1
}
return 0
}
// Rank1 counts the number of 1s from
// the beginning up to the ith index.
func (v *BitVector) Rank1(i int) int {
if i >= v.bits.Len() {
panic("ranksel: index out of range")
}
j := i / v.opts.Sr
ip := (j * v.opts.Sr) >> 6
rank := v.ranks[j]
aidx := i & 63
bidx := i >> 6
vbits := v.bits.Bits()
for _, b := range vbits[ip:bidx] {
rank += bit.PopCount(b)
}
return rank + bit.Rank(vbits[bidx], aidx)
}
// Rank0 counts the number of 0s from
// the beginning up to the ith index.
func (v *BitVector) Rank0(i int) int {
return i - v.Rank1(i) + 1
}
// Select1 returns the index of the ith set bit.
// Panics if i is zero or greater than the number
// of set bits.
func (v *BitVector) Select1(i int) int {
if i > v.popcount {
panic("ranksel: input exceeds number of 1s")
} else if i == 0 {
panic("ranksel: input must be greater than 0")
}
j := (i - 1) / v.opts.Ss
q := v.indices[j] / v.opts.Sr
k := 0
r := 0
rq := v.ranks[q:]
for k, r = range rq {
if r >= i {
k--
break
}
}
idx := 0
rank := rq[k]
vbits := v.bits.Bits()
aidx := ((q + k) * v.opts.Sr) >> 6
for ii, b := range vbits[aidx:] {
rank += bit.PopCount(b)
if rank >= i {
overflow := rank - i
popcnt := bit.PopCount(b)
idx = (aidx + ii) << 6
idx += bit.Select(b, popcnt-overflow)
break
}
}
return idx
}
// Select0 returns the index of the ith zero. Panics
// if i is zero or greater than the number of zeroes.
// This is slower than Select1 in most cases.
func (v *BitVector) Select0(i int) int {
if i > (v.bits.Len() - v.popcount) {
panic("ranksel: input exceeds number of 0s")
} else if i == 0 {
panic("ranksel: input must be greater than 0")
}
// Do a binary search on the rank samples to find
// the largest rank sample that is less than i.
// From https://en.wikipedia.org/wiki/Binary_search_algorithm
imin := 1
imax := len(v.ranks) - 1
for imin < imax {
imid := imin + ((imax - imin) >> 1)
rmid0 := (imid * v.opts.Sr) - v.ranks[imid]
if rmid0 < i {
imin = imid + 1
} else {
imax = imid
}
}
imin--
idx := 0
vbits := v.bits.Bits()
aidx := (imin * v.opts.Sr) >> 6
rank0 := (imin * v.opts.Sr) - v.ranks[imin]
for ii, b := range vbits[aidx:] {
b = ^b
rank0 += bit.PopCount(b)
if rank0 >= i {
overflow := rank0 - i
popcnt := bit.PopCount(b)
idx = (aidx + ii) << 6
idx += bit.Select(b, popcnt-overflow)
break
}
}
return idx
}
func checkErr(err ...error) error {
for _, e := range err {
if e != nil {
return e
}
}
return nil
}
// GobEncode encodes this vector into gob streams.
func (v *BitVector) GobEncode() ([]byte, error) {
buf := &bytes.Buffer{}
enc := gob.NewEncoder(buf)
err := checkErr(
enc.Encode(v.bits),
enc.Encode(v.ranks),
enc.Encode(v.indices),
enc.Encode(v.popcount),
enc.Encode(v.opts),
)
if err != nil {
err = fmt.Errorf("ranksel: encode failed (%v)", err)
}
return buf.Bytes(), err
}
// GobDecode populates this vector from gob streams.
func (v *BitVector) GobDecode(data []byte) error {
buf := bytes.NewReader(data)
dec := gob.NewDecoder(buf)
v.opts = NewOptions()
v.bits = bit.NewArray(0)
err := checkErr(
dec.Decode(v.bits),
dec.Decode(&v.ranks),
dec.Decode(&v.indices),
dec.Decode(&v.popcount),
dec.Decode(v.opts),
)
if err != nil {
err = fmt.Errorf("ranksel: decode failed (%v)", err)
}
return err
}
// Len returns the number of bits stored.
func (v *BitVector) Len() int {
return v.bits.Len()
}
// PopCount returns the total number of 1s.
func (v *BitVector) PopCount() int {
return v.popcount
}
// Size returns the vector size in bytes.
func (v *BitVector) Size() int {
sizeofInt := int(unsafe.Sizeof(int(0)))
size := v.bits.Size()
size += len(v.ranks) * sizeofInt
size += len(v.indices) * sizeofInt
return size
}
// String returns a hexadecimal
// string representation of the vector.
func (v *BitVector) String() string {
return v.bits.String()
}