bitstring Class Reference

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

#include <bitstring.h>

List of all members.

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.
unsigned int count ()
 Count number of set bits.
void set (size_t n)
 Set a bit to 1.
void set_data (_ul *srce, size_t nbwrd)
 Copy bits from an array of unsigned long words.
void reset ()
 Set all bits to 0.
void copy (const bitstring &b)
 Unchecked copy, assumes we have sames sizes.
void copy (const bitstring &b, size_t word_pos)
 Copy one word.
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.

Definition at line 51 of file bitstring.h.


Member Typedef Documentation

typedef unsigned long bitstring::_ul

Definition at line 55 of file bitstring.h.


Constructor & Destructor Documentation

bitstring::bitstring ( size_t  length  )  [inline]

Definition at line 115 of file bitstring.h.

00116     : _size(length), _words( BITSET_WORDS(length) ), _data(0)
00117   {
00118       _data = new _ul [_words];
00119       memset(_data, 0, _words * sizeof(_ul));
00120   }

bitstring::bitstring ( const bitstring b  )  [inline]

Definition at line 122 of file bitstring.h.

00123     : _size(b._size), _words(b._words), _data(0)
00124   {
00125       _data = new _ul [_words];
00126       memcpy(_data, b._data, _words * sizeof(_ul));
00127   }

bitstring::~bitstring (  )  [inline]

Definition at line 129 of file bitstring.h.

00129 {if(_data != NULL) delete [] _data;}


Member Function Documentation

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

Definition at line 253 of file bitstring.h.

00254   {
00255     assert(to <= _size);
00256     
00257     size_t start_w, end_w, start_l, end_l;
00258     _ul mask, tmpl;
00259     
00260     start_w = from / BITS_PER_WORD;
00261     end_w = to / BITS_PER_WORD;
00262     
00263     start_l = from % BITS_PER_WORD;
00264     end_l = to % BITS_PER_WORD;
00265     
00266     if(start_w != end_w) {
00267       //copy wihtin first word:
00268       mask =  (0xFFFFFFFF << start_l); 
00269       
00270       _data[ start_w ]   &= ~mask;;
00271       tmpl = b._data[ start_w ] & mask;
00272           
00273       _data[ start_w ] |= tmpl;
00274       
00275       //copy words in-between:
00276       for(size_t k = start_w + 1; k < end_w; ++k) {
00277         
00278         _data[ k ] = b._data[ k ];
00279                 
00280       }
00281       //copy within last word:
00282       mask =  (0xFFFFFFFF >> (BITS_PER_WORD - end_l) ); 
00283       
00284       _data[ end_w ]   &= ~mask;;
00285       tmpl = b._data[ end_w ] & mask;
00286           
00287       _data[ end_w ] |= tmpl;
00288       
00289     } else {
00290       //bits to copy are within a word:
00291       mask =  (0xFFFFFFFF << start_l) & (0xFFFFFFFF >> (BITS_PER_WORD - end_l) );
00292       
00293       _data[ start_w ]   &= ~mask;;
00294       tmpl = b._data[ start_w ] & mask;
00295           
00296       _data[ start_w ] |= tmpl;
00297       
00298     }
00299     
00300   }

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

Copy one word.

Definition at line 250 of file bitstring.h.

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

void bitstring::copy ( const bitstring b  )  [inline]

Unchecked copy, assumes we have sames sizes.

Definition at line 246 of file bitstring.h.

Referenced by TTDeletMutations_bitstring::inherit_0(), TTDeletMutations_bitstring::inherit_low(), TTDeletMutations_bitstring::operator=(), and TTDeletMutations_bitstring::set_sequence().

00247   { memcpy(_data, b._data, _words * sizeof(_ul)); }

unsigned int bitstring::count (  )  [inline]

Count number of set bits.

Definition at line 219 of file bitstring.h.

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

00220   {
00221     unsigned int cnt = 0;
00222     
00223     for(size_t i = 0; i < _words; i++)
00224       cnt += local_popcountl(_data[i]);
00225     
00226     return cnt;
00227   }

_ul* bitstring::getword_atIdx ( size_t  index  )  [inline]

Definition at line 134 of file bitstring.h.

Referenced by TTDeletMutations_bitstring::store_data().

00135   { return &_data[ index ]; }

_ul* bitstring::getword_atPos ( size_t  pos  )  [inline]

Definition at line 131 of file bitstring.h.

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

