shrinkArray

Shrinks an array by delta elements.

If array.length < delta, does nothing and returns false. Otherwise, destroys the last array.length - delta elements in the array and then reallocates the array's buffer. If reallocation fails, fills the array with default-initialized data.

version(HasDRuntime)
bool
shrinkArray
(
T
Allocator
)
(
auto ref Allocator alloc
,
ref T[] array
,
size_t delta
)

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 shrunk

delta size_t

number of elements to remove (upon success the new length of array is array.length - delta)

Return Value

Type: bool

true upon success, false if memory could not be reallocated. In the latter case, the slice array[$ - delta .. $] is left with default-initialized elements.

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

int[] a = theAllocator.makeArray!int(100, 42);
assert(a.length == 100);
assert(theAllocator.shrinkArray(a, 98));
assert(a.length == 2);
assert(a == [42, 42]);

Meta