On 7/25/06, Mark Winterhalder <[EMAIL PROTECTED]> wrote:
Hmm... now that I'm having coffee and slowly waking up, I'm getting
serious doubts about how to do depth-first without function calls. I'm
probably wrong, sorry.
Interesting problem, though. Got me thinking.
So, here's my proposal for a function to flatten an array without
calling it recursively:
class Main {
public static function main () : Void {
var foo : Array = [0, [1, 2], 3, [4, [5, 6], 7], 8, [], 9];
var out : String = flatten( foo ).toString();
_root.createTextField( "tf", 100, 0, 0, 1000, 100 );
_root.tf.text = out;
}
public static function flatten( inArray : Array ) : Array {
var outArray : Array = [];
var list = inArray;
list.index = 0;
var item : Object;
var index : Number;
var length : Number;
do {
index = list.index;
length = list.length;
while( index < length ) {
item = list[ index++ ];
if( item.__proto__ == Array.prototype ) {
item.parent = list;
list.index = index;
list = item;
index = 0;
length = list.length;
} else {
outArray.push( item );
}
}
} while( list = list.parent );
return outArray;
}
}
<<<
I hope you have a big enough testcase to compare performance, it would
be interesting which one is faster.
Mark
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com