Skip to content
Draft

collections

The collections module provides advanced data structures and algorithms.

import collections
import { Queue, Stack, LinkedList } from collections
@import collections#LinkedList
@list = @LinkedList<Int>::new()
@list#push_front(1)
@list#push_back(2)
@list#push_back(3)
@value = @list#pop_front() @// 1
@list#length @// 2
Method Description
new() Create new list
push_front(val) Add to front
push_back(val) Add to back
pop_front() Remove from front
pop_back() Remove from back
peek_front() View front
peek_back() View back
insert_at(idx, val) Insert at index
remove_at(idx) Remove at index
length Get length
is_empty Check if empty
@import collections#Stack
@stack = @Stack<Int>::new()
@stack#push(1)
@stack#push(2)
@stack#push(3)
@top = @stack#peek() @// 3
@value = @stack#pop() @// 3
@stack#is_empty @// false
@stack#length @// 2
Method Description
new() Create new stack
push(val) Push value
pop() Pop value
peek() View top
length Get size
is_empty Check if empty
@import collections#Queue
@queue = @Queue<String>::new()
@queue#enqueue("first")
@queue#enqueue("second")
@queue#enqueue("third")
@value = @queue#dequeue() @// "first"
@queue#peek() @// "second"
@queue#length @// 2
Method Description
new() Create new queue
enqueue(val) Add to back
dequeue() Remove from front
peek() View front
length Get size
is_empty Check if empty
@import collections#PriorityQueue
@pq = @PriorityQueue<Task>::new()
@pq#push("low", 1)
@pq#push("high", 10)
@pq#push("medium", 5)
@value = @pq#pop() @// "high" (highest priority)
@import collections#Deque
@deque = @Deque<Int>::new()
@deque#push_front(1)
@deque#push_back(2)
@deque#push_front(0)
@deque#pop_back() @// 2
@deque#pop_front() @// 0
@import collections#HashSet
@set = @HashSet<Int>::new()
@set#add(1)
@set#add(2)
@set#add(3)
@set#contains(2) @// true
@set#remove(2)
@set#length @// 2
@a = @HashSet<Int>::from([1, 2, 3])
@b = @HashSet<Int>::from([2, 3, 4])
@a#union(@b) @// {1, 2, 3, 4}
@a#intersection(@b) @// {2, 3}
@a#difference(@b) @// {1}
@import collections#HashMap
@map = @HashMap<String, Int>::new()
@map#insert("one", 1)
@map#insert("two", 2)
@value = @map#get("one") @// Option<Int>
@map#contains_key("one") @// true
@map#remove("one")
@map#length @// 1
@import collections#TreeMap
@tree = @TreeMap<String, Int>::new()
@tree#insert("c", 3)
@tree#insert("a", 1)
@tree#insert("b", 2)
@keys = @tree#keys() @// ["a", "b", "c"] (sorted)
@import collections#BinaryTree
@tree = @BinaryTree<Int>::new()
@tree#insert(5)
@tree#insert(3)
@tree#insert(7)
@tree#contains(3) @// true
@tree#in_order() @// [3, 5, 7]
@tree#pre_order() @// [5, 3, 7]
@tree#post_order() @// [3, 7, 5]
@tree#level_order() @// [5, 3, 7]
@import collections#Heap
@heap = @Heap<Int>::new()
@heap#push(3)
@heap#push(1)
@heap#push(2)
@min = @heap#pop() @// 1 (min-heap)
@max_heap = @Heap<Int>::new_max()
@max_heap#push(3)
@max_heap#push(1)
@max_heap#push(2)
@max = @max_heap#pop() @// 3
@import collections#Trie
@trie = @Trie::new()
@trie#insert("hello")
@trie#insert("help")
@trie#insert("world")
@trie#contains("hello") @// true
@trie#starts_with("hel") @// true
@trie#autocomplete("hel") @// ["hello", "help"]
@import collections#sort
@arr = [3, 1, 4, 1, 5, 9, 2, 6]
@sorted = @sort#quick(@arr)
@// [1, 1, 2, 3, 4, 5, 6, 9]

Available sorts: quick, merge, heap, insertion, bubble.

@import collections#search
@arr = [1, 2, 3, 4, 5]
@search#linear(@arr, 3) @// 2
@search#binary(@arr, 3) @// 2