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

monarch_dodra monarchdodra at gmail.com
Wed Nov 7 01:45:46 PST 2012


On Wednesday, 7 November 2012 at 03:45:06 UTC, Malte Skarupke 
wrote:
> Having no clear ownership for the array is not something I am 
> willing to accept.

"Strong ownership" puts you back into C++'s boat of "bordering 
psychotic duplication on every pass by value". In a GC language, 
and in particular, D, that favors pass by value, this might not 
be the best approach.

I'll re-iterate that you may consider looking into 
std.container.Array. It behaves much like would std::vector 
(reserve, etc)... You can extract an actual range from Array, but 
there is a clear "container" - "range" distinction.

An added bonus is that it uses "implicit reference" semantics. 
This means that when you write "a = b", then afterwards, you have 
"a is b", and they are basically alias. This is a good thing, as 
it avoids payload duplication without your explicit consent. The 
implicit means that it will lazily initialize if you haven't done 
so yet.

You claim you want "explicit ownership": Array gives you that, 
but not in the classic RAII sense. If you need to duplicate an 
Array, you call "dup" manually.

--------
Also, Array uses a deterministic memory model, just like vector, 
that releases content as soon as it goes out of scope. I could 
have done without that, personally, but to each their own.


More information about the Digitalmars-d mailing list