forked from cmuparlay/parlaylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman_tree.cpp
47 lines (39 loc) · 1.26 KB
/
huffman_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
#include <iostream>
#include <string>
#include <random>
#include <tuple>
#include <parlay/primitives.h>
#include <parlay/random.h>
#include <parlay/sequence.h>
#include <parlay/internal/get_time.h>
#include "huffman_tree.h"
// **************************************************************
// Driver
// **************************************************************
int main(int argc, char* argv[]) {
auto usage = "Usage: huffman_tree <num_points>";
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::random_generator gen;
std::uniform_real_distribution<float> dis(1.0, static_cast<float>(n));
// generate unnormalized probabilities
auto probs = parlay::tabulate(n, [&] (long i) -> float {
auto r = gen[i];
return 1.0/dis(r);
});
// normalize them
float total = parlay::reduce(probs);
probs = parlay::map(probs, [&] (float p) {return p/total;});
std::tuple<parlay::sequence<node*>,node*> result;
parlay::internal::timer t("Time");
for (int i=0; i < 5; i++) {
t.start();
result = huffman_tree(probs);
t.next("huffman_tree");
delete_tree(std::get<1>(result));
}
}
}