Class PrimitiveMinHeap
java.lang.Object
de.bsommerfeld.pathetic.engine.pathfinder.heap.impl.PrimitiveMinHeap
A highly optimized, array-backed binary min-heap for A* pathfinding.
This implementation provides MinHeap contract with additional optimizations:
- Guarantees zero object allocations during runtime (hot-path).
- Uses primitive arrays for perfect CPU cache locality.
- Supports O(log n)
decreaseKeyoperations via an internal lookup map. - Automatically resizes when capacity is exceeded (see
Resizable). - Uses 1-based indexing internally for efficient parent/child calculations.
Implements Siftable for heap property maintenance through sift operations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintcapacity()Returns the current capacity of the internal storage.voidclear()Removes all elements from the heap.booleancontains(long packedNode) Checks if a specific node is currently in the heap.doublecost(long packedNode) Gets the current cost of a node in the heap.voidEnsures the internal storage has sufficient capacity, resizing if necessary.longRemoves and returns the node with the minimum cost from the heap.voidinsertOrUpdate(long packedNode, double cost) Inserts a new node or updates an existing node's cost in the heap.booleanisEmpty()Checks if the heap is empty.voidsiftDown(int index) Moves a node down the heap until the heap property is restored.voidsiftUp(int index) Moves a node up the heap until the heap property is restored.intsize()Returns the number of elements currently in the heap.
-
Constructor Details
-
PrimitiveMinHeap
public PrimitiveMinHeap(int initialCapacity) - Parameters:
initialCapacity- The initial size of the heap. Pro-tip: Set this to ~1.5x expected path length to avoid resizing.
-
-
Method Details
-
isEmpty
public boolean isEmpty()Description copied from interface:MinHeapChecks if the heap is empty. -
size
public int size()Description copied from interface:MinHeapReturns the number of elements currently in the heap. -
clear
public void clear()Description copied from interface:MinHeapRemoves all elements from the heap. -
contains
public boolean contains(long packedNode) Description copied from interface:MinHeapChecks if a specific node is currently in the heap. -
cost
public double cost(long packedNode) Description copied from interface:MinHeapGets the current cost of a node in the heap. -
insertOrUpdate
public void insertOrUpdate(long packedNode, double cost) Description copied from interface:MinHeapInserts a new node or updates an existing node's cost in the heap.If the node is not currently in the heap, it is inserted with the given cost. If the node already exists and the new cost is lower, the node's cost is updated (decrease-key operation).
- Specified by:
insertOrUpdatein interfaceMinHeap- Parameters:
packedNode- the unique identifier of the nodecost- the cost associated with the node (typically F-cost for A*)
-
extractMin
public long extractMin()Description copied from interface:MinHeapRemoves and returns the node with the minimum cost from the heap.- Specified by:
extractMinin interfaceMinHeap- Returns:
- the identifier of the node with the minimum cost
-
capacity
public int capacity()Description copied from interface:ResizableReturns the current capacity of the internal storage. -
ensureCapacity
public void ensureCapacity()Description copied from interface:ResizableEnsures the internal storage has sufficient capacity, resizing if necessary.- Specified by:
ensureCapacityin interfaceResizable
-
siftUp
public void siftUp(int index) Description copied from interface:SiftableMoves a node up the heap until the heap property is restored.Used after inserting a new node or decreasing a node's cost.
-
siftDown
public void siftDown(int index) Description copied from interface:SiftableMoves a node down the heap until the heap property is restored.Used after removing the minimum element or increasing a node's cost.
-