generated from openacid/gotmpl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtailbitmap.go
347 lines (288 loc) · 6.46 KB
/
tailbitmap.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
package traft
import (
fmt "fmt"
"math/bits"
"strings"
proto "github.com/gogo/protobuf/proto"
"github.com/openacid/low/bitmap"
"github.com/openacid/low/mathext/util"
)
// reclaimThreshold is the size threshold in bit for reclamation of `Words`.
var reclaimThreshold = int64(1024) * 64
// NewTailBitmap creates an TailBitmap with a preset Offset and an empty
// tail bitmap.
//
// Optional arg `set` specifies what bit to set to 1.
// The bit positions in `set` is absolute, NOT based on offset.
//
// Since 0.1.22
func NewTailBitmap(offset int64, set ...int64) *TailBitmap {
residual := offset & 63
tb := &TailBitmap{
Offset: offset & ^63,
Reclamed: offset & ^63,
Words: make([]uint64, 0, reclaimThreshold>>6),
}
if residual != 0 {
for i := int64(0); i < residual; i++ {
tb.Set(tb.Offset + i)
}
}
for _, pos := range set {
tb.Set(pos)
}
return tb
}
// Compact all leading all-ones words in the bitmap.
//
// Since 0.1.22
func (tb *TailBitmap) Compact() {
allOnes := uint64(0xffffffffffffffff)
for len(tb.Words) > 0 && tb.Words[0] == allOnes {
tb.Offset += 64
tb.Words = tb.Words[1:]
}
if tb.Offset-tb.Reclamed >= reclaimThreshold {
l := len(tb.Words)
newWords := make([]uint64, l, l*2)
copy(newWords, tb.Words)
tb.Reclamed = tb.Offset
}
}
// Set the bit at `idx` to `1`.
//
// Since 0.1.22
func (tb *TailBitmap) Set(idx int64) {
if idx < tb.Offset {
return
}
idx = idx - tb.Offset
wordIdx := idx >> 6
for int(wordIdx) >= len(tb.Words) {
tb.Words = append(tb.Words, 0)
}
tb.Words[wordIdx] |= bitmap.Bit[idx&63]
if wordIdx == 0 {
tb.Compact()
}
}
// Get retrieves a bit at its 64-based offset.
//
// Since 0.1.22
func (tb *TailBitmap) Get(idx int64) uint64 {
if idx < tb.Offset {
return bitmap.Bit[idx&63]
}
idx = idx - tb.Offset
if int(idx>>6) >= len(tb.Words) {
return 0
}
return tb.Words[idx>>6] & bitmap.Bit[idx&63]
}
// Get1 retrieves a bit and returns a 1-bit word, i.e., putting the bit in the
// lowest bit.
//
// Since 0.1.22
func (tb *TailBitmap) Get1(idx int64) uint64 {
if idx < tb.Offset {
return 1
}
idx = idx - tb.Offset
return (tb.Words[idx>>6] >> uint(idx&63)) & 1
}
func (tb *TailBitmap) Clone() *TailBitmap {
return proto.Clone(tb).(*TailBitmap)
}
func (tb *TailBitmap) Normalize() *TailBitmap {
if tb.Words == nil {
tb.Words = make([]uint64, 0)
}
return tb
}
func (tb *TailBitmap) Union(tc *TailBitmap) {
if tc == nil {
return
}
lb := tb.Offset + int64(len(tb.Words)*64)
lc := tc.Offset + int64(len(tc.Words)*64)
if tb.Offset >= lc {
return
}
if lb <= tc.Offset {
tb.Offset = tc.Offset
tb.Words = make([]uint64, len(tc.Words))
copy(tb.Words, tc.Words)
// building a new Words reclames unused spaces in it.
tb.Reclamed = tb.Offset
return
}
var ws []uint64
if tb.Offset >= tc.Offset {
delta := tb.Offset - tc.Offset
ws = tc.Words[delta>>6:]
} else {
// tb.Offset < tc.Offset
delta := tc.Offset - tb.Offset
tb.Words = tb.Words[delta>>6:]
tb.Offset = tc.Offset
ws = tc.Words
}
var i int
for i = 0; i < len(tb.Words) && i < len(ws); i++ {
tb.Words[i] |= ws[i]
}
for ; i < len(ws); i++ {
tb.Words = append(tb.Words, ws[i])
}
tb.Compact()
}
func (ta *TailBitmap) Intersection(tb *TailBitmap) {
if tb == nil {
ta.Offset = 0
ta.Words = make([]uint64, 0)
ta.Reclamed = 0
return
}
la := ta.Offset + int64(len(ta.Words)*64)
lb := tb.Offset + int64(len(tb.Words)*64)
// 1111 1111 xxxx
// 1111 yyyy
if ta.Offset >= lb {
ta.Offset = tb.Offset
ta.Words = make([]uint64, len(tb.Words))
copy(ta.Words, tb.Words)
// building a new Words reclames unused spaces in it.
ta.Reclamed = ta.Offset
return
}
// 1111 xxxx
// 1111 1111 yyyy
if la <= tb.Offset {
return
}
s := util.MinI64(ta.Offset, tb.Offset)
e := util.MinI64(la, lb)
ws := make([]uint64, (e-s)>>6)
cur := int64(0)
i := int64(0)
j := int64(0)
if ta.Offset >= tb.Offset {
n := (ta.Offset - s) >> 6
n = util.MinI64(n, (e-s)>>6)
copy(ws, tb.Words[:n])
cur += n
j = n
} else {
n := (tb.Offset - s) >> 6
n = util.MinI64(n, (e-s)>>6)
copy(ws, ta.Words[:n])
cur += n
i = n
}
for cur < int64(len(ws)) {
ws[cur] = ta.Words[i] & tb.Words[j]
cur++
i++
j++
}
for len(ws) > 0 && ws[len(ws)-1] == 0 {
ws = ws[:len(ws)-1]
}
ta.Offset = s
ta.Words = ws
ta.Reclamed = s
}
// Diff AKA substraction A - B or A \ B
// TODO: This impl is wrong!!!
func (tb *TailBitmap) Diff(tc *TailBitmap) {
if tc == nil {
return
}
lb := tb.Offset + int64(len(tb.Words)*64)
lc := tc.Offset + int64(len(tc.Words)*64)
if lb <= tc.Offset {
for i := 0; i < len(tb.Words); i++ {
tb.Words[i] = ^tb.Words[i]
}
return
}
if tb.Offset > lc {
// 1111 1111 1111 xxxx xxxx
// 1111 yyyy
l := int((tb.Offset - tc.Offset) >> 6)
words := make([]uint64, l+len(tb.Words))
var i int
for i = 0; i < l && i < len(tc.Words); i++ {
words[i] = ^tc.Words[i]
}
for ; i < l; i++ {
words[i] = 0xffffffffffffffff
}
copy(words[i:], tb.Words)
tb.Words = words
tb.Offset = tc.Offset
tb.Reclamed = tb.Offset
return
}
if tb.Offset <= tc.Offset {
// 1111 1111 xxxx xxxx
// 1111 1111 1111 yyyy yyyy
delta := (tc.Offset - tb.Offset) >> 6
var i int64
for i = 0; i < delta; i++ {
tb.Words[i] = 0
}
for ; i < int64(len(tb.Words)) && i < (lc-tb.Offset)>>6; i++ {
tb.Words[i] &= ^tc.Words[i-delta]
}
} else {
// tb.Offset > tc.Offset
// 1111 1111 xxxx xxxx
// 1111 yyyy yyyy
delta := int((tb.Offset - tc.Offset) >> 6)
words := make([]uint64, delta+len(tb.Words))
var i int
for i = 0; i < delta; i++ {
words[i] = ^tc.Words[i]
}
for ; i < len(words) && i < len(tc.Words); i++ {
words[i] = tb.Words[i-delta] &^ tc.Words[i]
}
copy(words[i:], tb.Words[i-delta:])
tb.Words = words
tb.Offset = tc.Offset
tb.Reclamed = tb.Offset
}
}
// Last returns last set bit index + 1.
func (tb *TailBitmap) Len() int64 {
r := len(tb.Words) - 1
for ; r >= 0 && tb.Words[r] == 0; r-- {
}
if r < 0 {
// all Words are 0
return tb.Offset
}
return tb.Offset + int64(r+1)<<6 - int64(bits.LeadingZeros64(tb.Words[r]))
}
func (tb *TailBitmap) ShortStr() string {
if tb == nil {
return "0"
}
s := []string{fmt.Sprintf("%d", tb.Offset)}
for _, w := range tb.Words {
s = append(s, fmt.Sprintf(":%x", w))
}
return strings.Join(s, "")
}
func (tb *TailBitmap) DebugStr() string {
if tb == nil {
return "0"
}
s := []string{fmt.Sprintf("%d", tb.Offset)}
for _, w := range tb.Words {
v := bitmap.Fmt(w)
s = append(s, v)
}
return strings.Join(s, ",")
}