-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.go
More file actions
205 lines (167 loc) · 4.06 KB
/
array.go
File metadata and controls
205 lines (167 loc) · 4.06 KB
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
// Package bit provides a bit array implementation and some utility functions.
package bit
import (
"bytes"
"encoding/gob"
"fmt"
"strings"
)
// Array represents a bit array.
type Array struct {
bits []uint64
length int
}
// NewArray creates a new bit array
// with an initial bit capacity of n.
func NewArray(n int) *Array {
if n < 0 {
panic("bit: array size must be greater than or equal 0")
}
b := make([]uint64, 1, (n>>6)+1)
return &Array{b, 0}
}
// Add appends the bits given its size to the array.
func (a *Array) Add(bits uint64, size int) {
if size <= 0 || size > 64 {
panic("bit: bit size must be in range [1,64]")
}
// Extend bits if necessary
lenbits := len(a.bits)
freespace := (lenbits << 6) - a.length
overflow := size - freespace
if overflow > 0 {
a.bits = append(a.bits, 0)
}
// Append bits
idx := lenbits - 1
if freespace > 0 {
a.bits[idx] |= bits << uint(a.length&63)
}
if overflow > 0 {
a.bits[idx+1] |= bits >> uint(freespace)
}
// Increment size
a.length += size
}
// Insert inserts bits to index idx overwriting its contents.
func (a *Array) Insert(idx int, bits uint64, size int) {
if idx > a.length {
panic("bit: index out of bounds")
} else if size <= 0 || size > 64 {
panic("bit: bit size must be in range [1,64]")
}
// Extend bits if necessary
overflow := idx + size - a.length
if overflow > 0 {
lenbits := len(a.bits)
freespace := (lenbits << 6) - a.length
if overflow > freespace {
a.bits = append(a.bits, 0)
}
a.length += overflow
}
bitIdx := idx & 63
arrayIdx := idx >> 6
lowBitSz := 64 - bitIdx
overflow = size - lowBitSz
ba := a.bits[arrayIdx]
bb := bits << uint(bitIdx)
mask := uint64(1<<uint(size)) - 1
// Use bit twiddling hacks (merging bits)
// https://graphics.stanford.edu/~seander/bithacks.html
a.bits[arrayIdx] = ba ^ ((ba ^ bb) & (mask << uint(bitIdx)))
if overflow > 0 {
arrayIdx++
ba = a.bits[arrayIdx]
bb = bits >> uint(lowBitSz)
a.bits[arrayIdx] = ba ^ ((ba ^ bb) & (mask >> uint(lowBitSz)))
}
}
// Get returns the uint64 representation of
// bits starting from index idx given the bit size.
func (a *Array) Get(idx, size int) uint64 {
if idx > a.length {
panic("bit: index out of bounds")
} else if size <= 0 || size > 64 {
panic("bit: bit size must be in range [1,64]")
}
bitIdx := idx & 63
arrayIdx := idx >> 6
lowBitSz := 64 - bitIdx
overflow := size - lowBitSz
res := a.bits[arrayIdx] >> uint(bitIdx)
if overflow > 0 {
res |= a.bits[arrayIdx+1] << uint(lowBitSz)
}
return res & ((1 << uint(size)) - 1)
}
// Bits returns the underlying array.
func (a *Array) Bits() []uint64 {
return a.bits
}
// Reset resets the array
// to its initial state.
func (a *Array) Reset() {
for i := range a.bits {
a.bits[i] = 0
}
a.bits = a.bits[0:1]
a.length = 0
}
// Len returns the number
// of bits stored in the array.
func (a *Array) Len() int {
return a.length
}
// Size returns the size
// of the array in bytes.
func (a *Array) Size() int {
return len(a.bits) * 8
}
// String returns a hexadecimal
// string representation of the array.
func (a *Array) String() string {
buf := new(bytes.Buffer)
for i := len(a.bits) - 1; i >= 0; i-- {
bits := fmt.Sprintf("%16X", a.bits[i])
bits = strings.Replace(bits, " ", "0", -1)
fmt.Fprintf(buf, "%s [%d-%d] ", bits, (i<<6)+63, i<<6)
}
return buf.String()
}
// GobEncode allows this array
// to be encoded into gob streams.
func (a *Array) GobEncode() ([]byte, error) {
buf := &bytes.Buffer{}
enc := gob.NewEncoder(buf)
err := checkErr(
enc.Encode(a.bits),
enc.Encode(a.length),
)
if err != nil {
err = fmt.Errorf("bit: encode failed (%v)", err)
}
return buf.Bytes(), err
}
// GobDecode allows this array
// to be decoded from gob streams.
func (a *Array) GobDecode(data []byte) error {
buf := bytes.NewReader(data)
dec := gob.NewDecoder(buf)
err := checkErr(
dec.Decode(&a.bits),
dec.Decode(&a.length),
)
if err != nil {
err = fmt.Errorf("bit: decode failed (%v)", err)
}
return err
}
func checkErr(err ...error) error {
for _, e := range err {
if e != nil {
return e
}
}
return nil
}