Skip to content

Instantly share code, notes, and snippets.

@kg
Created April 13, 2011 10:19
Show Gist options
  • Save kg/917316 to your computer and use it in GitHub Desktop.
Save kg/917316 to your computer and use it in GitHub Desktop.
FannkuchRedux.cs JSIL output
/* The Computer Language Benchmarks Game
http://shootout.alioth.debian.org/
contributed by Isaac Gouy, transliterated from Mike Pall's Lua program
*/
using System;
public static class Program {
public static int[] fannkuch (int n) {
int[] p = new int[n], q = new int[n], s = new int[n];
int sign = 1, maxflips = 0, sum = 0, m = n - 1;
for (int i = 0; i < n; i++) {
p[i] = i;
q[i] = i;
s[i] = i;
}
do {
// Copy and flip.
var q0 = p[0]; // Cache 0th element.
if (q0 != 0) {
for (int i = 1; i < n; i++)
q[i] = p[i]; // Work on a copy.
var flips = 1;
do {
var qq = q[q0];
if (qq == 0) { // ... until 0th element is 0.
sum += sign * flips;
if (flips > maxflips)
maxflips = flips; // New maximum?
break;
}
q[q0] = q0;
if (q0 >= 3) {
int i = 1, j = q0 - 1, t;
do {
t = q[i];
q[i] = q[j];
q[j] = t;
i++;
j--;
} while (i < j);
}
q0 = qq; flips++;
} while (true);
}
// Permute.
if (sign == 1) {
var t = p[1]; p[1] = p[0]; p[0] = t; sign = -1; // Rotate 0<-1.
} else {
var t = p[1]; p[1] = p[2]; p[2] = t; sign = 1; // Rotate 0<-1 and 0<-1<-2.
for (int i = 2; i < n; i++) {
var sx = s[i];
if (sx != 0) {
s[i] = sx - 1;
break;
}
if (i == m)
return new int[] { sum, maxflips }; // Out of permutations.
s[i] = i;
// Rotate 0<-...<-i+1.
t = p[0];
for (int j = 0; j <= i; j++) {
p[j] = p[j + 1];
}
p[i + 1] = t;
}
}
} while (true);
}
public static void Main (string[] args) {
int n = (args.Length > 0) ? Int32.Parse(args[0]) : 7;
var pf = fannkuch(n);
Console.WriteLine("{0}\nPfannkuchen({1}) = {2}\n", pf[0], n, pf[1]);
}
}
System = {};
System.Console = {};
System.Console.WriteLine = function () {
print(System.String.Format.apply(null, arguments));
}
System.String = {};
System.String.Format = function (format) {
format = String(format);
var regex = new RegExp("{([0-9]*)(?::([^}]*))?}", "g");
var match = null;
var args = arguments;
var matcher = function (match, index, valueFormat, offset, str) {
index = parseInt(index);
var value = args[index + 1];
if (valueFormat) {
switch (valueFormat[0]) {
case 'f':
case 'F':
var digits = parseInt(valueFormat.substr(1));
return parseFloat(value).toFixed(digits);
default:
throw new Error("Unsupported format string: " + valueFormat);
}
} else {
return String(value);
}
};
return format.replace(regex, matcher);
};
System.Math = {};
System.Math.Max = Math.max;
System.Math.Sqrt = Math.sqrt;
System.Int32 = {};
System.Int32.Parse = function (text) {
return parseInt(text);
};
JSIL = {}
JSIL.Array = {}
JSIL.Array.New = function (type, size) {
return new Array(size);
}
JSIL.Dynamic = {};
JSIL.Dynamic.Cast = function (value, expectedType) {
return value;
};
Program = {
};
( function () {
Program.fannkuch = function ( n ) {
var _label0_ = "::enter";
_step0_:
while (true) {
switch (_label0_) {
case "::enter": {
{
var array = JSIL.Array.New(System.Int32, n);
var array2 = JSIL.Array.New(System.Int32, n);
var array3 = JSIL.Array.New(System.Int32, n);
var num = 1;
var num2 = 0;
var num3 = 0;
var num4 = n - 1;
_block0_:
for (var i = 0; i < n; i++) {
array [i] = i;
array2 [i] = i;
array3 [i] = i;
}
_block1_:
while (true) {
var num5 = array [0];
if (num5 != 0) {
_block2_:
for (var i = 1; i < n; i++) {
array2 [i] = array [i];
}
var num6 = 1;
_block3_:
while (true) {
var num7 = array2 [num5];
if (num7 == 0) {
break _block3_;
}
array2 [num5] = num5;
if (num5 >= 3) {
var i = 1;
var num8 = num5 - 1;
do {
var num9 = array2 [i];
array2 [i] = array2 [num8];
array2 [num8] = num9;
i++;
num8--;
}
while (i < num8);
}
num5 = num7;
num6++;
}
num3 += num * num6;
if (num6 > num2) {
num2 = num6;
}
}
if (num == 1) {
var num9 = array [1];
array [1] = array [0];
array [0] = num9;
num = -1;
}
else {
var num9 = array [1];
array [1] = array [2];
array [2] = num9;
num = 1;
_block4_:
for (var i = 2; i < n; i++) {
var num10 = array3 [i];
if (num10 != 0) {
array3 [i] = num10 - 1;
break _block4_;
}
if (i == num4) {
{
_label0_ = "Block_8";
continue _step0_;
}
}
array3 [i] = i;
num9 = array [0];
_block5_:
for (var num8 = 0; num8 <= i; num8++) {
array [num8] = array [num8 + 1];
}
array [i + 1] = num9;
}
}
}
{
_label0_ = "Block_8";
continue _step0_;
}
}
}
case "Block_8": {
return [num3, num2];
break _step0_;
}
}
}
};
Program.Main = function ( args ) {
var num = (args.length > 0) ? System.Int32.Parse (args [0]) : 7;
var array = Program.fannkuch (num);
System.Console.WriteLine ("{0}\nPfannkuchen({1}) = {2}\n", array [0], num, array [1]);
};
}) ();
Program.Main([]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment