36 #ifdef _GLIBCPP_BITSET_BITS_PER_WORD
37 #define BITS_PER_WORD _GLIBCPP_BITSET_BITS_PER_WORD
39 #define BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long))
42 #ifndef _GLIBCPP_BITSET_WORDS
43 #define BITSET_WORDS(__n) \
44 ((__n) < 1 ? 1 : ((__n) + BITS_PER_WORD - 1)/BITS_PER_WORD)
46 #define BITSET_WORDS(__n) _GLIBCPP_BITSET_WORDS(__n)
50 #define MASK ULLONG_MAX
57 typedef unsigned long _ul;
104 operator bool()
const
150 {
for(
unsigned int i = 0; i <
_words; i++)
_data[i] = 0UL; }
156 {
return &
_data[ index ]; }
162 bool at (
size_t word,
unsigned long bitmask)
const
164 return bool(
_data[ word ] & bitmask );
187 for (
size_t i = 0; i <
_words; i++)
194 for (
size_t i = 0; i <
_words; i++)
201 for (
size_t i = 0; i <
_words; i++)
208 for (
size_t i = 0; i <
_words; i++)
239 return (
unsigned int) __builtin_popcountl(wd);
244 unsigned char* c = (
unsigned char*)&wd;
245 unsigned short cnt = 0;
247 for(
unsigned int i = 0; i <
sizeof(
_ul); i++)
250 return (
unsigned int) cnt;
257 unsigned int cnt = 0;
259 for(
size_t i = 0; i <
_words; i++)
266 unsigned int count (
size_t from,
size_t to)
268 assert(from < to && to <=
_size);
270 unsigned int cnt = 0;
276 if (start_w == end_w) {
285 for (
size_t i = start_w + 1; i < end_w; ++i)
298 unsigned int cnt = 0;
299 for (
size_t i = 0; i <
_words; ++i)
307 unsigned int cnt = 0;
308 for (
size_t i = 0; i <
_words; ++i)
316 unsigned int cnt = 0;
317 for (
size_t i = 0; i <
_words; ++i)
325 unsigned int cnt = 0;
326 for (
size_t i = 0; i <
_words; ++i)
335 void set (
size_t n,
bool x) {
353 memcpy(
_data, srce, nbwrd *
sizeof(
_ul));
371 size_t start_w, end_w, start_l, end_l;
380 if(start_w != end_w) {
382 mask = (
MASK << start_l);
384 _data[ start_w ] &= ~mask;
385 tmpl = b.
_data[ start_w ] & mask;
387 _data[ start_w ] |= tmpl;
390 size_t k = start_w + 1;
400 _data[ end_w ] &= ~mask;
401 tmpl = b.
_data[ end_w ] & mask;
403 _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:43
#define MASK
Definition: bitstring.h:50
#define BITS_PER_WORD
Definition: bitstring.h:39
Definition: bitstring.h:60
reference(bitstring &bs, size_t pos)
Definition: bitstring.h:70
reference & operator=(bool x)
Definition: bitstring.h:80
_ul * _word
Definition: bitstring.h:63
reference & flip()
Definition: bitstring.h:108
size_t _bitpos
Definition: bitstring.h:64
~reference()
Definition: bitstring.h:76
bool operator~() const
Definition: bitstring.h:100
reference & operator=(const reference &j)
Definition: bitstring.h:90
Non-template and faster implementation of std::bitset.
Definition: bitstring.h:53
static unsigned char _bit_count[256]
Definition: bitstring.h:480
bitstring(const bitstring &b)
Definition: bitstring.h:126
bitstring operator|(const bitstring &x)
Definition: bitstring.h:220
unsigned int count()
Count number of set bits.
Definition: bitstring.h:255
bool at(size_t word, unsigned long bitmask) const
Definition: bitstring.h:162
unsigned int count(size_t from, size_t to)
Count set bits in the range [from, to).
Definition: bitstring.h:266
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:323
_ul * getword_atPos(size_t pos) const
Definition: bitstring.h:152
unsigned long _ul
Definition: bitstring.h:57
bitstring()
Definition: bitstring.h:117
size_t size() const
Definition: bitstring.h:158
_ul * getword_atIdx(size_t index) const
Definition: bitstring.h:155
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:305
void print(size_t from, size_t to) const
Definition: bitstring.h:453
bitstring operator&(const bitstring &x)
Definition: bitstring.h:213
unsigned int count_and(const bitstring &mask) const
Masked popcount: count set bits in (this AND mask).
Definition: bitstring.h:296
bitstring & operator|=(const bitstring &x)
Definition: bitstring.h:192
std::string to_string() const
Definition: bitstring.h:419
reference operator[](size_t pos)
Definition: bitstring.h:167
void set(size_t n, bool x)
Set a bit to 0 or 1.
Definition: bitstring.h:335
void reset()
Set all bits to 0.
Definition: bitstring.h:149
bitstring & operator=(const bitstring &b)
Definition: bitstring.h:173
bitstring & operator^=(const bitstring &x)
Definition: bitstring.h:199
friend class reference
Definition: bitstring.h:115
void set(size_t n)
Set a bit to 1.
Definition: bitstring.h:332
bitstring operator~(void)
Definition: bitstring.h:206
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:242
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:133
void set_data(_ul *srce, size_t nbwrd)
Copy bits from an array of unsigned long words.
Definition: bitstring.h:346
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:343
void print() const
Definition: bitstring.h:461
bitstring operator^(const bitstring &x)
Definition: bitstring.h:227
void copy(const bitstring &b, size_t word_pos)
Copy one word.
Definition: bitstring.h:361
bitstring & operator&=(const bitstring &x)
Definition: bitstring.h:185
bitstring(size_t length)
Definition: bitstring.h:119
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:314
void copy(const bitstring &b)
Unchecked copy, assumes we have sames sizes.
Definition: bitstring.h:357
void copy(const bitstring &b, size_t from, size_t to)
Copy a delimited sequence block.
Definition: bitstring.h:365
size_t nb_words() const
Definition: bitstring.h:160
void reset(size_t length)
Definition: bitstring.h:136