Skip to content

Commit e8e928c

Browse files
authored
add usage example
1 parent 1f7a799 commit e8e928c

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

examples/example.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <iostream>
2+
#include "../GridFile.h"
3+
#include <vector>
4+
#include <random>
5+
#include <chrono>
6+
7+
int main(int argc, const char* argv[])
8+
{
9+
int dimensions = 3;
10+
int records = 1e8;
11+
int grid_cell_size = 16384;
12+
13+
try{
14+
dimensions = stoi(argv[1]);
15+
records = stoi(argv[2]);
16+
grid_cell_size = stoi(argv[3]);
17+
}
18+
catch (exception e){
19+
//cout << e.what() << endl;
20+
cout <<"Some args are missing or failed to be parsed. Defaults will be used instead.\n\n";
21+
}
22+
23+
24+
/* Printing dataset metadata */
25+
cout << "num dimensions = " << dimensions << endl;
26+
cout << "num records = " << records << endl;
27+
cout << "grid cell size (bytes) = " << grid_cell_size << endl;
28+
cout << endl;
29+
30+
/* column oriented dataset. a new column begins after every set of (records). */
31+
vector<uint32_t> dataset;
32+
try {
33+
dataset.resize(dimensions * records);
34+
}
35+
catch (exception e)
36+
{
37+
cout << e.what();
38+
}
39+
40+
/* Generating random data from uniform distribution. */
41+
using key_type = uint32_t;
42+
std::mt19937 gen(55555);
43+
uniform_int_distribution<key_type> key_distrib(0, 4e9);
44+
auto rand = [&gen, &key_distrib] { return key_distrib(gen); };
45+
std::generate(dataset.begin(), dataset.end(), rand);
46+
47+
48+
/* Visitor object to store the range query results. */
49+
vector<vector<uint32_t>> visitor(dimensions);
50+
for (int i = 0; i < visitor.size(); i++)
51+
{
52+
/* visitor object result structure example */
53+
// attribute 1: 1, 2, 3, 394, 10
54+
// attribute 2: 8, 100, 23, 123, 20
55+
// attribute 3: 4, 403, 22, 99, 1, 4
56+
visitor[i].resize(records);
57+
}
58+
gf::GridFile gridfile(dimensions, records, grid_cell_size, dataset);
59+
dataset.clear();
60+
dataset.shrink_to_fit();
61+
62+
auto start = std::chrono::high_resolution_clock::now();
63+
uint64_t res = gridfile.range_query(dimensions, { 100000000,100000000, 100000000 }, { 300000000,300000000, 300000000 }, visitor);
64+
auto end = std::chrono::high_resolution_clock::now();
65+
66+
auto duration = end - start;
67+
68+
69+
for (int i=0;i<visitor.size();i++)
70+
visitor[i].resize(res);
71+
//cout <<"visitor size = " << visitor[0].size() << endl;
72+
cout << "\nRange query found = " << res << " matches"<< endl << endl;
73+
std::cout << "Range query time elapsed = "
74+
<< std::chrono::duration_cast<std::chrono::microseconds>(duration).count()
75+
<< " microseconds" << std::endl;
76+
77+
}

0 commit comments

Comments
 (0)