Skip to content

Commit

Permalink
Implemented logicalorCount + bug fixes in logicalxorCount
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Mar 17, 2017
1 parent ba7feaa commit dfcbc6d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
53 changes: 49 additions & 4 deletions include/concise.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,49 @@ template <bool wah_mode = false> class ConciseSet {
return answer;
}


size_t logicalorCount(const ConciseSet<wah_mode> &other) const {
if (this->isEmpty()) {
return other.size();
}
if (other.isEmpty()) {
return this->size();
}
size_t answer = 0;
// scan "this" and "other"
WordIterator<wah_mode> thisItr(*this);
WordIterator<wah_mode> otherItr(other);
while (true) {
if (!thisItr.IsLiteral) {
if (!otherItr.IsLiteral) {
int minCount = std::min(thisItr.count, otherItr.count);
if((thisItr.word | otherItr.word) & SEQUENCE_BIT)
answer += 31 * minCount;
if (!thisItr.prepareNext(minCount) |
!otherItr.prepareNext(minCount)) // NOT ||
break;
} else {
answer += getLiteralBitCount(thisItr.toLiteral() | otherItr.word);
thisItr.word--;
if (!thisItr.prepareNext(1) |
!otherItr.prepareNext()) // do NOT use "||"
break;
}
} else if (!otherItr.IsLiteral) {
answer += getLiteralBitCount(thisItr.word | otherItr.toLiteral());
otherItr.word--;
if (!thisItr.prepareNext() |
!otherItr.prepareNext(1)) // do NOT use "||"
break;
} else {
answer += getLiteralBitCount(thisItr.word | otherItr.word);
if (!thisItr.prepareNext() | !otherItr.prepareNext()) // do NOT use "||"
break;
}
}
answer += thisItr.flushCount();
answer += otherItr.flushCount();
return answer;
}

void clear() { reset(); }

Expand Down Expand Up @@ -1028,20 +1070,23 @@ template <bool wah_mode = false> class WordIterator {


uint32_t flushCount() {
if(exhausted()) return 0;
uint32_t cardsize = 0;
while(!exhausted()) {
do {
if (IsLiteral) {
cardsize += getLiteralBitCount(word);
} else {
if(word & SEQUENCE_BIT)
if(word & SEQUENCE_BIT) {
cardsize += 31 * count;
}
}
} while (prepareNext());
return cardsize;
}

bool flushEmpty() {
while(!exhausted()) {
if(exhausted()) return true;
do {
if (IsLiteral) {
if(!isLiteralZero(word)) return false;
} else {
Expand Down
8 changes: 8 additions & 0 deletions tests/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ template <bool wahmode> void basictest() {
assert(test2.contains(3000));
assert(test2.size() == 5);
ConciseSet<wahmode> tmp;
assert(test1.logicalorCount(test2) == 7);
tmp = test1.logicalor(test2);
assert(tmp.size() == 7);
assert(test1.logicalandCount(test2) == 3);
Expand Down Expand Up @@ -205,6 +206,7 @@ template <bool wahmode> void longtest() {
assert(test2.size() == 1000);

ConciseSet<wahmode> tmp;
assert(test1.logicalorCount(test2) == 2000);
tmp = test1.logicalor(test2);
assert(tmp.size() == 2000);
for (int k = 0; k < 1000; ++k) {
Expand Down Expand Up @@ -321,7 +323,9 @@ template <bool wahmode> void toytest() {
std::set<uint32_t> truesymsubtract = symmetrically_subtract(set1, set2);
ConciseSet<wahmode> union1;
ConciseSet<wahmode> union2;
size_t expunion1 = test1.logicalorCount(test2);
union1 = test1.logicalor(test2);
assert(union1.size() == expunion1);
union2 = test1.logicalor(test2);
assert(equals(trueunion, union1));
assert(equals(trueunion, union2));
Expand Down Expand Up @@ -417,7 +421,9 @@ template <bool wahmode> void variedtest() {

ConciseSet<wahmode> union1;
ConciseSet<wahmode> union2;
size_t expunion1 = test1.logicalorCount(test2);
union1 = test1.logicalor(test2);
assert(union1.size() == expunion1);
union2 = test1.logicalor(test2);
assert(equals(trueunion, union1));
assert(equals(trueunion, union2));
Expand Down Expand Up @@ -575,7 +581,9 @@ template <bool wahmode> void realtest() {
ConciseSet<wahmode> union1;
ConciseSet<wahmode> union2;

size_t expunion1 = test1.logicalorCount(test2);
union1 = test1.logicalor(test2);
assert(union1.size() == expunion1);
union2 = test1.logicalor(test2);
assert(equals(trueunion, union1));
assert(equals(trueunion, union2));
Expand Down

0 comments on commit dfcbc6d

Please sign in to comment.