Class: Lich::Common::MinHeap
- Inherits:
-
Object
- Object
- Lich::Common::MinHeap
- Defined in:
- documented/common/map/map_base.rb
Overview
MinHeap for efficient Dijkstra priority queue Extracted to be shared across all game implementations
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Checks whether the heap holds any elements.
-
#initialize ⇒ void
constructor
Initializes an empty binary min-heap.
-
#pop ⇒ [Numeric, Object]?
Removes and returns the minimum-priority element.
-
#push(priority, value) ⇒ void
Adds an element to the heap with the given priority.
Constructor Details
#initialize ⇒ void
Initializes an empty binary min-heap.
25 26 27 |
# File 'documented/common/map/map_base.rb', line 25 def initialize @heap = [] end |
Instance Method Details
#empty? ⇒ Boolean
Checks whether the heap holds any elements.
57 58 59 |
# File 'documented/common/map/map_base.rb', line 57 def empty? @heap.empty? end |
#pop ⇒ [Numeric, Object]?
Removes and returns the minimum-priority element.
45 46 47 48 49 50 51 52 |
# File 'documented/common/map/map_base.rb', line 45 def pop return nil if @heap.empty? swap(0, @heap.size - 1) min = @heap.pop bubble_down(0) unless @heap.empty? min end |
#push(priority, value) ⇒ void
This method returns an undefined value.
Adds an element to the heap with the given priority.
Lower priority values bubble toward the root. Maintains heap invariant after insertion.
37 38 39 40 |
# File 'documented/common/map/map_base.rb', line 37 def push(priority, value) @heap << [priority, value] bubble_up(@heap.size - 1) end |