-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cc
43 lines (38 loc) · 1.43 KB
/
example.cc
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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include "include/cht/builder.h"
#include "include/cht/cht.h"
template <class KeyType>
void CompactHistTreeExample(bool useCache = false) {
// Create random keys.
std::vector<KeyType> keys(1e6);
generate(keys.begin(), keys.end(), rand);
keys.push_back(424242);
std::sort(keys.begin(), keys.end());
// Build `CompactHistTree`.
KeyType min = keys.front();
KeyType max = keys.back();
const unsigned numBins = 64; // each node will have 64 separate bins
const unsigned maxError = 4; // the error of the index
cht::Builder<KeyType> chtb(min, max, numBins, maxError, false, useCache);
for (const auto& key : keys) chtb.AddKey(key);
cht::CompactHistTree<KeyType> cht = chtb.Finalize();
// Search using `CompactHistTree`.
cht::SearchBound bound = cht.GetSearchBound(424242);
std::cout << "The search key is in the range: [" << bound.begin << ", "
<< bound.end << ")" << std::endl;
auto start = std::begin(keys) + bound.begin,
last = std::begin(keys) + bound.end;
auto pos = std::lower_bound(start, last, 424242) - begin(keys);
assert(keys[pos] == 424242);
std::cout << "The key is at position: " << pos << std::endl;
}
int main(int argc, char** argv) {
CompactHistTreeExample<uint32_t>();
CompactHistTreeExample<uint64_t>();
CompactHistTreeExample<uint32_t>(true);
CompactHistTreeExample<uint64_t>(true);
return 0;
}