-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitarray.go
267 lines (229 loc) · 6.14 KB
/
bitarray.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
/*
BitArray for Golang.
*/
package bitarray
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
)
type BitArray struct {
lenpad int
length int
bytes []byte
}
const (
_BytesPW int = 8
BitsPW int = _BytesPW * 8
)
var msbmask = [8]byte{0xFF, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80}
var lsbmask = [8]byte{0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF}
// var count = [16]int{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}
func nbytes(length int) int {
return (((length + 7) & (^7)) / 8) // (length/8) + ((length%8)? 1:0)
}
func nwords(length int) int {
return (((length + BitsPW - 1) & (^(BitsPW - 1))) / BitsPW)
}
func bytes2word(bs []byte) uint64 {
var n uint64
buf := bytes.NewBuffer(bs)
err := binary.Read(buf, binary.BigEndian, &n)
if err != nil {
panic(err)
}
return n
}
// https://en.wikipedia.org/wiki/Hamming_weight
func countbits64(n uint64) int {
n -= (n >> 1) & 0x5555555555555555
n = (n & 0x3333333333333333) + ((n >> 2) & 0x3333333333333333)
n = (n + (n >> 4)) & 0x0f0f0f0f0f0f0f0f
return int((n * 0x0101010101010101) >> 56)
}
// New create a new BitArray with length(bits).
func New(length int) *BitArray {
lenpad := nwords(length) * _BytesPW
return &BitArray{
lenpad: lenpad,
length: length,
bytes: make([]byte, lenpad, lenpad),
}
}
// Len return the length of the BitArray.
func (bits *BitArray) Len() int {
return bits.length
}
// Count return the count of bit 1.
func (bits *BitArray) Count() int {
length := 0
for i := 0; i < bits.lenpad; i += _BytesPW {
w := bytes2word(bits.bytes[i : i+_BytesPW])
length += countbits64(w)
}
return length
}
func (bits *BitArray) indexOutOfRange(idx int) error {
if idx < 0 || idx >= bits.length {
msg := fmt.Sprintf("index %d out of range [%d, %d)", idx, 0, bits.length)
return errors.New(msg)
}
return nil
}
// Get return the bit by index n.
// If index out of range [0, BitArray.Len()), return error.
func (bits *BitArray) Get(n int) (int, error) {
if err := bits.indexOutOfRange(n); err != nil {
return 0, err
}
return int((bits.bytes[n/8] >> byte(n%8)) & 1), nil
}
// Put set the nth bit with 0/1, and return the old value of nth bit.
// If index out of range [0, BitArray.Len()), return error.
func (bits *BitArray) Put(n int, bit int) (int, error) {
if err := bits.indexOutOfRange(n); err != nil {
return 0, err
}
prev, _ := bits.Get(n)
if bit == 1 {
bits.bytes[n/8] |= 1 << byte(n%8)
} else {
bits.bytes[n/8] &= ^(1 << byte(n%8))
}
return prev, nil
}
// Set the value of all bits to 1, which index range between low and high,
// both low and high included.
// low must less than high, and low/high cannot out of range [0, BitArray.Len()).
func (bits *BitArray) Set(low int, high int) error {
if low > high {
msg := fmt.Sprintf("low %d should less than high %d", low, high)
return errors.New(msg)
}
for _, idx := range []int{low, high} {
if err := bits.indexOutOfRange(idx); err != nil {
return err
}
}
lb, hb := low/8, high/8
if lb < hb {
bits.bytes[lb] |= msbmask[low%8]
for i := lb + 1; i < hb; i++ {
bits.bytes[i] = 0xFF
}
bits.bytes[hb] |= lsbmask[high%8]
} else {
bits.bytes[lb] |= (msbmask[low%8] & lsbmask[high%8])
}
return nil
}
// Clear set the value of all bits to 0, which index range between low and high,
// both low and high included.
// low must less than high, and low/high cannot out of range [0, BitArray.Len()).
func (bits *BitArray) Clear(low int, high int) error {
if low > high {
msg := fmt.Sprintf("low %d should less than high %d", low, high)
return errors.New(msg)
}
for _, idx := range []int{low, high} {
if err := bits.indexOutOfRange(idx); err != nil {
return err
}
}
lb, hb := low/8, high/8
if lb < hb {
bits.bytes[lb] &= ^msbmask[low%8]
for i := lb + 1; i < hb; i++ {
bits.bytes[i] = 0
}
bits.bytes[hb] &= ^lsbmask[high%8]
} else {
bits.bytes[lb] &= ^(msbmask[low%8] & lsbmask[high%8])
}
return nil
}
// Not flips the value of all bits, which index range between low and high,
// both low and high included.
// low must less than high, and low/high cannot out of range [0, BitArray.Len()).
func (bits *BitArray) Not(low int, high int) error {
if low > high {
msg := fmt.Sprintf("low %d should less than high %d", low, high)
return errors.New(msg)
}
for _, idx := range []int{low, high} {
if err := bits.indexOutOfRange(idx); err != nil {
return err
}
}
lb, hb := low/8, high/8
if lb < hb {
bits.bytes[lb] ^= msbmask[low%8]
for i := lb + 1; i < hb; i++ {
bits.bytes[i] ^= 0xFF
}
bits.bytes[hb] ^= lsbmask[high%8]
} else {
bits.bytes[lb] ^= (msbmask[low%8] & lsbmask[high%8])
}
return nil
}
// Eq check whether the BitArray is equal to another BitArray.
// If length isn't same, return false.
func (bits *BitArray) Eq(obits *BitArray) bool {
if bits.length != obits.length {
return false
}
for i := 0; i < bits.lenpad; i += _BytesPW {
wself := bytes2word(bits.bytes[i : i+_BytesPW])
wother := bytes2word(obits.bytes[i : i+_BytesPW])
if wself != wother {
return false
}
}
return true
}
// Leq check whether the BitArray is the subset of the another.
// If length isn't same, return false.
func (bits *BitArray) Leq(obits *BitArray) bool {
if bits.length != obits.length {
return false
}
for i := 0; i < bits.lenpad; i += _BytesPW {
wself := bytes2word(bits.bytes[i : i+_BytesPW])
wother := bytes2word(obits.bytes[i : i+_BytesPW])
if (wself & ^wother) != 0 {
return false
}
}
return true
}
// Lt check whether the BitArray is the proper subset of the another.
// If length isn't same, return false.
func (bits *BitArray) Lt(obits *BitArray) bool {
if bits.length != obits.length {
return false
}
lt := 0
for i := 0; i < bits.lenpad; i += _BytesPW {
wself := bytes2word(bits.bytes[i : i+_BytesPW])
wother := bytes2word(obits.bytes[i : i+_BytesPW])
if (wself & ^wother) != 0 {
return false
} else if wself != wother { // at least one word does not equal
lt |= 1
}
}
if lt == 0 {
return false
}
return true
}
// Convert the BitArray to a array of integers, and return.
func (bits *BitArray) ToArray() []int {
ints := make([]int, bits.length, bits.length)
for i := 0; i < bits.length; i++ {
ints[i], _ = bits.Get(i)
}
return ints
}