Sourcestd/container/array.d
auto arr = Array!int(0, 2, 3);writeln(arr[0]);// 0writeln(arr.front);// 0writeln(arr.back);// 3// reserve spacearr.reserve(1000);writeln(arr.length);// 3assert(arr.capacity >= 1000);// insertionarr.insertBefore(arr[1..$], 1);writeln(arr.front);// 0writeln(arr.length);// 4arr.insertBack(4);writeln(arr.back);// 4writeln(arr.length);// 5// set elementsarr[1] *= 42;writeln(arr[1]);// 42
import std.algorithm.comparison : equal;auto arr = Array!int(1, 2, 3);// concatauto b = Array!int(11, 12, 13);arr ~= b;writeln(arr.length);// 6// slicingassert(arr[1 .. 3].equal([2, 3]));// removearr.linearRemove(arr[1 .. 3]);assert(arr[0 .. 2].equal([1, 11]));
auto arr = Array!bool([true,true,false,true,false]);writeln(arr.length);// 5
Array(T) if (!is(immutable(T) == immutable(bool)));Array usesmalloc,realloc andfree for managing its own memory.Array will become dangling as soon as the element is removed from theArray. On the other hand the memory allocated by anArray will be scanned by the GC and GC managed objects referenced from anArray will be kept alive.NoteWhen usingArray with range-based functions like those instd.algorithm,Array must be sliced to get a range (for example, usearray[].map! instead ofarray.map!). The container itself is not a range.
values...)single);r)opEquals(const Arrayrhs) const;opEquals(ref const Arrayrhs) const;Range = RangeT!Array;ConstRange = RangeT!(const(Array));ImmutableRange = RangeT!(immutable(Array));ConstRange is a variant withconst elements.ImmutableRange is a variant withimmutable elements.dup();ComplexityΟ(length).
empty() const;ComplexityΟ(1)
length() const;opDollar() const;ComplexityΟ(1).
capacity();ComplexityΟ(1)
data() inout;ComplexityΟ(1).
reserve(size_telements);Postconditioncapacity >= e
NoteIf the capacity is increased, one should assume that all iterators to the elements are invalidated.
Complexityat mostΟ(length) ife > capacity, otherwiseΟ(1).
opSlice();ComplexityΟ(1)
opSlice(size_ti, size_tj);i up to (excluding) indexj.Preconditioni <=j &&j <= length
ComplexityΟ(1)
front() inout;Preconditionempty == false
ComplexityΟ(1)
back() inout;Preconditionempty == false
ComplexityΟ(1)
opIndex(size_ti) inout;Preconditioni < length
ComplexityΟ(1)
opSliceAssign(Tvalue);opSliceAssign(Tvalue, size_ti, size_tj);opSliceUnary(string op)()opSliceUnary(string op)(size_ti, size_tj)opSliceOpAssign(string op)(Tvalue);opSliceOpAssign(string op)(Tvalue, size_ti, size_tj);Preconditioni <j &&j < length
ComplexityΟ(slice.length)
opBinary(string op, Stuff)(Stuffstuff)ComplexityΟ(length + m), wherem is the number of elements instuff.
opOpAssign(string op, Stuff)(auto ref Stuffstuff)clear();Postconditionempty == true && capacity == 0
ComplexityΟ(length)
length(size_tnewLength);newLength. IfnewLength is greater thanlength, the new elements are added to the end of the array and initialized withT.init. IfT is astruct whose default constructor is annotated with@disable,newLength must be lower than or equal tolength.ComplexityGuaranteedΟ(abs(length - newLength)) ifcapacity >=newLength. Ifcapacity <newLength the worst case isΟ(newLength).
Precondition__traits(compiles, { static T _; }) ||newLength <=length
Postconditionlength ==newLength
removeAny();stableRemoveAny = removeAny;Preconditionempty == false
ComplexityΟ(1).
insertBack(Stuff)(Stuffstuff)insert = insertBack;stuff can be a value convertible toT or a range of objects convertible toT.ComplexityΟ(length + m) if reallocation takes place, otherwiseΟ(m), wherem is the number of elements instuff.
removeBack();stableRemoveBack = removeBack;Preconditionempty == false
ComplexityΟ(1).
removeBack(size_thowMany);stableRemoveBack = removeBack;howMany values from the back of the array. Unlike the unparameterized versions above, these functions do not throw if they could not removehowMany elements. Instead, ifhowMany > n, all elements are removed. The returned value is the effective number of elements removed. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.ComplexityΟ(howMany).
insertBefore(Stuff)(Ranger, Stuffstuff)insertBefore(Stuff)(Ranger, Stuffstuff)stableInsertBefore = insertBefore;insertAfter(Stuff)(Ranger, Stuffstuff)replace(Stuff)(Ranger, Stuffstuff)replace(Stuff)(Ranger, Stuffstuff)stuff before, after, or instead ranger, which must be a valid range previously extracted from this array.stuff can be a value convertible toT or a range of objects convertible toT. Both stable and non-stable version behave the same and guarantee that ranges iterating over the array are never invalidated.ComplexityΟ(length + m), wherem is the length ofstuff.
r is not a range extracted from this array.linearRemove(Ranger);r, which must be a range obtained originally from this array.r.ComplexityΟ(length)
r is not a valid range extracted from this array.Array(T) if (is(immutable(T) == immutable(bool)));Range;save();empty();front();front(boolvalue);moveFront();popFront();back();back(boolvalue);moveBack();popBack();opIndex(size_ti);opIndexAssign(Tvalue, size_ti);moveAt(size_ti);length() const;opSlice(size_tlow, size_thigh);values...)r)empty();ComplexityΟ(1)
dup();ComplexityΟ(length).
length() const;ComplexityΟ(1).
capacity();ComplexityΟ(1).
reserve(size_te);e elements. Ife < capacity, this method does nothing.Postconditioncapacity >=e
NoteIf the capacity is increased, one should assume that all iterators to the elements are invalidated.
Complexityat mostΟ(length) ife > capacity, otherwiseΟ(1).
opSlice();ComplexityΟ(1)
opSlice(size_ta, size_tb);ComplexityΟ(1)
front();front(boolvalue);Preconditionempty == false
ComplexityΟ(1)
back();back(boolvalue);Preconditionempty == false
ComplexityΟ(1)
opIndex(size_ti);opIndexAssign(boolvalue, size_ti);opIndexOpAssign(string op)(boolvalue, size_ti);moveAt(size_ti);Preconditioni < length
ComplexityΟ(1)
opBinary(string op, Stuff)(Stuffrhs)ComplexityΟ(length + m), wherem is the number of elements instuff.
opOpAssign(string op, Stuff)(Stuffstuff)clear();Postconditionempty == true && capacity == 0
ComplexityΟ(length)
length(size_tnewLength);newLength. IfnewLength is greater thanlength, the new elements are added to the end of the array and initialized withfalse.ComplexityGuaranteedΟ(abs(length - newLength)) ifcapacity >=newLength. Ifcapacity <newLength the worst case isΟ(newLength).
Postconditionlength ==newLength
removeAny();stableRemoveAny = removeAny;Preconditionempty == false
ComplexityΟ(1).
insertBack(Stuff)(Stuffstuff)insertBack(Stuff)(Stuffstuff)stableInsertBack = insertBack;insert = insertBack;stableInsert = insertBack;linearInsert = insertBack;stableLinearInsert = insertBack;stuff can be a value convertible tobool or a range of objects convertible tobool.ComplexityΟ(length + m) if reallocation takes place, otherwiseΟ(m), wherem is the number of elements instuff.
removeBack();stableRemoveBack = removeBack;Preconditionempty == false
ComplexityΟ(1).
removeBack(size_thowMany);stableRemoveBack = removeBack;howMany values from the back of the array. Unlike the unparameterized versions above, these functions do not throw if they could not removehowMany elements. Instead, ifhowMany > n, all elements are removed. The returned value is the effective number of elements removed. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.ComplexityΟ(howMany).
insertBefore(Stuff)(Ranger, Stuffstuff);stableInsertBefore = insertBefore;insertAfter(Stuff)(Ranger, Stuffstuff)stableInsertAfter = insertAfter;replace(Stuff)(Ranger, Stuffstuff)stableReplace = replace;stuff before, after, or instead ranger, which must be a valid range previously extracted from this array.stuff can be a value convertible tobool or a range of objects convertible tobool. Both stable and non-stable version behave the same and guarantee that ranges iterating over the array are never invalidated.ComplexityΟ(length + m), wherem is the length ofstuff.
linearRemove(Ranger);r, which must be a range obtained originally from this array.r.ComplexityΟ(length)