This module provides an Array
type with deterministic memory usage not reliant on the GC, as an alternative to the built-in arrays.
This module is a submodule of std.container
.
auto arr = Array!int(0, 2, 3); writeln(arr[0]); // 0 writeln(arr.front); // 0 writeln(arr.back); // 3 // reserve space arr.reserve(1000); writeln(arr.length); // 3 assert(arr.capacity >= 1000); // insertion arr.insertBefore(arr[1..$], 1); writeln(arr.front); // 0 writeln(arr.length); // 4 arr.insertBack(4); writeln(arr.back); // 4 writeln(arr.length); // 5 // set elements arr[1] *= 42; writeln(arr[1]); // 42
import std.algorithm.comparison : equal; auto arr = Array!int(1, 2, 3); // concat auto b = Array!int(11, 12, 13); arr ~= b; writeln(arr.length); // 6 // slicing assert(arr[1 .. 3].equal([2, 3])); // remove arr.linearRemove(arr[1 .. 3]); assert(arr[0 .. 2].equal([1, 11]));
Array!bool
packs together values efficiently by allocating one bit per element Array!bool arr; arr.insert([true, true, false, true, false]); writeln(arr.length); // 5
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.
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.Constructor taking a number of items.
Constructor taking an input range
Comparison for equality.
Defines the array's primary range, which is a random-access range.
ConstRange
is a variant with const
elements. ImmutableRange
is a variant with immutable
elements.
Duplicates the array. The elements themselves are not transitively duplicated.
length
).true
if and only if the array has no elements. 1
)
1
).1
)
Ensures sufficient capacity to accommodate e
elements. If e < capacity
, this method does nothing.
capacity >= e
length
) if e > capacity
, otherwise Ο(1
).1
)
i
up to (excluding) index j
. i <= j && j <= length
1
)
empty == false
1
)
empty == false
1
)
i < length
1
)
Slicing operators executing the specified operation on the entire slice.
i < j && j < length
slice.length
)
this
and its argument. length + m
), where m
is the number of elements in stuff
.Forwards to insertBack
.
Removes all the elements from the array and releases allocated memory.
empty == true && capacity == 0
length
)
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
.
abs(length - newLength)
) if capacity >= newLength
. If capacity < newLength
the worst case is Ο(newLength
). length == newLength
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.
empty == false
1
). Exception
if the array is empty.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
.
length + m
) if reallocation takes place, otherwise Ο(m
), where m
is the number of elements in stuff
.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.
empty == false
1
). Exception
if the array is empty.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.
howMany
).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.
length + m
), where m
is the length of stuff
. Exception
if r
is not a range extracted from this array.Removes all elements belonging to r
, which must be a range obtained originally from this array.
r
. length
) Exception
if r
is not a valid range extracted from this array.Array specialized for bool
. Packs together values efficiently by allocating one bit per element.
Defines the array's primary range.
Range primitives
Property returning true
if and only if the array has no elements.
1
)
length
).Returns the number of elements in the array.
1
).1
).Ensures sufficient capacity to accommodate e
elements. If e < capacity
, this method does nothing.
capacity >= e
length
) if e > capacity
, otherwise Ο(1
).1
)
1
)
empty == false
1
) Exception
if the array is empty.empty == false
1
) Exception
if the array is empty.Indexing operators yielding or modifyng the value at the specified index.
i < length
1
)
this
and its argument. length + m
), where m
is the number of elements in stuff
.Forwards to insertBack
.
Removes all the elements from the array and releases allocated memory.
empty == true && capacity == 0
length
)
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 false
.
abs(length - newLength)
) if capacity >= newLength
. If capacity < newLength
the worst case is Ο(newLength
). length == newLength
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.
empty == false
1
). Exception
if the array is empty.Inserts the specified elements at the back of the array. stuff
can be a value convertible to bool
or a range of objects convertible to bool
.
length + m
) if reallocation takes place, otherwise Ο(m
), where m
is the number of elements in stuff
.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.
empty == false
1
). Exception
if the array is empty.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.
howMany
).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 bool
or a range of objects convertible to bool
. Both stable and non-stable version behave the same and guarantee that ranges iterating over the array are never invalidated.
length + m
), where m
is the length of stuff
.Removes all elements belonging to r
, which must be a range obtained originally from this array.
r
. length
)
© 1999–2019 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_container_array.html