forked from lemire/EWAHBoolArray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
153 lines (143 loc) · 5.42 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* This code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*
* (c) Daniel Lemire, http://lemire.me/en/
*/
#include <stdlib.h>
#include "ewah.h"
void easydemo() {
typedef EWAHBoolArray<uint32_t> bitmap;
bitmap bitset1 =
bitmap::bitmapOf(9, 1, 2, 1000, 1001, 1002, 1003, 1007, 1009, 100000);
std::cout << "first bitset : " << bitset1 << std::endl;
bitmap bitset2 = bitmap::bitmapOf(5, 1, 3, 1000, 1007, 100000);
std::cout << "second bitset : " << bitset2 << std::endl;
bitmap bitset3 = bitmap::bitmapOf(3, 10, 11, 12);
std::cout << "third bitset : " << bitset3 << std::endl;
bitmap orbitset = bitset1 | bitset2;
bitmap andbitset = bitset1 & bitset2;
bitmap xorbitset = bitset1 ^ bitset2;
bitmap andnotbitset = bitset1 - bitset2;
std::cout << "logical and: " << andbitset << std::endl;
std::cout << "memory usage of compressed bitset = " << andbitset.sizeInBytes()
<< " bytes" << std::endl;
// we will display the and
std::cout << "logical or: " << orbitset << std::endl;
std::cout << "memory usage of compressed bitset = " << orbitset.sizeInBytes()
<< " bytes" << std::endl;
// we will display the xor
std::cout << "logical xor: " << xorbitset << std::endl;
std::cout << "memory usage of compressed bitset = " << xorbitset.sizeInBytes()
<< " bytes" << std::endl;
std::cout << "union of all three bitsets = " << (orbitset | bitset3)
<< std::endl;
const bitmap *mybitmaps[] = {&bitset1, &bitset2, &bitset3};
std::cout << "fast union of all three bitsets = "
<< fast_logicalor(3, mybitmaps) << std::endl;
std::cout << std::endl;
}
template <class bitmap> void demo() {
bitmap bitset1 =
bitmap::bitmapOf(9, 1, 2, 1000, 1001, 1002, 1003, 1007, 1009, 100000);
std::cout << "first bitset : " << bitset1 << std::endl;
bitmap bitset2 = bitmap::bitmapOf(5, 1, 3, 1000, 1007, 100000);
std::cout << "second bitset : " << bitset2 << std::endl;
bitmap bitset3 = bitmap::bitmapOf(3, 10, 11, 12);
std::cout << "third bitset : " << bitset3 << std::endl;
bitmap orbitset;
bitmap andbitset;
bitmap xorbitset;
bitset1.logicalor(
bitset2,
orbitset); // "bitset1.logicalor(bitset2)" would also return orbitset
bitset1.logicaland(bitset2, andbitset);
bitset1.logicalxor(bitset2, xorbitset);
// we will display the or
std::cout << "logical and: " << andbitset << std::endl;
std::cout << "memory usage of compressed bitset = " << andbitset.sizeInBytes()
<< " bytes" << std::endl;
// we will display the and
std::cout << "logical or: " << orbitset << std::endl;
std::cout << "memory usage of compressed bitset = " << orbitset.sizeInBytes()
<< " bytes" << std::endl;
// we will display the xor
std::cout << "logical xor: " << xorbitset << std::endl;
std::cout << "memory usage of compressed bitset = " << xorbitset.sizeInBytes()
<< " bytes" << std::endl;
std::cout << "union of all three bitsets = " << bitset1.logicalor(bitset2)
<< std::endl;
const bitmap *mybitmaps[] = {&bitset1, &bitset2, &bitset3};
std::cout << "fast union of all three bitsets = "
<< fast_logicalor(3, mybitmaps) << std::endl;
std::cout << std::endl;
}
template <class bitmap> void demoSerialization() {
std::stringstream ss;
bitmap myarray;
myarray.addWord(234321); // this is not the same as "set(234321)"!!!
myarray.addWord(0);
myarray.addWord(0);
myarray.addWord(999999);
//
std::cout << "Writing: " << myarray << std::endl;
myarray.write(ss);
//
bitmap lmyarray;
lmyarray.read(ss);
std::cout << "Read back: " << lmyarray << std::endl;
//
if (lmyarray == myarray)
std::cout << "serialization works" << std::endl;
else
std::cout << "serialization does not works" << std::endl;
}
template <class bitmap> void smallIntersectBenchmark() {
// this is the worst case!!!
bitmap b1;
bitmap b2;
// we fill both of them with lots of values, but we
// do not let them intersect, to make it difficult!
size_t N = 100000000;
for (size_t i = 0; i < N; ++i) {
b1.set(2 * i);
b2.set(2 * i + 1);
}
size_t offset = 2 * N + 2;
for (size_t i = 0; i < 100000; ++i) {
for (size_t j = 0; j < 1024; ++j)
b1.set(offset + j);
offset += 1024;
for (size_t j = 0; j < 1024; ++j)
b2.set(offset + j);
offset += 1024;
}
std::cout << " First bitmap has " << b1.numberOfOnes() << " set bits"
<< std::endl;
std::cout << " Second bitmap has " << b2.numberOfOnes() << " set bits"
<< std::endl;
double inputsize = b1.numberOfOnes() + b2.numberOfOnes();
const clock_t START = clock();
if (b1.intersects(b2))
std::cout << "BUG!" << std::endl;
const clock_t END = clock();
double timee = (double)(END - START) / CLOCKS_PER_SEC;
std::cout << "Billions of values intersected per second : "
<< (inputsize / 1000000000.0) / timee << std::endl;
}
int main(void) {
std::cout << std::endl;
std::cout << "====uncompressed example====" << std::endl;
std::cout << std::endl;
demo<BoolArray<uint32_t>>();
demoSerialization<BoolArray<uint32_t>>();
std::cout << std::endl;
std::cout << "====compressed example====" << std::endl;
std::cout << std::endl;
easydemo();
demo<EWAHBoolArray<uint32_t>>();
demoSerialization<EWAHBoolArray<uint32_t>>();
std::cout << "==== benchmark intersecs === " << std::endl;
smallIntersectBenchmark<EWAHBoolArray<uint64_t>>();
return EXIT_SUCCESS;
}