Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
Dynamic array resizing for non-zero initialised elements
Browse files Browse the repository at this point in the history
  • Loading branch information
hmmdyl committed May 30, 2021
1 parent f074312 commit eb5de11
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions source/lifetime/array_.d
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,42 @@ extern (C) void[] _d_arraysetlengthT(const TypeInfo ti, size_t newlength, void[]

memcpy(newArr.ptr, p.ptr, p.length * sizeElem);

*p = newArr[0 .. newlength];
return *p;
}

extern (C) void[] _d_arraysetlengthiT(const TypeInfo ti, size_t newlength, void[]* p) pure nothrow
{
import core.stdc.string;
static void doInitialize(void *start, void *end, const void[] initializer)
{
if (initializer.length == 1)
{
memset(start, *(cast(ubyte*)initializer.ptr), end - start);
}
else
{
auto q = initializer.ptr;
immutable initsize = initializer.length;
for (; start < end; start += initsize)
{
memcpy(start, q, initsize);
}
}
}

auto tiNext = unqualify(ti.next);
auto sizeElem = tiNext.tsize;
auto newArr = _d_newarrayU(ti, newlength);

import core.stdc.string;

memcpy(newArr.ptr, p.ptr, p.length * sizeElem);

doInitialize(newArr.ptr + p.length * sizeElem,
newArr.ptr + newlength * sizeElem,
tiNext.initializer);

*p = newArr[0 .. newlength];
return *p;
}

0 comments on commit eb5de11

Please sign in to comment.