-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint16index.go
159 lines (144 loc) · 3.3 KB
/
int16index.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
package k2tree
import "fmt"
type int16index struct {
bits bitarray
counts []uint16
}
var _ bitarray = (*int16index)(nil)
const int16Max = 1 << 16
func newInt16Index(b bitarray) *int16index {
if b.Len() != 0 {
panic("unimplemented")
}
return &int16index{
bits: b,
counts: make([]uint16, 1),
}
}
// Len returns the number of bits in the bitarray.
func (ix *int16index) Len() int {
return ix.bits.Len()
}
// Set sets the bit at an index `at` to the value `val`.
func (ix *int16index) Set(at int, val bool) {
cur := ix.bits.Get(at)
if cur && val {
return
}
if !cur && !val {
return
}
ix.bits.Set(at, val)
var delta int
if val {
delta = 1
} else {
delta = -1
}
prevoff := 0
for i := range ix.counts {
off := (i + 1) * int16Max
if at < off && at >= prevoff {
ix.counts[i] = uint16(int(ix.counts[i]) + delta)
break
}
prevoff = off
}
}
// Get returns the value stored at `at`.
func (ix *int16index) Get(at int) bool {
return ix.bits.Get(at)
}
// Count returns the number of set bits in the interval [from, to).
func (ix *int16index) Count(from int, to int) int {
if from == 0 {
return ix.zeroCount(to)
}
out := ix.zeroCount(to) - ix.zeroCount(from)
return out
}
func (ix *int16index) zeroCount(to int) int {
// There's a speedup here where we're adding values only to subtract them.
total := 0
i := 0
ioff := 0
for i = 0; i < len(ix.counts); i++ {
ioff = (i + 1) * int16Max
if ioff >= to {
break
}
total += int(ix.counts[i])
}
offset := to - (i * int16Max)
if offset > (int16Max / 2) {
total += int(ix.counts[i])
total -= ix.bits.Count(to, min(ioff, ix.bits.Len()))
} else {
total += ix.bits.Count(i*int16Max, to)
}
//assert(total == ix.bits.Count(0, to), "Debug: Counts don't match")
return total
}
// Total returns the total number of set bits.
func (ix *int16index) Total() int {
return ix.bits.Total()
}
// Insert extends the bitarray by `n` bits. The bits are zeroed
// and start at index `at`. Example:
// Initial string: 11101
// Insert(3, 2)
// Resulting string: 11000101
func (ix *int16index) Insert(n int, at int) error {
entries := (ix.bits.Len() + n) / int16Max
for len(ix.counts) < entries+1 {
// extend
ix.counts = append(ix.counts, 0)
}
err := ix.bits.Insert(n, at)
if err != nil {
return err
}
if n >= int16Max {
ix.adjustBig(at)
} else {
ix.adjust(n, at)
}
// ix.adjustBig(at)
return nil
}
func (ix *int16index) adjustBig(at int) {
for i := range ix.counts {
off := (i + 1) * int16Max
if at >= off {
continue
} else {
c := ix.bits.Count(off-int16Max, min(off, ix.bits.Len()))
ix.counts[i] = uint16(c)
}
}
}
func (ix *int16index) adjust(n, at int) {
bitlen := ix.bits.Len()
for i := range ix.counts {
off := (i + 1) * int16Max
if at >= off {
continue
} else if (at + n) < off-int16Max {
if off >= bitlen {
// Do the last one the easy way
c := ix.bits.Count(off-int16Max, bitlen)
ix.counts[i] = uint16(c)
return
}
del := ix.bits.Count(off, off+n)
add := ix.bits.Count(off-int16Max, off-int16Max+n)
ix.counts[i] = uint16(int(ix.counts[i]) + add - del)
} else {
c := ix.bits.Count(off-int16Max, min(off, ix.bits.Len()))
ix.counts[i] = uint16(c)
}
}
}
func (ix *int16index) debug() string {
return fmt.Sprintf("Int16Index:\n internal: %s\nindex:%#v", ix.bits.debug(), ix.counts)
}