Module: collections.list

Source: ./collections/list.reef


Overview

collections/list - Generic List[T] collection

Provides a dynamic array-like collection with generic type support. Capacity grows automatically on append/insert when full (double capacity, minimum 1), matching collections.growable / text.stringbuilder. Bool returns are true on success; false is only for hard errors (e.g. invalid index on insert/set) — not for "full".

Factory helpers create_list() / create_list_cap() still set an initial capacity; growth reallocates via new [T](n) when that capacity is exceeded.

Usage: let nums: List[int] = create_int_list() append[:int](nums, 42) let value = get[:int](nums, 0) // Some(42) -- no annotation needed, infers Option[int]


Types

List

List data structure - generic struct with typed array

Fields:

Name Type
items [T]
len int
cap int

Functions

fn append(list: List[T], item: T): bool

Appends an item to the end of the list. Auto-grows when full. Returns true on success (false only on hard failure).

fn get(list: List[T], index: int): option.Option[T]

Gets an item at the given index Returns Some(elem) if index is in bounds, None if out of bounds.

fn set(list: List[T], index: int, item: T): bool

Sets an item at the given index Returns true on success, false if index out of bounds

fn list_length(list: List[T]): int

Returns the number of items in the list

fn list_capacity(list: List[T]): int

Returns the current capacity of the list

fn is_empty(list: List[T]): bool

Returns true if the list is empty

fn pop(list: List[T]): option.Option[T]

Removes and returns the last item from the list Returns Some(elem) if non-empty, None if empty.

fn last(list: List[T]): option.Option[T]

Returns the last item without removing it Returns Some(elem) if non-empty, None if empty.

fn insert(list: List[T], index: int, item: T): bool

Inserts an item at the given index, shifting subsequent items right. Auto-grows when full. Returns true on success, false if index is invalid.

fn remove_at(list: List[T], index: int): bool

Removes the item at the given index, shifting subsequent items left Returns true on success, false if invalid index

fn list_to_array(list: List[T], arr: [T], max: int): int

Copies list elements to an array for iteration Returns the number of elements copied

fn create_int_list(): List[int]

Creates an empty int list with capacity 16

fn create_string_list(): List[string]

Creates an empty string list with capacity 16

fn create_float_list(): List[float]

Creates an empty float list with capacity 16

fn create_bool_list(): List[bool]

Creates an empty bool list with capacity 16

fn create_int_list_cap(cap: int): List[int]

Creates an int list with specified capacity (8, 16, 32, or 64) Falls back to 16 for unsupported capacities

fn create_string_list_cap(cap: int): List[string]

Creates a string list with specified capacity (8, 16, 32, or 64)


Procedures

proc grow_list(list: List[T])

Grow backing storage to at least 2x current capacity (min 1). Called from append/insert when len >= cap.

proc clear(list: List[T])

Clears all items from the list (resets length to 0)


Generated by reefc doc