Class: Seafoam::BFS

Inherits:
Object
  • Object
show all
Defined in:
lib/seafoam/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ BFS

Returns a new instance of BFS.



9
10
11
# File 'lib/seafoam/search.rb', line 9

def initialize(root)
  @root = root
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/seafoam/search.rb', line 7

def root
  @root
end

Instance Method Details

#search(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/seafoam/search.rb', line 13

def search(&block)
  queue = root.outputs.collect(&:to)
  visited = Set.new

  until queue.empty?
    entry = queue.shift
    visited << entry

    result = entry.visit(&block)

    if result
      return result
    else
      entry.outputs.collect(&:to).each do |child|
        queue << child unless visited.include?(child)
      end
    end
  end
end