Skip to content

Commit

Permalink
Adding "empty" function.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Jul 3, 2018
1 parent f0e6b4e commit 44e00af
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
20 changes: 20 additions & 0 deletions headers/ewah.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ template <class uword = uint32_t> class EWAHBoolArray {
return false;
}

/**
* Returns true if no bit is set.
*/
bool empty() const {
size_t pointer(0);
while (pointer < buffer.size()) {
ConstRunningLengthWord<uword> rlw(buffer[pointer]);
if (rlw.getRunningBit()) {
if(rlw.getRunningLength() > 0) return false;
}
++pointer;
for (size_t k = 0; k < rlw.getNumberOfLiteralWords(); ++k) {
if(buffer[pointer] != 0) return false;
++pointer;
}
}
return true;
}


/**
* Set the ith bit to true (starting at zero).
* Auto-expands the bitmap. It has constant running time complexity.
Expand Down
24 changes: 22 additions & 2 deletions src/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,20 @@ template <class uword> bool testInEqualityEWAHBoolArray() {
}
return true;
}

template <class uword> bool testEmpty() {
cout << "[testing SizeInBits] sizeof(uword)=" << sizeof(uword) << endl;
EWAHBoolArray<uword> ewah0;
if(!ewah0.empty()) {
std::cout << "empty is buggy " << std::endl;
return false;
}
EWAHBoolArray<uword> ewah1 = EWAHBoolArray<uword>::bitmapOf(1, 1);
if(ewah1.empty()) {
std::cout << "empty is buggy " << std::endl;
return false;
}
return true;
}
template <class uword> bool testSizeInBits() {
cout << "[testing SizeInBits] sizeof(uword)=" << sizeof(uword) << endl;

Expand Down Expand Up @@ -1646,7 +1659,14 @@ int main(void) {
if (!funnytest()) {
++failures;
}

if (!testEmpty<uint64_t>()) {
std::cout << failtext << __LINE__ << std::endl;
++failures;
}
if (!testEmpty<uint16_t>()) {
std::cout << failtext << __LINE__ << std::endl;
++failures;
}
if (!testSizeInBits<uint64_t>()) {
std::cout << failtext << __LINE__ << std::endl;
++failures;
Expand Down

0 comments on commit 44e00af

Please sign in to comment.