Manuscript Documentation
v2026.12.0

@std/collections/sorted.ms#

Import each required symbol with import Name from "@std/collections/sorted.ms".

SortedEntry Type#

One key-value pair of a SortedMap.

Manuscript
type SortedEntry[K Comparable, V] {
    key K
    value V
}

SortedEntry.key#

The entry’s key, which orders it within the map.

Manuscript
key K

SortedEntry.value#

The value stored for key.

Manuscript
value V

SortedItem Type#

One key or value of a sorted collection, viewed in place: it borrows the collection and copies nothing.

Manuscript
type SortedItem[T] {
    value T
}

SortedItem.value#

The key, value or member this item views; it belongs to the collection that returned it and is not copied.

Manuscript
value T

SortedMap Type#

A map kept in ascending key order, so lookups by position (first, last, floor, ceiling, range) are as cheap as lookups by key. Keys use their Comparable order; keys that compare equal are the same key. Replacing a key’s value keeps the original key.

It is a list of sorted chunks of at most 128 entries. get, contains, floor and ceiling take O(log n). add and remove locate the key and shift entries within its chunk; splitting or removing a chunk also updates the chunk lists. Use clone() for an independent deep copy. Use Map when you do not need sorted order and want average O(1) key lookup.

Manuscript
type SortedMap[K Comparable, V] {
    key_chunks List[List[K]] = []
    value_chunks List[List[V]] = []
    size Int = 0
}

SortedMap.key_chunks#

Sorted chunks of keys; no chunk is empty. Internal state: use the operations, not this field.

Manuscript
key_chunks List[List[K]] = []

SortedMap.size#

Number of entries. Internal state; read it with length().

Manuscript
size Int = 0

SortedMap.value_chunks#

Values parallel to key_chunks. Internal state.

Manuscript
value_chunks List[List[V]] = []

SortedMap.add#

Inserts a key or replaces the value of an equal key, which keeps its original key.

Manuscript
var fn add(move key K, move value V) Void

SortedMap.ceiling#

Returns the entry with the smallest key that is at least key as a view of this map, or null.

Manuscript
fn ceiling(key K) SortedRef[K, V]?

SortedMap.clear#

Removes every entry and releases the storage; the map is empty afterwards.

Manuscript
var fn clear() Void

SortedMap.contains#

Reports whether an equal key is present.

Manuscript
fn contains(key K) Bool

SortedMap.entries#

Returns the entries in ascending key order as views of this map: the list is new, the keys and values are not copied.

Manuscript
fn entries() List[SortedRef[K, V]]

SortedMap.first#

Returns the smallest key, or null when empty. Look its value up with get.

Manuscript
fn first() K?

SortedMap.floor#

Returns the entry with the largest key that is at most key as a view of this map, or null.

Manuscript
fn floor(key K) SortedRef[K, V]?

SortedMap.for_each#

Calls operation(key, value) for each entry in ascending key order. operation reads the entries in place, so it cannot change the map it is called on.

Manuscript
// Receiver: SortedMap[K, V]; K Comparable
fn for_each(operation fn(K, V) Void) Void

SortedMap.get#

Returns the value stored for an equal key as a view of this map, or null when absent.

Manuscript
fn get(key K) V?

SortedMap.get_or#

Returns the value stored for an equal key, or fallback when absent, as a view of whichever it is.

Manuscript
fn get_or(key K, fallback V) V

SortedMap.is_empty#

Reports whether the map has no entries.

Manuscript
fn is_empty() Bool

SortedMap.keys#

Returns the keys in ascending order as views of this map: the list is new, the keys are not copied. Read each key as item.value.

Manuscript
fn keys() List[SortedItem[K]]

SortedMap.last#

Returns the largest key, or null when empty. Look its value up with get.

Manuscript
fn last() K?

SortedMap.length#

Returns the number of entries.

Manuscript
fn length() Int

SortedMap.pop_first#

Removes the entry with the smallest key and moves it out, or returns null when empty.

Manuscript
var fn pop_first() SortedEntry[K, V]?

SortedMap.pop_last#

Removes the entry with the largest key and moves it out, or returns null when empty.

Manuscript
var fn pop_last() SortedEntry[K, V]?

SortedMap.range#

Returns the entries with low <= key < high, in ascending order, as views of this map: the list is new, the keys and values are not copied.

Manuscript
fn range(low K, high K) List[SortedRef[K, V]]

SortedMap.remove#

Removes an equal key; an absent key changes nothing.

Manuscript
var fn remove(key K) Void

SortedMap.values#

Returns the values in ascending key order as views of this map: the list is new, the values are not copied. Read each value as item.value.

Manuscript
fn values() List[SortedItem[V]]

SortedRef Type#

One key-value pair of a SortedMap, viewed in place: it borrows the map and copies nothing.

Manuscript
type SortedRef[K Comparable, V] {
    key K
    value V
}

SortedRef.key#

The entry’s key.

Manuscript
key K

SortedRef.value#

The value stored for key.

Manuscript
value V

SortedSet Type#

A set kept in ascending order, with the same Comparable ordering and costs as SortedMap. Members that compare equal are the same member. Use clone() for an independent deep copy.

Manuscript
type SortedSet[T Comparable] {
    members SortedMap[T, Bool] = SortedMap[T, Bool]()
}

SortedSet.members#

Members as the keys of a map. Internal state: use the operations, not this field.

Manuscript
members SortedMap[T, Bool] = SortedMap[T, Bool]()

SortedSet.add#

Adds a member; an equal member already present is kept.

Manuscript
var fn add(move value T) Void

SortedSet.ceiling#

Returns the smallest member that is at least value as a view of this set, or null.

Manuscript
fn ceiling(value T) T?

SortedSet.clear#

Removes every member and releases the storage; the set is empty afterwards.

Manuscript
var fn clear() Void

SortedSet.contains#

Reports whether an equal member is present.

Manuscript
fn contains(value T) Bool

SortedSet.first#

Returns the smallest member, or null when empty.

Manuscript
fn first() T?

SortedSet.floor#

Returns the largest member that is at most value as a view of this set, or null.

Manuscript
fn floor(value T) T?

SortedSet.is_empty#

Reports whether the set has no members.

Manuscript
fn is_empty() Bool

SortedSet.last#

Returns the largest member, or null when empty.

Manuscript
fn last() T?

SortedSet.length#

Returns the number of members.

Manuscript
fn length() Int

SortedSet.pop_first#

Removes and returns the smallest member, or null when empty.

Manuscript
var fn pop_first() T?

SortedSet.pop_last#

Removes and returns the largest member, or null when empty.

Manuscript
var fn pop_last() T?

SortedSet.range#

Returns the members with low <= member < high, in ascending order, as views of this set. Read each member as item.value.

Manuscript
fn range(low T, high T) List[SortedItem[T]]

SortedSet.remove#

Removes an equal member; an absent value changes nothing.

Manuscript
var fn remove(value T) Void

SortedSet.values#

Returns the members in ascending order as views of this set. Read each member as item.value.

Manuscript
fn values() List[SortedItem[T]]