Class: Lich::Common::MinHeap

Inherits:
Object
  • Object
show all
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

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.

Returns:

  • (Boolean) —

    true if the heap is empty



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.

Returns:

  • (Array<(Numeric, Integer)>, nil) —

    a [priority, value] pair, or nil when the heap is empty



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.

Parameters:

  • priority (Numeric) —

    the priority value; lower values are dequeued first

  • value (Integer) —

    typically a room id



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