Nemo  2.4.0
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
bitstring Class Reference

Non-template and faster implementation of std::bitset. More...

#include <bitstring.h>

+ Collaboration diagram for bitstring:

Classes

class  reference
 

Public Types

typedef unsigned long _ul
 

Public Member Functions

 bitstring ()
 
 bitstring (size_t length)
 
 bitstring (const bitstring &b)
 
 ~bitstring ()
 
void reset (size_t length)
 
void reset ()
 Set all bits to 0. More...
 
_ulgetword_atPos (size_t pos) const
 
_ulgetword_atIdx (size_t index) const
 
size_t size () const
 
size_t nb_words () const
 
bool at (size_t word, unsigned long bitmask) const
 
reference operator[] (size_t pos)
 
bool operator[] (size_t n) const
 
bitstringoperator= (const bitstring &b)
 
bitstringoperator&= (const bitstring &x)
 
bitstringoperator|= (const bitstring &x)
 
bitstringoperator^= (const bitstring &x)
 
bitstring operator~ (void)
 
bitstring operator& (const bitstring &x)
 
bitstring operator| (const bitstring &x)
 
bitstring operator^ (const bitstring &x)
 
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 lookup otherwise. More...
 
unsigned int count ()
 Count number of set bits. More...
 
unsigned int count (size_t from, size_t to)
 Count set bits in the range [from, to). More...
 
unsigned int count_and (const bitstring &mask) const
 Masked popcount: count set bits in (this AND mask). More...
 
unsigned int count_xor (const bitstring &other) const
 Fused XOR popcount: count set bits in (this XOR other). More...
 
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). More...
 
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). More...
 
void set (size_t n)
 Set a bit to 1. More...
 
void set (size_t n, bool x)
 Set a bit to 0 or 1. More...
 
void flip (size_t n)
 Flip the bit at n. More...
 
void set_data (_ul *srce, size_t nbwrd)
 Copy bits from an array of unsigned long words. More...
 
void copy (const bitstring &b)
 Unchecked copy, assumes we have sames sizes. More...
 
void copy (const bitstring &b, size_t word_pos)
 Copy one word. More...
 
void copy (const bitstring &b, size_t from, size_t to)
 Copy a delimited sequence block. More...
 
std::string to_string () const
 
std::string to_string (size_t from, size_t to) const
 
void print (size_t from, size_t to) const
 
void print () const
 

Private Attributes

size_t _size
 Number of bits in the sequence. More...
 
size_t _words
 Number of _ul-long Words necessary to hold the _size bits. More...
 
_ul_data
 The sequence. More...
 

Static Private Attributes

static unsigned char _bit_count [256]
 

Friends

class reference
 

Detailed Description

Non-template and faster implementation of std::bitset.

Member Typedef Documentation

◆ _ul

typedef unsigned long bitstring::_ul

Constructor & Destructor Documentation

◆ bitstring() [1/3]

bitstring::bitstring ( )
inline
120 : _size(0), _words(0), _data(0) {}
size_t _words
Number of _ul-long Words necessary to hold the _size bits.
Definition: bitstring.h:476
size_t _size
Number of bits in the sequence.
Definition: bitstring.h:473
_ul * _data
The sequence.
Definition: bitstring.h:479

◆ bitstring() [2/3]

bitstring::bitstring ( size_t  length)
inline
123  : _size(length), _words( BITSET_WORDS(length) ), _data(0)
124  {
125  _data = new _ul [_words];
126  memset(_data, 0, _words * sizeof(_ul));
127  }
#define BITSET_WORDS(__n)
Definition: bitstring.h:44
unsigned long _ul
Definition: bitstring.h:60

References _data, and _words.

◆ bitstring() [3/3]

bitstring::bitstring ( const bitstring b)
inline
130  : _size(b._size), _words(b._words), _data(0)
131  {
132  _data = new _ul [_words];
133  memcpy(_data, b._data, _words * sizeof(_ul));
134  }

References _data, and _words.

◆ ~bitstring()

bitstring::~bitstring ( )
inline
136 {if(_data != NULL) delete [] _data;}

References _data.

Member Function Documentation

◆ at()

bool bitstring::at ( size_t  word,
unsigned long  bitmask 
) const
inline
166  {
167  return bool( _data[ word ] & bitmask );
168  }

References _data.

◆ copy() [1/3]

◆ copy() [2/3]

void bitstring::copy ( const bitstring b,
size_t  from,
size_t  to 
)
inline

Copy a delimited sequence block.

