2003-10-15 00:14:20 +04:00
|
|
|
#!/usr/bin/env ruby
|
2004-02-16 16:24:07 +03:00
|
|
|
#--
|
|
|
|
# $Idaemons: /home/cvs/rb/generator.rb,v 1.8 2001/10/03 08:54:32 knu Exp $
|
|
|
|
# $RoughId: generator.rb,v 1.10 2003/10/14 19:36:58 knu Exp $
|
|
|
|
# $Id$
|
2003-10-17 18:01:26 +04:00
|
|
|
#++
|
2004-02-16 16:24:07 +03:00
|
|
|
#
|
|
|
|
# = generator.rb: convert an internal iterator to an external one
|
|
|
|
#
|
2003-10-15 00:14:20 +04:00
|
|
|
# Copyright (c) 2001,2003 Akinori MUSHA <knu@iDaemons.org>
|
|
|
|
#
|
|
|
|
# All rights reserved. You can redistribute and/or modify it under
|
|
|
|
# the same terms as Ruby.
|
|
|
|
#
|
2003-10-17 18:01:26 +04:00
|
|
|
# == Overview
|
|
|
|
#
|
|
|
|
# This library provides the Generator class, which converts an
|
|
|
|
# internal iterator (i.e. an Enumerable object) to an external
|
|
|
|
# iterator. In that form, you can roll many iterators independently.
|
|
|
|
#
|
|
|
|
# The SyncEnumerator class, which is implemented using Generator,
|
|
|
|
# makes it easy to roll many Enumerable objects synchronously.
|
2003-10-15 00:14:20 +04:00
|
|
|
#
|
2003-10-17 18:01:26 +04:00
|
|
|
# See the respective classes for examples of usage.
|
2003-10-15 00:14:20 +04:00
|
|
|
|
2004-02-16 16:24:07 +03:00
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
#
|
|
|
|
# Generator converts an internal iterator (i.e. an Enumerable object)
|
|
|
|
# to an external iterator.
|
|
|
|
#
|
|
|
|
# == Example
|
|
|
|
#
|
|
|
|
# require 'generator'
|
|
|
|
#
|
|
|
|
# # Generator from an Enumerable object
|
|
|
|
# g = Generator.new(['A', 'B', 'C', 'Z'])
|
|
|
|
#
|
|
|
|
# while g.next?
|
|
|
|
# puts g.next
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # Generator from a block
|
|
|
|
# g = Generator.new { |g|
|
|
|
|
# for i in 'A'..'C'
|
|
|
|
# g.yield i
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# g.yield 'Z'
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# # The same result as above
|
|
|
|
# while g.next?
|
|
|
|
# puts g.next
|
|
|
|
# end
|
|
|
|
#
|
2003-10-15 00:14:20 +04:00
|
|
|
class Generator
|
|
|
|
include Enumerable
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Creates a new generator either from an Enumerable object or from a
|
|
|
|
# block.
|
|
|
|
#
|
|
|
|
# In the former, block is ignored even if given.
|
|
|
|
#
|
|
|
|
# In the latter, the given block is called with the generator
|
|
|
|
# itself, and expected to call the +yield+ method for each element.
|
2003-10-15 00:14:20 +04:00
|
|
|
def initialize(enum = nil, &block)
|
|
|
|
if enum
|
2005-12-29 05:29:26 +03:00
|
|
|
@block = proc{|g| enum.each{|value| g.yield value}}
|
2003-10-15 00:14:20 +04:00
|
|
|
else
|
|
|
|
@block = block
|
|
|
|
end
|
|
|
|
@index = 0
|
|
|
|
@queue = []
|
2005-12-29 05:29:26 +03:00
|
|
|
@loop_thread = Thread.new do
|
2005-12-30 12:57:49 +03:00
|
|
|
Thread.stop
|
|
|
|
begin
|
|
|
|
@block.call(self)
|
|
|
|
rescue
|
2005-12-30 13:17:39 +03:00
|
|
|
@main_thread.raise $!
|
2005-12-30 12:57:49 +03:00
|
|
|
ensure
|
|
|
|
@main_thread.wakeup
|
2005-12-29 05:29:26 +03:00
|
|
|
end
|
2003-10-15 00:14:20 +04:00
|
|
|
end
|
2005-12-30 12:57:49 +03:00
|
|
|
Thread.pass until @loop_thread.stop?
|
2003-10-15 00:14:20 +04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Yields an element to the generator.
|
2003-10-15 00:14:20 +04:00
|
|
|
def yield(value)
|
2005-12-29 05:29:26 +03:00
|
|
|
if Thread.current != @loop_thread
|
2005-12-29 19:08:44 +03:00
|
|
|
raise "should be called in Generator.new{|g| ... }"
|
2003-10-15 00:14:20 +04:00
|
|
|
end
|
2005-12-30 12:57:49 +03:00
|
|
|
Thread.critical = true
|
|
|
|
begin
|
|
|
|
@queue << value
|
|
|
|
@main_thread.wakeup
|
|
|
|
Thread.stop
|
|
|
|
ensure
|
|
|
|
Thread.critical = false
|
|
|
|
end
|
2003-10-15 00:14:20 +04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Returns true if the generator has reached the end.
|
2005-12-29 05:29:26 +03:00
|
|
|
def end?
|
2005-12-30 12:57:49 +03:00
|
|
|
if @queue.empty?
|
|
|
|
if @main_thread
|
|
|
|
raise "should not be called in Generator.new{|g| ... }"
|
|
|
|
end
|
|
|
|
Thread.critical = true
|
|
|
|
begin
|
|
|
|
@main_thread = Thread.current
|
|
|
|
@loop_thread.wakeup
|
|
|
|
Thread.stop
|
|
|
|
rescue ThreadError
|
|
|
|
# ignore
|
|
|
|
ensure
|
|
|
|
@main_thread = nil
|
|
|
|
Thread.critical = false
|
2005-12-29 05:29:26 +03:00
|
|
|
end
|
2003-10-15 00:14:20 +04:00
|
|
|
end
|
2005-12-29 05:29:26 +03:00
|
|
|
@queue.empty?
|
2003-10-15 00:14:20 +04:00
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Returns true if the generator has not reached the end yet.
|
2005-12-29 05:29:26 +03:00
|
|
|
def next?
|
2003-10-15 00:14:20 +04:00
|
|
|
!end?
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Returns the current index (position) counting from zero.
|
2005-12-29 05:29:26 +03:00
|
|
|
def index
|
2003-10-15 00:14:20 +04:00
|
|
|
@index
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Returns the current index (position) counting from zero.
|
2005-12-29 05:29:26 +03:00
|
|
|
def pos
|
2003-10-15 00:14:20 +04:00
|
|
|
@index
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Returns the element at the current position and moves forward.
|
2005-12-29 05:29:26 +03:00
|
|
|
def next
|
|
|
|
raise EOFError.new("no more elements available") if end?
|
2003-10-15 00:14:20 +04:00
|
|
|
@index += 1
|
|
|
|
@queue.shift
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Returns the element at the current position.
|
2005-12-29 05:29:26 +03:00
|
|
|
def current
|
|
|
|
raise EOFError.new("no more elements available") if end?
|
2003-10-15 00:14:20 +04:00
|
|
|
@queue.first
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Rewinds the generator.
|
2005-12-29 05:29:26 +03:00
|
|
|
def rewind
|
2003-10-15 00:14:20 +04:00
|
|
|
initialize(nil, &@block) if @index.nonzero?
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Rewinds the generator and enumerates the elements.
|
2003-10-15 00:14:20 +04:00
|
|
|
def each
|
|
|
|
rewind
|
|
|
|
until end?
|
|
|
|
yield self.next
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2003-10-17 18:01:26 +04:00
|
|
|
# SyncEnumerator creates an Enumerable object from multiple Enumerable
|
|
|
|
# objects and enumerates them synchronously.
|
|
|
|
#
|
|
|
|
# == Example
|
|
|
|
#
|
|
|
|
# require 'generator'
|
|
|
|
#
|
|
|
|
# s = SyncEnumerator.new([1,2,3], ['a', 'b', 'c'])
|
|
|
|
#
|
|
|
|
# # Yields [1, 'a'], [2, 'b'], and [3,'c']
|
|
|
|
# s.each { |row| puts row.join(', ') }
|
2003-10-15 00:14:20 +04:00
|
|
|
#
|
|
|
|
class SyncEnumerator
|
|
|
|
include Enumerable
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Creates a new SyncEnumerator which enumerates rows of given
|
|
|
|
# Enumerable objects.
|
2003-10-15 00:14:20 +04:00
|
|
|
def initialize(*enums)
|
|
|
|
@gens = enums.map { |e| Generator.new(e) }
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Returns the number of enumerated Enumerable objects, i.e. the size
|
|
|
|
# of each row.
|
2003-10-15 00:14:20 +04:00
|
|
|
def size
|
|
|
|
@gens.size
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Returns the number of enumerated Enumerable objects, i.e. the size
|
|
|
|
# of each row.
|
2003-10-15 00:14:20 +04:00
|
|
|
def length
|
|
|
|
@gens.length
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Returns true if the given nth Enumerable object has reached the
|
|
|
|
# end. If no argument is given, returns true if any of the
|
|
|
|
# Enumerable objects has reached the end.
|
2003-10-15 00:14:20 +04:00
|
|
|
def end?(i = nil)
|
|
|
|
if i.nil?
|
|
|
|
@gens.detect { |g| g.end? } ? true : false
|
|
|
|
else
|
|
|
|
@gens[i].end?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-10-17 18:01:26 +04:00
|
|
|
# Enumerates rows of the Enumerable objects.
|
2003-10-15 00:14:20 +04:00
|
|
|
def each
|
|
|
|
@gens.each { |g| g.rewind }
|
|
|
|
|
|
|
|
loop do
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
ret = @gens.map { |g|
|
|
|
|
if g.end?
|
|
|
|
count += 1
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
g.next
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == @gens.size
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
yield ret
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if $0 == __FILE__
|
|
|
|
eval DATA.read, nil, $0, __LINE__+4
|
|
|
|
end
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TC_Generator < Test::Unit::TestCase
|
|
|
|
def test_block1
|
|
|
|
g = Generator.new { |g|
|
|
|
|
# no yield's
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal(0, g.pos)
|
|
|
|
assert_raises(EOFError) { g.current }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_block2
|
|
|
|
g = Generator.new { |g|
|
|
|
|
for i in 'A'..'C'
|
|
|
|
g.yield i
|
|
|
|
end
|
|
|
|
|
|
|
|
g.yield 'Z'
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal(0, g.pos)
|
|
|
|
assert_equal('A', g.current)
|
|
|
|
|
|
|
|
assert_equal(true, g.next?)
|
|
|
|
assert_equal(0, g.pos)
|
|
|
|
assert_equal('A', g.current)
|
|
|
|
assert_equal(0, g.pos)
|
|
|
|
assert_equal('A', g.next)
|
|
|
|
|
|
|
|
assert_equal(1, g.pos)
|
|
|
|
assert_equal(true, g.next?)
|
|
|
|
assert_equal(1, g.pos)
|
|
|
|
assert_equal('B', g.current)
|
|
|
|
assert_equal(1, g.pos)
|
|
|
|
assert_equal('B', g.next)
|
|
|
|
|
|
|
|
assert_equal(g, g.rewind)
|
|
|
|
|
|
|
|
assert_equal(0, g.pos)
|
|
|
|
assert_equal('A', g.current)
|
|
|
|
|
|
|
|
assert_equal(true, g.next?)
|
|
|
|
assert_equal(0, g.pos)
|
|
|
|
assert_equal('A', g.current)
|
|
|
|
assert_equal(0, g.pos)
|
|
|
|
assert_equal('A', g.next)
|
|
|
|
|
|
|
|
assert_equal(1, g.pos)
|
|
|
|
assert_equal(true, g.next?)
|
|
|
|
assert_equal(1, g.pos)
|
|
|
|
assert_equal('B', g.current)
|
|
|
|
assert_equal(1, g.pos)
|
|
|
|
assert_equal('B', g.next)
|
|
|
|
|
|
|
|
assert_equal(2, g.pos)
|
|
|
|
assert_equal(true, g.next?)
|
|
|
|
assert_equal(2, g.pos)
|
|
|
|
assert_equal('C', g.current)
|
|
|
|
assert_equal(2, g.pos)
|
|
|
|
assert_equal('C', g.next)
|
|
|
|
|
|
|
|
assert_equal(3, g.pos)
|
|
|
|
assert_equal(true, g.next?)
|
|
|
|
assert_equal(3, g.pos)
|
|
|
|
assert_equal('Z', g.current)
|
|
|
|
assert_equal(3, g.pos)
|
|
|
|
assert_equal('Z', g.next)
|
|
|
|
|
|
|
|
assert_equal(4, g.pos)
|
|
|
|
assert_equal(false, g.next?)
|
|
|
|
assert_raises(EOFError) { g.next }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each
|
|
|
|
a = [5, 6, 7, 8, 9]
|
|
|
|
|
|
|
|
g = Generator.new(a)
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
g.each { |x|
|
|
|
|
assert_equal(a[i], x)
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
break if i == 3
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal(3, i)
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
g.each { |x|
|
|
|
|
assert_equal(a[i], x)
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal(5, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TC_SyncEnumerator < Test::Unit::TestCase
|
|
|
|
def test_each
|
|
|
|
r = ['a'..'f', 1..10, 10..20]
|
|
|
|
ra = r.map { |x| x.to_a }
|
|
|
|
|
|
|
|
a = (0...(ra.map {|x| x.size}.max)).map { |i| ra.map { |x| x[i] } }
|
|
|
|
|
|
|
|
s = SyncEnumerator.new(*r)
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
s.each { |x|
|
|
|
|
assert_equal(a[i], x)
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
break if i == 3
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal(3, i)
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
s.each { |x|
|
|
|
|
assert_equal(a[i], x)
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal(a.size, i)
|
|
|
|
end
|
|
|
|
end
|