Ha - you beat me to it...
> 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;
> }
> }
Just out of interest, I tried a speed test using the three methods - mine
was the fastest by a very long way:
iterative 3068 length: 343932
recursive 7865 length: 343932
Mark's version 6771 length: 343932
iterative 1814 length: 371470
recursive 7554 length: 371470
Mark's version 7607 length: 371470
iterative 1483 length: 312964
recursive 5822 length: 312964
Mark's version 5530 length: 312964
(the first number shows the time taken for the calculation, the second shows
the length of the flattened array)
This seemed a weird result, so I took a look for other differences. It turns
out that the test for array.__proto__ == Array.prototype is *way* slower
than using instanceof Array. When I replaced the two relevant lines in the
other two functions, mine was relegated to a rightful third place
iterative 2387 length: 264356
recursive 1175 length: 264356
Mark's version 1096 length: 264356
Danny
_______________________________________________
[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