Nemo  2.3.46
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 (size_t length)
 
 bitstring (const bitstring &b)
 
 ~bitstring ()
 
_ulgetword_atPos (size_t pos)
 
_ulgetword_atIdx (size_t index)
 
size_t size ()
 
size_t nb_words ()
 
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 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)
 

Private Attributes

size_t _size
 
size_t _words
 
_ul_data
 

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

typedef unsigned long bitstring::_ul

Constructor & Destructor Documentation

bitstring::bitstring ( size_t  length)
inline

References _data, and _words.

121  : _size(length), _words( BITSET_WORDS(length) ), _data(0)
122  {
123  _data = new _ul [_words];
124  memset(_data, 0, _words * sizeof(_ul));
125  }
size_t _size
Definition: bitstring.h:317
_ul * _data
Definition: bitstring.h:320
#define BITSET_WORDS(__n)
Definition: bitstring.h:45
unsigned long _ul
Definition: bitstring.h:60
size_t _words
Definition: bitstring.h:318
bitstring::bitstring ( const bitstring b)
inline

References _data, and _words.

128  : _size(b._size), _words(b._words), _data(0)
129  {
130  _data = new _ul [_words];
131  memcpy(_data, b._data, _words * sizeof(_ul));
132  }
size_t _size
Definition: bitstring.h:317
_ul * _data
Definition: bitstring.h:320
unsigned long _ul
Definition: bitstring.h:60
size_t _words
Definition: bitstring.h:318
bitstring::~bitstring ( )
inline

References _data.

134 {if(_data != NULL) delete [] _data;}
_ul * _data
Definition: bitstring.h:320

Member Function Documentation

void bitstring::copy ( const bitstring b)
inline

Unchecked copy, assumes we have sames sizes.

References _data, and _words.

Referenced by TProtoBDMI::inherit(), TProtoDeletMutations_bitstring::inherit_low(), TTDeletMutations_bitstring::operator=(), TT_BDMI::operator=(), TTDeletMutations_bitstring::set_sequence(), and TT_BDMI::set_sequence().

256  { memcpy(_data, b._data, _words * sizeof(_ul)); }
_ul * _data
Definition: bitstring.h:320
unsigned long _ul
Definition: bitstring.h:60
size_t _words
Definition: bitstring.h:318
void bitstring::copy ( const bitstring b,
size_t  word_pos 
)
inline

Copy one word.

References _data.

260  { _data[ word_pos ] = b._data[ word_pos ]; }
_ul * _data
Definition: bitstring.h:320
void bitstring::copy ( const bitstring b,
size_t  from,
size_t  to 
)
inline

References _data, _size, BITS_PER_WORD, and MASK.

263  {
264  assert(to <= _size);
265 
266  size_t start_w, end_w, start_l, end_l;
267  _ul mask, tmpl;
268 
269  start_w = from / BITS_PER_WORD;
270  end_w = to / BITS_PER_WORD;
271 
272  start_l = from % BITS_PER_WORD;
273  end_l = to % BITS_PER_WORD;
274 
275  if(start_w != end_w) {
276  //copy wihtin first word:
277  mask = (MASK << start_l);
278 
279  _data[ start_w ] &= ~mask;
280  tmpl = b._data[ start_w ] & mask;
281 
282  _data[ start_w ] |= tmpl;
283 
284  //copy words in-between:
285  size_t k = start_w + 1;
286 
287  memcpy(&_data[k], &b._data[k], (end_w - k)*sizeof(_ul));
288 
289 // for(size_t k = start_w + 1; k < end_w; ++k) {
290 //
291 // _data[ k ] = b._data[ k ];
292 //
293 // }
294  //copy within last word:
295  mask = (MASK >> (BITS_PER_WORD - end_l) );
296 
297  _data[ end_w ] &= ~mask;;
298  tmpl = b._data[ end_w ] & mask;
299 
300  _data[ end_w ] |= tmpl;
301 
302  } else {
303  //bits to copy are within a word:
304  mask = (MASK << start_l) & (MASK >> (BITS_PER_WORD - end_l) );
305 
306  _data[ start_w ] &= ~mask;
307  tmpl = b._data[ start_w ] & mask;
308 
309  _data[ start_w ] |= tmpl;
310 
311  }
312 
313  }
#define BITS_PER_WORD
Definition: bitstring.h:41
size_t _size
Definition: bitstring.h:317
_ul * _data
Definition: bitstring.h:320
#define MASK
Definition: bitstring.h:51
unsigned long _ul
Definition: bitstring.h:60
unsigned int bitstring::count ( )
inline

Count number of set bits.

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

225  {
226  unsigned int cnt = 0;
227 
228  for(size_t i = 0; i < _words; i++)
229  cnt += local_popcountl(_data[i]);
230 
231  return cnt;
232  }
_ul * _data
Definition: bitstring.h:320
unsigned int local_popcountl(_ul wd)
Counts number of one's in string using a bit table count.
Definition: bitstring.h:212
size_t _words
Definition: bitstring.h:318
void bitstring::flip ( size_t  n)
inline

Flip the bit at n.

References _data, and BITS_PER_WORD.

Referenced by TT_BDMI::mutate_diplo(), and TT_BDMI::mutate_haplo().

238 {_data[n / BITS_PER_WORD] ^= (1UL << (n % BITS_PER_WORD));}
#define BITS_PER_WORD
Definition: bitstring.h:41
_ul * _data
Definition: bitstring.h:320
_ul* bitstring::getword_atIdx ( size_t  index)
inline

References _data.

Referenced by TTDeletMutations_bitstring::store_data().

