-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.cc
39 lines (32 loc) · 1.05 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
#include <algorithm>
#include <iostream>
#include <vector>
#include "include/ts/builder.h"
#include "include/ts/common.h"
using namespace std;
void TrieSplineExample() {
// Create random keys.
std::vector<uint64_t> keys(1e6);
generate(keys.begin(), keys.end(), rand);
keys.push_back(424242);
std::sort(keys.begin(), keys.end());
// Build TS
uint64_t min = keys.front();
uint64_t max = keys.back();
ts::Builder<uint64_t> tsb(min, max, /*spline_max_error=*/32);
for (const auto& key : keys) tsb.AddKey(key);
auto ts = tsb.Finalize();
// Search using TS
ts::SearchBound bound = ts.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) {
TrieSplineExample();
return 0;
}