Nemo  2.4.0b
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
121 : _size(0), _words(0), _data(0) {}
size_t _words
Number of _ul-long Words necessary to hold the _size bits.
Definition: bitstring.h:473
size_t _size
Number of bits in the sequence.
Definition: bitstring.h:470
_ul * _data
The sequence.
Definition: bitstring.h:476

◆ bitstring() [2/3]

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

References _data, and _words.

◆ bitstring() [3/3]

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

References _data, and _words.

◆ ~bitstring()

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

References _data.

Member Function Documentation

◆ at()

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

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.

370  {
371  assert(to <= _size);
372 
373  if(to != from) { // only if we do have something to copy
374 
375  size_t start_w, end_w, start_l, end_l;
376  _ul mask, tmpl;
377 
378  start_w = from / BITS_PER_WORD;
379  end_w = to / BITS_PER_WORD;
380 
381  start_l = from % BITS_PER_WORD;
382  end_l = to % BITS_PER_WORD;
383 
384  if(start_w != end_w) {
385  //copy wihtin first word:
386  mask = (MASK << start_l);
387 
388  _data[ start_w ] &= ~mask;
389  tmpl = b._data[ start_w ] & mask;
390 
391  _data[ start_w ] |= tmpl;
392 
393  //copy words in-between:
394  size_t k = start_w + 1;
395 
396  memcpy(&_data[k], &b._data[k], (end_w - k)*sizeof(_ul));
397 
398  //copy within last word:
399  mask = (MASK >> (BITS_PER_WORD - end_l) );
400 
401  _data[ end_w ] &= ~mask;;
402  tmpl = b._data[ end_w ] & mask;
403 
404  _data[ end_w ] |= tmpl;
405 
406  } else {
407  //bits to copy are within a word:
408  mask = (MASK << start_l) & (MASK >> (BITS_PER_WORD - end_l) );
409 
410  _data[ start_w ] &= ~mask;
411  tmpl = b._data[ start_w ] & mask;
412 
413  _data[ start_w ] |= tmpl;
414 
415  }
416  }
417  }
#define MASK
Definition: bitstring.h:52
#define BITS_PER_WORD
Definition: bitstring.h:41

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.

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

References _data.

◆ count() [1/2]

unsigned int bitstring::count ( )
inline

Count number of set bits.

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

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().

◆ count() [2/2]

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

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

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

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).

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

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().

◆ 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).

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

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().

◆ count_xor()

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

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

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

References _data, _words, and local_popcountl().

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

◆ 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).

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

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().

◆ flip()

◆ getword_atIdx()

◆ getword_atPos()

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

References _data, and BITS_PER_WORD.

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

◆ 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.

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

References _bit_count.

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

◆ nb_words()

◆ operator&()

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

◆ operator&=()

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

References _data, and _words.

◆ operator=()

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

References _data, _size, and _words.

◆ operator[]() [1/2]

bool bitstring::operator[] ( size_t  n) const
inline
175  { 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
172  { return reference(*this, pos); }
friend class reference
Definition: bitstring.h:119

References reference.

◆ operator^()

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

◆ operator^=()

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

References _data, and _words.

◆ operator|()

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

◆ operator|=()

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

References _data, and _words.

◆ operator~()

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

References _data, and _words.

◆ print() [1/2]

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

References _data, _size, and BITS_PER_WORD.

◆ print() [2/2]

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

References _data, _size, and BITS_PER_WORD.

◆ reset() [1/2]

void bitstring::reset ( )
inline

Set all bits to 0.

154  { 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.

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

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.

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

References _data, and _words.

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

◆ size()

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

References _size.

◆ to_string() [1/2]

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

References _data, _size, and BITS_PER_WORD.

◆ to_string() [2/2]

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

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.0b by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR