Nemo  2.2.0
Classes | Public Types | Public Member Functions | Private Attributes | Static Private Attributes | Friends
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 flip (size_t n)
 Flip the bit at n.
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.

References _data, and _words.

  : _size(length), _words( BITSET_WORDS(length) ), _data(0)
  {
    _data = new _ul [_words];
    memset(_data, 0, _words * sizeof(_ul));
  }
bitstring::bitstring ( const bitstring b) [inline]

Definition at line 122 of file bitstring.h.

References _data, and _words.

  : _size(b._size), _words(b._words), _data(0)
  {
    _data = new _ul [_words];
    memcpy(_data, b._data, _words * sizeof(_ul));
  }
bitstring::~bitstring ( ) [inline]

Definition at line 129 of file bitstring.h.

References _data.

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

Member Function Documentation

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

Unchecked copy, assumes we have sames sizes.

Definition at line 249 of file bitstring.h.

References _data, and _words.

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

  { memcpy(_data, b._data, _words * sizeof(_ul)); }
void bitstring::copy ( const bitstring b,
size_t  word_pos 
) [inline]

Copy one word.

Definition at line 253 of file bitstring.h.

References _data.

  { _data[ word_pos ] = b._data[ word_pos ]; }
void bitstring::copy ( const bitstring b,
size_t  from,
size_t  to 
) [inline]

Definition at line 256 of file bitstring.h.

References _data, _size, and BITS_PER_WORD.

  {
    assert(to <= _size);
    
    size_t start_w, end_w, start_l, end_l;
    _ul mask, tmpl;
    
    start_w = from / BITS_PER_WORD;
    end_w = to / BITS_PER_WORD;
    
    start_l = from % BITS_PER_WORD;
    end_l = to % BITS_PER_WORD;
    
    if(start_w != end_w) {
      //copy wihtin first word:
      mask =  (0xFFFFFFFF << start_l); 
      
      _data[ start_w ]   &= ~mask;;
      tmpl = b._data[ start_w ] & mask;
          
      _data[ start_w ] |= tmpl;
      
      //copy words in-between:
      for(size_t k = start_w + 1; k < end_w; ++k) {
        
        _data[ k ] = b._data[ k ];
                
      }
      //copy within last word:
      mask =  (0xFFFFFFFF >> (BITS_PER_WORD - end_l) ); 
      
      _data[ end_w ]   &= ~mask;;
      tmpl = b._data[ end_w ] & mask;
          
      _data[ end_w ] |= tmpl;
      
    } else {
      //bits to copy are within a word:
      mask =  (0xFFFFFFFF << start_l) & (0xFFFFFFFF >> (BITS_PER_WORD - end_l) );
      
      _data[ start_w ] &= ~mask;
      tmpl = b._data[ start_w ] & mask;
          
      _data[ start_w ] |= tmpl;
      
    }
    
  }
unsigned int bitstring::count ( ) [inline]

Count number of set bits.

Definition at line 219 of file bitstring.h.

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

  {
    unsigned int cnt = 0;
    
    for(size_t i = 0; i < _words; i++)
      cnt += local_popcountl(_data[i]);
    
    return cnt;
  }
void bitstring::flip ( size_t  n) [inline]

Flip the bit at n.

Definition at line 233 of file bitstring.h.

References _data, and BITS_PER_WORD.

{_data[n / BITS_PER_WORD] ^= (1UL << (n % BITS_PER_WORD));}
_ul* bitstring::getword_atIdx ( size_t  index) [inline]

Definition at line 134 of file bitstring.h.

References _data.

Referenced by TTDeletMutations_bitstring::store_data().

  { return &_data[ index ]; }
_ul* bitstring::getword_atPos ( size_t  pos) [inline]

Definition at line 131 of file bitstring.h.

References _data, and BITS_PER_WORD.

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

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

References _bit_count.

Referenced by count().

  {
    unsigned char* c = (unsigned char*)&wd;
    unsigned short cnt = 0;
    
    for(unsigned int i = 0; i < sizeof(_ul); i++)
      cnt += _bit_count[ (unsigned int)c[i] ];
    
    return (unsigned int) cnt;
  }
size_t bitstring::nb_words ( ) [inline]

Definition at line 139 of file bitstring.h.

References _words.

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

{return _words;}
bitstring bitstring::operator& ( const bitstring x) [inline]

Definition at line 185 of file bitstring.h.

  {
    bitstring result(*this);
    result &= x;
    return result;
  }
bitstring& bitstring::operator&= ( const bitstring x) [inline]

Definition at line 157 of file bitstring.h.

References _data, and _words.

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

Definition at line 147 of file bitstring.h.

References _data, _size, and _words.

  {
    _size = b._size;
    _words = b._words;
    if(_data != NULL) delete [] _data;
    _data = new _ul [_words];
    memcpy(_data, b._data, _words * sizeof(_ul));
    return *this;
  }
reference bitstring::operator[] ( size_t  pos) [inline]

Definition at line 141 of file bitstring.h.

References reference.

  { return reference(*this, pos); }
bool bitstring::operator[] ( size_t  n) const [inline]

Definition at line 144 of file bitstring.h.

References _data, and BITS_PER_WORD.

  { return static_cast<bool>( _data[ n / BITS_PER_WORD ] & ( 1UL << ( n % BITS_PER_WORD ) ) ); }
bitstring bitstring::operator^ ( const bitstring x) [inline]

Definition at line 199 of file bitstring.h.

  {
    bitstring result(*this);
    result ^= x;
    return result;
  }
bitstring& bitstring::operator^= ( const bitstring x) [inline]

Definition at line 171 of file bitstring.h.

References _data, and _words.

  {
    for (size_t i = 0; i < _words; i++)
      _data[i] ^= x._data[i];
    return *this;
  }
bitstring bitstring::operator| ( const bitstring x) [inline]

Definition at line 192 of file bitstring.h.

  {
    bitstring result(*this);
    result |= x;
    return result;
  }
bitstring& bitstring::operator|= ( const bitstring x) [inline]

Definition at line 164 of file bitstring.h.

References _data, and _words.

  {
    for (size_t i = 0; i < _words; i++)
      _data[i] |= x._data[i];
    return *this;
  }
bitstring bitstring::operator~ ( void  ) [inline]

Definition at line 178 of file bitstring.h.

References _data, and _words.

  {
    for (size_t i = 0; i < _words; i++)
      _data[i] = ~(_data[i]);
    return *this;
  }
void bitstring::reset ( ) [inline]

Set all bits to 0.

Definition at line 246 of file bitstring.h.

References _data, and _words.

Referenced by TTDeletMutations_bitstring::init_sequence().

{ 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 236 of file bitstring.h.

References _data, and _words.

Referenced by TTDeletMutations_bitstring::retrieve_data().

  { 
    //    if(nbwrd != _words) { 
    //      std::cerr<<"bitstring::set_data: different sizes in memcpy!!\n";
    //      exit(1);
    //      }
    assert(nbwrd == _words);
    memcpy(_data, srce, nbwrd * sizeof(_ul));
  }
size_t bitstring::size ( ) [inline]

Definition at line 137 of file bitstring.h.

References _size.

{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 313 of file bitstring.h.

Referenced by local_popcountl().

_ul* bitstring::_data [private]
size_t bitstring::_size [private]

Definition at line 307 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.2.0 by  doxygen 1.7.5.1 -- Nemo is hosted by  SourceForge.net Logo