@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.
type SortedEntry[K Comparable, V] {
key K
value V
}SortedEntry.key#
The entry’s key, which orders it within the map.
key KSortedEntry.value#
The value stored for key.
value VSortedItem Type#
One key or value of a sorted collection, viewed in place: it borrows the collection and copies nothing.
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.
value TSortedMap 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.
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.
key_chunks List[List[K]] = []SortedMap.size#
Number of entries. Internal state; read it with length().
size Int = 0SortedMap.value_chunks#
Values parallel to key_chunks. Internal state.
value_chunks List[List[V]] = []SortedMap.add#
Inserts a key or replaces the value of an equal key, which keeps its original key.
var fn add(move key K, move value V) VoidSortedMap.ceiling#
Returns the entry with the smallest key that is at least key as a view of this map,
or null.
fn ceiling(key K) SortedRef[K, V]?SortedMap.clear#
Removes every entry and releases the storage; the map is empty afterwards.
var fn clear() VoidSortedMap.contains#
Reports whether an equal key is present.
fn contains(key K) BoolSortedMap.entries#
Returns the entries in ascending key order as views of this map: the list is new, the keys and values are not copied.
fn entries() List[SortedRef[K, V]]SortedMap.first#
Returns the smallest key, or null when empty. Look its value up with get.
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.
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.
// Receiver: SortedMap[K, V]; K Comparable
fn for_each(operation fn(K, V) Void) VoidSortedMap.get#
Returns the value stored for an equal key as a view of this map, or null when
absent.
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.
fn get_or(key K, fallback V) VSortedMap.is_empty#
Reports whether the map has no entries.
fn is_empty() BoolSortedMap.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.
fn keys() List[SortedItem[K]]SortedMap.last#
Returns the largest key, or null when empty. Look its value up with get.
fn last() K?SortedMap.length#
Returns the number of entries.
fn length() IntSortedMap.pop_first#
Removes the entry with the smallest key and moves it out, or returns null when
empty.
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.
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.
fn range(low K, high K) List[SortedRef[K, V]]SortedMap.remove#
Removes an equal key; an absent key changes nothing.
var fn remove(key K) VoidSortedMap.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.
fn values() List[SortedItem[V]]SortedRef Type#
One key-value pair of a SortedMap, viewed in place: it borrows the map and copies nothing.
type SortedRef[K Comparable, V] {
key K
value V
}SortedRef.key#
The entry’s key.
key KSortedRef.value#
The value stored for key.
value VSortedSet 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.
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.
members SortedMap[T, Bool] = SortedMap[T, Bool]()SortedSet.add#
Adds a member; an equal member already present is kept.
var fn add(move value T) VoidSortedSet.ceiling#
Returns the smallest member that is at least value as a view of this set, or
null.
fn ceiling(value T) T?SortedSet.clear#
Removes every member and releases the storage; the set is empty afterwards.
var fn clear() VoidSortedSet.contains#
Reports whether an equal member is present.
fn contains(value T) BoolSortedSet.first#
Returns the smallest member, or null when empty.
fn first() T?SortedSet.floor#
Returns the largest member that is at most value as a view of this set, or null.
fn floor(value T) T?SortedSet.is_empty#
Reports whether the set has no members.
fn is_empty() BoolSortedSet.last#
Returns the largest member, or null when empty.
fn last() T?SortedSet.length#
Returns the number of members.
fn length() IntSortedSet.pop_first#
Removes and returns the smallest member, or null when empty.
var fn pop_first() T?SortedSet.pop_last#
Removes and returns the largest member, or null when empty.
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.
fn range(low T, high T) List[SortedItem[T]]SortedSet.remove#
Removes an equal member; an absent value changes nothing.
var fn remove(value T) VoidSortedSet.values#
Returns the members in ascending order as views of this set. Read each member as
item.value.
fn values() List[SortedItem[T]]