File: bitset.h

package info (click to toggle)
bowtie 1.2.3+dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 16,888 kB
  • sloc: cpp: 35,784; perl: 5,903; ansic: 1,247; sh: 1,180; python: 487; makefile: 426
file content (524 lines) | stat: -rw-r--r-- 12,235 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#ifndef BITSET_H_
#define BITSET_H_

#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>
#include <stdexcept>
#include "assert_helpers.h"
#include "threading.h"
#include "btypes.h"

/**
 * Given a words array and a size, allocate a new, larger array, moving
 * data from the old to the new array, and set all newly-allocated
 * words to 0.  Return the new, larger array, which can be substituted
 * for the old one.  The new array is larger than the old by about 50%.
 */
static inline TIndexOffU*
bitsetRealloc(TIndexOffU& sz, TIndexOffU* words, const char *errmsg = NULL) {
	TIndexOffU oldsz = sz;
	if(sz > 0) {
		sz += (sz >> 1) + BITSET_MASK; // Add 50% more elements, plus a bit
		sz &= ~BITSET_MASK;            // Make sure it's 32-aligned
	} else {
		sz = 1024; // Start off at 1024 bits to avoid many expansions
	}
	assert_gt(sz, oldsz);
	assert_eq(0, (sz & BITSET_MASK));
	TIndexOffU *newwords;
	try {
		newwords = new TIndexOffU[sz / WORD_SIZE /* convert to words */];
	} catch(std::bad_alloc& ba) {
		if(errmsg != NULL) {
			// Output given error message
			std::cerr << errmsg;
		}
		throw 1;
	}
	if(oldsz > 0) {
		// Move old values into new array
		memcpy(newwords, words, oldsz >> 3 /* convert to bytes */);
	}
	// Initialize all new words to 0
	memset(newwords + (oldsz / WORD_SIZE /*convert to words*/), 0,
	       (sz - oldsz) >> 3 /* convert to bytes */);
	return newwords; // return new array
}

/**
 * A simple synchronized bitset class.
 */
class SyncBitset {

public:
	/**
	 * Allocate enough words to accommodate 'sz' bits.  Output the given
	 * error message and quit if allocation fails.
	 */
	SyncBitset(TIndexOffU sz, const char *errmsg = NULL) : _errmsg(errmsg) {
		TIndexOffU nwords = (sz / WORD_SIZE)+1; // divide by 32 and add 1
		try {
			_words = new TIndexOffU[nwords];
		} catch(std::bad_alloc& ba) {
			if(_errmsg != NULL) {
				std::cerr << _errmsg;
			}
			throw 1;
		}
		assert(_words != NULL);
		memset(_words, 0, nwords * OFF_SIZE /* words to bytes */);
		_sz = nwords * WORD_SIZE /* words to bits */;
	}

	/**
	 * Free memory for words.
	 */
	~SyncBitset() {
		delete[] _words;
	}

	/**
	 * Test whether the given bit is set in an unsynchronized manner.
	 */
	bool testUnsync(TIndexOffU i) {
		if(i < _sz) {
			return ((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) != 0;
		}
		return false;
	}

	/**
	 * Test whether the given bit is set in a synchronized manner.
	 */
	bool test(TIndexOffU i) {
		bool ret;
		ThreadSafe _ts(&mutex_m);
		ret = testUnsync(i);
		return ret;
	}

	/**
	 * Set a bit in the vector that hasn't been set before.  Assert if
	 * it has been set.  Uses synchronization.
	 */
	void set(TIndexOffU i) {
		ThreadSafe _ts(&mutex_m);
		while(i >= _sz) {
			// Slow path: bitset needs to be expanded before the
			// specified bit can be set
			ASSERT_ONLY(TIndexOffU oldsz = _sz);
			expand();
			assert_gt(_sz, oldsz);
		}
		// Fast path
		assert_lt(i, _sz);
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 0);
		_words[i / WORD_SIZE] |= ((TIndexOffU)1 << (i & BITSET_MASK));
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
	}

	/**
	 * Set a bit in the vector that might have already been set.  Uses
	 * synchronization.
	 */
	void setOver(TIndexOffU i) {
		ThreadSafe _ts(&mutex_m);
		while(i >= _sz) {
			// Slow path: bitset needs to be expanded before the
			// specified bit can be set
			ASSERT_ONLY(TIndexOffU oldsz = _sz);
			expand();
			assert_gt(_sz, oldsz);
		}
		// Fast path
		assert_lt(i, _sz);
		_words[i / WORD_SIZE] |= ((TIndexOffU)1 << (i & BITSET_MASK));
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
	}


private:

	/**
	 * Expand the size of the _words array by 50% to accommodate more
	 * bits.
	 */
	void expand() {
		TIndexOffU *newwords = bitsetRealloc(_sz, _words, _errmsg);
		delete[] _words;   // delete old array
		_words = newwords; // install new array
	}

