-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhuffman_create_tree.cpp
59 lines (55 loc) · 2.63 KB
/
huffman_create_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
55
56
57
58
59
#include "huffman.h"
#include "assert.h"
void create_tree (
/* input */ Symbol in[INPUT_SYMBOL_SIZE],
/* input */ int num_symbols,
/* output */ ap_uint<SYMBOL_BITS> parent[INPUT_SYMBOL_SIZE-1],
/* output */ ap_uint<SYMBOL_BITS> left[INPUT_SYMBOL_SIZE-1],
/* output */ ap_uint<SYMBOL_BITS> right[INPUT_SYMBOL_SIZE-1]) {
Frequency frequency[INPUT_SYMBOL_SIZE-1];
ap_uint<SYMBOL_BITS> tree_count = 0; // Number of intermediate nodes assigned a parent.
ap_uint<SYMBOL_BITS> in_count = 0; // Number of inputs consumed.
assert(num_symbols > 0);
assert(num_symbols <= INPUT_SYMBOL_SIZE);
for(int i = 0; i < (num_symbols-1); i++) {
#pragma HLS PIPELINE II=5
Frequency node_freq = 0;
// There are two cases.
// Case 1: remove a Symbol from in[]
// Case 2: remove an element from intermediate[]
// We do this twice, once for the left and once for the right of the new intermediate node.
assert(in_count < num_symbols || tree_count < i);
Frequency intermediate_freq = frequency[tree_count];
Symbol s = in[in_count];
if((in_count < num_symbols && s.frequency <= intermediate_freq) || tree_count == i) {
// Pick symbol from in[].
left[i] = s.value; // Set input symbol as left node
node_freq = s.frequency; // Add symbol frequency to total node frequency
in_count++; // Move to the next input symbol
} else {
// Pick internal node without a parent.
left[i] = INTERNAL_NODE; // Set symbol to indicate an internal node
node_freq = frequency[tree_count]; // Add child node frequency
parent[tree_count] = i; // Set this node as child's parent
tree_count++; // Go to next parentless internal node
}
assert(in_count < num_symbols || tree_count < i);
intermediate_freq = frequency[tree_count];
s = in[in_count];
if((in_count < num_symbols && s.frequency <= intermediate_freq) || tree_count == i) {
// Pick symbol from in[].
right[i] = s.value;
frequency[i] = node_freq + s.frequency;
in_count++;
} else {
// Pick internal node without a parent.
right[i] = INTERNAL_NODE;
frequency[i] = node_freq + intermediate_freq;
parent[tree_count] = i;
tree_count++;
}
// Verify that nodes in the tree are sorted by frequency
assert(i == 0 || frequency[i] >= frequency[i-1]);
}
parent[tree_count] = 0; //Set parent of last node (root) to 0
}