module Bundler::TSort

def self.each_strongly_connected_component(each_node, each_child) # :yields: nodes

:yields: nodes

# [1]
# [2, 3]
#=> [4]
Bundler::TSort.each_strongly_connected_component(each_node, each_child) {|scc| p scc }
each_child = lambda {|n, &b| g[n].each(&b) }
each_node = lambda {|&b| g.each_key(&b) }
g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}

# [1]
# [3]
# [2]
#=> [4]
Bundler::TSort.each_strongly_connected_component(each_node, each_child) {|scc| p scc }
each_child = lambda {|n, &b| g[n].each(&b) }
each_node = lambda {|&b| g.each_key(&b) }
g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}

_each_child_ should have +call+ method which takes a node argument and yields for each child node.
_each_node_ should have +call+ method which yields for each node in the graph.
The graph is represented by _each_node_ and _each_child_.

The iterator version of the Bundler::TSort.strongly_connected_components method.
def self.each_strongly_connected_component(each_node, each_child) # :yields: nodes
  return to_enum(__method__, each_node, each_child) unless block_given?
  id_map = {}
  stack = []
  each_node.call {|node|
    unless id_map.include? node
      each_strongly_connected_component_from(node, each_child, id_map, stack) {|c|
        yield c
      }
    end
  }
  nil
end