-
Notifications
You must be signed in to change notification settings - Fork 22
/
bit_index.hpp
393 lines (337 loc) · 11.4 KB
/
bit_index.hpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#pragma once
#include <stdint.h>
#include <assert.h>
#include <stdio.h>
#define LZERO(X) (__builtin_clzll((X)) )
template<uint64_t>
class bit_index;
template<uint64_t x>
struct log64;
template<>
struct log64<64> { enum { value = 1 }; };
template<>
struct log64<0> { enum { value = 0 }; };
template<uint64_t x>
struct log64 { enum { value = 1 + log64<x/64>::value }; };
template<uint64_t x>
struct pow64;
template<>
struct pow64<0> { enum ev{ value = 1 }; };
template<uint64_t x>
struct pow64 { enum ev{ value = pow64<x-1>::value*64ll }; };
template<>
class bit_index<1>
{
public:
enum size_enum { index_size = 1 };
void set( uint64_t pos = 0)
{
assert( pos == 0 );
bit = 1;
}
bool get( uint32_t pos = 0)const { return bit; }
uint64_t& get_bits(uint64_t ) { return bit; }
bool clear( uint64_t pos = 0)
{
assert( pos == 0 );
return !(bit = 0);
}
void clear_all() { clear(); }
void set_all() { set(); }
uint64_t first_set_bit()const { return !bit; }
uint64_t size()const { return 1; }
struct iterator
{
public:
uint64_t& get_bits() { return _self->bit; }
bool end()const { return _bit == 1; }
int64_t bit()const { return _bit; }
void set() { _self->set(_bit); }
bool clear() { return _self->clear(_bit); }
bool operator*()const { return _self->get(_bit); }
iterator& next_set_bit()
{
_bit = 1;
return *this;
}
iterator( bit_index* s=nullptr, uint8_t b = 64 ):_self(s),_bit(b){}
private:
bit_index* _self;
uint8_t _bit;
};
iterator at( uint64_t p ) { return iterator(this, p); }
private:
uint64_t bit;
};
template<>
class bit_index<0> : public bit_index<1>{};
template<>
class bit_index<64>
{
public:
enum size_enum { index_size = 64 };
bit_index(uint64_t s = 0):_bits(s){}
/**
* option A: use conditional to check for 0 and return 64
*/
uint64_t first_set_bit()const {
return _bits == 0 ? 64 : LZERO(_bits);
}
void dump( int depth )
{
for( int i = 0; i < depth; ++i )
fprintf( stderr, " " );
fprintf( stderr, "%llx\n", _bits );
}
/**
* Option 2, compare + shift + lzr + compare + mult + or... this approach.. while
* the result of LZERO(0) is undefined, multiplying it by 0 is defined.
*
* This code may be faster or slower depending upon this cache miss rate and
* the instruction level parallelism. Benchmarks are required.
*/
//uint64_t first_set_bit()const { return (_bits == 0)<<6 | (LZERO(_bits) * (_bits!=0)); }
bool get( uint64_t pos )const { return _bits & (1ll<<(63-pos)); }
void set( uint64_t pos )
{
assert( pos < 64 );
_bits |= (1ll<<(63-pos));
}
bool clear( uint64_t pos )
{
// fprintf( stderr, "bit_index<64>::clear %llu\n", pos );
_bits &= ~(1ll<<(63-pos));
//fprintf( stderr, "bit_index<64> clear: %p %llx\n", this, _bits );
//fprintf( stderr, "bit_index<64>::clear %llu return %llu == 0\n", pos, _bits );
return _bits == 0;
}
uint64_t size()const { return 64; }
uint64_t count()const { return __builtin_popcountll(_bits); }
void set_all() { _bits = -1; }
void clear_all() { _bits = 0; }
uint64_t& get_bits( uint64_t bit )
{
assert( bit < 64 );
return _bits;
}
struct iterator
{
public:
uint64_t& get_bits() { return _self->_bits; }
bool end()const { return _bit == 64; }
int64_t bit()const { return _bit; }
void set() { _self->set(_bit); }
bool clear() { return _self->clear(_bit); }
bool operator*()const { return _self->get(_bit); }
iterator& next_set_bit()
{
++_bit;
if( end() ) return *this;
bit_index tmp( (_self->_bits << (_bit))>>(_bit) );
_bit = tmp.first_set_bit();
return *this;
}
iterator( bit_index* s=nullptr, uint8_t b = 64 ):_self(s),_bit(b){}
private:
bit_index* _self;
uint8_t _bit;
};
iterator begin() { return iterator(this,0); }
iterator at(uint8_t i){ return iterator(this,i); }
iterator end() { return iterator(this,64); }
protected:
friend class iterator;
uint64_t _bits;
};
/**
* A bit_index is a bitset optimized for searching for set bits. The
* operations set and clear maintain higher-level indexes to optimize
* finding of set bits.
*
* The fundamental size is 64 bit and the first set bit can be found
* with a single instruction. For indexes up-to 64*64 in size, the
* first set bit can be found with 2 clz + 1 compare + 1 mult + 1 add.
*
*/
template<uint64_t Size>
class bit_index
{
public:
static_assert( Size >= 64, "smaller sizes not yet supported" );
enum size_enum {
index_size = Size,
sub_index_size = (Size+63) / 64,
sub_index_count = Size / sub_index_size
};
static_assert( bit_index::sub_index_count > 0, "array with size 0 is too small" );
static_assert( bit_index::sub_index_count <= 64, "array with size 64 is too big" );
void dump( int depth = 0 )
{
_base_index.dump( depth + 1 );
for( int i = 0; i < 3; ++i )
_sub_index[i].dump( depth + 2 );
/**
for( int i = 0; i < depth; ++i )
fprintf( stderr, " " );
fprintf( stderr, "%llx\n", _bits );
*/
}
uint64_t size()const { return index_size; }
uint64_t first_set_bit()const
{
uint64_t base = _base_index.first_set_bit();
if( base >= sub_index_count )
{
return Size;
}
auto subidx = _sub_index[base].first_set_bit();
return base * sub_index_size + subidx; //_sub_index[base].first_set_bit();
}
bool get( uint64_t bit )const
{
assert( bit < Size );
int64_t sub_idx = (bit/sub_index_size);
int64_t sub_idx_bit = (bit%sub_index_size);
return _sub_index[sub_idx].get( sub_idx_bit );
}
void set( uint64_t bit )
{
assert( bit < Size );
int64_t sub_idx = (bit/sub_index_size);
int64_t sub_idx_bit = (bit%sub_index_size);
_base_index.set(sub_idx);
return _sub_index[sub_idx].set( sub_idx_bit );
}
bool clear( uint64_t bit )
{
assert( bit < Size );
int64_t sub_idx = (bit/sub_index_size);
int64_t sub_idx_bit = (bit%sub_index_size);
if( _sub_index[sub_idx].clear( sub_idx_bit ) )
return _base_index.clear(sub_idx);
return false;
}
void set_all()
{
_base_index.set_all();
for( uint64_t i = 0; i < sub_index_count; ++i )
{
_sub_index[i].set_all();
}
}
void clear_all()
{
_base_index.clear_all();
for( uint64_t i = 0; i < sub_index_count; ++i )
{
_sub_index[i].clear_all();
}
}
uint64_t count()const
{
uint64_t c = 0;
for( uint64_t i = 0; i < sub_index_count; ++i )
{
c+=_sub_index[i].count();
}
return 0;
}
/**
* Returns the in64_t that contains bit
*/
uint64_t& get_bits( uint64_t bit )
{
int64_t sub_idx = (bit/sub_index_size);
int64_t sub_idx_bit = (bit%sub_index_size);
return _sub_index[sub_idx].get_bits( sub_idx_bit );
}
struct iterator
{
public:
uint64_t& get_bits() { return sub_itr.get_bits(); }
bool operator*()const { return *sub_itr; }
bool end()const { return sub_idx >= sub_index_count; }
int64_t bit()const { return pos; }
void set()
{
bit_idx->_base_index.set(sub_idx);
sub_itr.set();
}
bool clear()
{
if( sub_itr.clear() )
{
return bit_idx->_base_index.clear(sub_idx);
}
return false;
}
/**
* Find the next bit after this one that is set..
*/
iterator& next_set_bit()
{
if( end() ) return *this;
sub_itr.next_set_bit();
if( sub_itr.end() )
{
sub_idx = bit_idx->_base_index.at(sub_idx).next_set_bit().bit();
if( end() )
{
pos = Size;
return *this;
}
auto fb = bit_idx->_sub_index[sub_idx].first_set_bit();
sub_itr = bit_idx->_sub_index[sub_idx].at(fb);
}
pos = sub_idx * sub_index_size + sub_itr.bit();
return *this;
}
/**
* Move to the next bit.
*/
iterator& operator++()
{
assert( !end() );
++pos;
++sub_itr;
if( sub_itr.end() )
{
++sub_idx;
if( !end() )
{
sub_itr = bit_idx->_sub_index[sub_idx].begin();
}
else pos = Size;
}
return *this;
}
iterator& operator++(int) { return this->operator++(); }
iterator operator+(uint64_t delta) { return iterator( bit_idx, pos + delta ); }
iterator( bit_index* self=nullptr, int64_t bit=Size)
:bit_idx(self),pos(bit),sub_idx((bit/64)%64)
{
sub_itr = bit_idx->_sub_index[sub_idx].at(bit%sub_index_size);
}
iterator& operator=(const iterator& i )
{
bit_idx = i.bit_idx;
pos = i.pos;
sub_idx = i.sub_idx;
sub_itr = i.sub_itr;
return *this;
}
private:
friend class bit_index;
bit_index* bit_idx;
int64_t pos;
int8_t sub_idx;
typename bit_index<sub_index_size>::iterator sub_itr;
};
iterator begin() { return iterator( this, 0 ); }
iterator end() { return iterator( this, Size ); }
iterator at(int64_t p) { return iterator( this, p ); }
protected:
friend class iterator;
bit_index<64> _base_index;
bit_index<sub_index_size> _sub_index[sub_index_count];
};