-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytearray.go
180 lines (162 loc) · 3.29 KB
/
bytearray.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
package k2tree
import (
"fmt"
"math/bits"
"github.com/barakmich/k2tree/bytearray"
)
type byteArray struct {
bytes bytearray.ByteArray
length int
total int
}
var _ bitarray = (*byteArray)(nil)
func newByteArray(bytes bytearray.ByteArray) *byteArray {
return &byteArray{
bytes: bytes,
length: 0,
total: 0,
}
}
func (b *byteArray) Len() int {
return b.length
}
func (b *byteArray) Set(at int, val bool) {
if at >= b.length {
panic("can't set a bit beyond the size of the array")
}
off := at >> 3
bit := byte(at & 0x07)
t := byte(0x01 << (7 - bit))
orig := b.bytes.Get(off)
var newbyte byte
if val {
newbyte = orig | t
} else {
newbyte = orig &^ t
}
b.bytes.Set(off, newbyte)
if newbyte != orig {
if val {
b.total++
} else {
b.total--
}
}
}
func (b *byteArray) Count(from, to int) int {
if from > to {
from, to = to, from
}
if from > b.length || to > b.length {
panic("out of range")
}
if from == to {
return 0
}
c := 0
startoff := from >> 3
startbit := byte(from & 0x07)
endoff := to >> 3
endbit := byte(to & 0x07)
if startoff == endoff {
abit := byte(0xFF >> startbit)
bbit := byte(0xFF >> endbit)
return bits.OnesCount8(b.bytes.Get(startoff) & (abit &^ bbit))
}
if startbit != 0 {
c += bits.OnesCount8(b.bytes.Get(startoff) & (0xFF >> startbit))
startoff++
}
if endbit != 0 {
c += bits.OnesCount8(b.bytes.Get(endoff) & (0xFF &^ (0xFF >> endbit)))
}
c += int(b.bytes.PopCount(startoff, endoff))
return c
}
func (b *byteArray) Total() int {
return b.total
}
func (b *byteArray) Get(at int) bool {
off := at >> 3
lowb := byte(at & 0x07)
mask := byte(0x01 << (7 - lowb))
return !(b.bytes.Get(off)&mask == 0x00)
}
func (b *byteArray) String() string {
str := fmt.Sprintf("L%d T%d ", b.length, b.total)
return str
}
func (b *byteArray) debug() string {
return b.String()
}
func (b *byteArray) Insert(n, at int) (err error) {
if at > b.length {
panic("can't extend starting at a too large offset")
}
if n == 0 {
return nil
}
if at%4 != 0 {
panic("can only insert a sliceArray at offset multiples of 4")
}
if n%8 == 0 {
err = b.insertEight(n, at)
} else if n == 4 {
err = b.insertFour(at)
} else if n%4 == 0 {
mult8 := (n >> 3) << 3
err = b.insertEight(mult8, at)
if err != nil {
return err
}
err = b.insertFour(at)
} else {
panic("can only extend a sliceArray by nibbles or multiples of 8")
}
if err != nil {
return err
}
b.length = b.length + n
return nil
}
func (b *byteArray) insertFour(at int) error {
if b.length%8 == 0 {
// We need more space
b.bytes.Insert(b.bytes.Len(), []byte{0x00})
}
off := at >> 3
var inbyte byte
if at%8 != 0 {
inbyte = b.bytes.Get(off)
b.bytes.Set(off, inbyte&0xF0)
off++
}
inbyte = inbyte << 4
for i := off; i < b.bytes.Len(); i++ {
t := b.bytes.Get(i)
b.bytes.Set(i, t>>4|inbyte)
inbyte = t << 4
}
if inbyte != 0x00 {
panic("Overshot")
}
return nil
}
func (b *byteArray) insertEight(n, at int) error {
nBytes := n >> 3
newbytes := make([]byte, nBytes)
if at == b.length {
b.bytes.Insert(b.bytes.Len(), newbytes)
return nil
}
off := at >> 3
if at%8 == 0 {
b.bytes.Insert(off, newbytes)
} else {
b.bytes.Insert(off+1, newbytes)
oldoff := b.bytes.Get(off)
b.bytes.Set(off+nBytes, oldoff&0x0F)
b.bytes.Set(off, oldoff&0xF0)
}
return nil
}