Overview
TheHeap template class implements a binary min-heap data structure with efficient decrease/increase key operations. It uses an IntMap to track element positions, enabling O(log n) updates for arbitrary elements.
Template parameters
The key type stored in the heap
Comparator type defining the heap ordering. The heap maintains a minimum with respect to this comparator.
Functor that converts keys to integer indices for the internal position map
Constructor
Comparator instance used for heap ordering
Index conversion functor instance
Size and query operations
size
empty
inHeap
Key to check for membership
operator[]
Position in the internal heap array (must be < size())
Modification operations
insert
Key to insert (must not already be in heap)
Time complexity: O(log n) where n is the number of elements
remove
Key to remove (must be in heap)
Time complexity: O(log n)
removeMin
Time complexity: O(log n)
Priority update operations
decrease
k has decreased. The key must be in the heap.
Key whose priority decreased (must be in heap)
You must call this after externally modifying the key’s priority to maintain heap invariants.
increase
k has increased. The key must be in the heap.
Key whose priority increased (must be in heap)
update
Key to update or insert
This is the safest method when you’re unsure whether priority increased or decreased.
Bulk operations
build
Vector of keys to build heap from
Keys in
ns must already be reserved in the internal index map.Time complexity: O(n) where n is the number of elements
clear
If true, deallocates internal storage
Internal methods
The heap uses standard binary heap index traversal:- left child:
2*i + 1 - right child:
2*(i + 1) - parent:
(i - 1) / 2
percolateUp(): Restores heap property after decrease operationspercolateDown(): Restores heap property after increase operations
Usage example
Typical use case in MiniSat
MiniSat uses the heap to maintain variable activity scores for decision heuristics:Performance characteristics
- Insert: O(log n)
- Remove: O(log n)
- Remove min: O(log n)
- Decrease/increase: O(log n)
- Check membership: O(1) amortized
- Build: O(n)
- Space: O(n) for heap array plus O(n) for position map