ruby-changes:36502
From: akr <ko1@a...>
Date: Wed, 26 Nov 2014 19:47:10 +0900 (JST)
Subject: [ruby-changes:36502] akr:r48584 (trunk): * lib/tsort.rb: Returns an enumerator if no block is given.
akr 2014-11-26 19:46:50 +0900 (Wed, 26 Nov 2014) New Revision: 48584 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48584 Log: * lib/tsort.rb: Returns an enumerator if no block is given. [ruby-core:66270] [Feature #10508] Proposed by Andrey Savchenko. Modified files: trunk/ChangeLog trunk/lib/tsort.rb trunk/test/test_tsort.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 48583) +++ ChangeLog (revision 48584) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Nov 26 19:44:13 2014 Tanaka Akira <akr@f...> + + * lib/tsort.rb: Returns an enumerator if no block is given. + [ruby-core:66270] [Feature #10508] Proposed by Andrey Savchenko. + Wed Nov 26 17:25:45 2014 Nobuyoshi Nakada <nobu@r...> * parse.y (f_label, f_kw, formal_argument_gen): ignore invalid Index: lib/tsort.rb =================================================================== --- lib/tsort.rb (revision 48583) +++ lib/tsort.rb (revision 48584) @@ -171,9 +171,7 @@ module TSort https://github.com/ruby/ruby/blob/trunk/lib/tsort.rb#L171 # 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 https://github.com/ruby/ruby/blob/trunk/lib/tsort.rb#L219 # # 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 https://github.com/ruby/ruby/blob/trunk/lib/tsort.rb#L276 # #=> [[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 https://github.com/ruby/ruby/blob/trunk/lib/tsort.rb#L338 # # [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 https://github.com/ruby/ruby/blob/trunk/lib/tsort.rb#L404 # # [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 Index: test/test_tsort.rb =================================================================== --- test/test_tsort.rb (revision 48583) +++ test/test_tsort.rb (revision 48584) @@ -57,6 +57,9 @@ class TSortTest < Test::Unit::TestCase # https://github.com/ruby/ruby/blob/trunk/test/test_tsort.rb#L57 r = [] TSort.tsort_each(each_node, each_child) {|n| r << n } assert_equal([4, 2, 3, 1], r) + + r = TSort.tsort_each(each_node, each_child).map {|n| n.to_s } + assert_equal(['4', '2', '3', '1'], r) end def test_s_strongly_connected_components @@ -85,6 +88,11 @@ class TSortTest < Test::Unit::TestCase # https://github.com/ruby/ruby/blob/trunk/test/test_tsort.rb#L88 r << scc } assert_equal([[4], [2, 3], [1]], r) + + r = TSort.each_strongly_connected_component(each_node, each_child).map {|scc| + scc.map(&:to_s) + } + assert_equal([['4'], ['2', '3'], ['1']], r) end def test_s_each_strongly_connected_component_from @@ -95,6 +103,11 @@ class TSortTest < Test::Unit::TestCase # https://github.com/ruby/ruby/blob/trunk/test/test_tsort.rb#L103 r << scc } assert_equal([[4], [2, 3], [1]], r) + + r = TSort.each_strongly_connected_component_from(1, each_child).map {|scc| + scc.map(&:to_s) + } + assert_equal([['4'], ['2', '3'], ['1']], r) end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/