libstd/list
A growable array of ref with amortised O(1) append: any pointer (a str, an object, an array) is pushed as is and recovered with as str / as ClassName (a checked downcast); the list holds a counted reference to each value, released when the value is removed or overwritten, or when the list goes. LongList (llist_*) is the same structure over long. See Lists & dicts.
#include <libstd/list>
Example
#include <libstd/stdio>
#include <libstd/list>
l = list_new()
list_push(l, "ten")
list_push(l, "twenty")
i = 0 as long
while i < list_length(l):
print(list_get(l, i) as str, " ")
i = i + 1 as long
print("\n") // ten twenty
Classes
The list handle: a ref[] buffer, its capacity and the number of elements in use. The methods push and get are another way to write list_push and list_get: l.get(i) is list_get(l, i), at the same cost.
data ref[]
cap long
length long
push &list_push
get &list_getA thin wrapper around a list for code that reads it by index.
lst refA growable array of long: the same structure as List for numbers.
data long[]
cap long
length longFunctions
A new empty list. No buffer is allocated until the first push.
A new empty list with room for initial_cap elements, avoiding the doubling steps when the final size is known.
Number of elements.
1 if the list has no elements.
Ensures room for min_extra more elements, doubling the buffer when needed.
Appends a value. O(1) amortised; the list holds a counted reference to it.
Inserts at the front, shifting every element: O(n).
Removes and returns the last element. Undefined on an empty list.
Removes and returns the first element, shifting the rest: O(n). Undefined on an empty list.
The last element, without removing it. Undefined on an empty list.
The first element, without removing it. Undefined on an empty list.
The element at index (0-based). The index must be below list_length; only the underlying array's bounds check protects a wrong one.
Overwrites the element at index.
Removes every element keeping the buffer for reuse.
A new list with the elements in [start, end).
A new list with the elements of lst1 followed by those of lst2; neither input is modified.
A cursor over lst.
The element at index through the cursor.
Overwrites the element at index through the cursor.
Copies the elements of lst2 into lst1; afterwards they share no buffer.
An empty LongList; nothing is allocated until the first push.
Makes room for min_extra more elements, doubling the buffer.
The number of elements.
Appends a number, O(1) amortised.
The element at index (must be below llist_length).
Overwrites the element at index.
Removes and returns the last element; undefined on an empty list.
Empties the list, keeping its capacity.