How do you remove/insert elements in a dynamic array without allocating?

Dmitry Olshansky dmitry.olsh at gmail.com
Thu Nov 8 09:39:53 PST 2012


11/7/2012 2:44 PM, monarch_dodra пишет:
> On Wednesday, 7 November 2012 at 10:18:51 UTC, Jonathan M Davis wrote:
>> By the way, monarch_dodra, since you've been messing around with Array
>> recently, I would point out that it looks like setting the length
>> doesn't work
>> properly if you set it greater than the current length, let alone
>> greater than
>> the current capacity.  _capacity is not adjusted if newLength is
>> greater than
>> it, and no calls to GC.removeRange or GC.addRange are made, so it
>> doesn't look
>> like newly allocated memory is being tracked by the GC like it should if
>> length is allocating it.
>
> I kind of wanted to stay out of that part of the code, but good catch.
> This creates an assertion error:
> --------
>      auto a = Array!int();
>      a.length = 5;
>      a.insertBack(1);
> --------
> Because at the point of insert back, length > capacity...
>
> I'll correct the issues anyways. Good point about the GC.removeRange a,d
> GC.addRange too.

The ugly truth is that std.container even the most primitive collections 
are not tested well enough.

The stuff should have obligatory notes about it being *experimental* 
somewhere prominent so that people don't get tied strongly to its 
current behavior prematurely and don't get appalled because of the 
amount of bugs lurking inside.

That and it being quite novel in general (sealed containers, only range 
iteration/insertion/removal etc.) more then justifies the *experimental* 
status.


-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list