369  {
370  assert(to <= _size);
371 
372  if(to != from) { // only if we do have something to copy
373 
374  size_t start_w, end_w, start_l, end_l;
375  _ul mask, tmpl;
376 
377  start_w = from / BITS_PER_WORD;
378  end_w = to / BITS_PER_WORD;
379 
380  start_l = from % BITS_PER_WORD;
381  end_l = to % BITS_PER_WORD;
382 
383  if(start_w != end_w) {
384  //copy wihtin first word:
385  mask = (MASK << start_l);
386 
387  _data[ start_w ] &= ~mask;
388  tmpl = b._data[ start_w ] & mask;
389 
390  _data[ start_w ] |= tmpl;
391 
392  //copy words in-between:
393  size_t k = start_w + 1;
394 
395  memcpy(&_data[k], &b._data[k], (end_w - k)*sizeof(_ul));
396 
397  //copy within last word; skip when 'to' is word-aligned (end_l == 0),
398  //since end_w then sits one past the last valid word and there's
399  //nothing left to copy beyond what the memcpy above already did.
400  if (end_l != 0) {
401  mask = (MASK >> (BITS_PER_WORD - end_l) );
402 
403  _data[ end_w ] &= ~mask;
404  tmpl = b._data[ end_w ] & mask;
405 
406  _data[ end_w ] |= tmpl;
407  }
408 
409  } else {
410  //bits to copy are within a word:
411  mask = (MASK << start_l) & (MASK >> (BITS_PER_WORD - end_l) );
412 
413  _data[ start_w ] &= ~mask;
414  tmpl = b._data[ start_w ] & mask;
415 
416  _data[ start_w ] |= tmpl;
417 
418  }
419  }
420  }
#define MASK
Definition: bitstring.h:51
#define BITS_PER_WORD
Definition: bitstring.h:40

References _data, _size, BITS_PER_WORD, and MASK.

◆ copy() [3/3]

void bitstring::copy ( const bitstring b,
size_t  word_pos 
)
inline

Copy one word.

365  { _data[ word_pos ] = b._data[ word_pos ]; }

References _data.

◆ count() [1/2]

unsigned int bitstring::count ( )
inline

Count number of set bits.

259  {
260  unsigned int cnt = 0;
261 
262  for(size_t i = 0; i < _words; i++)
263  cnt += local_popcountl(_data[i]);
264 
265  return cnt;
266  }
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:245

References _data, _words, and local_popcountl().

Referenced by TTQuanti_diallelic_bitstring_no_pleio::get_additive_genotype_equal_effects(), TTDeletMutations_bitstring::set_nb_hmz_mutations(), TTDeletMutations_bitstring::set_nb_htz_mutations(), and TTDeletMutations_bitstring::set_nb_mutations().

+ Here is the caller graph for this function:

◆ count() [2/2]

unsigned int bitstring::count ( size_t  from,
size_t  to 
)
inline

Count set bits in the range [from, to).

270  {
271  assert(from < to && to <= _size);
272 
273  unsigned int cnt = 0;
274  size_t start_w = from / BITS_PER_WORD;
275  size_t end_w = to / BITS_PER_WORD;
276  size_t start_l = from % BITS_PER_WORD;
277  size_t end_l = to % BITS_PER_WORD;
278 
279  if (start_w == end_w) {
280  _ul mask = (MASK << start_l) & (MASK >> (BITS_PER_WORD - end_l));
281  return local_popcountl( _ul(_data[start_w] & mask) );
282  }
283 
284  // first partial word
285  cnt += local_popcountl(_data[start_w] & (MASK << start_l));
286 
287  // full words in between
288  for (size_t i = start_w + 1; i < end_w; ++i)
289  cnt += local_popcountl(_data[i]);
290 
291  // last partial word
292  if (end_l > 0)
293  cnt += local_popcountl(_data[end_w] & (MASK >> (BITS_PER_WORD - end_l)));
294 
295  return cnt;
296  }

References _data, _size, BITS_PER_WORD, local_popcountl(), and MASK.

◆ count_and()

unsigned int bitstring::count_and ( const bitstring mask) const
inline

Masked popcount: count set bits in (this AND mask).

300  {
301  unsigned int cnt = 0;
302  for (size_t i = 0; i < _words; ++i)
303  cnt += local_popcountl(_data[i] & mask._data[i]);
304  return cnt;
305  }

References _data, _words, and local_popcountl().

Referenced by TTQuanti_diallelic_bitstring_no_pleio::get_additive_genotype(), TTQuanti_diallelic_bitstring_full_pleio::get_additive_genotype(), TTQuanti_diallelic_bitstring_var_pleio::get_additive_genotype(), and TTDeletMutations_bitstring::set_value().

+ Here is the caller graph for this function:

◆ count_and_and()

unsigned int bitstring::count_and_and ( const bitstring other,
const bitstring mask 
) const
inline

Fused AND + mask popcount: count set bits in ((this AND other) AND mask).

327  {
328  unsigned int cnt = 0;
329  for (size_t i = 0; i < _words; ++i)
330  cnt += local_popcountl((_data[i] & other._data[i]) & mask._data[i]);
331  return cnt;
332  }

