-
Notifications
You must be signed in to change notification settings - Fork 167
/
bitlist.go
138 lines (124 loc) · 3.25 KB
/
bitlist.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
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2023, daeuniverse Organization <dae@v2raya.org>
*/
package bitlist
import (
"fmt"
"math/bits"
"github.com/daeuniverse/dae/pkg/anybuffer"
"github.com/mzz2017/softwind/common"
)
// CompactBitList allows your units to be of arbitrary bit size.
type CompactBitList struct {
unitBitSize int
size int
b *anybuffer.Buffer[uint16]
unitNum int
}
func NewCompactBitList(unitBitSize int) *CompactBitList {
return &CompactBitList{
unitBitSize: unitBitSize,
size: 0,
b: anybuffer.NewBuffer[uint16](8),
}
}
// Set function is not optimized yet.
func (m *CompactBitList) Set(iUnit int, v uint64) {
if bits.Len64(v) > m.unitBitSize {
panic(fmt.Sprintf("value %v exceeds unit bit size", v))
}
m.growByUnitIndex(iUnit)
b := m.b.Slice()
i := iUnit * m.unitBitSize / 16
j := iUnit * m.unitBitSize % 16
for unitToTravel := m.unitBitSize; unitToTravel > 0; unitToTravel -= 16 {
k := 0
for ; k < unitToTravel && j+k < 16; k++ {
b[i] &= ^(1 << (k + j)) // clear bit.
val := uint16((v & (1 << k)) << j)
b[i] |= val // set bit.
}
// Now unitBitSize is traveled and we should break the loop,
// OR we did not travel the byte and we need to travel the next byte.
if k >= unitToTravel {
break
}
i++
bakJ := j
j = k
for ; k < unitToTravel && k < 16; k++ {
b[i] &= ^(1 << (k - j)) // clear bit.
val := uint16((v & (1 << k)) >> j)
b[i] |= val // set bit.
}
v >>= 16
j = (bakJ + 16) % 16
}
m.unitNum = common.Max(m.unitNum, iUnit+1)
}
func (m *CompactBitList) Get(iUnit int) (v uint64) {
bitBoundary := (iUnit + 1) * m.unitBitSize
if m.b.Len()*16 < bitBoundary {
return 0
}
b := m.b.Slice()
i := iUnit * m.unitBitSize / 16
j := iUnit * m.unitBitSize % 16
var val uint16
byteSpace := 16 - j
// 11111111
// |
// j byteSpace = 6, unitBitSize = 2
// 11 We only copy those 2 bits, so we left shift 4 and right shift 4+2.
if byteSpace > m.unitBitSize {
toTrimLeft := byteSpace - m.unitBitSize
return uint64((b[i] << toTrimLeft) >> (toTrimLeft + j))
} else {
// Trim right only.
val = b[i] >> j
}
v |= uint64(val)
offset := 16 - j
i++
// Now we have multiple of 16 bits spaces to move.
unitToTravel := m.unitBitSize - offset
for ; unitToTravel >= 16; unitToTravel, i, offset = unitToTravel-16, i+1, offset+16 {
// 11111111
// |
// p
// 11111111 We copy whole 16 bits
v |= uint64(b[i]) << offset
}
if unitToTravel == 0 {
return v
}
// 11111111
// |
// p unitToTravel = 3
// 111 We only copy those 3 bits, so we left shift 5 and right shift 5.
toTrimLeft := 16 - unitToTravel
if offset > toTrimLeft {
v |= uint64(b[i]<<toTrimLeft) << (offset - toTrimLeft)
} else {
v |= uint64(b[i]<<toTrimLeft) >> (toTrimLeft - offset)
}
return v
}
func (m *CompactBitList) Append(v uint64) {
m.Set(m.unitNum, v)
}
func (m *CompactBitList) growByUnitIndex(i int) {
if bitBoundary := (i + 1) * m.unitBitSize; m.b.Len()*16 < bitBoundary {
needBytes := bitBoundary / 16
if bitBoundary%16 != 0 {
needBytes++
}
m.b.Extend(needBytes - m.b.Len())
}
}
func (m *CompactBitList) Tighten() {
a := make([]uint16, m.b.Len())
copy(a, m.b.Slice())
m.b = anybuffer.NewBufferFrom(a)
}