forked from tunabay/go-bitarray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitarray_bitwise.go
301 lines (283 loc) · 7.1 KB
/
bitarray_bitwise.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
// Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package bitarray
import (
"math/bits"
)
// LeadingZeros returns the number of leading zero bits in the BitArray.
func (ba *BitArray) LeadingZeros() int {
switch {
case ba.IsZero():
return 0
case ba.b == nil:
return ba.nBits
}
n := 0
nb := ba.nBits >> 3
for i := 0; i < nb; i++ {
z := bits.LeadingZeros8(ba.b[i])
n += z
if z != 8 {
return n
}
}
fb := ba.nBits & 7
if fb == 0 {
return n
}
z := bits.LeadingZeros8(ba.b[nb])
if fb < z {
z = fb
}
return n + z
}
// TrailingZeros returns the number of trailing zero bits in the BitArray.
func (ba *BitArray) TrailingZeros() int {
switch {
case ba.IsZero():
return 0
case ba.b == nil:
return ba.nBits
}
n := 0
for i := len(ba.b) - 1; 0 <= i; i-- {
z := bits.TrailingZeros8(ba.b[i])
n += z
if z != 8 {
return n - ba.NumPadding()
}
}
return n - ba.NumPadding()
}
func (ba *BitArray) hasTrailingZeros(n int) bool {
if n == 0 {
return true
}
n += ba.NumPadding()
for i := len(ba.b) - 1; 0 <= i; i-- {
z := bits.TrailingZeros8(ba.b[i])
n -= z
switch {
case n <= 0:
return true
case z != 8:
return false
}
}
return false
}
// OnesCount returns the number of one bits, population count, in the BitArray.
func (ba *BitArray) OnesCount() int {
if ba.IsZero() || ba.b == nil {
return 0
}
n := 0
for _, u64 := range asUint64Slice(ba.b) {
n += bits.OnesCount64(u64)
}
return n
}
// And returns a new BitArray as a result of a bitwise AND with x. The ba and x
// must be the same length, otherwise And will panic. Use AndAt instead to apply
// a partial AND with a short bit array.
func (ba *BitArray) And(x BitArrayer) *BitArray {
var bax *BitArray
if x != nil {
bax = x.BitArray()
}
baLen, xLen := ba.Len(), bax.Len()
switch {
case baLen != xLen:
panicf("And: length is not the same: %d != %d.", baLen, xLen)
case baLen == 0:
return zeroBitArray
case ba.b == nil:
return ba
case bax.b == nil:
return bax
}
zv := uint64(0)
buf := allocByteSlice(len(ba.b))
buf64 := asUint64Slice(buf)
x64 := asUint64Slice(bax.b)
for i, u64 := range asUint64Slice(ba.b) {
buf64[i] = u64 & x64[i]
zv |= buf64[i]
}
if zv == 0 {
return &BitArray{nBits: ba.nBits}
}
return &BitArray{b: buf, nBits: ba.nBits}
}
// Or returns a new BitArray as a result of a bitwise OR with x. The ba and x
// must be the same length, otherwise Or will panic. Use OrAt instead to apply a
// partial OR with a short bit array.
func (ba *BitArray) Or(x BitArrayer) *BitArray {
var bax *BitArray
if x != nil {
bax = x.BitArray()
}
baLen, xLen := ba.Len(), bax.Len()
switch {
case baLen != xLen:
panicf("Or: length is not the same: %d != %d.", baLen, xLen)
case baLen == 0:
return zeroBitArray
case ba.b == nil:
return bax
case bax.b == nil:
return ba
}
zv := uint64(0)
buf := allocByteSlice(len(ba.b))
buf64 := asUint64Slice(buf)
x64 := asUint64Slice(bax.b)
for i, u64 := range asUint64Slice(ba.b) {
buf64[i] = u64 | x64[i]
zv |= buf64[i]
}
if zv == 0 {
return &BitArray{nBits: ba.nBits}
}
return &BitArray{b: buf, nBits: ba.nBits}
}
// Xor returns a new BitArray as a result of a bitwise XOR with x. The ba and x
// must be the same length, otherwise Xor will panic. Use XorAt instead to apply
// a partial XOR with a short bit array.
func (ba *BitArray) Xor(x BitArrayer) *BitArray {
var bax *BitArray
if x != nil {
bax = x.BitArray()
}
baLen, xLen := ba.Len(), bax.Len()
switch {
case baLen != xLen:
panicf("Xor: length is not the same: %d != %d.", baLen, xLen)
case baLen == 0:
return zeroBitArray
case ba.b == nil:
return bax
case bax.b == nil:
return ba
}
zv := uint64(0)
buf := allocByteSlice(len(ba.b))
buf64 := asUint64Slice(buf)
x64 := asUint64Slice(bax.b)
for i, u64 := range asUint64Slice(ba.b) {
buf64[i] = u64 ^ x64[i]
zv |= buf64[i]
}
if zv == 0 {
return &BitArray{nBits: ba.nBits}
}
return &BitArray{b: buf, nBits: ba.nBits}
}
// Not returns a new BitArray that is the result of inverting all the bits.
func (ba *BitArray) Not() *BitArray {
switch {
case ba.IsZero():
return zeroBitArray
case ba.b == nil:
return NewOneFilled(ba.nBits)
}
// TODO: use asUint64Slice()
zv := byte(0)
buf := allocByteSlice(len(ba.b))
for i := 0; i < len(buf)-1; i++ {
buf[i] = ^ba.b[i]
zv |= buf[i]
}
buf[len(buf)-1] = ^ba.b[len(ba.b)-1] & (byte(0xff) << ba.NumPadding())
zv |= buf[len(buf)-1]
if zv == 0 {
return &BitArray{nBits: ba.nBits}
}
return &BitArray{b: buf, nBits: ba.nBits}
}
// AndAt returns a new BitArray resulting from applying a bitwise AND operation
// with x at the offset off. AND is applied only to the range from off to
// off+x.Len(), and other bits are preserved.
func (ba *BitArray) AndAt(off int, x BitArrayer) *BitArray {
var bax *BitArray
if x != nil {
bax = x.BitArray()
}
baLen, xLen := ba.Len(), bax.Len()
switch {
case off < 0:
panicf("AndAt: negative off %d.", off)
case baLen < off+xLen:
panicf("AndAt: out of range: off=%d + x.len=%d > len=%d.", off, xLen, baLen)
case baLen == 0:
return zeroBitArray
case ba.b == nil:
return ba
case bax.b == nil:
buf := allocByteSlice(len(ba.b))
copy(buf, ba.b)
clearBits(buf, off, xLen)
return &BitArray{b: buf, nBits: baLen}
}
buf := allocByteSlice(len(ba.b))
copy(buf, ba.b)
andBits(buf, bax.b, off, 0, xLen)
return &BitArray{b: buf, nBits: baLen}
}
// OrAt returns a new BitArray resulting from applying a bitwise OR operation
// with x at the offset off. OR is applied only to the range from off to
// off+x.Len(), and other bits are preserved.
func (ba *BitArray) OrAt(off int, x BitArrayer) *BitArray {
var bax *BitArray
if x != nil {
bax = x.BitArray()
}
baLen, xLen := ba.Len(), bax.Len()
switch {
case off < 0:
panicf("OrAt: negative off %d.", off)
case baLen < off+xLen:
panicf("OrAt: out of range: off=%d + x.len=%d > len=%d.", off, xLen, baLen)
case baLen == 0:
return zeroBitArray
case bax.b == nil:
return ba
case ba.b == nil:
buf := allocByteSlice((baLen + 7) >> 3)
_ = copyBits(buf, bax.b, off, 0, xLen)
return &BitArray{b: buf, nBits: baLen}
}
buf := allocByteSlice(len(ba.b))
copy(buf, ba.b)
orBits(buf, bax.b, off, 0, xLen)
return &BitArray{b: buf, nBits: baLen}
}
// XorAt returns a new BitArray resulting from applying a bitwise XOR operation
// with x at the offset off. XOR is applied only to the range from off to
// off+x.Len(), and other bits are preserved.
func (ba *BitArray) XorAt(off int, x BitArrayer) *BitArray {
var bax *BitArray
if x != nil {
bax = x.BitArray()
}
baLen, xLen := ba.Len(), bax.Len()
switch {
case off < 0:
panicf("XorAt: negative off %d.", off)
case baLen < off+xLen:
panicf("XorAt: out of range: off=%d + x.len=%d > len=%d", off, xLen, baLen)
case baLen == 0:
return zeroBitArray
case bax.b == nil:
return ba
case ba.b == nil:
buf := allocByteSlice((baLen + 7) >> 3)
_ = copyBits(buf, bax.b, off, 0, xLen)
return &BitArray{b: buf, nBits: baLen}
}
buf := allocByteSlice(len(ba.b))
copy(buf, ba.b)
xorBits(buf, bax.b, off, 0, xLen)
return &BitArray{b: buf, nBits: baLen}
}