Skip to content

std.collections

type SortedDict[K, V] = { root: Tree[K, V] = Leaf, size: Int = 0 }

Ordered dictionary: an AVL tree keyed by compare.

  • get, insert, remove, first, last, floor, ceiling: O(log n). Iterates in

key order.

  • SortedDict {} is empty. sorted_dict(pairs) builds one from pairs.
  • A change is in place when nothing else holds the dictionary. Otherwise

it copies the O(log n) nodes on the path to the key.

  • == compares the entries in key order, whatever the trees’ shapes.

Inside a list or a dictionary, which compare part by part, it compares the shapes too.

type Tree[K, V] =
| Leaf
| Node { key: K, value: V, left: Tree[K, V], right: Tree[K, V], height: Int }

An AVL tree of entries by key. A node’s two subtrees differ in height by at most one, so a path from the root has at most 1.44 log2(n) nodes.

type SortedSet[T] = { root: Tree[T, ()] = Leaf, size: Int = 0 }

Ordered set: an AVL tree keyed by compare. contains, insert, remove: O(log n). Iterates in order. SortedSet {} is empty. sorted_set(xs) builds one from a list. == compares the elements in order, whatever the trees’ shapes (inside a list or a dictionary, the shapes too).

type Heap[T] = { items: List[T] = [], order: Option[(T, T) -> Int] = None }

Priority queue: a binary heap in a List, smallest first. push, pop: O(log n). peek: O(1). Ordered by order (-1, 0 or 1, as compare gives), else by compare. Heap {} is empty. heap(xs) builds one from a list. A change copies the list when something else holds it.

fn len(self) -> Int

The number of entries. Cost: O(1).

fn is_empty(self) -> Bool

Whether it has no entries. Cost: O(1).

fn contains(self, k: K) -> Bool
ensures it == (find(self.root, k) != None)

Whether k is one of its keys. Cost: O(log n) comparisons.

fn get(self, k: K) -> Option[V]
ensures it == find(self.root, k)

The value of k, if it is one of its keys. Cost: O(log n) comparisons.

fn insert(mut self, k: K, v: V)
ensures self.size == old self.size
+ if find(old self.root, k) == None {
1
} else {
0
}
ensures count(self.root) == count(old self.root)
+ if find(old self.root, k) == None {
1
} else {
0
}

k holding v (replacing its value if it has one). Cost: O(log n) comparisons and new nodes.

fn remove(mut self, k: K) -> Option[V]
ensures it == find(old self.root, k)
ensures self.size == old self.size - if it == None {
0
} else {
1
}
ensures count(self.root) == count(old self.root) - if it == None {
0
} else {
1
}

k taken out: its value, if it was one of the keys. Cost: O(log n) comparisons and new nodes.

fn first(self) -> Option[(K, V)]

The entry of the smallest key. Cost: O(log n).

fn last(self) -> Option[(K, V)]

The entry of the largest key. Cost: O(log n).

fn pop_first(mut self) -> Option[(K, V)]
ensures self.size == old self.size - if it == None {
0
} else {
1
}
ensures count(self.root) == count(old self.root) - if it == None {
0
} else {
1
}

The entry of the smallest key, taken out. Cost: O(log n).

fn pop_last(mut self) -> Option[(K, V)]
ensures self.size == old self.size - if it == None {
0
} else {
1
}
ensures count(self.root) == count(old self.root) - if it == None {
0
} else {
1
}

The entry of the largest key, taken out. Cost: O(log n).

fn floor(self, k: K) -> Option[(K, V)]

The entry of the largest key at or below k. Cost: O(log n) comparisons.

fn ceiling(self, k: K) -> Option[(K, V)]

The entry of the smallest key at or above k. Cost: O(log n) comparisons.

fn before(self, k: K) -> Option[(K, V)]

The entry of the largest key strictly below k. Cost: O(log n) comparisons.

fn after(self, k: K) -> Option[(K, V)]

The entry of the smallest key strictly above k. Cost: O(log n) comparisons.

fn between(self, lo: K, hi: K) -> List[(K, V)]

The entries whose keys are from lo up to (not including) hi, in order. Cost: O(log n + m) for m entries given.

fn items(self) -> List[(K, V)]

The entries, in their keys’ order. Cost: O(n).

