heapqueue
nimony/lib/std/heapqueue.nim
A min-heap (priority queue) implemented as a binary heap over a seq.
The smallest element (per <) is always at index 0, so pop returns elements in ascending order. The element type must be Comparable.
type HeapQueue = object data: seq[T]
func initHeapQueue(): HeapQueue[T]Creates a new empty heap.
func len(heap: HeapQueue[T]): int64Number of elements in
heap.func [](heap: HeapQueue[T]; i: int64): var TAccesses the
i-th element ofheap's internal array.heap[0]is the smallest element.proc push(heap: var HeapQueue[T]; item: sink T)Pushes
itemontoheap, keeping the heap invariant.proc pop(heap: var HeapQueue[T]): TRemoves and returns the smallest element of
heap. Requires a non-empty heap.proc clear(heap: var HeapQueue[T])Removes all elements from
heap.proc toHeapQueue(xs: openArray[T]): HeapQueue[T]Builds a heap from the elements of
xs.iterator items(heap: HeapQueue[T]): var TYields each element of
heapin its internal (heap-array) order.func find(heap: HeapQueue[T]; x: T): int64Linear scan for
x; returns its internal index, or -1 if absent.func contains(heap: HeapQueue[T]; x: T): boolWhether
xis inheap(shortcut forfind(heap, x) >= 0).proc del(heap: var HeapQueue[T]; index: int64)Removes the element at internal
index, keeping the heap invariant.proc replace(heap: var HeapQueue[T]; item: sink T): TPops and returns the smallest element, then pushes
item— more efficient thanpop+push. Requires a non-empty heap. The returned value may be larger thanitem.proc pushpop(heap: var HeapQueue[T]; item: sink T): TA
push(item)immediately followed by apop(), but faster.