Skip to content
Standard library

velt/collections#

import { Deque, PriorityQueue, SortedMap, BTreeMap, ConcurrentMap } from "velt/collections". Containers beyond the prelude's arrays and Map, written in Velt on public APIs. Every type is exported from velt/collections itself; the files behind it are internal, and importing one (velt/collections/deque) is an error that names the module to use.

TypeWhat it isUse it for
Deque<T>a double-ended queue on a ring buffer: O(1) at both ends and by indexwork queues, sliding windows, BFS
PriorityQueue<T>a binary heap: pop returns what cmp orders firstschedulers, Dijkstra, top-k
SortedMap<K, V>a map ordered by key, on sorted arrayssmall maps that are read far more than written
BTreeMap<K, V>a map ordered by key, as a B+ treelarge ordered maps, range queries
ConcurrentMap<K, V>a map many threads read and write at onceshared caches, counters and indexes across tasks
Set<T>an insertion-ordered set of unique values, also a global as in JavaScriptmembership, deduplication, set algebra
ts
import { Deque, PriorityQueue, BTreeMap } from "velt/collections";

const queue = Deque.from([1, 2, 3]);
queue.pushFront(0);
console.log(queue.popBack(), queue.toArray()); // 3 [ 0, 1, 2 ]

const heap = new PriorityQueue<i64>((a, b) => a - b);
heap.push(5);
heap.push(1);
heap.push(4);
console.log(heap.pop(), heap.pop()); // 1 4

const index = new BTreeMap<string, i64>();
index.set("pear", 3);
index.set("apple", 1);
console.log(index.keys(), index.ceilingKey("b")); // [ 'apple', 'pear' ] pear

For a plain key-value map, use the prelude's Map: it is a hash map in insertion order, as in JavaScript. Reach for SortedMap or BTreeMap when you need the keys in order (first, floorKey, range).

Edit this page on GitHub