Skip to content

Commit

Permalink
intersects and logicalandCount
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Mar 17, 2017
1 parent 0f6f88b commit ba7feaa
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
81 changes: 81 additions & 0 deletions include/concise.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,85 @@ template <bool wah_mode = false> class ConciseSet {
return;
}

bool intersects(const ConciseSet<wah_mode> &other) const {
if (isEmpty() || other.isEmpty()) {
return 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(concise_and(thisItr.word, otherItr.word) & SEQUENCE_BIT)
if(minCount > 0 ) return true;
if (!thisItr.prepareNext(minCount) |
!otherItr.prepareNext(minCount)) // NOT ||
break;
} else {
if( !isLiteralZero(thisItr.toLiteral() & otherItr.word) ) return true;
thisItr.word--;
if (!thisItr.prepareNext(1) |
!otherItr.prepareNext()) // do NOT use "||"
break;
}
} else if (!otherItr.IsLiteral) {
if( !isLiteralZero(thisItr.word & otherItr.toLiteral()) ) return true;
otherItr.word--;
if (!thisItr.prepareNext() |
!otherItr.prepareNext(1)) // do NOT use "||"
break;
} else {
// Java code simply does thisItr.word & otherItr.word below
if ( !isLiteralZero(concise_and(thisItr.word , otherItr.word)) ) return true;
if (!thisItr.prepareNext() | !otherItr.prepareNext()) // do NOT use "||"
break;
}
}
return false;
}

size_t logicalandCount(const ConciseSet<wah_mode> &other) const {
if (isEmpty() || other.isEmpty()) {
return 0;
}
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(concise_and(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 {
// Java code simply does thisItr.word & otherItr.word below
answer += getLiteralBitCount(concise_and(thisItr.word , otherItr.word));
if (!thisItr.prepareNext() | !otherItr.prepareNext()) // do NOT use "||"
break;
}
}
return answer;
}

ConciseSet<wah_mode> logicalandnot(const ConciseSet<wah_mode> &other) const {
ConciseSet<wah_mode> res;
logicalandnotToContainer(other, res);
Expand Down Expand Up @@ -394,6 +473,8 @@ template <bool wah_mode = false> class ConciseSet {
return answer;
}



void clear() { reset(); }

void add(uint32_t e) {
Expand Down
19 changes: 18 additions & 1 deletion tests/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,17 @@ template <bool wahmode> void heaportest() {
longcounter++;
assert(longcounter == 60);
ConciseSet<wahmode> tmp;
size_t expectedandsize1 = answer.logicalandCount(test1);
tmp = answer.logicaland(test1);
assert(expectedandsize1 == tmp.size());
assert(tmp.size() == test1.size());
size_t expectedandsize2 = answer.logicalandCount(test2);
tmp = answer.logicaland(test2);
assert(expectedandsize2 == tmp.size());
assert(tmp.size() == test2.size());
size_t expectedandsize3 = answer.logicalandCount(test3);
tmp = answer.logicaland(test3);
assert(expectedandsize3 == tmp.size());
assert(tmp.size() == test3.size());
}

Expand Down Expand Up @@ -150,6 +156,7 @@ template <bool wahmode> void basictest() {
ConciseSet<wahmode> tmp;
tmp = test1.logicalor(test2);
assert(tmp.size() == 7);
assert(test1.logicalandCount(test2) == 3);
tmp = test1.logicaland(test2);
assert(tmp.size() == 3);
tmp.add(100000);
Expand Down Expand Up @@ -180,6 +187,7 @@ template <bool wahmode> void longtest() {
}
assert(test1.size() == 1000);
ConciseSet<wahmode> shouldbetest1;
assert(testc.logicalandCount(test1) == 1000);
shouldbetest1 = testc.logicaland(test1);
assert(shouldbetest1.size() == 1000);
for (int k = 0; k < 1000; ++k) {
Expand All @@ -203,9 +211,12 @@ template <bool wahmode> void longtest() {
assert(tmp.contains(k * 2));
assert(tmp.contains(k * 2 + 1));
}
assert(tmp.intersects(test2));
assert(tmp.logicalandCount(test2) == 1000);
tmp = tmp.logicaland(test2);
assert(tmp.size() == 1000);

assert(test1.intersects(test2) == false);
assert(test1.logicalandCount(test2) == 0);
tmp = test1.logicaland(test2);
assert(tmp.size() == 0);
}
Expand Down Expand Up @@ -316,7 +327,9 @@ template <bool wahmode> void toytest() {
assert(equals(trueunion, union2));
ConciseSet<wahmode> intersect1;
ConciseSet<wahmode> intersect2;
size_t expinter1 = test1.logicalandCount(test2);
intersect1 = test1.logicaland(test2);
assert(expinter1 == intersect1.size());
intersect2 = test1.logicaland(test2);
assert(equals(trueinter, intersect1));
assert(equals(trueinter, intersect2));
Expand Down Expand Up @@ -410,7 +423,9 @@ template <bool wahmode> void variedtest() {
assert(equals(trueunion, union2));
ConciseSet<wahmode> intersect1;
ConciseSet<wahmode> intersect2;
size_t expinter1 = test1.logicalandCount(test2);
intersect1 = test1.logicaland(test2);
assert(expinter1 == intersect1.size());
intersect2 = test1.logicaland(test2);
assert(equals(trueinter, intersect1));
assert(equals(trueinter, intersect2));
Expand Down Expand Up @@ -566,7 +581,9 @@ template <bool wahmode> void realtest() {
assert(equals(trueunion, union2));
ConciseSet<wahmode> intersect1;
ConciseSet<wahmode> intersect2;
size_t expinter1 = test1.logicalandCount(test2);
intersect1 = test1.logicaland(test2);
assert(expinter1 == intersect1.size());
intersect2 = test1.logicaland(test2);
assert(equals(trueinter, intersect1));
assert(equals(trueinter, intersect2));
Expand Down

0 comments on commit ba7feaa

Please sign in to comment.