Nemo  2.4.2
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
117 : _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
120  : _size(length), _words( BITSET_WORDS(length) ), _data(0)
121  {
122  _data = new _ul [_words];
123  memset(_data, 0, _words * sizeof(_ul));
124  }
#define BITSET_WORDS(__n)
Definition: bitstring.h:43
unsigned long _ul
Definition: bitstring.h:57

References _data, and _words.

◆ bitstring() [3/3]

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

References _data, and _words.

◆ ~bitstring()

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

References _data.

Member Function Documentation

◆ at()

bool bitstring::at ( size_t  word,
unsigned long  bitmask 
) const
inline
163  {
164  return bool( _data[ word ] & bitmask );
165  }

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.

366  {
367  assert(to <= _size);
368 
369  if(to != from) { // only if we do have something to copy
370 
371  size_t start_w, end_w, start_l, end_l;
372  _ul mask, tmpl;
373 
374  start_w = from / BITS_PER_WORD;
375  end_w = to / BITS_PER_WORD;
376 
377  start_l = from % BITS_PER_WORD;
378  end_l = to % BITS_PER_WORD;
379 
380  if(start_w != end_w) {
381  //copy wihtin first word:
382  mask = (MASK << start_l);
383 
384  _data[ start_w ] &= ~mask;
385  tmpl = b._data[ start_w ] & mask;
386 
387  _data[ start_w ] |= tmpl;
388 
389  //copy words in-between:
390  size_t k = start_w + 1;
391 
392  memcpy(&_data[k], &b._data[k], (end_w - k)*sizeof(_ul));
393 
394  //copy within last word; skip when 'to' is word-aligned (end_l == 0),
395  //since end_w then sits one past the last valid word and there's
396  //nothing left to copy beyond what the memcpy above already did.
397  if (end_l != 0) {
398  mask = (MASK >> (BITS_PER_WORD - end_l) );
399 
400  _data[ end_w ] &= ~mask;
401  tmpl = b._data[ end_w ] & mask;
402 
403  _data[ end_w ] |= tmpl;
404  }
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:50
#define BITS_PER_WORD
Definition: bitstring.h:39

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.

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

References _data.

◆ count() [1/2]

unsigned int bitstring::count ( )
inline

Count number of set bits.

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

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

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

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

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

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

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

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

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

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

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

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

243  {
244  unsigned char* c = (unsigned char*)&wd;
245  unsigned short cnt = 0;
246 
247  for(unsigned int i = 0; i < sizeof(_ul); i++)
248  cnt += _bit_count[ (unsigned int)c[i] ];
249 
250  return (unsigned int) cnt;
251  }
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().

+ Here is the caller graph for this function:

◆ nb_words()

◆ operator&()

bitstring bitstring::operator& ( const bitstring x)
inline
214  {
215  bitstring result(*this);
216  result &= x;
217  return result;
218  }
Non-template and faster implementation of std::bitset.
Definition: bitstring.h:53

◆ operator&=()

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

References _data, and _words.

◆ operator=()

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

References _data, _size, and _words.

◆ operator[]() [1/2]

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

References reference.

◆ operator^()

bitstring bitstring::operator^ ( const bitstring x)
inline
228  {
229  bitstring result(*this);
230  result ^= x;
231  return result;
232  }

◆ operator^=()

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

References _data, and _words.

◆ operator|()

bitstring bitstring::operator| ( const bitstring x)
inline
221  {
222  bitstring result(*this);
223  result |= x;
224  return result;
225  }

◆ operator|=()

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

References _data, and _words.

◆ operator~()

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

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.

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

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

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.

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

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
158 {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.2 by  doxygen 1.9.1

Catalogued on GSR