Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

dmd.root.array

Dynamic array implementation.
Authors:

Source root/array.d

pure nothrow @nogc @property inout(T)[] peekSlice(T)(inout(Array!T)* array);
Exposes the given root Array as a standard D array.
Parameters:
inout(Array!T)* array the array to expose.
Returns:
The given array exposed to a standard D array.
pure nothrow void split(T)(ref Array!T array, size_t index, size_t length);
Splits the array at index and expands it to make room for length elements by shifting everything past index to the right.
Parameters:
Array!T array the array to split.
size_t index the index to split the array from.
size_t length the number of elements to make room for starting at index.
pure nothrow @nogc @safe T[] reverse(T)(T[] a);
Reverse an array in-place.
Parameters:
T[] a array
Returns:
reversed a[]
void each(alias callable, T)(ref Array!T array)
if (is(ReturnType!(typeof((T t) => callable(t))) == void));
Iterates the given array and calls the given callable for each element.
Use this instead of foreach when the array may expand during iteration.
Parameters:
callable the callable to call for each element
Array!T array the array to iterate
Examples:
static immutable expected = [2, 3, 4, 5];

Array!int array;

foreach (e ; expected)
    array.push(e);

int[] result;
array.each!((e) {
    result ~= e;
});

assert(result == expected);
int each(alias callable, T)(ref Array!T array)
if (is(ReturnType!(typeof((T t) => callable(t))) == int));
Iterates the given array and calls the given callable for each element.
If callable returns != 0, it will stop the iteration and return that value, otherwise it will return 0.
Use this instead of foreach when the array may expand during iteration.
Parameters:
callable the callable to call for each element
Array!T array the array to iterate
Returns:
the last value returned by callable
Examples:
Array!int array;

foreach (e ; [2, 3, 4, 5])
    array.push(e);

int[] result;
const returnValue = array.each!((e) {
    result ~= e;

    if (e == 3)
        return 8;

    return 0;
});

assert(result == [2, 3]);
assert(returnValue == 8);
T[n] staticArray(T, size_t n)(auto ref T[n] array);
Returns:
A static array constructed from array.
Examples:
enum a = [0, 1].staticArray;
static assert(is(typeof(a) == int[2]));
static assert(a == [0, 1]);
bool equal(Range1, Range2)(Range1 range1, Range2 range2);
Returns:
true if the two given ranges are equal
Examples:
enum a = [ 1, 2, 4, 3 ].staticArray;
static assert(!equal(a[], a[1..$]));
static assert(equal(a[], a[]));

// different types
enum b = [ 1.0, 2, 4, 3].staticArray;
static assert(!equal(a[], b[1..$]));
static assert(equal(a[], b[]));
auto filter(alias predicate, Range)(Range range)
if (isInputRange!(Unqual!Range) && isPredicateOf!(predicate, ElementType!Range));
Lazily filters the given range based on the given predicate.
Returns:
a range containing only elements for which the predicate returns true
Examples:
enum a = [1, 2, 3, 4].staticArray;
enum result = a[].filter!(e => e > 2);

enum expected = [3, 4].staticArray;
static assert(result.equal(expected[]));
auto map(alias callable, Range)(Range range)
if (isInputRange!(Unqual!Range) && isCallableWith!(callable, ElementType!Range));
Lazily iterates the given range and calls the given callable for each element.
Returns:
a range containing the result of each call to callable
Examples:
enum a = [1, 2, 3, 4].staticArray;
enum expected = [2, 4, 6, 8].staticArray;

enum result = a[].map!(e => e * 2);
static assert(result.equal(expected[]));
auto walkLength(Range)(Range range)
if (isInputRange!Range);
Returns:
the length of the given range.
Examples:
enum a = [1, 2, 3, 4].staticArray;
static assert(a[].walkLength == 4);

enum c = a[].filter!(e => e > 2);
static assert(c.walkLength == 2);
template ElementType(R)
Evaluates to the element type of R.
enum auto isInputRange(R);
Evaluates to true if the given type satisfy the input range interface.
enum auto isPredicateOf(alias func, T);
Evaluates to true if func can be called with a value of T and returns a value that is convertible to bool.
enum auto isCallableWith(alias func, T);
Evaluates to true if func be called withl a value of T.