Skip to content

Commit

Permalink
Make Bitset classes 32 and 64 bit compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4rr0 committed Nov 17, 2017
1 parent 61efc87 commit b1a995c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 56 deletions.
106 changes: 53 additions & 53 deletions bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ static inline TIndexOffU*
bitsetRealloc(TIndexOffU& sz, TIndexOffU* words, const char *errmsg = NULL) {
TIndexOffU oldsz = sz;
if(sz > 0) {
sz += (sz >> 1) + 31; // Add 50% more elements, plus a bit
sz &= ~31; // Make sure it's 32-aligned
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 & 31));
assert_eq(0, (sz & BITSET_MASK));
TIndexOffU *newwords;
try {
newwords = new TIndexOffU[sz >> 5 /* convert to words */];
newwords = new TIndexOffU[sz / WORD_SIZE /* convert to words */];
} catch(std::bad_alloc& ba) {
if(errmsg != NULL) {
// Output given error message
Expand All @@ -42,7 +42,7 @@ bitsetRealloc(TIndexOffU& sz, TIndexOffU* words, const char *errmsg = NULL) {
memcpy(newwords, words, oldsz >> 3 /* convert to bytes */);
}
// Initialize all new words to 0
memset(newwords + (oldsz >> 5 /*convert to words*/), 0,
memset(newwords + (oldsz / WORD_SIZE /*convert to words*/), 0,
(sz - oldsz) >> 3 /* convert to bytes */);
return newwords; // return new array
}
Expand All @@ -58,7 +58,7 @@ class SyncBitset {
* error message and quit if allocation fails.
*/
SyncBitset(TIndexOffU sz, const char *errmsg = NULL) : _errmsg(errmsg) {
TIndexOffU nwords = (sz >> 5)+1; // divide by 32 and add 1
TIndexOffU nwords = (sz / WORD_SIZE)+1; // divide by 32 and add 1
try {
_words = new TIndexOffU[nwords];
} catch(std::bad_alloc& ba) {
Expand All @@ -68,8 +68,8 @@ class SyncBitset {
throw 1;
}
assert(_words != NULL);
memset(_words, 0, nwords * 4 /* words to bytes */);
_sz = nwords << 5 /* words to bits */;
memset(_words, 0, nwords * OFF_SIZE /* words to bytes */);
_sz = nwords * WORD_SIZE /* words to bits */;
}

/**
Expand All @@ -84,7 +84,7 @@ class SyncBitset {
*/
bool testUnsync(TIndexOffU i) {
if(i < _sz) {
return ((_words[i >> 5] >> (i & 0x1f)) & 1) != 0;
return ((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) != 0;
}
return false;
}
Expand Down Expand Up @@ -114,9 +114,9 @@ class SyncBitset {
}
// Fast path
assert_lt(i, _sz);
assert(((_words[i >> 5] >> (i & 0x1f)) & 1) == 0);
_words[i >> 5] |= (1 << (i & 0x1f));
assert(((_words[i >> 5] >> (i & 0x1f)) & 1) == 1);
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);
}

/**
Expand All @@ -134,8 +134,8 @@ class SyncBitset {
}
// Fast path
assert_lt(i, _sz);
_words[i >> 5] |= (1 << (i & 0x1f));
assert(((_words[i >> 5] >> (i & 0x1f)) & 1) == 1);
_words[i / WORD_SIZE] |= ((TIndexOffU)1 << (i & BITSET_MASK));
assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
}


Expand Down Expand Up @@ -164,7 +164,7 @@ class Bitset {

public:
Bitset(TIndexOffU sz, const char *errmsg = NULL) : _errmsg(errmsg) {
TIndexOffU nwords = (sz >> 5)+1;
TIndexOffU nwords = (sz / WORD_SIZE)+1;
try {
_words = new TIndexOffU[nwords];
} catch(std::bad_alloc& ba) {
Expand All @@ -174,8 +174,8 @@ class Bitset {
throw 1;
}
assert(_words != NULL);
memset(_words, 0, nwords * 4);
_sz = nwords << 5;
memset(_words, 0, nwords * OFF_SIZE);
_sz = nwords * WORD_SIZE;
_cnt = 0;
}

Expand All @@ -193,7 +193,7 @@ class Bitset {
bool test(TIndexOffU i) const {
bool ret = false;
if(i < _sz) {
ret = ((_words[i >> 5] >> (i & 0x1f)) & 1) != 0;
ret = ((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) != 0;
}
return ret;
}
Expand All @@ -211,10 +211,10 @@ class Bitset {
assert_gt(_sz, oldsz);
}
// Fast path
assert(((_words[i >> 5] >> (i & 0x1f)) & 1) == 0);
assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 0);
_cnt++;
_words[i >> 5] |= (1 << (i & 0x1f));
assert(((_words[i >> 5] >> (i & 0x1f)) & 1) == 1);
_words[i / WORD_SIZE] |= ((TIndexOffU)1 << (i & BITSET_MASK));
assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
}

/**
Expand All @@ -229,16 +229,16 @@ class Bitset {
assert_gt(_sz, oldsz);
}
// Fast path
if(((_words[i >> 5] >> (i & 0x1f)) & 1) == 0) _cnt++;
_words[i >> 5] |= (1 << (i & 0x1f));
assert(((_words[i >> 5] >> (i & 0x1f)) & 1) == 1);
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+31)>>5); i++) {
for(size_t i = 0; i < ((_sz+BITSET_MASK) / WORD_SIZE); i++) {
_words[i] = 0;
}
_cnt = 0;
Expand Down Expand Up @@ -266,8 +266,8 @@ class Bitset {
_sz = o._sz;
_cnt = o._cnt;
if(_words != NULL) delete[] _words;
_words = new TIndexOffU[(_sz+31)>>5];
for(size_t i = 0; i < (_sz+31)>>5; i++) {
_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;
Expand Down Expand Up @@ -299,14 +299,14 @@ class FixedBitset {

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

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

/**
Expand All @@ -315,7 +315,7 @@ class FixedBitset {
bool test(TIndexOffU i) const {
bool ret = false;
assert_lt(i, LEN);
ret = ((_words[i >> 5] >> (i & 0x1f)) & 1) != 0;
ret = ((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) != 0;
return ret;
}

Expand All @@ -325,13 +325,13 @@ class FixedBitset {
void set(TIndexOffU i) {
// Fast path
assert_lt(i, LEN);
assert(((_words[i >> 5] >> (i & 0x1f)) & 1) == 0);
_words[i >> 5] |= (1 << (i & 0x1f));
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 >> 5] >> (i & 0x1f)) & 1) == 1);
assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
}

/**
Expand All @@ -341,12 +341,12 @@ class FixedBitset {
void setOver(TIndexOffU i) {
// Fast path
assert_lt(i, LEN);
_words[i >> 5] |= (1 << (i & 0x1f));
_words[i / WORD_SIZE] |= ((TIndexOffU)1 << (i & BITSET_MASK));
_cnt++;
if(i >= _size) {
_size = i+1;
}
assert(((_words[i >> 5] >> (i & 0x1f)) & 1) == 1);
assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
}

TIndexOffU count() const { return _cnt; }
Expand All @@ -357,7 +357,7 @@ class FixedBitset {
* FixedBitset 'that'.
*/
bool operator== (const FixedBitset<LEN>& that) const {
for(TIndexOffU i = 0; i < (LEN>>5)+1; i++) {
for(TIndexOffU i = 0; i < (LEN / WORD_SIZE)+1; i++) {
if(_words[i] != that._words[i]) {
return false;
}
Expand All @@ -370,7 +370,7 @@ class FixedBitset {
* as FixedBitset 'that'.
*/
bool operator!= (const FixedBitset<LEN>& that) const {
for(TIndexOffU i = 0; i < (LEN>>5)+1; i++) {
for(TIndexOffU i = 0; i < (LEN / WORD_SIZE)+1; i++) {
if(_words[i] != that._words[i]) {
return true;
}
Expand All @@ -392,7 +392,7 @@ class FixedBitset {
private:
TIndexOffU _cnt;
TIndexOffU _size;
TIndexOffU _words[(LEN>>5)+1]; // storage
TIndexOffU _words[(LEN / WORD_SIZE)+1]; // storage
};

/**
Expand All @@ -402,8 +402,8 @@ class FixedBitset2 {

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

~FixedBitset2() { delete[] _words; }
Expand All @@ -412,7 +412,7 @@ class FixedBitset2 {
* Unset all bits.
*/
void clear() {
memset(_words, 0, ((len_ >> 5)+1) * 4);
memset(_words, 0, ((len_ / WORD_SIZE)+1) * OFF_SIZE);
_cnt = 0;
_size = 0;
}
Expand All @@ -423,7 +423,7 @@ class FixedBitset2 {
bool test(TIndexOffU i) const {
bool ret = false;
assert_lt(i, len_);
ret = ((_words[i >> 5] >> (i & 0x1f)) & 1) != 0;
ret = ((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) != 0;
return ret;
}

Expand All @@ -433,13 +433,13 @@ class FixedBitset2 {
void set(TIndexOffU i) {
// Fast path
assert_lt(i, len_);
assert(((_words[i >> 5] >> (i & 0x1f)) & 1) == 0);
_words[i >> 5] |= (1 << (i & 0x1f));
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 >> 5] >> (i & 0x1f)) & 1) == 1);
assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
}

/**
Expand All @@ -448,13 +448,13 @@ class FixedBitset2 {
void clear(TIndexOffU i) {
// Fast path
assert_lt(i, len_);
assert(((_words[i >> 5] >> (i & 0x1f)) & 1) == 1);
_words[i >> 5] &= ~(1 << (i & 0x1f));
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 >> 5] >> (i & 0x1f)) & 1) == 0);
assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 0);
}

/**
Expand All @@ -464,14 +464,14 @@ class FixedBitset2 {
void setOver(TIndexOffU i) {
// Fast path
assert_lt(i, len_);
if(((_words[i >> 5] >> (i & 0x1f)) & 1) == 0) {
_words[i >> 5] |= (1 << (i & 0x1f));
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 >> 5] >> (i & 0x1f)) & 1) == 1);
assert(((_words[i / WORD_SIZE] >> (i & BITSET_MASK)) & 1) == 1);
}

TIndexOffU count() const { return _cnt; }
Expand All @@ -482,7 +482,7 @@ class FixedBitset2 {
* FixedBitset 'that'.
*/
bool operator== (const FixedBitset2& that) const {
for(TIndexOffU i = 0; i < (len_>>5)+1; i++) {
for(TIndexOffU i = 0; i < (len_ / WORD_SIZE)+1; i++) {
if(_words[i] != that._words[i]) {
return false;
}
Expand All @@ -495,7 +495,7 @@ class FixedBitset2 {
* as FixedBitset 'that'.
*/
bool operator!= (const FixedBitset2& that) const {
for(TIndexOffU i = 0; i < (len_>>5)+1; i++) {
for(TIndexOffU i = 0; i < (len_ / WORD_SIZE)+1; i++) {
if(_words[i] != that._words[i]) {
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions btypes.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


#ifndef BOWTIE_INDEX_TYPES_H
#define BOWTIE_INDEX_TYPES_H

Expand All @@ -9,6 +7,8 @@
#define LS_SIZE 0x100000000000000
#define OFF_SIZE 8
#define CACHE_WRAPPER_BIT 0x8000000000000000
#define WORD_SIZE 64
#define BITSET_MASK 0x000000000000003f

typedef uint64_t TIndexOffU;
typedef int64_t TIndexOff;
Expand All @@ -19,6 +19,8 @@ typedef int64_t TIndexOff;
#define LS_SIZE 0x10000000
#define OFF_SIZE 4
#define CACHE_WRAPPER_BIT 0x80000000
#define WORD_SIZE 32
#define BITSET_MASK 0x0000001f

typedef uint32_t TIndexOffU;
typedef int TIndexOff;
Expand All @@ -28,4 +30,3 @@ typedef int TIndexOff;
extern const std::string gEbwt_ext;

#endif /* BOWTIE_INDEX_TYPES_H */

0 comments on commit b1a995c

Please sign in to comment.