	const char *_errmsg; // error message if an allocation fails
	TIndexOffU _sz;        // size as # of bits
	MUTEX_T mutex_m;       // mutex
	TIndexOffU *_words;    // storage
};

/**
 * A simple unsynchronized bitset class.
 */
class Bitset {

public:
	Bitset(TIndexOffU sz, const char *errmsg = NULL) : _errmsg(errmsg) {
		TIndexOffU nwords = (sz / WORD_SIZE)+1;
		try {
			_words = new TIndexOffU[nwords];
		} catch(std::bad_alloc& ba) {
			if(_errmsg != NULL) {
				std::cerr << _errmsg;
			}
			throw 1;
		}
		assert(_words != NULL);
		memset(_words, 0, nwords * OFF_SIZE);
		_sz = nwords * WORD_SIZE;
		_cnt = 0;
	}

	Bitset(const Bitset& o) : _words(NULL) {
		this->operator=(o);
	}

	~Bitset() {
		delete[] _words;
	}

	/**
	 * Test whether the given bit is set.
	 */
	bool test(TIndexOffU i) const {
		bool ret = false;
		if(i < _sz) {
			ret = ((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) != 0;
		}
		return ret;
	}

	/**
	 * Set a bit in the vector that hasn't been set before.  Assert if
	 * it has been set.
	 */
	void set(TIndexOffU i) {
		while(i >= _sz) {
			// Slow path: bitset needs to be expanded before the
			// specified bit can be set
			ASSERT_ONLY(TIndexOffU oldsz = _sz);
			expand();
			assert_gt(_sz, oldsz);
		}
		// Fast path
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 0);
		_cnt++;
		_words[i / WORD_SIZE] |= ((TIndexOffU)1 << (i & BITSET_MASK));
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
	}

	/**
	 * Set a bit in the vector that might have already been set.
	 */
	void setOver(TIndexOffU i) {
		while(i >= _sz) {
			// Slow path: bitset needs to be expanded before the
			// specified bit can be set
			ASSERT_ONLY(TIndexOffU oldsz = _sz);
			expand();
			assert_gt(_sz, oldsz);
		}
		// Fast path
		if(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 0) _cnt++;
		_words[i / WORD_SIZE] |= ((TIndexOffU)1 << (i & BITSET_MASK));
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
	}

	/**
	 * Unset all entries.  Don't adjust size.
	 */
	void clear() {
		for(size_t i = 0; i < ((_sz+BITSET_MASK) / WORD_SIZE); i++) {
			_words[i] = 0;
		}
		_cnt = 0;
	}

	/**
	 * Return the number of set bits.
	 */
	TIndexOffU count() const {
		return _cnt;
	}

	/**
	 * Return true iff no bits are set.
	 */
	bool empty() const {
		return _cnt == 0;
	}

	/**
	 * Deep copy from given Bitset to this one.
	 */
	Bitset& operator=(const Bitset& o) {
		_errmsg = o._errmsg;
		_sz = o._sz;
		_cnt = o._cnt;
		if(_words != NULL) delete[] _words;
		_words = new TIndexOffU[(_sz+BITSET_MASK) / WORD_SIZE];
		for(size_t i = 0; i < (_sz+BITSET_MASK) / WORD_SIZE; i++) {
			_words[i] = o._words[i];
		}
		return *this;
	}

private:

	/**
	 * Expand the size of the _words array by 50% to accommodate more
	 * bits.
	 */
	void expand() {
		TIndexOffU *newwords = bitsetRealloc(_sz, _words, _errmsg);
		delete[] _words;   // delete old array
		_words = newwords; // install new array
	}

	TIndexOffU _cnt;       // number of set bits
	const char *_errmsg; // error message if an allocation fails
	TIndexOffU _sz;        // size as # of bits
	TIndexOffU *_words;    // storage
};

/**
 * A simple fixed-length unsynchronized bitset class.
 */
template<int LEN>
class FixedBitset {

public:
	FixedBitset() : _cnt(0), _size(0) {
		memset(_words, 0, ((LEN / WORD_SIZE)+1) * OFF_SIZE);
	}

	/**
	 * Unset all bits.
	 */
	void clear() {
		memset(_words, 0, ((LEN / WORD_SIZE)+1) * OFF_SIZE);
	}

	/**
	 * Return true iff the bit at offset i has been set.
	 */
	bool test(TIndexOffU i) const {
		bool ret = false;
		assert_lt(i, LEN);
		ret = ((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) != 0;
		return ret;
	}

	/**
	 * Set the bit at offset i.  Assert if the bit was already set.
	 */
	void set(TIndexOffU i) {
		// Fast path
		assert_lt(i, LEN);
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 0);
		_words[i / WORD_SIZE] |= ((TIndexOffU)1 << (i & BITSET_MASK));
		_cnt++;
		if(i >= _size) {
			_size = i+1;
		}
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
	}

