number of elements that were removed.
TODO implement version that doesn't use a temporary array tmp, which is probably faster for small arrays.
construct and append from slices
alias T = int; alias A = DynamicArray!(T, null, uint); static if (size_t.sizeof == 8) // only 64-bit static assert(A.sizeof == 2 * size_t.sizeof); // only two words auto a = A([10, 11, 12].s); a ~= a[]; assert(a[] == [10, 11, 12, 10, 11, 12].s); a ~= false; assert(a[] == [10, 11, 12, 10, 11, 12, 0].s);
1 alias T = int; 2 alias A = DynamicArray!(T); 3 4 A a; 5 6 a.length = 1; 7 assert(a.length == 1); 8 assert(a.capacity >= 1); 9 10 a[0] = 10; 11 12 a.insertBack(11, 12); 13 14 a ~= T.init; 15 a.insertBack([3].s); 16 assert(a[] == [10, 11, 12, 0, 3].s); 17 18 import std.algorithm.iteration : filter; 19 20 a.insertBack([42].s[].filter!(_ => _ is 42)); 21 assert(a[] == [10, 11, 12, 0, 3, 42].s); 22 23 a.insertBack([42].s[].filter!(_ => _ !is 42)); 24 assert(a[] == [10, 11, 12, 0, 3, 42].s); 25 26 a ~= a[]; 27 assert(a[] == [10, 11, 12, 0, 3, 42, 28 10, 11, 12, 0, 3, 42].s);
1 alias T = int; 2 alias A = DynamicArray!(T); 3 4 A a; // default construction allowed 5 assert(a.empty); 6 assert(a.length == 0); 7 assert(a.capacity == 0); 8 assert(a[] == []); 9 10 auto b = DynamicArray!int.withLength(3); 11 assert(!b.empty); 12 assert(b.length == 3); 13 assert(b.capacity == 3); 14 b[0] = 1; 15 b[1] = 2; 16 b[2] = 3; 17 assert(b[] == [1, 2, 3].s); 18 19 b[] = [4, 5, 6].s; 20 assert(b[] == [4, 5, 6].s); 21 22 const c = DynamicArray!int.withCapacity(3); 23 assert(c.empty); 24 assert(c.capacity == 3); 25 assert(c[] == []); 26 27 // TODO this should fail with -dip1000 28 auto f() @safe 29 { 30 A a; 31 return a[]; 32 } 33 auto d = f(); 34 35 const e = DynamicArray!int([1, 2, 3, 4].s); 36 assert(e.length == 4); 37 assert(e[] == [1, 2, 3, 4].s);
1 alias T = int; 2 alias A = DynamicArray!(T); 3 4 auto a = A([1, 2, 3].s); 5 A b = a.dup; // copy construction enabled 6 7 assert(a[] == b[]); // same content 8 assert(&a[0] !is &b[0]); // but not the same 9 10 assert(b[] == [1, 2, 3].s); 11 assert(b.length == 3); 12 13 b ~= 4; 14 assert(a != b); 15 a.clear(); 16 assert(a != b); 17 b.clear(); 18 assert(a == b); 19 20 auto c = A([1, 2, 3].s);
DIP-1000 return ref escape analysis
1 alias T = int; 2 alias A = DynamicArray!T; 3 4 T[] leakSlice() @safe pure nothrow @nogc 5 { 6 A a; 7 return a[]; // TODO shouldn't compile with -dip1000 8 } 9 10 T* leakPointer() @safe pure nothrow @nogc 11 { 12 A a; 13 return a._store.ptr; // TODO shouldn't compile with -dip1000 14 } 15 16 auto lp = leakPointer(); // TODO shouldn't compile with -dip1000 17 auto ls = leakSlice(); // TODO shouldn't compile with -dip1000
Remove all elements matching predicate.