140  { return &_data[ index ]; }
_ul * _data
Definition: bitstring.h:320
_ul* bitstring::getword_atPos ( size_t  pos)
inline

References _data, and BITS_PER_WORD.

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

137  { return &_data[ pos / BITS_PER_WORD ]; }
#define BITS_PER_WORD
Definition: bitstring.h:41
_ul * _data
Definition: bitstring.h:320
unsigned int bitstring::local_popcountl ( _ul  wd)
inline

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

References _bit_count.

Referenced by count().

213  {
214  unsigned char* c = (unsigned char*)&wd;
215  unsigned short cnt = 0;
216 
217  for(unsigned int i = 0; i < sizeof(_ul); i++)
218  cnt += _bit_count[ (unsigned int)c[i] ];
219 
220  return (unsigned int) cnt;
221  }
static unsigned char _bit_count[256]
Definition: bitstring.h:323
unsigned long _ul
Definition: bitstring.h:60
size_t bitstring::nb_words ( )
inline

References _words.

Referenced by TTDeletMutations_bitstring::retrieve_data(), and TTDeletMutations_bitstring::store_data().

144 {return _words;}
size_t _words
Definition: bitstring.h:318
bitstring bitstring::operator& ( const bitstring x)
inline
191  {
192  bitstring result(*this);
193  result &= x;
194  return result;
195  }
Non-template and faster implementation of std::bitset.
Definition: bitstring.h:56
bitstring& bitstring::operator&= ( const bitstring x)
inline

References _data, and _words.

163  {
164  for (size_t i = 0; i < _words; i++)
165  _data[i] &= x._data[i];
166  return *this;
167  }
_ul * _data
Definition: bitstring.h:320
size_t _words
Definition: bitstring.h:318
bitstring& bitstring::operator= ( const bitstring b)
inline

References _data, _size, and _words.

153  {
154  _size = b._size;
155  _words = b._words;
156  if(_data != NULL) delete [] _data;
157  _data = new _ul [_words];
158  memcpy(_data, b._data, _words * sizeof(_ul));
159  return *this;
160  }
size_t _size
Definition: bitstring.h:317
_ul * _data
Definition: bitstring.h:320
unsigned long _ul
Definition: bitstring.h:60
size_t _words
Definition: bitstring.h:318
reference bitstring::operator[] ( size_t  pos)
inline

References reference.

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

References _data, and BITS_PER_WORD.

150  { return static_cast<bool>( _data[ n / BITS_PER_WORD ] & ( 1UL << ( n % BITS_PER_WORD ) ) ); }
#define BITS_PER_WORD
Definition: bitstring.h:41
_ul * _data
Definition: bitstring.h:320
bitstring bitstring::operator^ ( const bitstring x)
inline
205  {
206  bitstring result(*this);
207  result ^= x;
208  return result;
209  }
Non-template and faster implementation of std::bitset.
Definition: bitstring.h:56
bitstring& bitstring::operator^= ( const bitstring x)
inline

References _data, and _words.

177  {
178  for (size_t i = 0; i < _words; i++)
179  _data[i] ^= x._data[i];
180  return *this;
181  }
_ul * _data
Definition: bitstring.h:320
size_t _words
Definition: bitstring.h:318
bitstring bitstring::operator| ( const bitstring x)
inline
198  {
199  bitstring result(*this);
200  result |= x;
201  return result;
202  }
Non-template and faster implementation of std::bitset.
Definition: bitstring.h:56
bitstring& bitstring::operator|= ( const bitstring x)
inline

References _data, and _words.

170  {
171  for (size_t i = 0; i < _words; i++)
172  _data[i] |= x._data[i];
173  return *this;
174  }
_ul * _data
Definition: bitstring.h:320
size_t _words
Definition: bitstring.h:318
bitstring bitstring::operator~ ( void  )
inline

References _data, and _words.

184  {
185  for (size_t i = 0; i < _words; i++)
186  _data[i] = ~(_data[i]);
187  return *this;
188  }
_ul * _data
Definition: bitstring.h:320
size_t _words
Definition: bitstring.h:318
void bitstring::reset ( )
inline

Set all bits to 0.

References _data, and _words.

Referenced by TTDeletMutations_bitstring::init_sequence(), and TT_BDMI::init_sequence().

252  { for(unsigned int i = 0; i < _words; i++) _data[i] = 0UL; }
_ul * _data
Definition: bitstring.h:320
size_t _words
Definition: bitstring.h:318
void bitstring::set ( size_t  n)
inline
void bitstring::set_data ( _ul srce,
size_t  nbwrd 
)
inline

Copy bits from an array of unsigned long words.

References _data, and _words.

Referenced by TTDeletMutations_bitstring::retrieve_data().

242  {
243  // if(nbwrd != _words) {
244  // std::cerr<<"bitstring::set_data: different sizes in memcpy!!\n";
245  // exit(1);
246  // }
247  assert(nbwrd == _words);
248  memcpy(_data, srce, nbwrd * sizeof(_ul));
249  }
_ul * _data
Definition: bitstring.h:320
unsigned long _ul
Definition: bitstring.h:60
size_t _words
Definition: bitstring.h:318
size_t bitstring::size ( )
inline

References _size.

142 {return _size;}
size_t _size
Definition: bitstring.h:317

Friends And Related Function Documentation

friend class reference
friend

Referenced by operator[]().

Member Data Documentation

unsigned char bitstring::_bit_count
staticprivate

Referenced by local_popcountl().

size_t bitstring::_size
private

Referenced by copy(), operator=(), and size().

size_t bitstring::_words
private

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

Generated for Nemo v2.3.0 by  doxygen 1.8.8 --
Catalogued on GSR