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.
fn heap_new(): int[]Returns: int[] with h[0]=0 (size) and h[1..64] pre-allocated as zeros
Example
let h: int[] = heap_new()heap_size v1.0.0
Return the number of elements currently stored in the heap.
fn heap_size(h: int[]): intParameters
| Parameter | Description |
|---|---|
h | Heap array (created with heap_new) |
Returns: Current element count (h[0])
Example
let sz: int = heap_size(h)heap_peek v1.0.0
Return the root element without removing it (min for MinHeap, max for MaxHeap).
fn heap_peek(h: int[]): intParameters
| Parameter | Description |
|---|---|
h | Non-empty heap array; precondition: heap_size(h) > 0 |
Returns: The root element (h[1])
Example
let top: int = heap_peek(h) // peek min without modifying heapheap_push v1.0.0
Insert a value into a MinHeap (smallest element at root).
fn heap_push(h: int[], val: int): int[]Parameters
| Parameter | Description |
|---|---|
h | Heap array (from heap_new) |
val | Value to insert |
Returns: Updated heap array with val inserted and heap property maintained
Example
h = heap_push(h, 42)
h = heap_push(h, 7)
// heap_peek(h) == 7heap_pop v1.0.0
Remove and discard the minimum element from a MinHeap.
fn heap_pop(h: int[]): int[]Parameters
| Parameter | Description |
|---|---|
h | Non-empty MinHeap array; precondition: heap_size(h) > 0 |
Returns: Updated heap array with minimum removed and heap property restored
Example
let min_val: int = heap_peek(h)
h = heap_pop(h) // remove the minimummax_heap_push v1.0.0
Insert a value into a MaxHeap (largest element at root).
fn max_heap_push(h: int[], val: int): int[]Parameters
| Parameter | Description |
|---|---|
h | Heap array (from heap_new) |
val | Value to insert |
Returns: Updated heap array with val inserted and max-heap property maintained
Example
h = max_heap_push(h, 5)
h = max_heap_push(h, 99)
// heap_peek(h) == 99max_heap_pop v1.0.0
Remove and discard the maximum element from a MaxHeap.
fn max_heap_pop(h: int[]): int[]Parameters
| Parameter | Description |
|---|---|
h | Non-empty MaxHeap array; precondition: heap_size(h) > 0 |
Returns: Updated heap array with maximum removed and max-heap property restored
Example
let max_val: int = heap_peek(h)
h = max_heap_pop(h) // remove the maximum