Project

General

Profile

Feature #10508 ยป 0001-Return-enumerator-in-TSort-each_-methods.patch

ptico (Andrey Savchenko), 11/13/2014 09:04 PM

View differences:

lib/tsort.rb
171 171
  #   p TSort.tsort(each_node, each_child) # raises TSort::Cyclic
172 172
  #
173 173
  def TSort.tsort(each_node, each_child)
174
    result = []
175
    TSort.tsort_each(each_node, each_child) {|element| result << element}
176
    result
174
    TSort.tsort_each(each_node, each_child).to_a
177 175
  end
178 176

  
179 177
  # The iterator version of the #tsort method.
......
221 219
  #   #   1
222 220
  #
223 221
  def TSort.tsort_each(each_node, each_child) # :yields: node
222
    return to_enum(__method__, each_node, each_child) unless block_given?
223

  
224 224
    TSort.each_strongly_connected_component(each_node, each_child) {|component|
225 225
      if component.size == 1
226 226
        yield component.first
......
276 276
  #   #=> [[4], [2, 3], [1]]
277 277
  #
278 278
  def TSort.strongly_connected_components(each_node, each_child)
279
    result = []
280
    TSort.each_strongly_connected_component(each_node, each_child) {|component| result << component}
281
    result
279
    TSort.each_strongly_connected_component(each_node, each_child).to_a
282 280
  end
283 281

  
284 282
  # The iterator version of the #strongly_connected_components method.
......
340 338
  #   #   [1]
341 339
  #
342 340
  def TSort.each_strongly_connected_component(each_node, each_child) # :yields: nodes
341
    return to_enum(__method__, each_node, each_child) unless block_given?
342

  
343 343
    id_map = {}
344 344
    stack = []
345 345
    each_node.call {|node|
......
404 404
  #   #   [1]
405 405
  #
406 406
  def TSort.each_strongly_connected_component_from(node, each_child, id_map={}, stack=[]) # :yields: nodes
407
    return to_enum(__method__, node, each_child, id_map, stack) unless block_given?
408

  
407 409
    minimum_id = node_id = id_map[node] = id_map.size
408 410
    stack_length = stack.length
409 411
    stack << node
test/test_tsort.rb
57 57
    r = []
58 58
    TSort.tsort_each(each_node, each_child) {|n| r << n }
59 59
    assert_equal([4, 2, 3, 1], r)
60

  
61
    r = TSort.tsort_each(each_node, each_child).map {|n| n.to_s }
62
    assert_equal(['4', '2', '3', '1'], r)
60 63
  end
61 64

  
62 65
  def test_s_strongly_connected_components
......
85 88
      r << scc
86 89
    }
87 90
    assert_equal([[4], [2, 3], [1]], r)
91

  
92
    r = TSort.each_strongly_connected_component(each_node, each_child).map {|scc|
93
      scc.map(&:to_s)
94
    }
95
    assert_equal([['4'], ['2', '3'], ['1']], r)
88 96
  end
89 97

  
90 98
  def test_s_each_strongly_connected_component_from
......
95 103
      r << scc
96 104
    }
97 105
    assert_equal([[4], [2, 3], [1]], r)
106

  
107
    r = TSort.each_strongly_connected_component_from(1, each_child).map {|scc|
108
      scc.map(&:to_s)
109
    }
110
    assert_equal([['4'], ['2', '3'], ['1']], r)
98 111
  end
99 112
end
100 113

  
101
-