Your approach:
var a = [...];
for (i in 0...a.length)
if (!keep(a[a.length-i]])
a.splice(a.length-1, 1);
Given that a.splice is actually O(R) (where R is the length of the
"right" array), this is actually rather expensive.
If for every element, the probability, that it should be removed is p,
then for the k-th element from the end of the array, the average cost
is O(p * (p-1) * k), so the overall complexity is O(N + N * (p-1) * p
* N / 2), which is O(N^2) unless p is sufficiently close to 0 or 1.
I suggest building a new array:
var ret = [];
for (e in a)
if (keep(e))
a.push(e);
Or perhaps like this:
using Lambda;
a.filter(keep).array();
The last one does involve quite a lot of overhead, but has overall
linear complexity.
On Tue, Jan 10, 2012 at 1:45 PM, Adam Holland <adgho...@gmail.com> wrote:
> Hopefully a simple question. Is there a way to do a reverse loop through an
> Array? I need to loop through it and splice elements out of it. Thanks
>
> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
--
static public function filter<S>(input:Iterable<S>, f: S->Bool,
?output:Collection<S>):Collection<S>
{
if (output == null) output = new List<S>();
for ( x in input )
if( f(x) )
output.add(x);
return output;
}
so the caller could supply a new List() or new FastList() if he so desires:
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var colList = MyLambda.filter(array, function(i) return i > 5);
// works just like before
var colArray = MyLambda.filter(array, function(i) return i > 5,
new Array()); // works on an array
var colFastList = MyLambda.filter(array, function(i) return i >
5, new FastList()); // sort of works on a FastList
(Actually FastList is a bad example because it does everything in
reverse.) This would even allow Lambda usage for custom collections if
they implement the Collection interface.
Regards
Simon
Imagine the following (constructed) scenario:
You have 10000 entries in your array, and all the elements at even
indices need removal.
When splicing at index 9998, you need to copy 1 entry to a new
position, when at index 9996, you will need to copy 2 entries, at 9994
it's 3 entries and at 0 it's 5000 entries, which means that in the end
you are going to cause 12502500 copy operations.
If you build a new array instead you have 5000 copy operations.
So for the sake of your game speed, don't do that.
I posted this on the haXe site some time ago. IMHO the most efficient
solution for you: http://haxe.org/doc/snip/countdownintiter
Good luck,
--
-Martijn
--
-Martijn @..@ ( Martijn Loots - Hengelo [NL] )
- (`--') ( martijn<@>cosix.com - www.cosix.com )
- ( >__< ) ----------------------------------------
- ^^^ ^^^ ( Netwerken, Security, Open Source )
> Martjn
>
Hiya.
It would be perfect. He *did* ask for a reverse iterator though... :)
I'm currently using haxe remoting with a haxe.remoting.HttpConnection.
Is there a way to set custom haxe remoting headers in call ?
If not, is there a way to set custom HTTP headers with
haxe.remoting.HttpConnection and how to read them on the receiving side ?
Cheers,
zabojad
--