38 #ifdef _GLIBCPP_BITSET_BITS_PER_WORD
39 #define BITS_PER_WORD _GLIBCPP_BITSET_BITS_PER_WORD
41 #define BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long))
44 #ifndef _GLIBCPP_BITSET_WORDS
45 #define BITSET_WORDS(__n) \
46 ((__n) < 1 ? 1 : ((__n) + BITS_PER_WORD - 1)/BITS_PER_WORD)
48 #define BITSET_WORDS(__n) _GLIBCPP_BITSET_WORDS(__n)
52 #define MASK ULLONG_MAX
61 typedef unsigned long _ul;
108 operator bool()
const
154 {
for(
unsigned int i = 0; i <
_words; i++)
_data[i] = 0UL; }
160 {
return &
_data[ index ]; }
166 bool at (
size_t word,
unsigned long bitmask)
const
168 return bool(
_data[ word ] & bitmask );
191 for (
size_t i = 0; i <
_words; i++)
198 for (
size_t i = 0; i <
_words; i++)
205 for (
size_t i = 0; i <
_words; i++)
212 for (
size_t i = 0; i <
_words; i++)
243 return (
unsigned int) __builtin_popcountl(wd);
248 unsigned char* c = (
unsigned char*)&wd;
249 unsigned short cnt = 0;
251 for(
unsigned int i = 0; i <
sizeof(
_ul); i++)
254 return (
unsigned int) cnt;
261 unsigned int cnt = 0;
263 for(
size_t i = 0; i <
_words; i++)
270 unsigned int count (
size_t from,
size_t to)
272 assert(from < to && to <=
_size);
274 unsigned int cnt = 0;
280 if (start_w == end_w) {
289 for (
size_t i = start_w + 1; i < end_w; ++i)
302 unsigned int cnt = 0;
303 for (
size_t i = 0; i <
_words; ++i)
311 unsigned int cnt = 0;
312 for (
size_t i = 0; i <
_words; ++i)
320 unsigned int cnt = 0;
321 for (
size_t i = 0; i <
_words; ++i)
329 unsigned int cnt = 0;
330 for (
size_t i = 0; i <
_words; ++i)
339 void set (
size_t n,
bool x) {
357 memcpy(
_data, srce, nbwrd *
sizeof(
_ul));
375 size_t start_w, end_w, start_l, end_l;
384 if(start_w != end_w) {
386 mask = (
MASK << start_l);
388 _data[ start_w ] &= ~mask;
389 tmpl = b.
_data[ start_w ] & mask;
391 _data[ start_w ] |= tmpl;
394 size_t k = start_w + 1;
401 _data[ end_w ] &= ~mask;;
402 tmpl = b.
_data[ end_w ] & mask;
404 _data[ end_w ] |= tmpl;
410 _data[ start_w ] &= ~mask;
411 tmpl = b.
_data[ start_w ] & mask;
413 _data[ start_w ] |= tmpl;
421 const char one =
'1';
424 result.assign(
_size,
'0');
426 for(
size_t i = 0; i <
_size; ++i) {
436 assert(from < to && to <
_size+1);
438 const char one =
'1';
440 size_t len = to - from;
444 result.assign(len,
'0');
446 for(
size_t i = from, s = 0; i < to && s < len; ++i, ++s) {
453 void print (
size_t from,
size_t to)
const
455 assert(from < to && to <
_size+1);
456 for(
unsigned int i = from; i < to; ++i)
458 std::cout<<std::endl;
463 for(
unsigned int i = 0; i <
_size; ++i)
465 std::cout<<std::endl;
#define BITSET_WORDS(__n)
Definition: bitstring.h:45
#define MASK
Definition: bitstring.h:52
#define BITS_PER_WORD
Definition: bitstring.h:41
Definition: bitstring.h:64
reference(bitstring &bs, size_t pos)
Definition: bitstring.h:74
reference & operator=(bool x)
Definition: bitstring.h:84
_ul * _word
Definition: bitstring.h:67
reference & flip()
Definition: bitstring.h:112
size_t _bitpos
Definition: bitstring.h:68
~reference()
Definition: bitstring.h:80
bool operator~() const
Definition: bitstring.h:104
reference & operator=(const reference &j)
Definition: bitstring.h:94
Non-template and faster implementation of std::bitset.
Definition: bitstring.h:57
static unsigned char _bit_count[256]
Definition: bitstring.h:480
bitstring(const bitstring &b)
Definition: bitstring.h:130
bitstring operator|(const bitstring &x)
Definition: bitstring.h:224
unsigned int count()
Count number of set bits.
Definition: bitstring.h:259
bool at(size_t word, unsigned long bitmask) const
Definition: bitstring.h:166
unsigned int count(size_t from, size_t to)
Count set bits in the range [from, to).
Definition: bitstring.h:270
unsigned int count_and_and(const bitstring &other, const bitstring &mask) const
Fused AND + mask popcount: count set bits in ((this AND other) AND mask).
Definition: bitstring.h:327
_ul * getword_atPos(size_t pos) const
Definition: bitstring.h:156
unsigned long _ul
Definition: bitstring.h:61
bitstring()
Definition: bitstring.h:121
size_t size() const
Definition: bitstring.h:162
_ul * getword_atIdx(size_t index) const
Definition: bitstring.h:159
size_t _words
Number of _ul-long Words necessary to hold the _size bits.
Definition: bitstring.h:473
unsigned int count_xor(const bitstring &other) const
Fused XOR popcount: count set bits in (this XOR other).
Definition: bitstring.h:309
void print(size_t from, size_t to) const
Definition: bitstring.h:453
bitstring operator&(const bitstring &x)
Definition: bitstring.h:217
unsigned int count_and(const bitstring &mask) const
Masked popcount: count set bits in (this AND mask).
Definition: bitstring.h:300
bitstring & operator|=(const bitstring &x)
Definition: bitstring.h:196
std::string to_string() const
Definition: bitstring.h:419
reference operator[](size_t pos)
Definition: bitstring.h:171
void set(size_t n, bool x)
Set a bit to 0 or 1.
Definition: bitstring.h:339
void reset()
Set all bits to 0.
Definition: bitstring.h:153
bitstring & operator=(const bitstring &b)
Definition: bitstring.h:177
bitstring & operator^=(const bitstring &x)
Definition: bitstring.h:203
friend class reference
Definition: bitstring.h:119
void set(size_t n)
Set a bit to 1.
Definition: bitstring.h:336
bitstring operator~(void)
Definition: bitstring.h:210
unsigned int local_popcountl(_ul wd) const
Counts number of one's in a word using hardware POPCNT when available, falling back to a byte-table l...
Definition: bitstring.h:246
size_t _size
Number of bits in the sequence.
Definition: bitstring.h:470
_ul * _data
The sequence.
Definition: bitstring.h:476
~bitstring()
Definition: bitstring.h:137
void set_data(_ul *srce, size_t nbwrd)
Copy bits from an array of unsigned long words.
Definition: bitstring.h:350
std::string to_string(size_t from, size_t to) const
Definition: bitstring.h:434
void flip(size_t n)
Flip the bit at n.
Definition: bitstring.h:347
void print() const
Definition: bitstring.h:461
bitstring operator^(const bitstring &x)
Definition: bitstring.h:231
void copy(const bitstring &b, size_t word_pos)
Copy one word.
Definition: bitstring.h:365
bitstring & operator&=(const bitstring &x)
Definition: bitstring.h:189
bitstring(size_t length)
Definition: bitstring.h:123
unsigned int count_xor_and(const bitstring &other, const bitstring &mask) const
Fused XOR + mask popcount: count set bits in ((this XOR other) AND mask).
Definition: bitstring.h:318
void copy(const bitstring &b)
Unchecked copy, assumes we have sames sizes.
Definition: bitstring.h:361
void copy(const bitstring &b, size_t from, size_t to)
Copy a delimited sequence block.
Definition: bitstring.h:369
size_t nb_words() const
Definition: bitstring.h:164
void reset(size_t length)
Definition: bitstring.h:140