Class: MiniGraph::DSL::SearchContext

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_graph/dsl/search_context.rb

Constant Summary collapse

ALGORITHM_IMPL =
{
  dfs: MiniGraph::Core::Search::DFS,
  bfs: MiniGraph::Core::Search::BFS
}

Class Method Summary collapse

Class Method Details

.evaluate(graph, start_at, algorithm:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mini_graph/dsl/search_context.rb', line 13

def self.evaluate(graph, start_at, algorithm:)
  algorithm_impl = ALGORITHM_IMPL[algorithm]

  unless algorithm_impl
    raise MiniGraph::Core::Error::InvalidSearchAlgorithmError,
          "An unknown algorithm was provided to SearchContext: #{algorithm}"
  end

  vertex_index  = graph.vertices.map.with_index { |v, i| [v, i] }.to_h
  start_index   = start_at.nil? ? 0 : vertex_index[start_at]

  algorithm_impl.new(graph, start_index)
end