Debian Codegraph @

src/benchs/binary-trees.neko

raw
1 /* The Computer Language Shootout
2 http://shootout.alioth.debian.org/
3
4 contributed by Nicolas Cannasse
5 */
6 make = function(i,d) {
7 if( d == 0 ) return $array(i,null,null);
8 var i2 = 2 * i;
9 d -= 1;
10 $array(i,make(i2-1,d),make(i2,d));
11 };
12
13 check = function(n) {
14 if( n[1] == null ) return n[0];
15 return n[0] + check(n[1]) - check(n[2]);
16 }
17
18 var arg = $int($loader.args[0]);
19 if( arg == null ) arg = 10;
20 var min_depth = 4;
21 var max_depth = if( min_depth + 2 < arg ) arg else min_depth + 2;
22 var stretch_depth = max_depth + 1
23
24 var c = check(make(0,stretch_depth));
25 $print("stretch tree of depth ",stretch_depth,"\t check: ",c,"\n");
26
27 var long_lived_tree = make(0,max_depth);
28
29 loop_depths = function(d) {
30 if( d <= max_depth ) {
31 var niter = 1 << (max_depth - d + min_depth);
32 var c = 0;
33 var i = 0;
34 while( i < niter ) {
35 i += 1;
36 c += check(make(i,d))+check(make(0-i,d));
37 }
38 $print(2*niter,"\t trees of depth ",d,"\t check: ",c,"\n");
39 loop_depths(d + 2);
40 }
41 }
42
43 loop_depths(min_depth);
44 $print("long lived tree of depth ",max_depth,"\t check: ",check(long_lived_tree),"\n");