Skip to content

Commit 84aba77

Browse files
committed
Adding support for logical operators (overloading) plus a potential overflow fix.
1 parent 664b7aa commit 84aba77

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

headers/ewah.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ template <class uword = uint32_t> class EWAHBoolArray {
189189
logicaland(a, answer);
190190
return answer;
191191
}
192+
EWAHBoolArray operator&(const EWAHBoolArray &a) const {
193+
return logicaland(a);
194+
}
192195

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

205+
EWAHBoolArray operator-(const EWAHBoolArray &a) const {
206+
return logicalandnot(a);
207+
}
202208

203209
/**
204210
* computes the logical and not with another compressed bitmap
@@ -211,6 +217,8 @@ template <class uword = uint32_t> class EWAHBoolArray {
211217
logicalandnot(a, answer);
212218
return answer;
213219
}
220+
221+
214222
/**
215223
* tests whether the bitmaps "intersect" (have at least one 1-bit at the same
216224
* position). This function does not modify the existing bitmaps.
@@ -229,6 +237,7 @@ template <class uword = uint32_t> class EWAHBoolArray {
229237
void logicalor(const EWAHBoolArray &a, EWAHBoolArray &container) const;
230238

231239

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

287+
EWAHBoolArray operator|(const EWAHBoolArray &a) const {
288+
return logicalor(a);
289+
}
290+
278291
/**
279292
* computes the logical xor with another compressed bitmap
280293
* answer goes into container
@@ -294,6 +307,10 @@ template <class uword = uint32_t> class EWAHBoolArray {
294307
logicalxor(a, answer);
295308
return answer;
296309
}
310+
311+
EWAHBoolArray operator^(const EWAHBoolArray &a) const {
312+
return logicalxor(a);
313+
}
297314
/**
298315
* clear the content of the bitmap. It does not
299316
* release the memory.
@@ -1502,7 +1519,7 @@ size_t EWAHBoolArray<uword>::addStreamOfDirtyWords(const uword *v,
15021519
buffer.push_back(0);
15031520
lastRLW = buffer.size() - 1;
15041521
++wordadded;
1505-
wordadded += addStreamOfDirtyWords(v + howmanywecanadd, NumberOfLiteralWords - howmanywecanadd);
1522+
wordadded += addStreamOfDirtyWords(v + howmanywecanadd, number - howmanywecanadd);
15061523
return wordadded;
15071524
}
15081525

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

15341551

@@ -1558,7 +1575,7 @@ size_t EWAHBoolArray<uword>::addStreamOfNegatedDirtyWords(const uword *v,
15581575
buffer.push_back(0);
15591576
lastRLW = buffer.size() - 1;
15601577
++wordadded;
1561-
wordadded += addStreamOfDirtyWords(v + howmanywecanadd, NumberOfLiteralWords - howmanywecanadd);
1578+
wordadded += addStreamOfDirtyWords(v + howmanywecanadd, number - howmanywecanadd);
15621579
return wordadded;
15631580
}
15641581

src/unit.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,55 @@ template <class uword> bool testCardinalityEWAHBoolArray() {
183183
return true;
184184
}
185185

186+
template <class uword> bool testLargeDirty() {
187+
cout << "[testing LargeDirty] sizeof(uword)=" << sizeof(uword)
188+
<< endl;
189+
size_t N = 80000000;
190+
vector<uint32_t> bigarray1(N);
191+
vector<uint32_t> bigarray2(N);
192+
for(size_t k = 0; k < N; k++) {
193+
bigarray1[k] = (uint32_t)(8 * k);
194+
bigarray2[k] = (uint32_t)(32 * k);
195+
}
196+
EWAHBoolArray<uword> b1 = EWAHBoolArray<uword>();
197+
EWAHBoolArray<uword> b2 = EWAHBoolArray<uword>();
198+
for(size_t k = 0; k < N; k++) {
199+
b1.set(8 * k);
200+
b2.set(64 * k);
201+
}
202+
203+
EWAHBoolArray<uword> b3 = EWAHBoolArray<uword>::bitmapOf(3,1,10,1000);
204+
EWAHBoolArray<uword> b4 = EWAHBoolArray<uword>::bitmapOf(3,1,10,1000);
205+
206+
if (b1.numberOfOnes() != N) {
207+
return false;
208+
}
209+
if (b2.numberOfOnes() != N) {
210+
return false;
211+
}
212+
if (b3.numberOfOnes() != 3) {
213+
return false;
214+
}
215+
b3 = b3 | b1;
216+
b4 = b4 | b2;
217+
b3 = b3 | b2;
218+
b4 = b4 | b1;
219+
if( b3 != b4 ) {
220+
return false;
221+
}
222+
b4 = b4 - b1;
223+
if (b4.numberOfOnes() != 3) {
224+
return false;
225+
}
226+
b3 = b3 ^ b4;
227+
if( b3 != b1 ) {
228+
return false;
229+
}
230+
231+
return true;
232+
}
233+
234+
186235
template <class uword> bool testAndNotEWAHBoolArray() {
187236
cout << "[testing AndNotEWAHBoolArray] sizeof(uword)=" << sizeof(uword) << endl;
188237
EWAHBoolArray<uword> b1 = EWAHBoolArray<uword>::bitmapOf(1,1);
@@ -1423,6 +1472,13 @@ int main(void) {
14231472
if (!funnytest()) {
14241473
++failures;
14251474
}
1475+
if (!testLargeDirty<uint64_t>())
1476+
++failures;
1477+
if (!testLargeDirty<uint16_t>())
1478+
++failures;
1479+
if (!testLargeDirty<uint32_t>())
1480+
++failures;
1481+
14261482
if (!countstest4<uint64_t>())
14271483
++failures;
14281484
if (!countstest4<uint16_t>())

0 commit comments

Comments
 (0)