← Standard library

List

sealed class List<T>
import List from Wax;

A growable, contiguous collection. Adding elements expands its capacity as needed.

Use WithCapacity when the expected size is known. size counts initialized elements; capacity counts the available slots. Spans view the current backing storage and do not follow a later reallocation.

import List from Wax;
api fn Main() : int32 {
    var values = new List<int32>();
    values.Add(3);
    values.Add(1);
    values.Sort();
    return values[0];
}

List.wax:35

Inherits from object. Follow these links for inherited members.

Members

constructorconstructor

public constructor ()

Creates an empty collection. No elements are inserted.

List.wax:43

WithCapacityconstructor

public constructor WithCapacity (int32 capacity)

Creates an empty collection with reserved storage. Capacity reservation does not change the element count.

List.wax:52

sizegetter

public get size() : int32

The number of live elements; valid element indices are [0, size). Capacity is not part of the readable range.

List.wax:69

capacitygetter

public get capacity() : int32

The number of elements the current backing storage can hold before growth.

List.wax:76

EnsureCapacitymethod

public fn EnsureCapacity(int32 capacity) : void

Ensures room for at least capacity elements without changing size.

List.wax:107

EnsureAdditionalCapacitymethod

public fn EnsureAdditionalCapacity(int32 additional) : void

Reserves space for the current element count plus additional. Does not insert elements.

List.wax:122

Addmethod

public fn Add(T item) : void

Appends one element, growing the backing storage when necessary.

List.wax:127

thisindexer-getter

public get this(int32 index) : T

Reads or replaces a live element at a zero-based index. An index outside [0, size) panics. Replacing an element does not change the collection size.

List.wax:145

thisindexer-setter

public set this(int32 index, T value) : void

Reads or replaces a live element at a zero-based index. An index outside [0, size) panics. Replacing an element does not change the collection size.

List.wax:153

ToSpanmethod

public fn ToSpan() : Span<T>

Borrows a mutable view of the current live elements without copying. The view has a fixed length. Writes affect shared storage; it does not follow a later collection reallocation.

List.wax:161

ToReadOnlySpanmethod

public fn ToReadOnlySpan() : ReadOnlySpan<T>

Borrows the current live elements without copying or permitting writes through this view. Other aliases can still mutate the storage; the view does not follow a later reallocation.

List.wax:169

AddRangemethod

public fn AddRange(scoped ReadOnlySpan<T> items) : void

Appends source elements in order, growing storage as needed. Source views over the old backing remain valid during growth. An empty source does nothing.

List.wax:177

InsertAtmethod

public fn InsertAt(int32 index, T item) : void

Inserts at an index between zero and size, shifting later elements right. Invalid indices panic. Inserting at size appends.

List.wax:194

RemoveAtmethod

public fn RemoveAt(int32 index) : void

Removes an in-range element and shifts the surviving tail left. Invalid indices panic.

List.wax:216

Clearmethod

public fn Clear() : void

Removes all live elements while keeping the backing capacity.

List.wax:226

RemoveAllmethod

public fn RemoveAll(fn<(T) : bool> predicate) : int32

Removes items for which the predicate returns true, preserving survivor order, and returns the number removed. The predicate must not structurally modify this list.

List.wax:235

ToArraymethod

public fn ToArray() : T[]

Returns a separate array snapshot of the live elements. Reference elements still point to the same objects.

List.wax:255

Slicemethod

public fn Slice(int32 start, int32 len) : Span<T>

Borrows a mutable window [start, start + len). Negative arguments or a range outside the live elements panic. An empty slice at size is valid.

List.wax:266

Sortmethod

public fn Sort() : void where T is Comparable

Sorts in place using the element comparison. The ordering is deterministic but not stable.

List.wax:273

SortBymethod

public fn SortBy(fn<(T,T) : int32> compare) : void

Sorts in place using a comparator returning a negative value, zero, or a positive value for less, equal, or greater. The comparator must provide a consistent ordering. Equal elements may be reordered.

List.wax:282

SortStablemethod

public fn SortStable() : void where T is Comparable

Sorts in place while preserving the order of equal elements.

List.wax:289

SortStableBymethod

public fn SortStableBy(fn<(T,T) : int32> compare) : void

Sorts in place with a consistent negative/zero/positive comparator, preserving the original order of equal elements. Larger inputs allocate a scratch array.

List.wax:297

Containsmethod

public fn Contains(T item) : bool where T is Equatable

Returns whether the collection contains an equal element.

List.wax:302

IndexOfmethod

public fn IndexOf(T item) : int32 where T is Equatable

Returns the first matching index, or -1 when the element is absent.

List.wax:307

LastIndexOfmethod

public fn LastIndexOf(T item) : int32 where T is Equatable

Returns the last matching element index using Equals, or -1 when absent, including an empty view.

List.wax:315

Fillmethod

public fn Fill(T value) : void

Assigns value to every element of the backing window. An empty view does nothing. Reference elements all receive the same reference.

List.wax:323

FillRangemethod

public fn FillRange(T value, int32 start, int32 count) : void

Fills [start, start + count) with value. Both endpoints must lie in [0, size]; invalid endpoints panic before any write. An empty or reversed range whose endpoints are valid performs no writes.

List.wax:332

CopyTomethod

public fn CopyTo(Span<T> dest) : void

Copies the first min(size, dest.length) elements into dest, safely handling overlapping views. A shorter destination truncates the copy without a panic; any remaining destination elements are unchanged. An empty source or destination does nothing. Reference elements copy their references, not the referenced objects.

List.wax:342

Reversemethod

public fn Reverse() : void

Reverses the element order in place.

List.wax:347

BinarySearchmethod

public fn BinarySearch(T item) : int32 where T is Comparable

Searches a view already sorted ascending under CompareTo. Returns a matching element index; when duplicates exist, the chosen match is unspecified. If absent, returns ~insertionIndex; apply ~ again to recover the insertion position. An empty view returns -1 (~0).

List.wax:357

Removemethod

public fn Remove(T item) : bool where T is Equatable

Removes the first element equal to the item and returns true, or returns false if no match exists.

List.wax:365