Feature suggestion: in-place append to array

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 4 04:06:37 PST 2010


On Thu, 04 Mar 2010 04:02:46 -0500, KF <kfleszar at gmail.com> wrote:

> I hope this is the right place to post it.
> In my work, I often need to add elements at the end of dynamic arrays  
> and remove them from the end. This incremental changes would most
> conveniently be performed by a~=e for addition of e at the end of a, and  
> say a=a[0..$-1]. Unfortunately, ~= always creates a copy and
> is thus too time consuming.

No, it does not create a copy.  a = a ~ e always creates a copy, a ~= e  
appends in place if possible.  This still will be slower than appender,  
but in the next release of DMD it will be much faster than the current  
release.

With the next release of DMD, a = a[0..$-1] will shrink the array, but the  
next append to a will reallocate.  There will be a function to get around  
this, but use it only if you know that the data removed from the end isn't  
referenced anywhere else.

-Steve



More information about the Digitalmars-d mailing list