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)
 
_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)
 Counts number of one's in string using a bit table count. More...
 
unsigned int count ()
 Count number of set bits. 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 reset ()
 Set all bits to 0. 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:396
size_t _size
Number of bits in the sequence.
Definition: bitstring.h:393
_ul * _data
The sequence.
Definition: bitstring.h:399

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

293  {
294  assert(to <= _size);
295 
296  if(to != from) { // only if we do have something to copy
297 
298  size_t start_w, end_w, start_l, end_l;
299  _ul mask, tmpl;
300 
301  start_w = from / BITS_PER_WORD;
302  end_w = to / BITS_PER_WORD;
303 
304  start_l = from % BITS_PER_WORD;
305  end_l = to % BITS_PER_WORD;
306 
307  if(start_w != end_w) {
308  //copy wihtin first word:
309  mask = (MASK << start_l);
310 
311  _data[ start_w ] &= ~mask;
312  tmpl = b._data[ start_w ] & mask;
313 
314  _data[ start_w ] |= tmpl;
315 
316  //copy words in-between:
317  size_t k = start_w + 1;
318 
319  memcpy(&_data[k], &b._data[k], (end_w - k)*sizeof(_ul));
320 
321  //copy within last word:
322  mask = (MASK >> (BITS_PER_WORD - end_l) );
323 
324  _data[ end_w ] &= ~mask;;
325  tmpl = b._data[ end_w ] & mask;
326 
327  _data[ end_w ] |= tmpl;
328 
329  } else {
330  //bits to copy are within a word:
331  mask = (MASK << start_l) & (MASK >> (BITS_PER_WORD - end_l) );
332 
333  _data[ start_w ] &= ~mask;
334  tmpl = b._data[ start_w ] & mask;
335 
336  _data[ start_w ] |= tmpl;
337 
338  }
339  }
340  }
#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.

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

References _data.

◆ count()

unsigned int bitstring::count ( )
inline

Count number of set bits.

246  {
247  unsigned int cnt = 0;
248 
249  for(size_t i = 0; i < _words; i++)
250  cnt += local_popcountl(_data[i]);
251 
252  return cnt;
253  }
unsigned int local_popcountl(_ul wd)
Counts number of one's in string using a bit table count.
Definition: bitstring.h:233

References _data, _words, and local_popcountl().

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

◆ flip()

◆ getword_atIdx()

_ul* bitstring::getword_atIdx ( size_t  index) const
inline

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

◆ local_popcountl()

unsigned int bitstring::local_popcountl ( _ul  wd)
inline

Counts number of one's in string using a bit table count.

234  {
235  unsigned char* c = (unsigned char*)&wd;
236  unsigned short cnt = 0;
237 
238  for(unsigned int i = 0; i < sizeof(_ul); i++)
239  cnt += _bit_count[ (unsigned int)c[i] ];
240 
241  return (unsigned int) cnt;
242  }
static unsigned char _bit_count[256]
Definition: bitstring.h:402

References _bit_count.

Referenced by count().

◆ nb_words()

◆ operator&()

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

◆ operator&=()

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

References _data, and _words.

◆ operator=()

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

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:119

References reference.

◆ operator^()

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

◆ operator^=()

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

References _data, and _words.

◆ operator|()

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

◆ operator|=()

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

References _data, and _words.

◆ operator~()

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

References _data, and _words.

◆ print() [1/2]

void bitstring::print ( ) const
inline
385  {
386  for(unsigned int i = 0; i < _size; ++i)
387  std::cout<< (bool)(_data[ i / BITS_PER_WORD ] & ( 1UL << ( i % BITS_PER_WORD ) ));
388  std::cout<<std::endl;
389  }

References _data, _size, and BITS_PER_WORD.

◆ print() [2/2]

void bitstring::print ( size_t  from,
size_t  to 
) const
inline
377  {
378  assert(from < to && to < _size+1);
379  for(unsigned int i = from; i < to; ++i)
380  std::cout<< (bool)(_data[ i / BITS_PER_WORD ] & ( 1UL << ( i % BITS_PER_WORD ) ));
381  std::cout<<std::endl;
382  }

References _data, _size, and BITS_PER_WORD.

◆ reset() [1/2]

void bitstring::reset ( )
inline

Set all bits to 0.

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

References _data, and _words.

◆ reset() [2/2]

void bitstring::reset ( size_t  length)
inline
141  {
142  _size = length;
143 
144  _words = BITSET_WORDS(length);
145 
146  if(_data) delete [] _data;
147 
148  _data = new _ul [_words];
149  memset(_data, 0, _words * sizeof(_ul));
150  }

References _data, _size, _words, and BITSET_WORDS.

Referenced by TTDeletMutBitstrFH::FHread(), TT_BDMI::init(), TTQuanti_diallelic_bitstring::init(), TT_BDMI::init_sequence(), TTDeletMutations_bitstring::init_sequence(), and TT_BDMI::reset().

◆ set() [1/2]

◆ set() [2/2]

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

Set a bit to 0 or 1.

259  {
260  if (x)
261  _data[n / BITS_PER_WORD] |= ( 1UL << ( n % BITS_PER_WORD ) );
262  else
263  _data[n / BITS_PER_WORD] &= ~( 1UL << ( n % BITS_PER_WORD ) );
264  }

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.

271  {
272  // if(nbwrd != _words) {
273  // std::cerr<<"bitstring::set_data: different sizes in memcpy!!\n";
274  // exit(1);
275  // }
276  assert(nbwrd == _words);
277  memcpy(_data, srce, nbwrd * sizeof(_ul));
278  }

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
158 {return _size;}

References _size.

◆ to_string() [1/2]

std::string bitstring::to_string ( ) const
inline
343  {
344  const char one = '1';
345  std::string result;
346 
347  result.assign(_size, '0');
348 
349  for(size_t i = 0; i < _size; ++i) {
350  if(_data[ i / BITS_PER_WORD ] & ( 1UL << ( i % BITS_PER_WORD ) ))
351  result[i] = one;
352  }
353 
354  return result;
355  }

References _data, _size, and BITS_PER_WORD.

◆ to_string() [2/2]

std::string bitstring::to_string ( size_t  from,
size_t  to 
) const
inline
358  {
359  assert(from < to && to < _size+1);
360 
361  const char one = '1';
362 
363  size_t len = to - from;
364 
365  std::string result;
366 
367  result.assign(len, '0');
368 
369  for(size_t i = from, s = 0; i < to && s < len; ++i, ++s) {
370  if(_data[ i / BITS_PER_WORD ] & ( 1UL << ( i % BITS_PER_WORD ) ))
371  result[s] = one;
372  }
373  return result;
374  }

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