Skip to content

Heap

Auto-generated from src/stdlib/heap.mcrs — do not edit manually.

API


heap_new v1.0.0

Allocate a new empty heap with capacity for 64 elements.

redscript
fn heap_new(): int[]

Returns: int[] with h[0]=0 (size) and h[1..64] pre-allocated as zeros

Example

redscript
let h: int[] = heap_new()

heap_size v1.0.0

Return the number of elements currently stored in the heap.

redscript
fn heap_size(h: int[]): int

Parameters

ParameterDescription
hHeap array (created with heap_new)

Returns: Current element count (h[0])

Example

redscript
let sz: int = heap_size(h)

heap_peek v1.0.0

Return the root element without removing it (min for MinHeap, max for MaxHeap).

redscript
fn heap_peek(h: int[]): int

Parameters

ParameterDescription
hNon-empty heap array; precondition: heap_size(h) > 0

Returns: The root element (h[1])

Example

redscript
let top: int = heap_peek(h)  // peek min without modifying heap

heap_push v1.0.0

Insert a value into a MinHeap (smallest element at root).

redscript
fn heap_push(h: int[], val: int): int[]

Parameters

ParameterDescription
hHeap array (from heap_new)
valValue to insert

Returns: Updated heap array with val inserted and heap property maintained

Example

redscript
h = heap_push(h, 42)
h = heap_push(h, 7)
// heap_peek(h) == 7

heap_pop v1.0.0

Remove and discard the minimum element from a MinHeap.

redscript
fn heap_pop(h: int[]): int[]

Parameters

ParameterDescription
hNon-empty MinHeap array; precondition: heap_size(h) > 0

Returns: Updated heap array with minimum removed and heap property restored

Example

redscript
let min_val: int = heap_peek(h)
h = heap_pop(h)  // remove the minimum

max_heap_push v1.0.0

Insert a value into a MaxHeap (largest element at root).

redscript
fn max_heap_push(h: int[], val: int): int[]

Parameters

ParameterDescription
hHeap array (from heap_new)
valValue to insert

Returns: Updated heap array with val inserted and max-heap property maintained

Example

redscript
h = max_heap_push(h, 5)
h = max_heap_push(h, 99)
// heap_peek(h) == 99

max_heap_pop v1.0.0

Remove and discard the maximum element from a MaxHeap.

redscript
fn max_heap_pop(h: int[]): int[]

Parameters

ParameterDescription
hNon-empty MaxHeap array; precondition: heap_size(h) > 0

Returns: Updated heap array with maximum removed and max-heap property restored

Example

redscript
let max_val: int = heap_peek(h)
h = max_heap_pop(h)  // remove the maximum

Released under the MIT License.