append uninitialized elements to array

Ali Çehreli acehreli at yahoo.com
Mon Jul 30 21:10:32 UTC 2018


On 07/30/2018 10:40 AM, realhet wrote:
> Hello,
> 
> I've already found out how to create an array with uninitialized 
> elements, but what I'm looking for is a way to append 16 uninitialized 
> ushorts to it and after I will it directly from 2 SSE registers.
> 
> The approximate array length is known at the start so I could be able to 
> do this by making an uninitializedArray and then doing the appending 
> manually, but I wonder if there is a way to do this with array.reserve().
> 
> Basically it would be a thing that when this special uninitialized 
> append is happening and when the reserved array size is big enough, it 
> only increments the internal array length effectively.
> 
> Thanks

Knowing that the length of a slice is its first member:

void appendUninitialized(T)(ref T[] arr, size_t N = 1) {
     arr.reserve(arr.length + N);
     auto length_p = cast(size_t*)(&arr);
     *length_p += N;
}

unittest {
     ushort[] arr;
     arr.appendUninitialized(2);
     assert(arr.length == 2);
     arr[0] = 1;
     arr[1] = 2;
     assert(arr == [ 1, 2 ]);
}

void main() {
     int[] arr;
     arr.appendUninitialized(100);
     import std.stdio : writeln;
     writeln(arr);
}

Ali


More information about the Digitalmars-d-learn mailing list