Class: AStar::PriorityQueue

Inherits:
Object show all
Defined in:
lib/ext/astar/priority_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(nodes = []) ⇒ PriorityQueue

Returns a new instance of PriorityQueue.



11
12
13
14
15
# File 'lib/ext/astar/priority_queue.rb', line 11

def initialize(nodes=[])
  @nodes=nodes
  #tie-breaker multiplier (tbmul) is 1+1/(the sqrt of the map size)
  @tbmul=1+1/(Math.sqrt(@nodes.size))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methodname, *args) ⇒ Object



16
17
18
19
# File 'lib/ext/astar/priority_queue.rb', line 16

def method_missing(methodname, *args)
  #if in doubt, act like an array
  @nodes.send(methodname, *args)
end

Instance Method Details

#find(node) ⇒ Object



28
29
30
31
# File 'lib/ext/astar/priority_queue.rb', line 28

def find(node)
  #finds a node - requires that node implements ==
  @nodes.find {|x| x==node }
end

#find_bestObject



20
21
22
23
24
25
26
27
# File 'lib/ext/astar/priority_queue.rb', line 20

def find_best
  #finds the best node, then pops it out
  best=@nodes.first
  @nodes.each do |node|
    if node.better?(best,@tbmul) then best=node end
  end
  remove(best)
end

#remove(node) ⇒ Object



33
34
35
36
# File 'lib/ext/astar/priority_queue.rb', line 33

def remove(node)
  #removes a node
  @nodes.delete(find(node))
end

#to_sObject



38
39
40
41
# File 'lib/ext/astar/priority_queue.rb', line 38

def to_s
  output=""
  @nodes.each {|e| output<<"#{e};"}
end