1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
/**
This module contains support for controlling dynamic arrays' concatenation
Copyright: Copyright Digital Mars 2000 - 2019.
License: Distributed under the
$(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
(See accompanying file LICENSE)
Source: $(DRUNTIMESRC core/internal/_array/_concatenation.d)
*/
module core.internal.array.concatenation;
/**
* Concatenate the arrays inside of `froms`.
* `_d_arraycatnTX(a, b, c)` means `a ~ b ~ c`.
*
* Params:
* froms = Arrays to be concatenated.
* Returns:
* A newly allocated array that contains all the elements from `froms`.
*/
Tret _d_arraycatnTX(Tret, Tarr...)(auto ref Tarr froms) @trusted
{
import core.internal.array.capacity : _d_arraysetlengthTImpl;
import core.internal.traits : hasElaborateCopyConstructor, Unqual;
import core.lifetime : copyEmplace;
import core.stdc.string : memcpy;
Tret res;
size_t totalLen;
alias T = typeof(res[0]);
enum elemSize = T.sizeof;
enum hasPostblit = __traits(hasPostblit, T);
static foreach (from; froms)
static if (is (typeof(from) : T))
totalLen++;
else
totalLen += from.length;
if (totalLen == 0)
return res;
// We cannot use this, because it refuses to work if the array type has disabled default construction.
// res.length = totalLen;
// Call the runtime function directly instead.
// TODO: once `__arrayAlloc` is templated, call that instead.
version (D_ProfileGC)
{
// TODO: forward file, line, name from _d_arraycatnTXTrace
_d_arraysetlengthTImpl!(typeof(res))._d_arraysetlengthTTrace(
__FILE__, __LINE__, "_d_arraycatnTX", res, totalLen);
}
else
{
_d_arraysetlengthTImpl!(typeof(res))._d_arraysetlengthT(res, totalLen);
}
/* Currently, if both a postblit and a cpctor are defined, the postblit is
* used. If this changes, the condition below will have to be adapted.
*/
static if (hasElaborateCopyConstructor!T && !hasPostblit)
{
size_t i = 0;
foreach (ref from; froms)
static if (is (typeof(from) : T))
copyEmplace(cast(T) from, res[i++]);
else
{
if (from.length)
foreach (ref elem; from)
copyEmplace(cast(T) elem, res[i++]);
}
}
else
{
auto resptr = cast(Unqual!T *) res;
foreach (ref from; froms)
static if (is (typeof(from) : T))
memcpy(resptr++, cast(Unqual!T *) &from, elemSize);
else
{
const len = from.length;
if (len)
{
memcpy(resptr, cast(Unqual!T *) from, len * elemSize);
resptr += len;
}
}
static if (hasPostblit)
foreach (ref elem; res)
(cast() elem).__xpostblit();
}
return res;
}
// postblit
@safe unittest
{
int counter;
struct S
{
int val;
this(this)
{
counter++;
}
}
S[] arr1 = [S(0), S(1), S(2)];
S[] arr2 = [];
S[] arr3 = [S(6), S(7), S(8)];
S elem = S(9);
S[] result = _d_arraycatnTX!(S[])(arr1, arr2, arr3, elem);
assert(counter == 7);
assert(result == [S(0), S(1), S(2), S(6), S(7), S(8), S(9)]);
}
// copy constructor
@safe unittest
{
int counter;
struct S
{
int val;
this(ref return scope S rhs)
{
val = rhs.val;
counter++;
}
}
S[] arr1 = [S(0), S(1), S(2)];
S[] arr2 = [S(3), S(4), S(5)];
S[] arr3 = [S(6), S(7), S(8)];
S elem = S(9);
S[] result = _d_arraycatnTX!(S[])(arr1, elem, arr2, arr3);
assert(counter == 10);
assert(result == [S(0), S(1), S(2), S(9), S(3), S(4), S(5), S(6), S(7), S(8)]);
}
// throwing
@safe unittest
{
int counter;
bool didThrow;
struct S
{
int val;
this(this)
{
counter++;
if (counter == 4)
throw new Exception("");
}
}
try
{
S[] arr1 = [S(0), S(1), S(2)];
S[] arr2 = [S(3), S(4), S(5)];
_d_arraycatnTX!(S[])(arr1, arr2);
}
catch (Exception)
{
didThrow = true;
}
assert(counter == 4);
assert(didThrow);
}
version (D_ProfileGC)
{
/**
* TraceGC wrapper around $(REF _d_arraycatnTX, core,internal,array,concatenation).
*/
Tret _d_arraycatnTXTrace(Tret, Tarr...)(string file, int line, string funcname, scope auto ref Tarr froms) @trusted
{
version (D_TypeInfo)
{
import core.internal.array.utils: TraceHook, gcStatsPure, accumulatePure;
mixin(TraceHook!(Tarr.stringof, "_d_arraycatnTX"));
import core.lifetime: forward;
return _d_arraycatnTX!Tret(forward!froms);
}
else
assert(0, "Cannot concatenate arrays if compiling without support for runtime type information!");
}
}
|