00132   { return &_data[ pos / BITS_PER_WORD ]; }

unsigned int bitstring::local_popcountl ( _ul  wd  )  [inline]

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

Definition at line 207 of file bitstring.h.

Referenced by count().

00208   {
00209     unsigned char* c = (unsigned char*)&wd;
00210     unsigned short cnt = 0;
00211     
00212     for(unsigned int i = 0; i < sizeof(_ul); i++)
00213       cnt += _bit_count[ (unsigned int)c[i] ];
00214     
00215     return (unsigned int) cnt;
00216   }

size_t bitstring::nb_words (  )  [inline]

Definition at line 139 of file bitstring.h.

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

00139 {return _words;}

bitstring bitstring::operator& ( const bitstring x  )  [inline]

Definition at line 185 of file bitstring.h.

00186   {
00187     bitstring result(*this);
00188     result &= x;
00189     return result;
00190   }

bitstring& bitstring::operator&= ( const bitstring x  )  [inline]

Definition at line 157 of file bitstring.h.

00158   {
00159     for (size_t i = 0; i < _words; i++)
00160       _data[i] &= x._data[i];
00161     return *this;
00162   }

bitstring& bitstring::operator= ( const bitstring b  )  [inline]

Definition at line 147 of file bitstring.h.

00148   {
00149     _size = b._size;
00150     _words = b._words;
00151     if(_data != NULL) delete [] _data;
00152     _data = new _ul [_words];
00153     memcpy(_data, b._data, _words * sizeof(_ul));
00154     return *this;
00155   }

bool bitstring::operator[] ( size_t  n  )  const [inline]

Definition at line 144 of file bitstring.h.

00145   { return static_cast<bool>( _data[ n / BITS_PER_WORD ] & ( 1UL << ( n % BITS_PER_WORD ) ) ); }

reference bitstring::operator[] ( size_t  pos  )  [inline]

Definition at line 141 of file bitstring.h.

00142   { return reference(*this, pos); }

bitstring bitstring::operator^ ( const bitstring x  )  [inline]

Definition at line 199 of file bitstring.h.

00200   {
00201     bitstring result(*this);
00202     result ^= x;
00203     return result;
00204   }

bitstring& bitstring::operator^= ( const bitstring x  )  [inline]

Definition at line 171 of file bitstring.h.

00172   {
00173     for (size_t i = 0; i < _words; i++)
00174       _data[i] ^= x._data[i];
00175     return *this;
00176   }

bitstring bitstring::operator| ( const bitstring x  )  [inline]

Definition at line 192 of file bitstring.h.

00193   {
00194     bitstring result(*this);
00195     result |= x;
00196     return result;
00197   }

bitstring& bitstring::operator|= ( const bitstring x  )  [inline]

Definition at line 164 of file bitstring.h.

00165   {
00166     for (size_t i = 0; i < _words; i++)
00167       _data[i] |= x._data[i];
00168     return *this;
00169   }

bitstring bitstring::operator~ ( void   )  [inline]

Definition at line 178 of file bitstring.h.

00179   {
00180     for (size_t i = 0; i < _words; i++)
00181       _data[i] = ~(_data[i]);
00182     return *this;
00183   }

void bitstring::reset (  )  [inline]

Set all bits to 0.

Definition at line 243 of file bitstring.h.

Referenced by TTDeletMutations_bitstring::init_sequence().

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

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.

Definition at line 233 of file bitstring.h.

Referenced by TTDeletMutations_bitstring::retrieve_data().

00234   { 
00235     //    if(nbwrd != _words) { 
00236     //      std::cerr<<"bitstring::set_data: different sizes in memcpy!!\n";
00237     //      exit(1);
00238     //      }
00239     assert(nbwrd == _words);
00240     memcpy(_data, srce, nbwrd * sizeof(_ul));
00241   }

size_t bitstring::size (  )  [inline]

Definition at line 137 of file bitstring.h.

00137 {return _size;}


Friends And Related Function Documentation

friend class reference [friend]

Definition at line 113 of file bitstring.h.

Referenced by operator[]().


Member Data Documentation

unsigned char bitstring::_bit_count [static, private]

Definition at line 310 of file bitstring.h.

Referenced by local_popcountl().

_ul* bitstring::_data [private]

size_t bitstring::_size [private]

Definition at line 304 of file bitstring.h.

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.1.2 by  doxygen 1.5.8 -- Nemo is hosted by  SourceForge.net Logo