in reply to Removing certain elements from an array
This works by flagging the elements you want to delete, then using @hugearray as a circular shift register, shift-pushing each element in the array in turn, until it gets back to the beginning. As it goes, it doesn't push back in the elements to be deleted.@hugearray = ( ... ); @toremove = ( ... ); # remove from @hugearray elements indexed in @toremove @hugearray[@toremove] = ("RemoveMe") x @toremove; push @hugearray, "SentinelFlag"; for (my $_;;) { $_ = shift @hugearray; last if $_ eq "SentinelFlag"; next if $_ eq "RemoveMe"; push @hugearray, $temp; }
How does it fare efficiency-wise? This routine touches each data element once, and likely does at most a single implicit array-copy. I think it is probably an O(n+m) operation (n=@hugearray, m=@toremove). The splice solution is O(m log m + m*O(splice(n)). Since perl arrays are not linked lists, I think splice is an O(n) operation, meaning that the splice solutions are O(m log m + m*n). Which is faster? My guess would be the shift-register, but I don't know.
|
|---|