in reply to Fastest way to grep multiple time on some array
Can you grep and remove simultaneously?
sub grep_in_place(&\@) { my ($cb, $array) = @_; my $src = -1; my $dst = -1; while (++$src < @$array) { my $keep; $keep = $cb->() for $array->[$src]; $array->[++$dst] = $array->[$src] if $keep; } $#$array = $dst; } grep_in_place { ... } @array;
Uses O(1) memory
This could be done faster using XS since the the elements could be moved instead of copied.
Update: Fixed the proto. Added disappeared if $keep.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Fastest way to grep multiple time on some array
by repellent (Priest) on Feb 19, 2009 at 23:47 UTC | |
by ikegami (Patriarch) on Feb 19, 2009 at 23:57 UTC | |
by repellent (Priest) on Feb 20, 2009 at 02:16 UTC |