Overview
Zero-dependency binary MinHeap, MaxHeap, and PriorityQueue data structures for HolyPHP.
bundled — works offline
zero dependencies
priority
queue
Quick start
import "heap";
$min = heap_min_from([5, 1, 8, 2]);
println($min->peek()); // 1
println($min->pop()); // 1
println($min->pop()); // 2
$max = heap_max_from([5, 1, 8, 2]);
println($max->pop()); // 8
$pq = heap_priority_queue();
$pq->push("send email", 5);
$pq->push("fix outage", 1);
$pq->push("write docs", 10);
println($pq->pop()); // "fix outage"
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages):
hphp install heap
This adds the library to hphp.yaml, installs it into .hphp/packages/heap/ and pins the exact version in hphp.lock:
require:
heap: "^1.0"
Use it
import "heap";
heap is bundled with the compiler, so import "heap"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install heap # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install heap
Documentation
API Reference
Helper Functions
heap_min(): MinHeap — Create an empty MinHeap.
heap_min_from(array $items): MinHeap — Create a MinHeap initialized with $items.
heap_max(): MaxHeap — Create an empty MaxHeap.
heap_max_from(array $items): MaxHeap — Create a MaxHeap initialized with $items.
heap_priority_queue(): PriorityQueue — Create a min-priority PriorityQueue (lowest numeric priority dequeued first).
heap_max_priority_queue(): PriorityQueue — Create a max-priority PriorityQueue (highest numeric priority dequeued first).
MinHeap & MaxHeap
Both classes support int, float, and string values.
new MinHeap() / new MaxHeap()
push($value): void — Insert a value into the heap in $O(\log n)$ time.
pushAll(array $items): void — Insert all elements of $items into the heap.
pop() — Remove and return the minimum (MinHeap) or maximum (MaxHeap) value in $O(\log n)$ time.
peek() — Return the top value without removing it in $O(1)$ time.
size(): int — Return the number of elements in the heap.
isEmpty(): bool — Return true if the heap has no elements.
clear(): void — Remove all elements from the heap.
toArray(): array — Return all elements in sorted order (ascending for MinHeap, descending for MaxHeap) without mutating the heap.
PriorityQueue
Supports arbitrary item values paired with numeric (int or float) priorities. Ties are broken stably in FIFO insertion order.
new PriorityQueue(bool $maxFirst = false)
push($item, $priority): void — Enqueue $item with numeric $priority.
pop() — Remove and return the highest-priority item.
peek() — Return the highest-priority item without removing it.
peekPriority() — Return the numeric priority of the top item without removing it.
size(): int — Return the number of queued items.
isEmpty(): bool — Return true if the queue is empty.
clear(): void — Remove all items from the queue.
toArray(): array — Return all items in priority order without mutating the queue.
Error Handling
All invalid operations throw an Exception prefixed with heap::
- Popping or peeking an empty
MinHeap, MaxHeap, or PriorityQueue:
heap: cannot pop from an empty MinHeap
heap: cannot peek into an empty MaxHeap
heap: cannot pop from an empty PriorityQueue
heap: cannot peekPriority into an empty PriorityQueue
- Pushing an unsupported type into
MinHeap or MaxHeap:
heap: MinHeap::push expects int, float, or string
- Pushing a non-numeric priority into
PriorityQueue:
heap: PriorityQueue::push expects numeric priority (int or float)
Examples
01Quick start
Bundled with the compiler — import "heap"; works offline in any file, or add it to a project with hphp install heap.
import "heap";
$min = heap_min_from([5, 1, 8, 2]);
println($min->peek()); // 1
println($min->pop()); // 1
println($min->pop()); // 2
$max = heap_max_from([5, 1, 8, 2]);
println($max->pop()); // 8
$pq = heap_priority_queue();
$pq->push("send email", 5);
$pq->push("fix outage", 1);
$pq->push("write docs", 10);
println($pq->pop()); // "fix outage"
Try ithphp install heap
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_heap.hphp.