3 # $Idaemons: /home/cvs/rb/generator.rb,v 1.8 2001/10/03 08:54:32 knu Exp $
4 # $RoughId: generator.rb,v 1.10 2003/10/14 19:36:58 knu Exp $
5 # $Id: generator.rb 11708 2007-02-12 23:01:19Z shyouhei $
8 # = generator.rb: convert an internal iterator to an external one
10 # Copyright (c) 2001,2003 Akinori MUSHA <knu@iDaemons.org>
12 # All rights reserved. You can redistribute and/or modify it under
13 # the same terms as Ruby.
17 # This library provides the Generator class, which converts an
18 # internal iterator (i.e. an Enumerable object) to an external
19 # iterator. In that form, you can roll many iterators independently.
21 # The SyncEnumerator class, which is implemented using Generator,
22 # makes it easy to roll many Enumerable objects synchronously.
24 # See the respective classes for examples of usage.
28 # Generator converts an internal iterator (i.e. an Enumerable object)
29 # to an external iterator.
31 # Note that it is not very fast since it is implemented using
32 # continuations, which are currently slow.
38 # # Generator from an Enumerable object
39 # g = Generator.new(['A', 'B', 'C', 'Z'])
45 # # Generator from a block
46 # g = Generator.new { |g|
54 # # The same result as above
62 # Creates a new generator either from an Enumerable object or from a
65 # In the former, block is ignored even if given.
67 # In the latter, the given block is called with the generator
68 # itself, and expected to call the +yield+ method for each element.
69 def initialize(enum = nil, &block)
72 enum.each { |x| g.yield x }
80 @cont_next = @cont_yield = @cont_endp = nil
82 if @cont_next = callcc { |c| c }
85 @cont_endp.call(nil) if @cont_endp
91 # Yields an element to the generator.
93 if @cont_yield = callcc { |c| c }
101 # Returns true if the generator has reached the end.
103 if @cont_endp = callcc { |c| c }
104 @cont_yield.nil? && @queue.empty?
110 # Returns true if the generator has not reached the end yet.
115 # Returns the current index (position) counting from zero.
120 # Returns the current index (position) counting from zero.
125 # Returns the element at the current position and moves forward.
128 raise EOFError, "no more elements available"
131 if @cont_next = callcc { |c| c }
132 @cont_yield.call(nil) if @cont_yield
140 # Returns the element at the current position.
143 raise EOFError, "no more elements available"
149 # Rewinds the generator.
151 initialize(nil, &@block) if @index.nonzero?
156 # Rewinds the generator and enumerates the elements.
169 # SyncEnumerator creates an Enumerable object from multiple Enumerable
170 # objects and enumerates them synchronously.
174 # require 'generator'
176 # s = SyncEnumerator.new([1,2,3], ['a', 'b', 'c'])
178 # # Yields [1, 'a'], [2, 'b'], and [3,'c']
179 # s.each { |row| puts row.join(', ') }
184 # Creates a new SyncEnumerator which enumerates rows of given
185 # Enumerable objects.
186 def initialize(*enums)
187 @gens = enums.map { |e| Generator.new(e) }
190 # Returns the number of enumerated Enumerable objects, i.e. the size
196 # Returns the number of enumerated Enumerable objects, i.e. the size
202 # Returns true if the given nth Enumerable object has reached the
203 # end. If no argument is given, returns true if any of the
204 # Enumerable objects has reached the end.
207 @gens.detect { |g| g.end? } ? true : false
213 # Enumerates rows of the Enumerable objects.
215 @gens.each { |g| g.rewind }
220 ret = @gens.map { |g|
229 if count == @gens.size
241 eval DATA.read, nil, $0, __LINE__+4
248 class TC_Generator < Test::Unit::TestCase
250 g = Generator.new { |g|
254 assert_equal(0, g.pos)
255 assert_raises(EOFError) { g.current }
259 g = Generator.new { |g|
267 assert_equal(0, g.pos)
268 assert_equal('A', g.current)
270 assert_equal(true, g.next?)
271 assert_equal(0, g.pos)
272 assert_equal('A', g.current)
273 assert_equal(0, g.pos)
274 assert_equal('A', g.next)
276 assert_equal(1, g.pos)
277 assert_equal(true, g.next?)
278 assert_equal(1, g.pos)
279 assert_equal('B', g.current)
280 assert_equal(1, g.pos)
281 assert_equal('B', g.next)
283 assert_equal(g, g.rewind)
285 assert_equal(0, g.pos)
286 assert_equal('A', g.current)
288 assert_equal(true, g.next?)
289 assert_equal(0, g.pos)
290 assert_equal('A', g.current)
291 assert_equal(0, g.pos)
292 assert_equal('A', g.next)
294 assert_equal(1, g.pos)
295 assert_equal(true, g.next?)
296 assert_equal(1, g.pos)
297 assert_equal('B', g.current)
298 assert_equal(1, g.pos)
299 assert_equal('B', g.next)
301 assert_equal(2, g.pos)
302 assert_equal(true, g.next?)
303 assert_equal(2, g.pos)
304 assert_equal('C', g.current)
305 assert_equal(2, g.pos)
306 assert_equal('C', g.next)
308 assert_equal(3, g.pos)
309 assert_equal(true, g.next?)
310 assert_equal(3, g.pos)
311 assert_equal('Z', g.current)
312 assert_equal(3, g.pos)
313 assert_equal('Z', g.next)
315 assert_equal(4, g.pos)
316 assert_equal(false, g.next?)
317 assert_raises(EOFError) { g.next }
328 assert_equal(a[i], x)
340 assert_equal(a[i], x)
349 class TC_SyncEnumerator < Test::Unit::TestCase
351 r = ['a'..'f', 1..10, 10..20]
352 ra = r.map { |x| x.to_a }
354 a = (0...(ra.map {|x| x.size}.max)).map { |i| ra.map { |x| x[i] } }
356 s = SyncEnumerator.new(*r)
361 assert_equal(a[i], x)
373 assert_equal(a[i], x)
378 assert_equal(a.size, i)