forked from cmuparlay/parlaylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartesian_tree.cpp
54 lines (45 loc) · 1.34 KB
/
cartesian_tree.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
#include <iostream>
#include <string>
#include <random>
#include <parlay/primitives.h>
#include <parlay/sequence.h>
#include <parlay/random.h>
#include <parlay/internal/get_time.h>
#include "cartesian_tree.h"
// **************************************************************
// Driver
// **************************************************************
parlay::sequence<long> generate_values(long n) {
parlay::random_generator gen;
std::uniform_int_distribution<long> dis(0, n-1);
return parlay::tabulate(n, [&] (long i) {
auto r = gen[i];
return dis(r);
});
}
int main(int argc, char* argv[]) {
auto usage = "Usage: cartesian_tree <n>";
if (argc != 2) std::cout << usage << std::endl;
else {
long n;
try { n = std::stol(argv[1]); }
catch (...) { std::cout << usage << std::endl; return 1; }
parlay::sequence<long> values = generate_values(n);
parlay::sequence<long> parents;
parlay::internal::timer t("Time");
for (int i=0; i < 5; i++) {
parents = cartesian_tree(values);
t.next("Cartesian Tree");
}
auto h = parlay::tabulate(n, [&] (long i) {
long depth = 1;
while (i != parents[i]) {
i = parents[i];
depth++;
}
return depth;});
std::cout << "depth of tree: "
<< parlay::reduce(h, parlay::maximum<long>())
<< std::endl;
}
}