References _data, _words, and local_popcountl().

Referenced by TTQuanti_diallelic_bitstring_no_pleio::get_dominant_genotype(), TTQuanti_diallelic_bitstring_full_pleio::get_dominant_genotype(), and TTQuanti_diallelic_bitstring_var_pleio::get_dominant_genotype().

+ Here is the caller graph for this function:

◆ count_xor()

unsigned int bitstring::count_xor ( const bitstring other) const
inline

Fused XOR popcount: count set bits in (this XOR other).

309  {
310  unsigned int cnt = 0;
311  for (size_t i = 0; i < _words; ++i)
312  cnt += local_popcountl(_data[i] ^ other._data[i]);
313  return cnt;
314  }

References _data, _words, and local_popcountl().

Referenced by TTDeletMutations_bitstring::set_value(), and TTNeutralGenesSH::setHo().

+ Here is the caller graph for this function:

◆ count_xor_and()

unsigned int bitstring::count_xor_and ( const bitstring other,
const bitstring mask 
) const
inline

Fused XOR + mask popcount: count set bits in ((this XOR other) AND mask).

318  {
319  unsigned int cnt = 0;
320  for (size_t i = 0; i < _words; ++i)
321  cnt += local_popcountl((_data[i] ^ other._data[i]) & mask._data[i]);
322  return cnt;
323  }

References _data, _words, and local_popcountl().

Referenced by TTQuanti_diallelic_bitstring_no_pleio::get_dominant_genotype(), TTQuanti_diallelic_bitstring_full_pleio::get_dominant_genotype(), and TTQuanti_diallelic_bitstring_var_pleio::get_dominant_genotype().

+ Here is the caller graph for this function:

◆ flip()

◆ getword_atIdx()

◆ getword_atPos()

_ul* bitstring::getword_atPos ( size_t  pos) const
inline
156  { return &_data[ pos / BITS_PER_WORD ]; }

References _data, and BITS_PER_WORD.

Referenced by bitstring::reference::reference().

+ Here is the caller graph for this function:

◆ local_popcountl()

unsigned int bitstring::local_popcountl ( _ul  wd) const
inline

Counts number of one's in a word using hardware POPCNT when available, falling back to a byte-table lookup otherwise.

246  {
247  unsigned char* c = (unsigned char*)&wd;
248  unsigned short cnt = 0;
249 
250  for(unsigned int i = 0; i < sizeof(_ul); i++)
251  cnt += _bit_count[ (unsigned int)c[i] ];
252 
253  return (unsigned int) cnt;
254  }
static unsigned char _bit_count[256]
Definition: bitstring.h:483

References _bit_count.

Referenced by count(), count_and(), count_and_and(), count_xor(), and count_xor_and().

+ Here is the caller graph for this function:

◆ nb_words()

◆ operator&()

bitstring bitstring::operator& ( const bitstring x)
inline
217  {
218  bitstring result(*this);
219  result &= x;
220  return result;
221  }
Non-template and faster implementation of std::bitset.
Definition: bitstring.h:56

◆ operator&=()

bitstring& bitstring::operator&= ( const bitstring x)
inline
189  {
190  for (size_t i = 0; i < _words; i++)
191  _data[i] &= x._data[i];
192  return *this;
193  }

References _data, and _words.

◆ operator=()

bitstring& bitstring::operator= ( const bitstring b)
inline
177  {
178  _size = b._size;
179  if (_words != b._words) {
180  _words = b._words;
181  if(_data != NULL) delete [] _data;
182  _data = new _ul [_words];
183  }
184  memcpy(_data, b._data, _words * sizeof(_ul));
185  return *this;
186  }

References _data, _size, and _words.

◆ operator[]() [1/2]

bool bitstring::operator[] ( size_t  n) const
inline
174  { return bool( _data[ n / BITS_PER_WORD ] & ( 1UL << ( n % BITS_PER_WORD ) ) ); }

References _data, and BITS_PER_WORD.

◆ operator[]() [2/2]

reference bitstring::operator[] ( size_t  pos)
inline
171  { return reference(*this, pos); }
friend class reference
Definition: bitstring.h:118

References reference.

◆ operator^()

bitstring bitstring::operator^ ( const bitstring x)
inline
231  {
232  bitstring result(*this);
233  result ^= x;
234  return result;
235  }

◆ operator^=()

bitstring& bitstring::operator^= ( const bitstring x)
inline
203  {
204  for (size_t i = 0; i < _words; i++)
205  _data[i] ^= x._data[i];
206  return *this;
207  }

References _data, and _words.

◆ operator|()

bitstring bitstring::operator| ( const bitstring x)
inline
224  {
225  bitstring result(*this);
226  result |= x;
227  return result;
228  }