	/**
	 * Set the bit at offset i.  Do not assert if the bit was already
	 * set.
	 */
	void setOver(TIndexOffU i) {
		// Fast path
		assert_lt(i, LEN);
		_words[i / WORD_SIZE] |= ((TIndexOffU)1 << (i & BITSET_MASK));
		_cnt++;
		if(i >= _size) {
			_size = i+1;
		}
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
	}

	TIndexOffU count() const { return _cnt; }
	TIndexOffU size() const  { return _size; }

	/**
	 * Return true iff this FixedBitset has the same bits set as
	 * FixedBitset 'that'.
	 */
	bool operator== (const FixedBitset<LEN>& that) const {
		for(TIndexOffU i = 0; i < (LEN / WORD_SIZE)+1; i++) {
			if(_words[i] != that._words[i]) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Return true iff this FixedBitset does not have the same bits set
	 * as FixedBitset 'that'.
	 */
	bool operator!= (const FixedBitset<LEN>& that) const {
		for(TIndexOffU i = 0; i < (LEN / WORD_SIZE)+1; i++) {
			if(_words[i] != that._words[i]) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Return a string-ized version of this FixedBitset.
	 */
	std::string str() const {
		std::ostringstream oss;
		for(int i = (int)size()-1; i >= 0; i--) {
			oss << (test(i)? "1" : "0");
		}
		return oss.str();
	}

private:
	TIndexOffU _cnt;
	TIndexOffU _size;
	TIndexOffU _words[(LEN / WORD_SIZE)+1]; // storage
};

/**
 * A simple fixed-length unsynchronized bitset class.
 */
class FixedBitset2 {

public:
	FixedBitset2(TIndexOffU len) : len_(len), _cnt(0), _size(0) {
		_words = new TIndexOffU[((len_ / WORD_SIZE)+1)];
		memset(_words, 0, ((len_ / WORD_SIZE)+1) * OFF_SIZE);
	}

	~FixedBitset2() { delete[] _words; }

	/**
	 * Unset all bits.
	 */
	void clear() {
		memset(_words, 0, ((len_ / WORD_SIZE)+1) * OFF_SIZE);
		_cnt = 0;
		_size = 0;
	}

	/**
	 * Return true iff the bit at offset i has been set.
	 */
	bool test(TIndexOffU i) const {
		bool ret = false;
		assert_lt(i, len_);
		ret = ((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) != 0;
		return ret;
	}

	/**
	 * Set the bit at offset i.  Assert if the bit was already set.
	 */
	void set(TIndexOffU i) {
		// Fast path
		assert_lt(i, len_);
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 0);
		_words[i / WORD_SIZE] |= ((TIndexOffU)1 << (i & BITSET_MASK));
		_cnt++;
		if(i >= _size) {
			_size = i+1;
		}
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
	}

	/**
	 * Clear the bit at offset i.  Assert if the bit was not already set.
	 */
	void clear(TIndexOffU i) {
		// Fast path
		assert_lt(i, len_);
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
		_words[i / WORD_SIZE] &= ~((TIndexOffU)1 << (i & BITSET_MASK));
		_cnt--;
		if(i >= _size) {
			_size = i+1;
		}
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 0);
	}

	/**
	 * Set the bit at offset i.  Do not assert if the bit was already
	 * set.
	 */
	void setOver(TIndexOffU i) {
		// Fast path
		assert_lt(i, len_);
		if(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 0) {
			_words[i / WORD_SIZE] |= ((TIndexOffU)1 << (i & BITSET_MASK));
			_cnt++;
		}
		if(i >= _size) {
			_size = i+1;
		}
		assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
	}

	TIndexOffU count() const { return _cnt; }
	TIndexOffU size() const  { return _size; }

	/**
	 * Return true iff this FixedBitset has the same bits set as
	 * FixedBitset 'that'.
	 */
	bool operator== (const FixedBitset2& that) const {
		for(TIndexOffU i = 0; i < (len_ / WORD_SIZE)+1; i++) {
			if(_words[i] != that._words[i]) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Return true iff this FixedBitset does not have the same bits set
	 * as FixedBitset 'that'.
	 */
	bool operator!= (const FixedBitset2& that) const {
		for(TIndexOffU i = 0; i < (len_ / WORD_SIZE)+1; i++) {
			if(_words[i] != that._words[i]) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Return a string-ized version of this FixedBitset.
	 */
	std::string str() const {
		std::ostringstream oss;
		for(int i = (int)size()-1; i >= 0; i--) {
			oss << (test(i)? "1" : "0");
		}
		return oss.str();
	}

private:
	const TIndexOffU len_;
	TIndexOffU _cnt;
	TIndexOffU _size;
	TIndexOffU *_words; // storage
};

#endif /* BITSET_H_ */