forked from lemire/EWAHBoolArray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.cpp
More file actions
260 lines (244 loc) · 9.09 KB
/
example2.cpp
File metadata and controls
260 lines (244 loc) · 9.09 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* This is just a more "practical" example that illustrates how
* to index a table using a bitmap. It is based of a piece of
* code by Kelly Sommers.
*/
#include <vector>
#include <iostream>
#include <map>
#include <string>
#include <ewah.h>
#include <string>
#include <algorithm>
using namespace std;
vector<string> fromarraytovector(string *x, const size_t n) {
vector<string> ans;
for (size_t i = 0; i < n; ++i) {
ans.push_back(x[i]);
}
return ans;
}
vector<vector<string>> ReadFile() {
vector<vector<string>> ans;
string row1[9] = {"Afghanistan",
"2011",
"TOTAL",
"ALL COMMODITIES",
"Import",
"6390310947",
"",
"No Quantity",
""};
string row2[9] = {"Afghanistan",
"2011",
"TOTAL",
"ALL COMMODITIES",
"Export",
"375850935",
"",
"No Quantity",
""};
string row3[9] = {"Afghanistan",
"2010",
"TOTAL",
"ALL COMMODITIES",
"Import",
"5154249867",
"",
"No Quantity",
""};
string row4[9] = {"Afghanistan",
"2010",
"TOTAL",
"ALL COMMODITIES",
"Export",
"388483635",
"",
"No Quantity",
""};
string row5[9] = {"Afghanistan",
"2009",
"TOTAL",
"ALL COMMODITIES",
"Import",
"3336434781",
"",
"No Quantity",
""};
string row6[9] = {"Afghanistan",
"2009",
"TOTAL",
"ALL COMMODITIES",
"Export",
"403441006",
"",
"No Quantity",
""};
string row7[9] = {"Afghanistan",
"2008",
"TOTAL",
"ALL COMMODITIES",
"Import",
"3019860129",
"",
"No Quantity",
""};
string row8[9] = {"Afghanistan",
"2008",
"TOTAL",
"ALL COMMODITIES",
"Export",
"540065594",
"",
"No Quantity",
""};
string row9[9] = {"Albania", "2011", "TOTAL", "ALL COMMODITIES",
"Import", "5395853069", "", "No Quantity",
""};
string row10[9] = {"Albania", "2011", "TOTAL", "ALL COMMODITIES",
"Export", "1948207305", "", "No Quantity",
""};
string row11[9] = {"Albania", "2010", "TOTAL", "ALL COMMODITIES",
"Import", "4602774967", "", "No Quantity",
""};
string row12[9] = {"Albania", "2010", "TOTAL", "ALL COMMODITIES",
"Export", "1549955724", "", "No Quantity",
""};
string row13[9] = {"Albania", "2010", "TOTAL", "ALL COMMODITIES",
"Re-Import", "26393", "", "No Quantity",
""};
string row14[9] = {"Albania", "2009", "TOTAL", "ALL COMMODITIES",
"Import", "4548287875", "", "No Quantity",
""};
string row15[9] = {"Albania", "2009", "TOTAL", "ALL COMMODITIES",
"Export", "1087914902", "", "No Quantity",
""};
string row16[9] = {"Albania", "2009", "TOTAL", "ALL COMMODITIES",
"Re-Import", "272403", "", "No Quantity",
""};
string row17[9] = {"Albania", "2008", "TOTAL", "ALL COMMODITIES",
"Import", "5250490022", "", "No Quantity",
""};
string row18[9] = {"Albania", "2008", "TOTAL", "ALL COMMODITIES",
"Export", "1354921653", "", "No Quantity",
""};
string row19[9] = {"Albania", "2008", "TOTAL", "ALL COMMODITIES",
"Re-Export", "810868093", "", "No Quantity",
""};
string row20[9] = {"Albania", "2008", "TOTAL", "ALL COMMODITIES",
"Re-Import", "509068", "", "No Quantity",
""};
string row21[9] = {"Albania", "2007", "TOTAL", "ALL COMMODITIES",
"Import", "4200864046", "", "No Quantity",
""};
string row22[9] = {"Albania", "2007", "TOTAL", "ALL COMMODITIES",
"Export", "1077690359", "", "No Quantity",
""};
string row23[9] = {"Albania", "2007", "TOTAL", "ALL COMMODITIES",
"Re-Import", "4494753", "", "No Quantity",
""};
ans.push_back(fromarraytovector(&row1[0], 9));
ans.push_back(fromarraytovector(&row2[0], 9));
ans.push_back(fromarraytovector(&row3[0], 9));
ans.push_back(fromarraytovector(&row4[0], 9));
ans.push_back(fromarraytovector(&row5[0], 9));
ans.push_back(fromarraytovector(&row6[0], 9));
ans.push_back(fromarraytovector(&row7[0], 9));
ans.push_back(fromarraytovector(&row8[0], 9));
ans.push_back(fromarraytovector(&row9[0], 9));
ans.push_back(fromarraytovector(&row10[0], 9));
ans.push_back(fromarraytovector(&row11[0], 9));
ans.push_back(fromarraytovector(&row12[0], 9));
ans.push_back(fromarraytovector(&row13[0], 9));
ans.push_back(fromarraytovector(&row14[0], 9));
ans.push_back(fromarraytovector(&row15[0], 9));
ans.push_back(fromarraytovector(&row16[0], 9));
ans.push_back(fromarraytovector(&row17[0], 9));
ans.push_back(fromarraytovector(&row18[0], 9));
ans.push_back(fromarraytovector(&row19[0], 9));
ans.push_back(fromarraytovector(&row20[0], 9));
ans.push_back(fromarraytovector(&row21[0], 9));
ans.push_back(fromarraytovector(&row22[0], 9));
ans.push_back(fromarraytovector(&row23[0], 9));
return ans;
}
map<string, EWAHBoolArray<uint32_t>> indexColumn(vector<vector<string>> rows,
size_t whichcolumn) {
map<string, EWAHBoolArray<uint32_t>> indexes;
for (size_t i = 0; i < rows.size(); i++) {
indexes[rows[i][whichcolumn]].set(i);
}
return indexes;
}
int main() {
vector<vector<string>> rows = ReadFile();
cout << "We index the second column of our fictitious table." << endl;
map<string, EWAHBoolArray<uint32_t>> index = indexColumn(rows, 1);
size_t rowCount = 0;
size_t actualRowCount = 0;
size_t diskByteCount = 0;
size_t compressedByteCount = 0;
for (map<string, EWAHBoolArray<uint32_t>>::iterator i = index.begin();
i != index.end(); ++i) {
const string &term = i->first;
EWAHBoolArray<uint32_t> &bitmap = i->second;
cout << " the term '" << term << "' appears " << bitmap.numberOfOnes()
<< " times" << endl;
for (EWAHBoolArray<uint32_t>::const_iterator j = bitmap.begin();
j != bitmap.end(); ++j) {
cout << "term '" << term << "' appears at row index " << *j << endl;
++actualRowCount;
}
rowCount += bitmap.numberOfOnes();
diskByteCount += bitmap.sizeOnDisk();
compressedByteCount += bitmap.computeStatistics().getCompressedSize();
}
cout << "TOTAL ROWS: " << rows.size() << endl;
cout << "ACTUAL ROWS: " << actualRowCount << endl;
cout << "INDEXED ROWS: " << rowCount << endl;
cout << "TERMS: " << index.size() << endl;
cout << "DISK BYTES: " << diskByteCount << endl;
cout << "COMPRESSED BYTES: " << compressedByteCount << endl;
return 0;
}
/**
* Expected output:
*
* $ g++ -Wall -o example2 example2.cpp -Iheaders
$ ./example2
We index the second column of our fictitious table.
the term '2007' appears 3 times
term '2007' appears at row index 20
term '2007' appears at row index 21
term '2007' appears at row index 22
the term '2008' appears 6 times
term '2008' appears at row index 6
term '2008' appears at row index 7
term '2008' appears at row index 16
term '2008' appears at row index 17
term '2008' appears at row index 18
term '2008' appears at row index 19
the term '2009' appears 5 times
term '2009' appears at row index 4
term '2009' appears at row index 5
term '2009' appears at row index 13
term '2009' appears at row index 14
term '2009' appears at row index 15
the term '2010' appears 5 times
term '2010' appears at row index 2
term '2010' appears at row index 3
term '2010' appears at row index 10
term '2010' appears at row index 11
term '2010' appears at row index 12
the term '2011' appears 4 times
term '2011' appears at row index 0
term '2011' appears at row index 1
term '2011' appears at row index 8
term '2011' appears at row index 9
TOTAL ROWS: 23
ACTUAL ROWS: 23
INDEXED ROWS: 23
TERMS: 5
DISK BYTES: 120
COMPRESSED BYTES: 10
*/