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 contains any elements.
-
#initialize ⇒ MinHeap
constructor
Creates an empty min-heap for use in Dijkstra's algorithm priority queue.
-
#pop ⇒ Array<(Numeric, Integer)>?
Removes and returns the element with the minimum priority.
-
#push(priority, value) ⇒ void
Adds an element to the heap with the given priority.
Constructor Details
#initialize ⇒ MinHeap
Creates an empty min-heap for use in Dijkstra's algorithm priority queue.
17 18 19 |
# File 'documented/common/map/map_base.rb', line 17 def initialize @heap = [] end |
Instance Method Details
#empty? ⇒ Boolean
Checks whether the heap contains any elements.
50 51 52 |
# File 'documented/common/map/map_base.rb', line 50 def empty? @heap.empty? end |
#pop ⇒ Array<(Numeric, Integer)>?
Removes and returns the element with the minimum priority.
Restores the min-heap property by bubbling the new root down the tree.
38 39 40 41 42 43 44 45 |
# File 'documented/common/map/map_base.rb', line 38 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.
Maintains the min-heap property by bubbling the element up the tree.
28 29 30 31 |
# File 'documented/common/map/map_base.rb', line 28 def push(priority, value) @heap << [priority, value] bubble_up(@heap.size - 1) end |