Skip to content

Instantly share code, notes, and snippets.

@kg
Created April 17, 2011 01:04
Show Gist options
  • Save kg/923657 to your computer and use it in GitHub Desktop.
Save kg/923657 to your computer and use it in GitHub Desktop.
BinaryTrees.cs JSIL output
/* The Computer Language Benchmarks Game
http://shootout.alioth.debian.org/
contributed by Marek Safar
*/
using System;
public static class Program {
const int minDepth = 4;
public static void Main (String[] args) {
int n = 0;
if (args.Length > 0) n = Int32.Parse(args[0]);
int maxDepth = Math.Max(minDepth + 2, n);
int stretchDepth = maxDepth + 1;
int check = (TreeNode.bottomUpTree(0, stretchDepth)).itemCheck();
Console.WriteLine("stretch tree of depth {0}\t check: {1}", stretchDepth, check);
TreeNode longLivedTree = TreeNode.bottomUpTree(0, maxDepth);
for (int depth = minDepth; depth <= maxDepth; depth += 2) {
int iterations = 1 << (maxDepth - depth + minDepth);
check = 0;
for (int i = 1; i <= iterations; i++) {
check += (TreeNode.bottomUpTree(i, depth)).itemCheck();
check += (TreeNode.bottomUpTree(-i, depth)).itemCheck();
}
Console.WriteLine("{0}\t trees of depth {1}\t check: {2}",
iterations * 2, depth, check);
}
Console.WriteLine("long lived tree of depth {0}\t check: {1}",
maxDepth, longLivedTree.itemCheck());
}
struct TreeNode {
class Next {
public TreeNode left, right;
}
private Next next;
private int item;
internal static TreeNode bottomUpTree (int item, int depth) {
if (depth > 0) {
return new TreeNode(
new Next {
left = bottomUpTree(2 * item - 1, depth - 1),
right = bottomUpTree(2 * item, depth - 1)
}, item
);
} else {
return new TreeNode(null, item);
}
}
TreeNode (Next next, int item) {
this.next = next;
this.item = item;
}
internal int itemCheck () {
// if necessary deallocate here
if (next == null) return item;
else return item + next.left.itemCheck() - next.right.itemCheck();
}
}
}
Program = {
};
( function () {
Program.TreeNode = function (next, item) {
this.__ctor(next, item);
};
( function () {
Program.TreeNode.prototype = JSIL.CloneObject(System.Object.prototype);
Program.TreeNode.prototype.__TypeName__ = "Program/TreeNode";
Program.TreeNode.prototype.__ctor = function(next, item) {
this.item = 0;
this.next = next;
this.item = item;
};
Program.TreeNode.Next = function () {
this.__ctor();
};
( function () {
Program.TreeNode.Next.prototype = JSIL.CloneObject(System.Object.prototype);
Program.TreeNode.Next.prototype.__TypeName__ = "Program/TreeNode/Next";
Program.TreeNode.Next.prototype.__ctor = function() {
};
}) ();
Program.TreeNode.bottomUpTree = function ( item, depth ) {
var CS$1$0000;
if (depth > 0) {
var $lt$gtg__initLocal0 = new Program.TreeNode.Next();
$lt$gtg__initLocal0.left = Program.TreeNode.bottomUpTree (2 * item - 1, depth - 1);
$lt$gtg__initLocal0.right = Program.TreeNode.bottomUpTree (2 * item, depth - 1);
CS$1$0000 = new Program.TreeNode($lt$gtg__initLocal0, item);
}
else {
CS$1$0000 = new Program.TreeNode(null, item);
}
return CS$1$0000;
};
Program.TreeNode.prototype.itemCheck = function () {
var CS$1$0000;
if (this.next == null) {
CS$1$0000 = this.item;
}
else {
CS$1$0000 = this.item + this.next.left.itemCheck () - this.next.right.itemCheck ();
}
return CS$1$0000;
};
}) ();
Program.minDepth = 4;
Program.Main = function ( args ) {
var n = 0;
if (args.length > 0) {
n = System.Int32.Parse (args [0]);
}
var maxDepth = System.Math.Max (6, n);
var stretchDepth = maxDepth + 1;
var CS$0$0001 = Program.TreeNode.bottomUpTree (0, stretchDepth);
var check = CS$0$0001.itemCheck ();
System.Console.WriteLine ("stretch tree of depth {0}\t check: {1}", stretchDepth, check);
var longLivedTree = Program.TreeNode.bottomUpTree (0, maxDepth);
_block0_:
for (var depth = 4; depth <= maxDepth; depth += 2) {
var iterations = 1 << (maxDepth - depth + 4 & 31);
check = 0;
_block1_:
for (var i = 1; i <= iterations; i++) {
var arg_86_0 = check;
CS$0$0001 = Program.TreeNode.bottomUpTree (i, depth);
check = arg_86_0 + CS$0$0001.itemCheck ();
var arg_9C_0 = check;
CS$0$0001 = Program.TreeNode.bottomUpTree (-i, depth);
check = arg_9C_0 + CS$0$0001.itemCheck ();
}
System.Console.WriteLine ("{0}\t trees of depth {1}\t check: {2}", iterations * 2, depth, check);
}
System.Console.WriteLine ("long lived tree of depth {0}\t check: {1}", maxDepth, longLivedTree.itemCheck ());
};
}) ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment