forked from tunabay/go-bitarray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_bitwise.go
104 lines (95 loc) · 3.12 KB
/
buffer_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
// 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
// ToggleBitAt flips a single bit at the position specified by off in the
// buffer.
func (buf *Buffer) ToggleBitAt(off int) {
switch {
case off < 0:
panicf("ToggleBitAt: negative off %d.", off)
case buf.nBits <= off:
panicf("ToggleBitAt: out of range: off=%d >= len=%d.", off, buf.nBits)
}
off += buf.off
buf.b[off>>3] ^= byte(0x80) >> (off & 7)
}
// ToggleBitsAt inverts the nBits bits starting at off.
func (buf *Buffer) ToggleBitsAt(off, nBits int) {
switch {
case off < 0:
panicf("ToggleBitsAt: negative off %d.", off)
case nBits < 0:
panicf("ToggleBitsAt: negative nBits %d.", nBits)
case buf.nBits < off+nBits:
panicf("ToggleBitsAt: out of range: off=%d + nBits=%d > len=%d.", off, nBits, buf.nBits)
case nBits == 0:
// no-op
default:
toggleBits(buf.b, buf.off+off, nBits)
}
}
// AndAt applies 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 (buf *Buffer) AndAt(off int, x BitArrayer) {
var bax *BitArray
if x != nil {
bax = x.BitArray()
}
switch {
case off < 0:
panicf("AndAt: negative off %d.", off)
case buf.nBits < off+bax.Len():
panicf("AndAt: out of range: off=%d + x.len=%d > len=%d.", off, bax.Len(), buf.nBits)
case bax.IsZero():
// no-op
case bax.b == nil:
clearBits(buf.b, buf.off+off, bax.nBits)
default:
andBits(buf.b, bax.b, buf.off+off, 0, bax.nBits)
}
}
// OrAt applies 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 (buf *Buffer) OrAt(off int, x BitArrayer) {
var bax *BitArray
if x != nil {
bax = x.BitArray()
}
switch {
case off < 0:
panicf("OrAt: negative off %d.", off)
case buf.nBits < off+bax.Len():
panicf("OrAt: out of range: off=%d + x.len=%d > len=%d.", off, bax.Len(), buf.nBits)
case bax.IsZero(), bax.b == nil:
// no-op
default:
orBits(buf.b, bax.b, buf.off+off, 0, bax.nBits)
}
}
// XorAt applies 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 (buf *Buffer) XorAt(off int, x BitArrayer) {
var bax *BitArray
if x != nil {
bax = x.BitArray()
}
switch {
case off < 0:
panicf("XorAt: negative off %d.", off)
case buf.nBits < off+bax.Len():
panicf("XorAt: out of range: off=%d + x.len=%d > len=%d.", off, bax.Len(), buf.nBits)
case bax.IsZero(), bax.b == nil:
// no-op
default:
xorBits(buf.b, bax.b, buf.off+off, 0, bax.nBits)
}
}
// LeadingZeros returns the number of leading zero bits in the Buffer.
func (buf *Buffer) LeadingZeros() int { return buf.BitArray().LeadingZeros() }
// TrailingZeros returns the number of trailing zero bits in the Buffer.
func (buf *Buffer) TrailingZeros() int { return buf.BitArray().TrailingZeros() }
// OnesCount returns the number of one bits, population count, in the Buffer.
func (buf *Buffer) OnesCount() int { return buf.BitArray().OnesCount() }