heap

Binary MinHeap, MaxHeap and PriorityQueue data structures.

v1.0.0Data structuresbundledimport "heap";
Overview Installation Documentation Examples

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
Packageheap
Version1.0.0 (follows the compiler)
Entry fileheap.hphp
Sourcepackages/lib/heap ↗

Documentation

API Reference

Helper Functions

MinHeap & MaxHeap

Both classes support int, float, and string values.

PriorityQueue

Supports arbitrary item values paired with numeric (int or float) priorities. Ties are broken stably in FIFO insertion order.

Error Handling

All invalid operations throw an Exception prefixed with heap::

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 it
hphp install heap
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_heap.hphp.