Class: RubyAi::Search::Core::PriorityQueue

Inherits:
Frontier
  • Object
show all
Defined in:
lib/ruby_ai/search/core/frontier.rb

Instance Method Summary collapse

Constructor Details

#initialize(order: nil) ⇒ PriorityQueue

Returns a new instance of PriorityQueue.



59
60
61
62
# File 'lib/ruby_ai/search/core/frontier.rb', line 59

def initialize(order: nil)
  @order = Proc.new { |priorities| priorities.min }
  @store = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#append(element:, priority:) ⇒ Object



64
65
66
67
# File 'lib/ruby_ai/search/core/frontier.rb', line 64

def append(element:, priority:)
  @store[priority].push(element)
  self
end

#empty?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/ruby_ai/search/core/frontier.rb', line 73

def empty?
  clean_store && super
end

#include?(element:) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/ruby_ai/search/core/frontier.rb', line 81

def include?(element:)
  @store.values.flatten.include?(element)
end

#popObject



69
70
71
# File 'lib/ruby_ai/search/core/frontier.rb', line 69

def pop
  clean_store && @store[max_priority].pop
end

#sizeObject



77
78
79
# File 'lib/ruby_ai/search/core/frontier.rb', line 77

def size
  @store.values.flatten.size
end

#storeObject



85
86
87
88
# File 'lib/ruby_ai/search/core/frontier.rb', line 85

def store
  clean_store
  super
end