Last active
August 29, 2015 14:14
-
-
Save triplefox/0edbfd1a14af0f5513f1 to your computer and use it in GitHub Desktop.
Version 3 - the "most correct" iteration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node | |
{ | |
public var ci : Array<Int>; /* channel in */ | |
public var co : Array<Int>; /* channel out */ | |
public var i : Int; /* id */ | |
public var b : Int; /* behavior */ | |
public var a : Bool; /* alive */ | |
public function new(i,a) { ci = []; co = []; this.i = i; this.b = -1; this.a = a; } | |
} | |
class Channel | |
{ | |
public var a : Bool; /* alive */ | |
public var i : Int; /* id */ | |
public var d : Array<Int>; /* data */ | |
public function new(i, a) { this.a = a; this.i = i; d = []; } | |
} | |
typedef Behavior = Node->Array<Channel>->Array<Channel>->Bool; | |
class PrimFlow | |
{ | |
public var node : Array<Node>; | |
public var channel : Array<Channel>; | |
public var behavior : Array<Behavior>; | |
public function new(nodec = 100, channelc = 100) | |
{ | |
node = [for (i in 0...nodec) new Node(i,false)]; | |
channel = [for (i in 0...channelc) new Channel(i,false)]; | |
behavior = []; | |
} | |
public function allocNode(ci, co, b) | |
{ | |
var c = 0; | |
while (c < node.length) | |
{ | |
if (!node[c].a) | |
{ | |
node[c].a = true; | |
node[c].ci = ci; | |
node[c].co = co; | |
node[c].b = b; | |
return node[c]; | |
} | |
c++; | |
} | |
var r = new Node(node.length, true); node.push(r); r.ci = ci; r.co = co; r.b = b; return r; | |
} | |
public function allocChannel() | |
{ | |
var c = 0; | |
while (c < channel.length) | |
{ | |
if (!channel[c].a) | |
{ | |
channel[c].a = true; | |
channel[c].d = []; | |
return channel[c]; | |
} | |
c++; | |
} | |
var r = new Channel(channel.length,true); channel.push(r); return r; | |
} | |
public function loop() | |
{ | |
var cont = true; | |
while (cont) | |
{ | |
cont = false; | |
for (n0 in node) | |
{ | |
if (n0.a) | |
{ | |
for (c0 in n0.ci) | |
{ | |
var cc0 = channel[c0]; | |
if (cc0.a && cc0.d.length > 0) | |
{ | |
if (behavior[n0.b](n0, | |
[for (cx in n0.ci) channel[cx]], | |
[for (cx in n0.co) channel[cx]] | |
)) | |
{ | |
for (cx in n0.ci) channel[cx].d = []; | |
} | |
cont = true; break; | |
} | |
} | |
} | |
} | |
} | |
} | |
public function gc() | |
{ | |
/* turn off nodes that no longer have live inputs */ | |
for (n0 in node) | |
{ | |
n0.a = false; | |
for (c0 in n0.ci) | |
{ | |
if (channel[c0].a) | |
n0.a = true; | |
} | |
} | |
} | |
public static function bCopy(n0:Node, ci:Array<Channel>, co:Array<Channel>):Bool | |
{ | |
var result : Array<Int> = []; | |
for (c0 in ci) { result = result.concat(c0.d); } | |
for (c0 in co) c0.d = c0.d.concat(result); | |
trace('${n0.i} copied $result'); return true; | |
} | |
public static function bEcho(n0:Node, ci:Array<Channel>, co:Array<Channel>):Bool | |
{ | |
var result : Array<Int> = []; | |
for (c0 in ci) result = result.concat(c0.d); | |
trace('${n0.i}: $result'); return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment