Fastest Way to Append Multiple Elements to an Array

Tobias Pankrath via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 18 12:12:00 PST 2014


On Thursday, 18 December 2014 at 15:12:39 UTC, Nordlöw wrote:
> On Wednesday, 17 December 2014 at 12:30:37 UTC, Tobias Pankrath 
> wrote:
>>> void append(T, Args...)(ref T[] arr, auto ref Args args){
>>> {
>>>  static if (args.length == 1)
>>>     arr ~= args[0];     // inlined
>>>  else{
>>>     arr.length += args.length;
>>>     foreach(i, e; args)
>>>        arr[$ - args.length + i] = e;
>>>  }
>>> }
>
> Is it un-Phobos-like to make append return a reference to data 
> like at
>
> https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L1605

I don't now, returning it at least allows chaining, which is nice.

You shouldn't use my code verbatim though. For example, you 
should only use x.length, if x is of element type of data. 
Otherwise if you have e.g. an array of array you'd get totally 
wrong numbers.


More information about the Digitalmars-d-learn mailing list