Nemo  2.3.56
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 (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 ()
 
bool at (size_t word, unsigned long bitmask)
 
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

◆ _ul

typedef unsigned long bitstring::_ul

Constructor & Destructor Documentation

◆ bitstring() [1/2]

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

References _data, and _words.

◆ bitstring() [2/2]

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

References _data, and _words.

◆ ~bitstring()

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

References _data.

Member Function Documentation

◆ at()

bool bitstring::at ( size_t  word,
unsigned long  bitmask 
)
inline
147 {
148 return static_cast<bool>( _data[ word ] & bitmask );
149 }

References _data.

◆ copy() [1/3]

void bitstring::copy ( const bitstring b)
inline

Unchecked copy, assumes we have sames sizes.

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

References _data, and _words.

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

+ Here is the caller graph for this function:

◆ copy() [2/3]

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

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

References _data.

◆ count()

unsigned int bitstring::count ( )
inline

Count number of set bits.

230 {
231 unsigned int cnt = 0;
232
233 for(size_t i = 0; i < _words; i++)
234 cnt += local_popcountl(_data[i]);
235
236 return cnt;
237 }
unsigned int local_popcountl(_ul wd)
Counts number of one's in string using a bit table count.
Definition: bitstring.h:217

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

+ Here is the caller graph for this function:

◆ flip()

void bitstring::flip ( size_t  n)
inline

Flip the bit at n.

243{_data[n / BITS_PER_WORD] ^= (1UL << (n % BITS_PER_WORD));}

References _data, and BITS_PER_WORD.

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

+ Here is the caller graph for this function:

◆ getword_atIdx()

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

References _data.

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

+ Here is the caller graph for this function:

◆ getword_atPos()

_ul * bitstring::getword_atPos ( size_t  pos)
inline
137 { 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)
inline

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

218 {
219 unsigned char* c = (unsigned char*)&wd;
220 unsigned short cnt = 0;
221
222 for(unsigned int i = 0; i < sizeof(_ul); i++)
223 cnt += _bit_count[ (unsigned int)c[i] ];
224
225 return (unsigned int) cnt;
226 }
static unsigned char _bit_count[256]
Definition: bitstring.h:328

References _bit_count.

Referenced by count().

+ Here is the caller graph for this function:

◆ nb_words()

size_t bitstring::nb_words ( )
inline
144{return _words;}

References _words.

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

+ Here is the caller graph for this function:

◆ operator&()

bitstring bitstring::operator& ( const bitstring x)
inline
196 {
197 bitstring result(*this);
198 result &= x;
199 return result;
200 }
Non-template and faster implementation of std::bitset.
Definition: bitstring.h:56

◆ operator&=()

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

References _data, and _words.

◆ operator=()

bitstring & bitstring::operator= ( const bitstring b)
inline
158 {
159 _size = b._size;
160 _words = b._words;
161 if(_data != NULL) delete [] _data;
162 _data = new _ul [_words];
163 memcpy(_data, b._data, _words * sizeof(_ul));
164 return *this;
165 }

References _data, _size, and _words.

◆ operator[]() [1/2]

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

◆ operator^()

bitstring bitstring::operator^ ( const bitstring x)
inline
210 {
211 bitstring result(*this);
212 result ^= x;
213 return result;
214 }

◆ operator^=()

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

References _data, and _words.

◆ operator|()

bitstring bitstring::operator| ( const bitstring x)
inline
203 {
204 bitstring result(*this);
205 result |= x;
206 return result;
207 }

◆ operator|=()

bitstring & bitstring::operator|= ( const bitstring x)
inline
175 {
176 for (size_t i = 0; i < _words; i++)
177 _data[i] |= x._data[i];
178 return *this;
179 }

References _data, and _words.

◆ operator~()

bitstring bitstring::operator~ ( void  )
inline
189 {
190 for (size_t i = 0; i < _words; i++)
191 _data[i] = ~(_data[i]);
192 return *this;
193 }

References _data, and _words.

◆ reset()

void bitstring::reset ( )
inline

Set all bits to 0.

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

References _data, and _words.

Referenced by TTDeletMutBitstrFH::FHread(), TT_BDMI::init_sequence(), TTDeletMutations_bitstring::init_sequence(), and TTNeutralGenesFH::write_PLINK_BED().

+ Here is the caller graph for this function:

◆ set()

◆ set_data()

void bitstring::set_data ( _ul srce,
size_t  nbwrd 
)
inline

Copy bits from an array of unsigned long words.

247 {
248 // if(nbwrd != _words) {
249 // std::cerr<<"bitstring::set_data: different sizes in memcpy!!\n";
250 // exit(1);
251 // }
252 assert(nbwrd == _words);
253 memcpy(_data, srce, nbwrd * sizeof(_ul));
254 }

References _data, and _words.

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

+ Here is the caller graph for this function:

◆ size()

size_t bitstring::size ( )
inline
142{return _size;}

References _size.

Friends And Related Function Documentation

◆ reference

friend class reference
friend

Member Data Documentation

◆ _bit_count

unsigned char bitstring::_bit_count
staticprivate

Referenced by local_popcountl().

◆ _data

◆ _size

size_t bitstring::_size
private

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

◆ _words

size_t bitstring::_words
private

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

Generated for Nemo v2.3.56 by  doxygen 1.9.0 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR