crow/col/mut-array.crow (source)

mut-array[t] builtin

Mutable array type that can change in size.
This is represented as a reference to a mut-slice with extra capacity.
Appending (or removing) elements to the end is amortized O(1),
but appending (or removing) from the beginning is always O(N).

size[t] nat64(a t mut[]) bare

subscript[t] t(a t mut[], index nat64) bare

set-subscript[t] void(a t mut[], index nat64, value t) bare

==[t] bool(a t mut[], b t mut[]) bare

True if they are identical.

begin-pointer[t] t mut*(a t mut[]) bare, unsafe

Pointer to the first element.
Equivalent to a.temp-as-mut-slice.begin-pointer.

end-pointer[t] t mut*(a t mut[]) bare, unsafe

Pointer to one past the last element.
Equivalent to a.temp-as-mut-slice.end-pointer.

new[t] t mut[](...a t array)

Creates a new list with the given elements.

to[t] t mut[](a t array)

clear[t] void(a t mut[]) bare

reduce-size-to[t] void(a t mut[], new-size nat64) bare

copy[t] t mut[](a t mut[])

Copies the array.
Writes to the original won't affect the copy.

to[t] t array(a t mut[])

Copies a mut-array to an array.
Writes to the mut-array won't affect the array.

move-to[t] t array(a t mut[]) bare

'move-to' is like 'to' but sets the input to empty.

move-to[t] t mut-slice(a t mut[]) bare

move-to[t] t mut[](a t mut[])

swap[t] void(a t mut[], b t mut[])

temp-as-mut-slice[t] t mut-slice(a t mut[]) bare, unsafe

Returns a mut-slice of all elements currently in the mut-array.

Since a mut-array may re-allocate its underlying buffer,
it's unsafe to use this after the size of a changes.

temp-as-array[t] t array(a t mut[]) bare, unsafe

Converts to an array.
"Temp" because it's UB if the result is used after a is written to.

is-empty[t] bool(a t mut[]) bare

True iff a.size == 0.

pop-n-from-start[t] t array(a t mut[], n nat64)

Removes the first n elements and returns them as an array.
This is O(1) in native code since it simply gives up the memory.
Throws if n > a.size.

peek[t] t?(a t mut[])

pop[t] t?(a t mut[]) bare

Removes the last element and returns it.
Returns none if a is empty.

pop-n[t] void(a t mut[], n nat64)

Pops n times, discarding the result.
Throws if n > a.size.

prepend=[t] void(a t mut[], value t)

~=[t] void(a t mut[], value t)

Push a value onto the end of the list.

~~=[t, col] void(a t mut[], values col) (col, t) iterate

Push multiple values onto the end of the list.

gc-safe-mut-array[t] t mut[](size nat64) unsafe

Create a new mut-array full of gc-safe-value.

push-gc-safe-values[t] void(a t mut[], n nat64) unsafe

Pushes n of gc-safe-value to the mut-array.
This can be useful to prepare the array for being written by low-level code.
Unsafe for the same reason as gc-safe-value.

insert-at[t] void(a t mut[], index nat64, value t)

Sets a[index] := value,
but first shifts all elements from index onwards to the right to make room.
Increases the size by 1.
This is O(a.size - index) due to having to move other elements.
Throws if index > a.size.

remove-at[t] t(a t mut[], index nat64)

Removes a[index], and shifts all values from index onwards to the left to fill the hole.
Returns the old a[index].
Decreases the size by 1.
This is O(a.size - index) due to having to move other elements.
Throws if index >= a.size.

filter=[t] void(a t mut[], f mut bool(t))

Removes every element x where !f[x].
This can be written as a.filter := f.

iterate[t] bool(a t mut[], f mut bool(t))

mut-array-iterator[t] record mut

begin[t] t mut-array-iterator(a t mut[]) bare

end[t] t mut-array-iterator(a t mut[]) bare

prev[t] (t mut-array-iterator, t) tuple2?(a t mut-array-iterator) bare

next[t] (t, t mut-array-iterator) tuple2?(a t mut-array-iterator) bare

+[t] t mut-array-iterator(a t mut-array-iterator, n nat64) bare

-[t] t mut-array-iterator(a t mut-array-iterator, n nat64) bare

-[t] nat64(a t mut-array-iterator, b t mut-array-iterator) bare

set-prev[t] void(a t mut-array-iterator, value t) bare

set-next[t] void(a t mut-array-iterator, value t) bare

slice[t] t mut[](begin t mut-array-iterator, end t mut-array-iterator)

build[t] t mut[](a build-options, f mut void(t mut[]))