If T[new] is the container for T[], then what is the container for T[U]?

Robert Jacques sandford at jhu.edu
Sun Apr 26 07:25:30 PDT 2009


On Sun, 26 Apr 2009 03:21:08 -0400, Rainer Deyke <rainerd at eldwood.com>  
wrote:
> Robert Jacques wrote:
>> 1) Immutable strings are often concatenated, which you don't address
>
> There are two types of concatenation: in-place (operator ~=) and not
> (operator ~).  The former can take advantage of capacity to avoid new
> allocation, the latter can not.

Actually, both ~= and ~ use capacity to avoid new allocation.

> When concatenating a lot of immutable strings, it might make sense to
> make the left side mutable:
>
> char[] tmp = string1.dup;
> tmp ~= string2;
> tmp ~= string3;
> // ...
> tmp ~= string99;
> string result = tmp.idup;

Why make 2 extra copies if you don't need too?

>> 2) int[], real[], and basically anything not a string rarely
>> concatenates, but often mutable, which you don't address
>
> I frequently append individual elements to non-string arrays.

Cool. Care to elaborate on your use case?
I was thinking of scientific computing (linear algebra, optimization) and  
game programming.

Since we're off the topic of T[new] and onto a capacity field in general,  
I'd like to point out the older newsgroup posts:
http://www.digitalmars.com/d/archives/digitalmars/D/Array_Capacity_Field_71119.html
http://www.digitalmars.com/d/archives/digitalmars/D/Array_append_performance_75410.html
http://www.digitalmars.com/d/archives/digitalmars/D/Array_append_performance_2_75811.html



More information about the Digitalmars-d mailing list