Basic module

array_insert_at

procedure · 1 overloadSource

Inserts item so that it ends up at index, shifting the elements from there on up by one.

index may equal the count, which appends. Anything else out of range fails an assert. The array grows as in array_add, and every element after index moves, so pointers to them are stale. Costs time linear in the number of elements shifted.

ExampleOpen in playground ↗
list: [..] int;
array_add(*list, 1, 2, 4);
array_insert_at(*list, 3, 2);
assert(list.count == 4 && list[2] == 3 && list[3] == 4);
array_free(list);
array_insert_at :: (array: *[..] $T, item: T, index: int)

Inserts item so that it ends up at index, shifting the elements from there on up by one.

index may equal the count, which appends. Anything else out of range fails an assert. The array grows as in array_add, and every element after index moves, so pointers to them are stale. Costs time linear in the number of elements shifted.

Parameters

array: *[..] $T
item: T
index: int
ExampleOpen in playground ↗
list: [..] int;
array_add(*list, 1, 2, 4);
array_insert_at(*list, 3, 2);
assert(list.count == 4 && list[2] == 3 && list[3] == 4);
array_free(list);

See also