module Bundler::TSort
def self.tsort(each_node, each_child)
p Bundler::TSort.tsort(each_node, each_child) # raises Bundler::TSort::Cyclic
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=>[]}
p Bundler::TSort.tsort(each_node, each_child) #=> [4, 2, 3, 1]
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=>[]}
If there is a cycle, Bundler::TSort::Cyclic is raised.
_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 first element has no child and the last node has no parent.
The array is sorted from children to parents, i.e.
Returns a topologically sorted array of nodes.
def self.tsort(each_node, each_child) tsort_each(each_node, each_child).to_a end