mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/tsort.rb: Returns an enumerator if no block is given.
[ruby-core:66270] [Feature #10508] Proposed by Andrey Savchenko. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48584 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
eab191040e
commit
0eadc632c1
3 changed files with 26 additions and 6 deletions
14
lib/tsort.rb
14
lib/tsort.rb
|
@ -171,9 +171,7 @@ module TSort
|
|||
# p TSort.tsort(each_node, each_child) # raises TSort::Cyclic
|
||||
#
|
||||
def TSort.tsort(each_node, each_child)
|
||||
result = []
|
||||
TSort.tsort_each(each_node, each_child) {|element| result << element}
|
||||
result
|
||||
TSort.tsort_each(each_node, each_child).to_a
|
||||
end
|
||||
|
||||
# The iterator version of the #tsort method.
|
||||
|
@ -221,6 +219,8 @@ module TSort
|
|||
# # 1
|
||||
#
|
||||
def TSort.tsort_each(each_node, each_child) # :yields: node
|
||||
return to_enum(__method__, each_node, each_child) unless block_given?
|
||||
|
||||
TSort.each_strongly_connected_component(each_node, each_child) {|component|
|
||||
if component.size == 1
|
||||
yield component.first
|
||||
|
@ -276,9 +276,7 @@ module TSort
|
|||
# #=> [[4], [2, 3], [1]]
|
||||
#
|
||||
def TSort.strongly_connected_components(each_node, each_child)
|
||||
result = []
|
||||
TSort.each_strongly_connected_component(each_node, each_child) {|component| result << component}
|
||||
result
|
||||
TSort.each_strongly_connected_component(each_node, each_child).to_a
|
||||
end
|
||||
|
||||
# The iterator version of the #strongly_connected_components method.
|
||||
|
@ -340,6 +338,8 @@ module TSort
|
|||
# # [1]
|
||||
#
|
||||
def TSort.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|
|
||||
|
@ -404,6 +404,8 @@ module TSort
|
|||
# # [1]
|
||||
#
|
||||
def TSort.each_strongly_connected_component_from(node, each_child, id_map={}, stack=[]) # :yields: nodes
|
||||
return to_enum(__method__, node, each_child, id_map, stack) unless block_given?
|
||||
|
||||
minimum_id = node_id = id_map[node] = id_map.size
|
||||
stack_length = stack.length
|
||||
stack << node
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue