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

class
List

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_get
class
ListCursor

A thin wrapper around a list for code that reads it by index.

    lst ref
class
LongList

A growable array of long: the same structure as List for numbers.

    data long[]
    cap long
    length long

Functions

def list_new() -> List

A new empty list. No buffer is allocated until the first push.

def list_new_cap(initial_cap: long) -> List

A new empty list with room for initial_cap elements, avoiding the doubling steps when the final size is known.

def list_length(lst: List) -> long

Number of elements.

def list_is_empty(lst: List) -> int

1 if the list has no elements.

def list_grow(lst: List, min_extra: long) -> None

Ensures room for min_extra more elements, doubling the buffer when needed.

def list_push(lst: List, value: ref) -> None

Appends a value. O(1) amortised; the list holds a counted reference to it.

def list_push_first(lst: List, value: ref) -> None

Inserts at the front, shifting every element: O(n).

def list_pop_back(lst: List) -> ref

Removes and returns the last element. Undefined on an empty list.

def list_pop_front(lst: List) -> ref

Removes and returns the first element, shifting the rest: O(n). Undefined on an empty list.

def list_peek_back(lst: List) -> ref

The last element, without removing it. Undefined on an empty list.

def list_peek_front(lst: List) -> ref

The first element, without removing it. Undefined on an empty list.

def list_get(lst: List, index: long) -> ref

The element at index (0-based). The index must be below list_length; only the underlying array's bounds check protects a wrong one.

def list_set(lst: List, index: long, value: ref) -> None

Overwrites the element at index.

def list_clear(lst: List) -> None

Removes every element keeping the buffer for reuse.

def list_get_range(lst: List, start: long, end: long) -> List

A new list with the elements in [start, end).

def list_merge(lst1: List, lst2: List) -> List

A new list with the elements of lst1 followed by those of lst2; neither input is modified.

def list_cursor_new(lst: List) -> ListCursor

A cursor over lst.

def list_cursor_get(cur: ListCursor, index: long) -> ref

The element at index through the cursor.

def list_cursor_set(cur: ListCursor, index: long, value: ref) -> None

Overwrites the element at index through the cursor.

def list_cpy(lst1: List, lst2: List) -> None

Copies the elements of lst2 into lst1; afterwards they share no buffer.

def llist_new() -> LongList

An empty LongList; nothing is allocated until the first push.

def llist_grow(lst: LongList, min_extra: long) -> None

Makes room for min_extra more elements, doubling the buffer.

def llist_length(lst: LongList) -> long

The number of elements.

def llist_push(lst: LongList, value: long) -> None

Appends a number, O(1) amortised.

def llist_get(lst: LongList, index: long) -> long

The element at index (must be below llist_length).

def llist_set(lst: LongList, index: long, value: long) -> None

Overwrites the element at index.

def llist_pop_back(lst: LongList) -> long

Removes and returns the last element; undefined on an empty list.

def llist_clear(lst: LongList) -> None

Empties the list, keeping its capacity.