Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/yeremyacuna/LYNX/llms.txt

Use this file to discover all available pages before exploring further.

The Heap<T> class is a generic binary heap backed by a dynamically-resizing array. It operates as a max-heap by default — the element with the highest priority sits at the root and is always accessible in O(1). By swapping in a custom comparator lambda, you flip the ordering to produce a min-heap with no other changes to the interface. This dual-mode design is what allows the same data structure to serve both driver-rating prioritisation and Dijkstra’s shortest-path algorithm inside LYNX.

Template Signature

template <typename T, typename Compare = bool(*)(const T&, const T&)>
class Heap
The second template parameter Compare defaults to a plain function pointer bool(*)(const T&, const T&). When you pass a lambda, you must supply its type explicitly via decltype.

Private Members

MemberTypePurpose
data_T*Raw heap array on the free store
size_intNumber of live elements
capacity_intAllocated slots before a resize is needed
greater_CompareComparator — greater_(a, b) returns true if a has higher priority than b

Method Reference

MethodSignatureDescription
pushvoid push(T value)Appends the value and sifts it up to restore the heap property
popvoid pop()Removes the top element, replaces it with the last, and sifts down
peekT peek() constReturns the top element without removing it — O(1)
buildHeapvoid buildHeap(T* arr, int n)Copies n elements from a raw array and heapifies in O(n)
clearvoid clear()Resets size_ to zero without freeing memory
isEmptybool isEmpty() constReturns true when the heap holds no elements
getSizeint getSize() constCurrent element count
getCapacityint getCapacity() constAllocated capacity
printvoid print() constPrints all elements in storage order

Internal Re-balancing

siftUp — called after push. Starting from the newly-added leaf, the element is repeatedly swapped with its parent as long as greater_(child, parent) is true. This restores the heap property from the bottom up in O(log n). siftDown — called after pop. The replacement element placed at index 0 is compared against both children; it swaps with the highest-priority child and repeats until it is in the correct position. This also runs in O(log n).

Dynamic Resizing

The heap starts with the capacity you supply at construction (default: 16). The moment size_ == capacity_ and a new element is pushed, the private resize method allocates a new array of capacity_ * 2 slots, copies all existing elements, and frees the old block. Capacity only ever grows; it does not shrink automatically.

Comparator and Heap Order

// Max-heap of integers (default behaviour: a > b)
Heap<int> maxHeap;
maxHeap.push(10);
maxHeap.push(30);
maxHeap.push(20);
cout << maxHeap.peek(); // 30

// Min-heap via lambda comparator
auto minCmp = [](const int& a, const int& b) { return a < b; };
Heap<int, decltype(minCmp)> minHeap(16, minCmp);
minHeap.push(10);
minHeap.push(5);
minHeap.push(20);
cout << minHeap.peek(); // 5

// Build heap from existing array
int arr[] = {3, 1, 4, 1, 5, 9};
maxHeap.buildHeap(arr, 6);

LYNX Usage

Heap<T> appears in two separate contexts inside the codebase:
  1. AdvancedSorts::heapSort — performs an in-place O(n log n) sort on a plain array by conceptually treating it as a heap. The implementation calls buildHeap once then iteratively extracts the maximum to sort descending, without allocating a separate Heap<T> object.
  2. Graph::dijkstra and Graph::dijkstraDistance — both methods construct a local Heap<DijkstraNode> as a min-heap priority queue to always process the unvisited vertex with the shortest known distance first:
    auto minCmp = [](const DijkstraNode& a, const DijkstraNode& b) {
        return a.dist < b.dist;
    };
    Heap<DijkstraNode, decltype(minCmp)> heap(size_ * 2 + 1, minCmp);
    

Complexity

OperationTime Complexity
pushO(log n)
popO(log n)
peekO(1)
buildHeapO(n)
clearO(1)
getSize / getCapacityO(1)

Build docs developers (and LLMs) love