Xo is experimental. The source is now public on GitHub, or try Xo in your browser.
Menu · heap

Docs

std/heap

On this page

Index

Types

Heap

struct Heap[T] {
  _items: List[T]
  _less: fn(T, T) -> Bool
  _moved: fn(T, Int)
}

A binary heap: the element for which less holds against all others comes out first. A value, as List: a copy shares the elements until one of them is written. Equal elements come out in no particular order.

Source: std/heap.xo:11

Heap.push

fn (var h: Heap[T]) push(x: T)

Adds x in O(log n) (heap.Push).

Source: std/heap.xo:29

Heap.pop

fn (var h: Heap[T]) pop() -> T?

The least element, removed, in O(log n); None when empty (heap.Pop).

Source: std/heap.xo:31

Heap.peek

fn (h: Heap[T]) peek() -> T?

The least element, kept; None when empty.

Source: std/heap.xo:33

Heap.get

fn (h: Heap[T]) get(i: Int) -> T?

The element at heap position i (0 is the least); None out of range.

Source: std/heap.xo:35

Heap.remove

fn (var h: Heap[T]) remove(i: Int) -> T?

The element at position i, removed, in O(log n); None out of range (heap.Remove).

Source: std/heap.xo:38

Heap.fix

fn (var h: Heap[T]) fix(i: Int, x: T)

Replaces the element at position i by x and restores the order, in O(log n) (heap.Fix after changing it); a fault when i is out of range.

Source: std/heap.xo:41

Heap.len

fn (h: Heap[T]) len() -> Int

Source: std/heap.xo:42

Heap.is_empty

fn (h: Heap[T]) is_empty() -> Bool

Source: std/heap.xo:43

Heap.to_list

fn (h: Heap[T]) to_list() -> List[T]

The elements in heap order (the least first, the rest not sorted).

Source: std/heap.xo:45

Functions

new

fn new[T](less: fn(T, T) -> Bool) -> Heap[T]

An empty heap; less(a, b) is true when a comes out before b.

Source: std/heap.xo:21

tracked

fn tracked[T](less: fn(T, T) -> Bool, moved: fn(T, Int)) -> Heap[T]

An empty heap that calls moved(x, i) each time an element x lands at position i (a push, a sift, the element a remove moves into the gap), so a caller can keep the position of each element for remove and fix.

Source: std/heap.xo:25

from_list

fn from_list[T](items: List[T], less: fn(T, T) -> Bool) -> Heap[T]

The heap of items, in O(n) (heap.Init).

Source: std/heap.xo:27

↑ index