Array

Array type with deterministic control of memory. The memory allocated for the array is reclaimed as soon as possible; there is no reliance on the garbage collector. Array uses malloc, realloc and free for managing its own memory.

This means that pointers to elements of an Array will become dangling as soon as the element is removed from the Array. On the other hand the memory allocated by an Array will be scanned by the GC and GC managed objects referenced from an Array will be kept alive.

Note:

When using Array with range-based functions like those in std.algorithm, Array must be sliced to get a range (for example, use array[].map! instead of array.map!). The container itself is not a range.

  1. struct Array(T)
    struct Array (
    T
    ) if (
    !is(immutable T == immutable bool)
    ) {}
  2. struct Array(T)

Constructors

this
this(U[] values)

Constructor taking a number of items.

this
this(Range r)

Constructor taking an input range

Members

Aliases

ConstRange
alias ConstRange = RangeT!(const Array)
ImmutableRange
alias ImmutableRange = RangeT!(immutable Array)

Defines the array's primary range, which is a random-access range.

Range
alias Range = RangeT!Array

Defines the array's primary range, which is a random-access range.

insert
alias insert = insertBack

Inserts the specified elements at the back of the array. stuff can be a value convertible to T or a range of objects convertible to T.

stableInsertBefore
alias stableInsertBefore = insertBefore

Inserts stuff before, after, or instead range r, which must be a valid range previously extracted from this array. stuff can be a value convertible to T or a range of objects convertible to T. Both stable and non-stable version behave the same and guarantee that ranges iterating over the array are never invalidated.

stableRemoveAny
alias stableRemoveAny = removeAny

Removes the last element from the array and returns it. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.

stableRemoveBack
alias stableRemoveBack = removeBack

Removes the value from the back of the array. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.

stableRemoveBack
alias stableRemoveBack = removeBack

Removes howMany values from the back of the array. Unlike the unparameterized versions above, these functions do not throw if they could not remove howMany elements. Instead, if howMany > 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.

Functions

clear
void clear()

Removes all the elements from the array and releases allocated memory.

insertAfter
size_t insertAfter(Range r, Stuff stuff)

Inserts stuff before, after, or instead range r, which must be a valid range previously extracted from this array. stuff can be a value convertible to T or a range of objects convertible to T. Both stable and non-stable version behave the same and guarantee that ranges iterating over the array are never invalidated.

insertBack
size_t insertBack(Stuff stuff)

Inserts the specified elements at the back of the array. stuff can be a value convertible to T or a range of objects convertible to T.

insertBefore
size_t insertBefore(Range r, Stuff stuff)

Inserts stuff before, after, or instead range r, which must be a valid range previously extracted from this array. stuff can be a value convertible to T or a range of objects convertible to T. Both stable and non-stable version behave the same and guarantee that ranges iterating over the array are never invalidated.

linearRemove
Range linearRemove(Range r)

Removes all elements belonging to r, which must be a range obtained originally from this array.

opBinary
Array opBinary(Stuff stuff)
opDollar
size_t opDollar()
opEquals
bool opEquals(Array rhs)

Comparison for equality.

opIndex
inout(T) opIndex(size_t i)
opOpAssign
void opOpAssign(Stuff stuff)

Forwards to insertBack.

opSlice
Range opSlice()
opSlice
ConstRange opSlice()
Undocumented in source. Be warned that the author may not have intended to support it.
opSlice
ImmutableRange opSlice()
Undocumented in source. Be warned that the author may not have intended to support it.
opSlice
Range opSlice(size_t i, size_t j)
opSlice
ConstRange opSlice(size_t i, size_t j)
Undocumented in source. Be warned that the author may not have intended to support it.
opSlice
ImmutableRange opSlice(size_t i, size_t j)
Undocumented in source. Be warned that the author may not have intended to support it.
opSliceAssign
void opSliceAssign(T value)
void opSliceAssign(T value, size_t i, size_t j)
opSliceOpAssign
void opSliceOpAssign(T value)
void opSliceOpAssign(T value, size_t i, size_t j)
opSliceUnary
void opSliceUnary()
void opSliceUnary(size_t i, size_t j)

Slicing operators executing the specified operation on the entire slice.

removeAny
T removeAny()

Removes the last element from the array and returns it. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.

removeBack
void removeBack()

Removes the value from the back of the array. Both stable and non-stable versions behave the same and guarantee that ranges iterating over the array are never invalidated.

removeBack
size_t removeBack(size_t howMany)

Removes howMany values from the back of the array. Unlike the unparameterized versions above, these functions do not throw if they could not remove howMany elements. Instead, if howMany > 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.

replace
size_t replace(Range r, Stuff stuff)

Inserts stuff before, after, or instead range r, which must be a valid range previously extracted from this array. stuff can be a value convertible to T or a range of objects convertible to T. Both stable and non-stable version behave the same and guarantee that ranges iterating over the array are never invalidated.

reserve
void reserve(size_t elements)

Ensures sufficient capacity to accommodate e _elements. If e < capacity, this method does nothing.

Properties

back
inout(T) back [@property getter]
capacity
size_t capacity [@property getter]
dup
Array dup [@property getter]

Duplicates the array. The elements themselves are not transitively duplicated.

empty
bool empty [@property getter]
front
inout(T) front [@property getter]
length
size_t length [@property getter]
length
size_t length [@property setter]

Sets the number of elements in the array to newLength. If newLength is greater than length, the new elements are added to the end of the array and initialized with T.init.

Examples

typeof may give wrong result in case of classes defining opCall operator https://issues.dlang.org/show_bug.cgi?id=20589

destructor std.container.array.Array!(MyClass).Array.~this is @system so the unittest is @system too

class MyClass
{
    T opCall(T)(T p)
    {
        return p;
    }
}

Array!MyClass arr;

Meta