Nemo  2.4.0b
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
bitstring.h
Go to the documentation of this file.
1 
29 #ifndef BITSRING_H
30 #define BITSRING_H
31 
32 #include <bitset>
33 #include <assert.h> //for CHAR_BIT
34 #include <limits.h>
35 #include <string.h>
36 #include <iostream>
37 
38 #ifdef _GLIBCPP_BITSET_BITS_PER_WORD
39 #define BITS_PER_WORD _GLIBCPP_BITSET_BITS_PER_WORD
40 #else
41 #define BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long))
42 #endif
43 
44 #ifndef _GLIBCPP_BITSET_WORDS
45 #define BITSET_WORDS(__n) \
46 ((__n) < 1 ? 1 : ((__n) + BITS_PER_WORD - 1)/BITS_PER_WORD)
47 #else
48 #define BITSET_WORDS(__n) _GLIBCPP_BITSET_WORDS(__n)
49 #endif
50 
51 
52 #define MASK ULLONG_MAX
53 
54 //define MASK = (BITS_PER_WORD == 64 ? 0xFFFFFFFFFFFFFFFF : 0xFFFFFFFF)
55 
57 class bitstring {
58 
59 public:
60 
61  typedef unsigned long _ul;
62 
63  class reference
64  {
65  friend class bitstring;
66 
68  size_t _bitpos;
69 
70  // left undefined
72 
73  public:
74  reference(bitstring& bs, size_t pos)
75  {
76  _word = bs.getword_atPos( pos );
77  _bitpos = pos % BITS_PER_WORD;
78  }
79 
81  { }
82 
83  // For b[i] = x;
85  {
86  if (x)
87  *_word |= ( 1UL << ( _bitpos % BITS_PER_WORD ) );
88  else
89  *_word &= ~( 1UL << ( _bitpos % BITS_PER_WORD ) );
90  return *this;
91  }
92 
93  // For b[i] = b[j];
95  {
96  if ((*(j._word) & ( 1UL << ( j._bitpos % BITS_PER_WORD ) ) ))
97  *_word |= ( 1UL << ( _bitpos % BITS_PER_WORD ) );
98  else
99  *_word &= ~( 1UL << ( _bitpos % BITS_PER_WORD ) );
100  return *this;
101  }
102 
103  // Flips the bit
104  bool operator~() const
105  { return (*(_word) & ( 1UL << ( _bitpos % BITS_PER_WORD ) ) ) == 0; }
106 
107  // For __x = b[i];
108  operator bool() const
109  { return (*(_word) & ( 1UL << ( _bitpos % BITS_PER_WORD ) ) ) != 0; }
110 
111  // For b[i].flip();
113  {
114  *_word ^= ( 1UL << ( _bitpos % BITS_PER_WORD ) );
115  return *this;
116  }
117  };
118 
119  friend class reference;
120 
121  bitstring () : _size(0), _words(0), _data(0) {}
122 
123  bitstring (size_t length)
124  : _size(length), _words( BITSET_WORDS(length) ), _data(0)
125  {
126  _data = new _ul [_words];
127  memset(_data, 0, _words * sizeof(_ul));
128  }
129 
130  bitstring (const bitstring& b)
131  : _size(b._size), _words(b._words), _data(0)
132  {
133  _data = new _ul [_words];
134  memcpy(_data, b._data, _words * sizeof(_ul));
135  }
136 
137  ~bitstring () {if(_data != NULL) delete [] _data;}
138 
139 
140  void reset(size_t length)
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  }
151 
152  _ul* getword_atPos (size_t pos) const
153  { return &_data[ pos / BITS_PER_WORD ]; }
154 
155  _ul* getword_atIdx (size_t index) const
156  { return &_data[ index ]; }
157 
158  size_t size ( ) const {return _size;}
159 
160  size_t nb_words ( ) const {return _words;}
161 
162  bool at (size_t word, unsigned long bitmask) const
163  {
164  return bool( _data[ word ] & bitmask );
165  }
166 
167  reference operator[] (size_t pos)
168  { return reference(*this, pos); }
169 
170  bool operator[] (size_t n) const
171  { return bool( _data[ n / BITS_PER_WORD ] & ( 1UL << ( n % BITS_PER_WORD ) ) ); }
172 
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  }
182 
184  {
185  for (size_t i = 0; i < _words; i++)
186  _data[i] &= x._data[i];
187  return *this;
188  }
189 
191  {
192  for (size_t i = 0; i < _words; i++)
193  _data[i] |= x._data[i];
194  return *this;
195  }
196 
198  {
199  for (size_t i = 0; i < _words; i++)
200  _data[i] ^= x._data[i];
201  return *this;
202  }
203 
205  {
206  for (size_t i = 0; i < _words; i++)
207  _data[i] = ~(_data[i]);
208  return *this;
209  }
210 
212  {
213  bitstring result(*this);
214  result &= x;
215  return result;
216  }
217 
219  {
220  bitstring result(*this);
221  result |= x;
222  return result;
223  }
224 
226  {
227  bitstring result(*this);
228  result ^= x;
229  return result;
230  }
231 
233  unsigned int local_popcountl (_ul wd)
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  }
243 
245  unsigned int count ( )
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  }
254 
256  void set (size_t n) {_data[n / BITS_PER_WORD] |= (1UL << (n % BITS_PER_WORD));}
257 
259  void set (size_t n, bool x) {
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  }
265 
267  void flip (size_t n) {_data[n / BITS_PER_WORD] ^= (1UL << (n % BITS_PER_WORD));}
268 
270  void set_data (_ul* srce, size_t nbwrd)
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  }
280  void reset ( )
281  { for(unsigned int i = 0; i < _words; i++) _data[i] = 0UL; }
282 
284  void copy (const bitstring& b)
285  { memcpy(_data, b._data, _words * sizeof(_ul)); }
286 
288  void copy (const bitstring& b, size_t word_pos)
289  { _data[ word_pos ] = b._data[ word_pos ]; }
290 
292  void copy (const bitstring& b, size_t from, size_t to)
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  }
341 
342  std::string to_string() const
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  }
356 
357  std::string to_string(size_t from, size_t to) const
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  }
375 
376  void print (size_t from, size_t to) const
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  }
383 
384  void print() const
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  }
390 
391 private:
393  size_t _size;
394 
396  size_t _words;
397 
400 
401 
402  static unsigned char _bit_count[256];
403 
404 };
405 
406 #endif
407 
#define BITSET_WORDS(__n)
Definition: bitstring.h:45
#define MASK
Definition: bitstring.h:52
#define BITS_PER_WORD
Definition: bitstring.h:41
Definition: bitstring.h:64
reference(bitstring &bs, size_t pos)
Definition: bitstring.h:74
reference & operator=(bool x)
Definition: bitstring.h:84
_ul * _word
Definition: bitstring.h:67
reference & flip()
Definition: bitstring.h:112
size_t _bitpos
Definition: bitstring.h:68
~reference()
Definition: bitstring.h:80
bool operator~() const
Definition: bitstring.h:104
reference & operator=(const reference &j)
Definition: bitstring.h:94
Non-template and faster implementation of std::bitset.
Definition: bitstring.h:57
static unsigned char _bit_count[256]
Definition: bitstring.h:402
bitstring(const bitstring &b)
Definition: bitstring.h:130
bitstring operator|(const bitstring &x)
Definition: bitstring.h:218
unsigned int count()
Count number of set bits.
Definition: bitstring.h:245
bool at(size_t word, unsigned long bitmask) const
Definition: bitstring.h:162
_ul * getword_atPos(size_t pos) const
Definition: bitstring.h:152
unsigned long _ul
Definition: bitstring.h:61
bitstring()
Definition: bitstring.h:121
size_t size() const
Definition: bitstring.h:158
_ul * getword_atIdx(size_t index) const
Definition: bitstring.h:155
size_t _words
Number of _ul-long Words necessary to hold the _size bits.
Definition: bitstring.h:396
void print(size_t from, size_t to) const
Definition: bitstring.h:376
bitstring operator&(const bitstring &x)
Definition: bitstring.h:211
bitstring & operator|=(const bitstring &x)
Definition: bitstring.h:190
std::string to_string() const
Definition: bitstring.h:342
reference operator[](size_t pos)
Definition: bitstring.h:167
unsigned int local_popcountl(_ul wd)
Counts number of one's in string using a bit table count.
Definition: bitstring.h:233
void set(size_t n, bool x)
Set a bit to 0 or 1.
Definition: bitstring.h:259
void reset()
Set all bits to 0.
Definition: bitstring.h:280
bitstring & operator=(const bitstring &b)
Definition: bitstring.h:173
bitstring & operator^=(const bitstring &x)
Definition: bitstring.h:197
friend class reference
Definition: bitstring.h:119
void set(size_t n)
Set a bit to 1.
Definition: bitstring.h:256
bitstring operator~(void)
Definition: bitstring.h:204
size_t _size
Number of bits in the sequence.
Definition: bitstring.h:393
_ul * _data
The sequence.
Definition: bitstring.h:399
~bitstring()
Definition: bitstring.h:137
void set_data(_ul *srce, size_t nbwrd)
Copy bits from an array of unsigned long words.
Definition: bitstring.h:270
std::string to_string(size_t from, size_t to) const
Definition: bitstring.h:357
void flip(size_t n)
Flip the bit at n.
Definition: bitstring.h:267
void print() const
Definition: bitstring.h:384
bitstring operator^(const bitstring &x)
Definition: bitstring.h:225
void copy(const bitstring &b, size_t word_pos)
Copy one word.
Definition: bitstring.h:288
bitstring & operator&=(const bitstring &x)
Definition: bitstring.h:183
bitstring(size_t length)
Definition: bitstring.h:123
void copy(const bitstring &b)
Unchecked copy, assumes we have sames sizes.
Definition: bitstring.h:284
void copy(const bitstring &b, size_t from, size_t to)
Copy a delimited sequence block.
Definition: bitstring.h:292
size_t nb_words() const
Definition: bitstring.h:160
void reset(size_t length)
Definition: bitstring.h:140

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