-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitarray.go
265 lines (205 loc) · 5.27 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
// A bit array (also known as bit map, bit set, bit string, or bit vector)
// is an array data structure that compactly stores bits.
// https://en.wikipedia.org/wiki/Bit_array
package bitarray
import (
"sync"
"unsafe"
"github.com/aermolaev/atomicvalue"
)
// BitArray array of binary values.
type BitArray struct {
mu sync.RWMutex
blocks []BitBlock
curIndex int64
size int64
capacity int64
count atomicvalue.Int
}
type BitBlock uint64
const (
blockSize = int64(unsafe.Sizeof(BitBlock(0)) * 8)
bitBlockFull = (1 << blockSize) - 1
bitBlockMark = true
bitBlockUnmark = false
BitBlockNotFound = -1
)
// NewBitArray creates and initializes a new BitArray using capacity as its
// initial capacity.
func NewBitArray(capacity int64) *BitArray {
size := (capacity / blockSize) + 1
return &BitArray{
blocks: make([]BitBlock, size),
capacity: capacity,
size: size,
}
}
// HasRoom reports true if this BitArray contains bits that are set to true.
func (b *BitArray) HasRoom() bool {
return b.count.Get64() < b.capacity
}
// IsEmpty reports true if this BitArray contains no bits that are set to true.
func (b *BitArray) IsEmpty() bool {
return !b.HasRoom()
}
// Len returns the number of occupied bits.
func (b *BitArray) Len() int {
return b.count.Get()
}
// Cap returns the BitArray capacity, that is, the total bits allocated
// for the data.
func (b *BitArray) Cap() int {
return int(b.capacity)
}
// Reset resets BitArray to initial state.
func (b *BitArray) Reset() {
b.mu.Lock()
defer b.mu.Unlock()
for i := int64(0); i < b.size; i++ {
b.blocks[i] = BitBlock(0)
}
b.count.Set(0)
}
// Set sets the bit at the specified index to the specified value.
func (b *BitArray) Set(index int64, mark bool) (changed bool) {
if i, j := bitIndexAndNum(index); i < b.size {
block := &b.blocks[i]
b.mu.Lock()
if mark == bitBlockMark {
if changed = block.compareAndMark(j); changed {
b.count.Inc()
}
} else {
if changed = block.compareAndUnmark(j); changed {
b.count.Dec()
if i < b.curIndex {
b.curIndex = i // move pointer closer to the beginning
}
}
}
b.mu.Unlock()
}
return
}
// Get returns the value of the bit with the specified index.
func (b *BitArray) Get(index int64) (res bool) {
if i, j := bitIndexAndNum(index); i < b.size {
block := &b.blocks[i]
b.mu.RLock()
res = block.value(j)
b.mu.RUnlock()
}
return
}
// Mark sets the bit at the specified index to true.
func (b *BitArray) Mark(index int64) {
b.Set(index, bitBlockMark)
}
// Unmark sets the bit at the specified index to false.
func (b *BitArray) Unmark(index int64) {
b.Set(index, bitBlockUnmark)
}
// MarkFree finds the index of the first bit that is set to false and
// sets the bit to true. Returns index of changed bit. Returns BitBlockNotFound
// unless array has room.
func (b *BitArray) MarkFree() (index int64) {
index = BitBlockNotFound
if !b.HasRoom() { // fast check w/o lock
return
}
b.mu.Lock()
if b.HasRoom() {
if block := b.nextFree(); block != nil {
b.count.Inc()
j := block.ffz()
block.mark(j)
index = (b.curIndex * blockSize) + j
}
}
b.mu.Unlock()
return
}
func (b *BitArray) nextFree() *BitBlock {
for i := int64(0); i < b.size; i++ {
if block := b.current(); block.hasRoom() {
return block
}
b.curIndex = (b.curIndex + 1) % b.size
}
return nil
}
func (b *BitArray) current() *BitBlock {
return &b.blocks[b.curIndex]
}
func bitIndexAndNum(i int64) (int64, int64) {
return i / blockSize, i % blockSize
}
func (b BitBlock) value(bit int64) bool {
return (b & mask(bit)) != 0
}
func (b *BitBlock) mark(bit int64) {
*b |= mask(bit)
}
func (b *BitBlock) unmark(bit int64) {
*b &^= mask(bit)
}
func (b *BitBlock) compareAndMark(bit int64) (changed bool) {
n := mask(bit)
changed = (*b & n) == 0
if changed {
*b |= n
}
return
}
func (b *BitBlock) compareAndUnmark(bit int64) (changed bool) {
n := mask(bit)
changed = (*b & n) != 0
if changed {
*b &^= n
}
return
}
func (b BitBlock) hasRoom() bool {
return b != bitBlockFull
}
func (b BitBlock) ffz() int64 {
v := (b & (^b - 1))
switch blockSize {
case 64:
return popcount64(uint64(v))
case 32:
return popcount32(uint32(v))
default:
panic("wrong block size")
}
}
func popcount64(b uint64) int64 {
const (
m1 = 0x5555555555555555 // binary: 0101...
m2 = 0x3333333333333333 // binary: 00110011..
m4 = 0x0f0f0f0f0f0f0f0f // binary: 4 zeros, 4 ones ...
)
b -= (b >> 1) & m1 // put count of each 2 bits into those 2 bits
b = (b & m2) + ((b >> 2) & m2) // put count of each 4 bits into those 4 bits
b = (b + (b >> 4)) & m4 // put count of each 8 bits into those 8 bits
b += b >> 8 // put count of each 16 bits into their lowest 8 bits
b += b >> 16 // put count of each 32 bits into their lowest 8 bits
b += b >> 32 // put count of each 64 bits into their lowest 8 bits
return int64(b & 0x7f)
}
func popcount32(b uint32) int64 {
const (
m1 = 0x55555555 // binary: 0101...
m2 = 0x33333333 // binary: 00110011..
m4 = 0x0f0f0f0f // binary: 4 zeros, 4 ones ...
)
b -= (b >> 1) & m1
b = (b & m2) + ((b >> 2) & m2)
b = (b + (b >> 4)) & m4
b = b + (b >> 8)
b = b + (b >> 16)
return int64(b & 0x3f)
}
func mask(bit int64) BitBlock {
return 1 << bit
}