expandArray

Grows array by appending delta more elements. The needed memory is allocated using alloc. The extra elements added are either default- initialized, filled with copies of init, or initialized with values fetched from range.

  1. bool expandArray(Allocator alloc, T[] array, size_t delta)
  2. bool expandArray(Allocator alloc, T[] array, size_t delta, T init)
  3. bool expandArray(Allocator alloc, T[] array, R range)
    bool
    expandArray
    (
    T
    Allocator
    R
    )
    (
    auto ref Allocator alloc
    ,
    ref T[] array
    ,)

Parameters

T

element type of the array being created

alloc Allocator

the allocator used for getting memory

array T[]

a reference to the array being grown

range R

range used for initializing the array elements

Return Value

Type: bool

true upon success, false if memory could not be allocated. In the latter case array is left unaffected.

Throws

The first two overloads throw only if alloc's primitives do. The overloads that involve copy initialization deallocate memory and propagate the exception if the copy operation throws.

Examples

auto arr = theAllocator.makeArray!int([1, 2, 3]);
assert(theAllocator.expandArray(arr, 2));
assert(arr == [1, 2, 3, 0, 0]);
import std.range : only;
assert(theAllocator.expandArray(arr, only(4, 5)));
assert(arr == [1, 2, 3, 0, 0, 4, 5]);

Meta