dmd.root.array

Dynamic array implementation.

Authors

Walter Bright

Source: root/array.d

  • Declaration

    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.

    Return Value

    The given array exposed to a standard D array.

  • Declaration

    pure nothrow void split(T)(ref Array!T array, size_t index, size_t length);

    Splits the array at and expands it to make room for elements by shifting everything past 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 .

  • Declaration

    pure nothrow @nogc @safe T[] reverse(T)(T[] a);

    Reverse an array in-place.

    Parameters

    T[] a

    array

    Return Value

    reversed a[]

  • Declaration

    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.

    Discussion

    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

    See Also

    Examples

    1. 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);

  • Declaration

    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.

    Discussion

    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

    Return Value

    the last value returned by callable

    See Also

    Examples

    1. 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);

  • Declaration

    T[n] staticArray(T, size_t n)(auto ref T[n] array);

    Return Value

    A static array constructed from array.

    Examples

    1. enum a = [0, 1].staticArray; static assert(is(typeof(a) == int[2])); static assert(a == [0, 1]);

  • Declaration

    bool equal(Range1, Range2)(Range1 range1, Range2 range2);

    Return Value

    true if the two given ranges are equal

    Examples

    1. 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[]));

  • Declaration

    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.

    Return Value

    a range containing only elements for which the predicate returns true

    Examples

    1. enum a = [1, 2, 3, 4].staticArray; enum result = a[].filter!(e => e > 2); enum expected = [3, 4].staticArray; static assert(result.equal(expected[]));

  • map

    Declaration

    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.

    Return Value

    a range containing the result of each call to callable

    Examples

    1. 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[]));

  • Declaration

    template ElementType(R)

    Evaluates to the element type of R.

  • Declaration

    enum auto isInputRange(R);

    Evaluates to true if the given type satisfy the input range interface.

  • Declaration

    enum auto isPredicateOf(alias func, T);

    Evaluates to true if func can be called with a value of T and returns bool.

  • Declaration

    enum auto isCallableWith(alias func, T);

    Evaluates to true if func be called withl a value of T.