fn keys(self) -> List[K]

The keys, in order. Cost: O(n).

fn values(self) -> List[V]

The values, in their keys’ order. Cost: O(n).

fn len(self) -> Int

The number of elements. Cost: O(1).

fn is_empty(self) -> Bool

Whether it has no elements. Cost: O(1).

fn contains(self, x: T) -> Bool
ensures it == (find(self.root, x) != None)

Whether x is one of its elements. Cost: O(log n) comparisons.

fn insert(mut self, x: T)
ensures self.size == old self.size
+ if find(old self.root, x) == None {
1
} else {
0
}
ensures count(self.root) == count(old self.root)
+ if find(old self.root, x) == None {
1
} else {
0
}

x added (nothing changes if it is already there). Cost: O(log n) comparisons and new nodes.

fn remove(mut self, x: T) -> Bool
ensures it == (find(old self.root, x) != None)
ensures self.size == old self.size - if it {
1
} else {
0
}
ensures count(self.root) == count(old self.root) - if it {
1
} else {
0
}

x taken out: whether it was there. Cost: O(log n) comparisons and new nodes.

fn first(self) -> Option[T]

The smallest element. Cost: O(log n).

fn last(self) -> Option[T]

The largest element. Cost: O(log n).

fn pop_first(mut self) -> Option[T]
ensures self.size == old self.size - if it == None {
0
} else {
1
}
ensures count(self.root) == count(old self.root) - if it == None {
0
} else {
1
}

The smallest element, taken out. Cost: O(log n).

fn pop_last(mut self) -> Option[T]
ensures self.size == old self.size - if it == None {
0
} else {
1
}
ensures count(self.root) == count(old self.root) - if it == None {
0
} else {
1
}

The largest element, taken out. Cost: O(log n).

fn floor(self, x: T) -> Option[T]

The largest element at or below x. Cost: O(log n) comparisons.

fn ceiling(self, x: T) -> Option[T]

The smallest element at or above x. Cost: O(log n) comparisons.

fn before(self, x: T) -> Option[T]

The largest element strictly below x. Cost: O(log n) comparisons.

fn after(self, x: T) -> Option[T]

The smallest element strictly above x. Cost: O(log n) comparisons.

fn between(self, lo: T, hi: T) -> List[T]

The elements from lo up to (not including) hi, in order. Cost: O(log n + m) for m elements given.

fn to_list(self) -> List[T]

The elements, in order. Cost: O(n).

fn by[B](key: (T) -> B) -> Heap[T]

An empty heap, smallest first by key’s values. Cost: O(1); each comparison then calls key twice.

fn with(order: (T, T) -> Int) -> Heap[T]

An empty heap, smallest first by order (-1, 0 or 1, as compare gives): Heap.with((a, b) -> compare(b, a)) is largest first. Cost: O(1).

fn less(self, a: T, b: T) -> Bool

Whether a comes before b. Cost: one comparison.

fn len(self) -> Int

The number of elements. Cost: O(1).

fn is_empty(self) -> Bool

Whether it has no elements. Cost: O(1).

fn peek(self) -> Option[T]

The smallest element. Cost: O(1).

fn push(mut self, x: T)

x added. Cost: O(log n) comparisons (O(n) when the list is shared).

fn pop(mut self) -> Option[T]

The smallest element, taken out. Cost: O(log n) comparisons (O(n) when the list is shared).

fn sift_down(mut self, start: Int, x: T)

x put at i (or below, as far as it goes), the elements on its way moved up. Cost: O(log n) comparisons.

fn to_list(self) -> List[T]

The elements, smallest first. Cost: O(n log n).

fn sorted_dict[K, V](pairs: List[(K, V)]) -> SortedDict[K, V]

A SortedDict of the pairs (a later pair of the same key wins). Cost: O(n log n).

fn sorted_set[T](xs: List[T]) -> SortedSet[T]

A SortedSet of the elements of xs. Cost: O(n log n).

fn heap[T](xs: List[T]) -> Heap[T]

A heap of the elements of xs, smallest first by their order. Cost: O(n).

  • impl[K, V] Eq for SortedDict[K, V]: The same entries in key order.
  • impl[T] Eq for SortedSet[T]: The same elements in order.