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
fn main() {
// Test frequency tree with 4 symbols, all freq=1
let tl = 4;
let mut tree = vec![0u16; tl * 2];
// Init leaves
for item in tree.iter_mut().take(2 * tl).skip(tl) {
*item = 1;
}
// Build internal nodes
let mut j = tl * 2 - 2;
for i in (1..tl).rev() {
tree[i] = tree[j] + tree[j + 1];
j = j.saturating_sub(2);
}
println!("Tree: {:?}", tree);
println!("Total at [1]: {}", tree[1]);
// Test decode for threshold=0,1,2,3
for threshold in 0..4 {
let mut l = 2;
let mut lt = 0;
while l < tl {
println!(" l={}, lt={}, tree[l]={}, threshold={}", l, lt, tree[l], threshold);
if lt + tree[l] <= threshold {
println!(" MATCH");
lt += tree[l];
l += 1;
} else {
println!(" NO MATCH");
}
l *= 2;
}
let symbol = l - tl;
println!("threshold={}: symbol={}, cumulative={}", threshold, symbol, lt);
println!();
}
}