Nemo  2.3.56
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#define MASK ULLONG_MAX
52
53//define MASK = (BITS_PER_WORD == 64 ? 0xFFFFFFFFFFFFFFFF : 0xFFFFFFFF)
54
56class bitstring {
57
58public:
59
60 typedef unsigned long _ul;
61
63 {
64 friend class bitstring;
65
67 size_t _bitpos;
68
69 // left undefined
71
72 public:
73 reference(bitstring& bs, size_t pos)
74 {
75 _word = bs.getword_atPos( pos );
76 _bitpos = pos % BITS_PER_WORD;
77 }
78
80 { }
81
82 // For b[i] = x;
84 {
85 if (x)
86 *_word |= ( 1UL << ( _bitpos % BITS_PER_WORD ) );
87 else
88 *_word &= ~( 1UL << ( _bitpos % BITS_PER_WORD ) );
89 return *this;
90 }
91
92 // For b[i] = b[j];
94 {
95 if ((*(j._word) & ( 1UL << ( j._bitpos % BITS_PER_WORD ) ) ))
96 *_word |= ( 1UL << ( _bitpos % BITS_PER_WORD ) );
97 else
98 *_word &= ~( 1UL << ( _bitpos % BITS_PER_WORD ) );
99 return *this;
100 }
101
102 // Flips the bit
103 bool operator~() const
104 { return (*(_word) & ( 1UL << ( _bitpos % BITS_PER_WORD ) ) ) == 0; }
105
106 // For __x = b[i];
107 operator bool() const
108 { return (*(_word) & ( 1UL << ( _bitpos % BITS_PER_WORD ) ) ) != 0; }
109
110 // For b[i].flip();
112 {
113 *_word ^= ( 1UL << ( _bitpos % BITS_PER_WORD ) );
114 return *this;
115 }
116 };
117
118 friend class reference;
119
120 bitstring (size_t length)
121 : _size(length), _words( BITSET_WORDS(length) ), _data(0)
122 {
123 _data = new _ul [_words];
124 memset(_data, 0, _words * sizeof(_ul));
125 }
126
128 : _size(b._size), _words(b._words), _data(0)
129 {
130 _data = new _ul [_words];
131 memcpy(_data, b._data, _words * sizeof(_ul));
132 }
133
134 ~bitstring () {if(_data != NULL) delete [] _data;}
135
136 _ul* getword_atPos (size_t pos)
137 { return &_data[ pos / BITS_PER_WORD ]; }
138
139 _ul* getword_atIdx (size_t index)
140 { return &_data[ index ]; }
141
142 size_t size ( ) {return _size;}
143
144 size_t nb_words ( ) {return _words;}
145
146 bool at (size_t word, unsigned long bitmask)
147 {
148 return static_cast<bool>( _data[ word ] & bitmask );
149 }
150
152 { return reference(*this, pos); }
153
154 bool operator[] (size_t n) const
155 { return static_cast<bool>( _data[ n / BITS_PER_WORD ] & ( 1UL << ( n % BITS_PER_WORD ) ) ); }
156
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 }
166
168 {
169 for (size_t i = 0; i < _words; i++)
170 _data[i] &= x._data[i];
171 return *this;
172 }
173
175 {
176 for (size_t i = 0; i < _words; i++)
177 _data[i] |= x._data[i];
178 return *this;
179 }
180
182 {
183 for (size_t i = 0; i < _words; i++)
184 _data[i] ^= x._data[i];
185 return *this;
186 }
187
189 {
190 for (size_t i = 0; i < _words; i++)
191 _data[i] = ~(_data[i]);
192 return *this;
193 }
194
196 {
197 bitstring result(*this);
198 result &= x;
199 return result;
200 }
201
203 {
204 bitstring result(*this);
205 result |= x;
206 return result;
207 }
208
210 {
211 bitstring result(*this);
212 result ^= x;
213 return result;
214 }
215
217 unsigned int local_popcountl (_ul wd)
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 }
227
229 unsigned int count ( )
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 }
238
240 void set (size_t n) {_data[n / BITS_PER_WORD] |= (1UL << (n % BITS_PER_WORD));}
241
243 void flip (size_t n) {_data[n / BITS_PER_WORD] ^= (1UL << (n % BITS_PER_WORD));}
244
246 void set_data (_ul* srce, size_t nbwrd)
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 }
256 void reset ( )
257 { for(unsigned int i = 0; i < _words; i++) _data[i] = 0UL; }
258
260 void copy (const bitstring& b)
261 { memcpy(_data, b._data, _words * sizeof(_ul)); }
262
264 void copy (const bitstring& b, size_t word_pos)
265 { _data[ word_pos ] = b._data[ word_pos ]; }
266
267 void copy (const bitstring& b, size_t from, size_t to)
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 }
319
320private:
321
322 size_t _size;
323 size_t _words;
324
326
327
328 static unsigned char _bit_count[256];
329
330};
331
332#endif
333
#define BITSET_WORDS(__n)
Definition: bitstring.h:45
#define MASK
Definition: bitstring.h:51
#define BITS_PER_WORD
Definition: bitstring.h:41
Definition: bitstring.h:63
reference(bitstring &bs, size_t pos)
Definition: bitstring.h:73
_ul * _word
Definition: bitstring.h:66
size_t _bitpos
Definition: bitstring.h:67
~reference()
Definition: bitstring.h:79
bool operator~() const
Definition: bitstring.h:103
reference & flip()
Definition: bitstring.h:111
reference & operator=(const reference &j)
Definition: bitstring.h:93
reference & operator=(bool x)
Definition: bitstring.h:83
Non-template and faster implementation of std::bitset.
Definition: bitstring.h:56
static unsigned char _bit_count[256]
Definition: bitstring.h:328
bitstring(const bitstring &b)
Definition: bitstring.h:127
bool at(size_t word, unsigned long bitmask)
Definition: bitstring.h:146
bitstring operator|(const bitstring &x)
Definition: bitstring.h:202
unsigned int count()
Count number of set bits.
Definition: bitstring.h:229
bitstring & operator|=(const bitstring &x)
Definition: bitstring.h:174
unsigned long _ul
Definition: bitstring.h:60
size_t _words
Definition: bitstring.h:323
_ul * getword_atIdx(size_t index)
Definition: bitstring.h:139
bitstring operator&(const bitstring &x)
Definition: bitstring.h:195
reference operator[](size_t pos)
Definition: bitstring.h:151
unsigned int local_popcountl(_ul wd)
Counts number of one's in string using a bit table count.
Definition: bitstring.h:217
void reset()
Set all bits to 0.
Definition: bitstring.h:256
size_t size()
Definition: bitstring.h:142
void set(size_t n)
Set a bit to 1.
Definition: bitstring.h:240
size_t nb_words()
Definition: bitstring.h:144
bitstring operator~(void)
Definition: bitstring.h:188
size_t _size
Definition: bitstring.h:322
bitstring & operator^=(const bitstring &x)
Definition: bitstring.h:181
_ul * _data
Definition: bitstring.h:325
~bitstring()
Definition: bitstring.h:134
void set_data(_ul *srce, size_t nbwrd)
Copy bits from an array of unsigned long words.
Definition: bitstring.h:246
void flip(size_t n)
Flip the bit at n.
Definition: bitstring.h:243
bitstring & operator&=(const bitstring &x)
Definition: bitstring.h:167
_ul * getword_atPos(size_t pos)
Definition: bitstring.h:136
bitstring operator^(const bitstring &x)
Definition: bitstring.h:209
void copy(const bitstring &b, size_t word_pos)
Copy one word.
Definition: bitstring.h:264
bitstring(size_t length)
Definition: bitstring.h:120
void copy(const bitstring &b)
Unchecked copy, assumes we have sames sizes.
Definition: bitstring.h:260
void copy(const bitstring &b, size_t from, size_t to)
Definition: bitstring.h:267
bitstring & operator=(const bitstring &b)
Definition: bitstring.h:157

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