Class: Lich::Common::MinHeap
- Inherits:
-
Object
- Object
- Lich::Common::MinHeap
- Defined in:
- documented/common/map/map_base.rb
Overview
A minimum heap implementation.
This class provides methods to manage a priority queue where the smallest element is always at the top.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Checks if the heap is empty.
-
#initialize ⇒ MinHeap
constructor
A new instance of MinHeap.
-
#pop ⇒ Array<Object>?
Removes and returns the smallest value from the heap.
-
#push(priority, value) ⇒ void
Adds a value with a given priority to the heap.
Constructor Details
#initialize ⇒ MinHeap
Returns a new instance of MinHeap.
11 12 13 |
# File 'documented/common/map/map_base.rb', line 11 def initialize @heap = [] end |
Instance Method Details
#empty? ⇒ Boolean
Checks if the heap is empty.
37 38 39 |
# File 'documented/common/map/map_base.rb', line 37 def empty? @heap.empty? end |
#pop ⇒ Array<Object>?
Removes and returns the smallest value from the heap.
26 27 28 29 30 31 32 33 |
# File 'documented/common/map/map_base.rb', line 26 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 a value with a given priority to the heap.
19 20 21 22 |
# File 'documented/common/map/map_base.rb', line 19 def push(priority, value) @heap << [priority, value] bubble_up(@heap.size - 1) end |