-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy path1.chpl
77 lines (68 loc) · 1.79 KB
/
1.chpl
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use DynamicIters;
config const n = 10; // the maximum tree depth
proc main() {
const minDepth = 4, // the shallowest tree
maxDepth = max(minDepth + 2, n), // the deepest normal tree
strDepth = maxDepth + 1, // the depth of the "stretch" tree
depths = minDepth..maxDepth by 2; // the range of depths to create
{
const strTree = new Tree(strDepth);
strTree.calHash();
writeln("stretch tree of depth ", strDepth, "\t root hash: ", strTree.getHash(), "check: ", strTree.check());
}
const llTree = new Tree(maxDepth);
for depth in dynamic(depths) {
const iterations = 2**(maxDepth - depth + minDepth);
var sum = 0;
for i in 1..iterations {
const t = new Tree(depth);
t.calHash();
sum += t.getHash();
}
writeln(iterations, "\t trees of depth ", depth, "\t root hash sum: ", sum);
}
llTree.calHash();
writeln("long lived tree of depth ", maxDepth, "\t root hash: ", llTree.getHash(), "check: ", llTree.check());
}
class Int {
var value:int;
proc init(v) {
value = v;
}
}
class Tree {
var value, hash: owned Int?;
var left, right: owned Tree?;
proc init(depth) {
if depth > 0 {
left = new owned Tree(depth-1);
right = new owned Tree(depth-1);
} else {
value = new owned Int(1);
}
}
proc check(): bool {
if (hash) {
return value != nil || left!.check() && right!.check();
}
return false;
}
proc getHash(): int {
if hash {
return hash!.value;
}
return 0;
}
proc calHash() {
if hash {
return;
}
if value {
hash = value;
} else if (left != nil && right != nil) {
left!.calHash();
right!.calHash();
hash = new Int(left!.getHash() + right!.getHash());
}
}
}