Basic module

array_add

procedure · 3 overloadsSource

Appends an element to a dynamic array and returns a pointer to it.

With no item the new element is initialized to the default value of T; with one it is set to item (and nothing is returned); with several, all of them are appended in order. An empty list does nothing. The array grows when it is full: the first allocation holds 8 elements, after that the capacity doubles, or is exactly enough when a long list is added at once. Growth uses the array's own allocator, or context.allocator when it has none yet, and may move the elements, so a pointer from an earlier call is only good until the next add. Appending a view of the array to itself is fine.

ExampleOpen in playground ↗
values: [..] int;
array_add(*values, 10);
array_add(*values, 20, 30);

slot := array_add(*values); // a zeroed element, here at the end
slot.* = 40;

assert(values.count == 4 && values[0] == 10 && values[3] == 40);
array_free(values);
array_add :: (array: *[..] $T) -> *T

Appends an element to a dynamic array and returns a pointer to it.

With no item the new element is initialized to the default value of T; with one it is set to item (and nothing is returned); with several, all of them are appended in order. An empty list does nothing. The array grows when it is full: the first allocation holds 8 elements, after that the capacity doubles, or is exactly enough when a long list is added at once. Growth uses the array's own allocator, or context.allocator when it has none yet, and may move the elements, so a pointer from an earlier call is only good until the next add. Appending a view of the array to itself is fine.

Parameters

array: *[..] $T

Returns

*T
ExampleOpen in playground ↗
values: [..] int;
array_add(*values, 10);
array_add(*values, 20, 30);

slot := array_add(*values); // a zeroed element, here at the end
slot.* = 40;

assert(values.count == 4 && values[0] == 10 && values[3] == 40);
array_free(values);
array_add :: (array: *[..] $T, item: T)

Appends item; see the first array_add for how the array grows.

Parameters

array: *[..] $T
item: T
array_add :: (array: *[..] $T, to_append: ..T)

Appends all of to_append in order; see the first array_add for how the array grows.

Parameters

array: *[..] $T
to_append: ..T

See also