Arrays are sufficient for ArrayLists? Really??

Sean Kelly sean at invisibleduck.org
Mon May 16 11:40:11 PDT 2011


On May 16, 2011, at 11:29 AM, Mehrdad wrote:
> 
>> As has been mentioned, std.algorithm.remove can be of help. You may want to look at three of its
> capabilities in particular: (a) remove multiple offsets in one pass, e.g. remove(a, 0, 4) removes
> the first and fifth element, (b) you can remove subranges, e.g. remove(a, tuple(1, 3)) removes the
> second through fourth element, and (c) if you don't care about the order in which elements are left
> after removal you may want to look into unstable remove, which does considerably less work.
> 
> Thanks for the idea. This seems great, except for a couple of things:
> 
> - I _do_ need the order to stay the same, so I can't just put in the last element.

Thus the stable remove, which doesn't affect element order.

> - I still don't understand how this helps. Either this modifies the array directly, in which case
> adding a new element to the array after a removal would still cause a reallocation every time, or
> this returns a filtered range, in which case it doesn't do what I need (since it doesn't modify the
> array directly)... am I missing something?

Removing and then appending one element to an array won't cause any allocations to occur.  The GC lock may be acquired to determine whether the memory block backing the array has room for the new element, but that's it.


More information about the Digitalmars-d mailing list