◆ operator|=()

bitstring& bitstring::operator|= ( const bitstring x)
inline
196  {
197  for (size_t i = 0; i < _words; i++)
198  _data[i] |= x._data[i];
199  return *this;
200  }

References _data, and _words.

◆ operator~()

bitstring bitstring::operator~ ( void  )
inline
210  {
211  for (size_t i = 0; i < _words; i++)
212  _data[i] = ~(_data[i]);
213  return *this;
214  }

References _data, and _words.

◆ print() [1/2]

void bitstring::print ( ) const
inline
465  {
466  for(unsigned int i = 0; i < _size; ++i)
467  std::cout<< (bool)(_data[ i / BITS_PER_WORD ] & ( 1UL << ( i % BITS_PER_WORD ) ));
468  std::cout<<std::endl;
469  }

References _data, _size, and BITS_PER_WORD.

◆ print() [2/2]

void bitstring::print ( size_t  from,
size_t  to 
) const
inline
457  {
458  assert(from < to && to < _size+1);
459  for(unsigned int i = from; i < to; ++i)
460  std::cout<< (bool)(_data[ i / BITS_PER_WORD ] & ( 1UL << ( i % BITS_PER_WORD ) ));
461  std::cout<<std::endl;
462  }

References _data, _size, and BITS_PER_WORD.

◆ reset() [1/2]

void bitstring::reset ( )
inline

Set all bits to 0.

153  { for(unsigned int i = 0; i < _words; i++) _data[i] = 0UL; }

References _data, and _words.

◆ reset() [2/2]

◆ set() [1/2]

◆ set() [2/2]

void bitstring::set ( size_t  n,
bool  x 
)
inline

Set a bit to 0 or 1.

338  {
339  if (x)
340  _data[n / BITS_PER_WORD] |= ( 1UL << ( n % BITS_PER_WORD ) );
341  else
342  _data[n / BITS_PER_WORD] &= ~( 1UL << ( n % BITS_PER_WORD ) );
343  }

References _data, and BITS_PER_WORD.

◆ set_data()

void bitstring::set_data ( _ul srce,
size_t  nbwrd 
)
inline

Copy bits from an array of unsigned long words.

350  {
351  // if(nbwrd != _words) {
352  // std::cerr<<"bitstring::set_data: different sizes in memcpy!!\n";
353  // exit(1);
354  // }
355  assert(nbwrd == _words);
356  memcpy(_data, srce, nbwrd * sizeof(_ul));
357  }

References _data, and _words.

Referenced by TT_BDMI::retrieve_data(), TTDeletMutations_bitstring::retrieve_data(), and TTQuanti_diallelic_bitstring::retrieve_data().

+ Here is the caller graph for this function:

◆ size()

size_t bitstring::size ( ) const
inline
161 {return _size;}

References _size.

◆ to_string() [1/2]

std::string bitstring::to_string ( ) const
inline
423  {
424  const char one = '1';
425  std::string result;
426 
427  result.assign(_size, '0');
428 
429  for(size_t i = 0; i < _size; ++i) {
430  if(_data[ i / BITS_PER_WORD ] & ( 1UL << ( i % BITS_PER_WORD ) ))
431  result[i] = one;
432  }
433 
434  return result;
435  }

References _data, _size, and BITS_PER_WORD.

◆ to_string() [2/2]

std::string bitstring::to_string ( size_t  from,
size_t  to 
) const
inline
438  {
439  assert(from < to && to < _size+1);
440 
441  const char one = '1';
442 
443  size_t len = to - from;
444 
445  std::string result;
446 
447  result.assign(len, '0');
448 
449  for(size_t i = from, s = 0; i < to && s < len; ++i, ++s) {
450  if(_data[ i / BITS_PER_WORD ] & ( 1UL << ( i % BITS_PER_WORD ) ))
451  result[s] = one;
452  }
453  return result;
454  }

References _data, _size, and BITS_PER_WORD.

Friends And Related Function Documentation

◆ reference

friend class reference
friend

Referenced by operator[]().

Member Data Documentation

◆ _bit_count

unsigned char bitstring::_bit_count
staticprivate

Referenced by local_popcountl().

◆ _data

◆ _size

size_t bitstring::_size
private

Number of bits in the sequence.

Referenced by copy(), count(), operator=(), print(), reset(), size(), and to_string().

◆ _words

size_t bitstring::_words
private

Number of _ul-long Words necessary to hold the _size bits.

Referenced by bitstring(), copy(), count(), count_and(), count_and_and(), count_xor(), count_xor_and(), nb_words(), operator&=(), operator=(), operator^=(), operator|=(), operator~(), reset(), and set_data().


The documentation for this class was generated from the following files:

Generated for Nemo v2.4.0 by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR