Skip to content

Commit

Permalink
Adding support for logical operators (overloading) plus a potential o…
Browse files Browse the repository at this point in the history
…verflow fix.
  • Loading branch information
lemire committed Sep 2, 2017
1 parent 664b7aa commit 84aba77
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
23 changes: 20 additions & 3 deletions headers/ewah.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ template <class uword = uint32_t> class EWAHBoolArray {
logicaland(a, answer);
return answer;
}
EWAHBoolArray operator&(const EWAHBoolArray &a) const {
return logicaland(a);
}

/**
* computes the logical and with another compressed bitmap
Expand All @@ -199,6 +202,9 @@ template <class uword = uint32_t> class EWAHBoolArray {
*/
void logicalandnot(const EWAHBoolArray &a, EWAHBoolArray &container) const;

EWAHBoolArray operator-(const EWAHBoolArray &a) const {
return logicalandnot(a);
}

/**
* computes the logical and not with another compressed bitmap
Expand All @@ -211,6 +217,8 @@ template <class uword = uint32_t> class EWAHBoolArray {
logicalandnot(a, answer);
return answer;
}


/**
* tests whether the bitmaps "intersect" (have at least one 1-bit at the same
* position). This function does not modify the existing bitmaps.
Expand All @@ -229,6 +237,7 @@ template <class uword = uint32_t> class EWAHBoolArray {
void logicalor(const EWAHBoolArray &a, EWAHBoolArray &container) const;



/**
* computes the size (in number of set bits) of the logical or with another compressed bitmap
* Running time complexity is proportional to the sum of the compressed
Expand Down Expand Up @@ -275,6 +284,10 @@ template <class uword = uint32_t> class EWAHBoolArray {
return answer;
}

EWAHBoolArray operator|(const EWAHBoolArray &a) const {
return logicalor(a);
}

/**
* computes the logical xor with another compressed bitmap
* answer goes into container
Expand All @@ -294,6 +307,10 @@ template <class uword = uint32_t> class EWAHBoolArray {
logicalxor(a, answer);
return answer;
}

EWAHBoolArray operator^(const EWAHBoolArray &a) const {
return logicalxor(a);
}
/**
* clear the content of the bitmap. It does not
* release the memory.
Expand Down Expand Up @@ -1502,7 +1519,7 @@ size_t EWAHBoolArray<uword>::addStreamOfDirtyWords(const uword *v,
buffer.push_back(0);
lastRLW = buffer.size() - 1;
++wordadded;
wordadded += addStreamOfDirtyWords(v + howmanywecanadd, NumberOfLiteralWords - howmanywecanadd);
wordadded += addStreamOfDirtyWords(v + howmanywecanadd, number - howmanywecanadd);
return wordadded;
}

Expand All @@ -1528,7 +1545,7 @@ void EWAHBoolArray<uword>::fastaddStreamOfDirtyWords(const uword *v,
//buffer.insert(buffer.end(), v, v+howmanywecanadd);// seems slower than push_back?
buffer.push_back(0);
lastRLW = buffer.size() - 1;
fastaddStreamOfDirtyWords(v + howmanywecanadd, NumberOfLiteralWords - howmanywecanadd);
fastaddStreamOfDirtyWords(v + howmanywecanadd, number - howmanywecanadd);
}


Expand Down Expand Up @@ -1558,7 +1575,7 @@ size_t EWAHBoolArray<uword>::addStreamOfNegatedDirtyWords(const uword *v,
buffer.push_back(0);
lastRLW = buffer.size() - 1;
++wordadded;
wordadded += addStreamOfDirtyWords(v + howmanywecanadd, NumberOfLiteralWords - howmanywecanadd);
wordadded += addStreamOfDirtyWords(v + howmanywecanadd, number - howmanywecanadd);
return wordadded;
}

Expand Down
56 changes: 56 additions & 0 deletions src/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,55 @@ template <class uword> bool testCardinalityEWAHBoolArray() {
return true;
}

template <class uword> bool testLargeDirty() {
cout << "[testing LargeDirty] sizeof(uword)=" << sizeof(uword)
<< endl;
size_t N = 80000000;
vector<uint32_t> bigarray1(N);
vector<uint32_t> bigarray2(N);
for(size_t k = 0; k < N; k++) {
bigarray1[k] = (uint32_t)(8 * k);
bigarray2[k] = (uint32_t)(32 * k);
}
EWAHBoolArray<uword> b1 = EWAHBoolArray<uword>();
EWAHBoolArray<uword> b2 = EWAHBoolArray<uword>();
for(size_t k = 0; k < N; k++) {
b1.set(8 * k);
b2.set(64 * k);
}

EWAHBoolArray<uword> b3 = EWAHBoolArray<uword>::bitmapOf(3,1,10,1000);
EWAHBoolArray<uword> b4 = EWAHBoolArray<uword>::bitmapOf(3,1,10,1000);

if (b1.numberOfOnes() != N) {
return false;
}
if (b2.numberOfOnes() != N) {
return false;
}
if (b3.numberOfOnes() != 3) {
return false;
}
b3 = b3 | b1;
b4 = b4 | b2;
b3 = b3 | b2;
b4 = b4 | b1;
if( b3 != b4 ) {
return false;
}
b4 = b4 - b1;
if (b4.numberOfOnes() != 3) {
return false;
}
b3 = b3 ^ b4;
if( b3 != b1 ) {
return false;
}

return true;
}


template <class uword> bool testAndNotEWAHBoolArray() {
cout << "[testing AndNotEWAHBoolArray] sizeof(uword)=" << sizeof(uword) << endl;
EWAHBoolArray<uword> b1 = EWAHBoolArray<uword>::bitmapOf(1,1);
Expand Down Expand Up @@ -1423,6 +1472,13 @@ int main(void) {
if (!funnytest()) {
++failures;
}
if (!testLargeDirty<uint64_t>())
++failures;
if (!testLargeDirty<uint16_t>())
++failures;
if (!testLargeDirty<uint32_t>())
++failures;

if (!countstest4<uint64_t>())
++failures;
if (!countstest4<uint16_t>())
Expand Down

0 comments on commit 84aba77

Please sign in to comment.