Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: generator.rb
#!/usr/bin/env ruby
[0] Fix | Delete
#--
[1] Fix | Delete
# $Idaemons: /home/cvs/rb/generator.rb,v 1.8 2001/10/03 08:54:32 knu Exp $
[2] Fix | Delete
# $RoughId: generator.rb,v 1.10 2003/10/14 19:36:58 knu Exp $
[3] Fix | Delete
# $Id: generator.rb 15954 2008-04-10 10:52:50Z knu $
[4] Fix | Delete
#++
[5] Fix | Delete
#
[6] Fix | Delete
# = generator.rb: convert an internal iterator to an external one
[7] Fix | Delete
#
[8] Fix | Delete
# Copyright (c) 2001,2003 Akinori MUSHA <knu@iDaemons.org>
[9] Fix | Delete
#
[10] Fix | Delete
# All rights reserved. You can redistribute and/or modify it under
[11] Fix | Delete
# the same terms as Ruby.
[12] Fix | Delete
#
[13] Fix | Delete
# == Overview
[14] Fix | Delete
#
[15] Fix | Delete
# This library provides the Generator class, which converts an
[16] Fix | Delete
# internal iterator (i.e. an Enumerable object) to an external
[17] Fix | Delete
# iterator. In that form, you can roll many iterators independently.
[18] Fix | Delete
#
[19] Fix | Delete
# The SyncEnumerator class, which is implemented using Generator,
[20] Fix | Delete
# makes it easy to roll many Enumerable objects synchronously.
[21] Fix | Delete
#
[22] Fix | Delete
# See the respective classes for examples of usage.
[23] Fix | Delete
[24] Fix | Delete
[25] Fix | Delete
#
[26] Fix | Delete
# Generator converts an internal iterator (i.e. an Enumerable object)
[27] Fix | Delete
# to an external iterator.
[28] Fix | Delete
#
[29] Fix | Delete
# Note that it is not very fast since it is implemented using
[30] Fix | Delete
# continuations, which are currently slow.
[31] Fix | Delete
#
[32] Fix | Delete
# == Example
[33] Fix | Delete
#
[34] Fix | Delete
# require 'generator'
[35] Fix | Delete
#
[36] Fix | Delete
# # Generator from an Enumerable object
[37] Fix | Delete
# g = Generator.new(['A', 'B', 'C', 'Z'])
[38] Fix | Delete
#
[39] Fix | Delete
# while g.next?
[40] Fix | Delete
# puts g.next
[41] Fix | Delete
# end
[42] Fix | Delete
#
[43] Fix | Delete
# # Generator from a block
[44] Fix | Delete
# g = Generator.new { |g|
[45] Fix | Delete
# for i in 'A'..'C'
[46] Fix | Delete
# g.yield i
[47] Fix | Delete
# end
[48] Fix | Delete
#
[49] Fix | Delete
# g.yield 'Z'
[50] Fix | Delete
# }
[51] Fix | Delete
#
[52] Fix | Delete
# # The same result as above
[53] Fix | Delete
# while g.next?
[54] Fix | Delete
# puts g.next
[55] Fix | Delete
# end
[56] Fix | Delete
#
[57] Fix | Delete
class Generator
[58] Fix | Delete
include Enumerable
[59] Fix | Delete
[60] Fix | Delete
# Creates a new generator either from an Enumerable object or from a
[61] Fix | Delete
# block.
[62] Fix | Delete
#
[63] Fix | Delete
# In the former, block is ignored even if given.
[64] Fix | Delete
#
[65] Fix | Delete
# In the latter, the given block is called with the generator
[66] Fix | Delete
# itself, and expected to call the +yield+ method for each element.
[67] Fix | Delete
def initialize(enum = nil, &block)
[68] Fix | Delete
if enum
[69] Fix | Delete
@block = proc { |g|
[70] Fix | Delete
enum.each { |x| g.yield x }
[71] Fix | Delete
}
[72] Fix | Delete
else
[73] Fix | Delete
@block = block
[74] Fix | Delete
end
[75] Fix | Delete
[76] Fix | Delete
@index = 0
[77] Fix | Delete
@queue = []
[78] Fix | Delete
@cont_next = @cont_yield = @cont_endp = nil
[79] Fix | Delete
[80] Fix | Delete
if @cont_next = callcc { |c| c }
[81] Fix | Delete
@block.call(self)
[82] Fix | Delete
[83] Fix | Delete
@cont_endp.call(nil) if @cont_endp
[84] Fix | Delete
end
[85] Fix | Delete
[86] Fix | Delete
self
[87] Fix | Delete
end
[88] Fix | Delete
[89] Fix | Delete
# Yields an element to the generator.
[90] Fix | Delete
def yield(value)
[91] Fix | Delete
if @cont_yield = callcc { |c| c }
[92] Fix | Delete
@queue << value
[93] Fix | Delete
@cont_next.call(nil)
[94] Fix | Delete
end
[95] Fix | Delete
[96] Fix | Delete
self
[97] Fix | Delete
end
[98] Fix | Delete
[99] Fix | Delete
# Returns true if the generator has reached the end.
[100] Fix | Delete
def end?()
[101] Fix | Delete
if @cont_endp = callcc { |c| c }
[102] Fix | Delete
@cont_yield.nil? && @queue.empty?
[103] Fix | Delete
else
[104] Fix | Delete
@queue.empty?
[105] Fix | Delete
end
[106] Fix | Delete
end
[107] Fix | Delete
[108] Fix | Delete
# Returns true if the generator has not reached the end yet.
[109] Fix | Delete
def next?()
[110] Fix | Delete
!end?
[111] Fix | Delete
end
[112] Fix | Delete
[113] Fix | Delete
# Returns the current index (position) counting from zero.
[114] Fix | Delete
def index()
[115] Fix | Delete
@index
[116] Fix | Delete
end
[117] Fix | Delete
[118] Fix | Delete
# Returns the current index (position) counting from zero.
[119] Fix | Delete
def pos()
[120] Fix | Delete
@index
[121] Fix | Delete
end
[122] Fix | Delete
[123] Fix | Delete
# Returns the element at the current position and moves forward.
[124] Fix | Delete
def next()
[125] Fix | Delete
if end?
[126] Fix | Delete
raise EOFError, "no more elements available"
[127] Fix | Delete
end
[128] Fix | Delete
[129] Fix | Delete
if @cont_next = callcc { |c| c }
[130] Fix | Delete
@cont_yield.call(nil) if @cont_yield
[131] Fix | Delete
end
[132] Fix | Delete
[133] Fix | Delete
@index += 1
[134] Fix | Delete
[135] Fix | Delete
@queue.shift
[136] Fix | Delete
end
[137] Fix | Delete
[138] Fix | Delete
# Returns the element at the current position.
[139] Fix | Delete
def current()
[140] Fix | Delete
if @queue.empty?
[141] Fix | Delete
raise EOFError, "no more elements available"
[142] Fix | Delete
end
[143] Fix | Delete
[144] Fix | Delete
@queue.first
[145] Fix | Delete
end
[146] Fix | Delete
[147] Fix | Delete
# Rewinds the generator.
[148] Fix | Delete
def rewind()
[149] Fix | Delete
initialize(nil, &@block) if @index.nonzero?
[150] Fix | Delete
[151] Fix | Delete
self
[152] Fix | Delete
end
[153] Fix | Delete
[154] Fix | Delete
# Rewinds the generator and enumerates the elements.
[155] Fix | Delete
def each
[156] Fix | Delete
rewind
[157] Fix | Delete
[158] Fix | Delete
until end?
[159] Fix | Delete
yield self.next
[160] Fix | Delete
end
[161] Fix | Delete
[162] Fix | Delete
self
[163] Fix | Delete
end
[164] Fix | Delete
end
[165] Fix | Delete
[166] Fix | Delete
class Enumerable::Enumerator
[167] Fix | Delete
def __generator
[168] Fix | Delete
@generator ||= Generator.new(self)
[169] Fix | Delete
end
[170] Fix | Delete
private :__generator
[171] Fix | Delete
[172] Fix | Delete
# call-seq:
[173] Fix | Delete
# e.next => object
[174] Fix | Delete
#
[175] Fix | Delete
# Returns the next object in the enumerator, and move the internal
[176] Fix | Delete
# position forward. When the position reached at the end, internal
[177] Fix | Delete
# position is rewinded then StopIteration is raised.
[178] Fix | Delete
#
[179] Fix | Delete
# Note that enumeration sequence by next method does not affect other
[180] Fix | Delete
# non-external enumeration methods, unless underlying iteration
[181] Fix | Delete
# methods itself has side-effect, e.g. IO#each_line.
[182] Fix | Delete
#
[183] Fix | Delete
# Caution: This feature internally uses Generator, which uses callcc
[184] Fix | Delete
# to stop and resume enumeration to fetch each value. Use with care
[185] Fix | Delete
# and be aware of the performance loss.
[186] Fix | Delete
def next
[187] Fix | Delete
g = __generator
[188] Fix | Delete
return g.next unless g.end?
[189] Fix | Delete
[190] Fix | Delete
g.rewind
[191] Fix | Delete
raise StopIteration, 'iteration reached at end'
[192] Fix | Delete
end
[193] Fix | Delete
[194] Fix | Delete
# call-seq:
[195] Fix | Delete
# e.rewind => e
[196] Fix | Delete
#
[197] Fix | Delete
# Rewinds the enumeration sequence by the next method.
[198] Fix | Delete
def rewind
[199] Fix | Delete
__generator.rewind
[200] Fix | Delete
self
[201] Fix | Delete
end
[202] Fix | Delete
end
[203] Fix | Delete
[204] Fix | Delete
#
[205] Fix | Delete
# SyncEnumerator creates an Enumerable object from multiple Enumerable
[206] Fix | Delete
# objects and enumerates them synchronously.
[207] Fix | Delete
#
[208] Fix | Delete
# == Example
[209] Fix | Delete
#
[210] Fix | Delete
# require 'generator'
[211] Fix | Delete
#
[212] Fix | Delete
# s = SyncEnumerator.new([1,2,3], ['a', 'b', 'c'])
[213] Fix | Delete
#
[214] Fix | Delete
# # Yields [1, 'a'], [2, 'b'], and [3,'c']
[215] Fix | Delete
# s.each { |row| puts row.join(', ') }
[216] Fix | Delete
#
[217] Fix | Delete
class SyncEnumerator
[218] Fix | Delete
include Enumerable
[219] Fix | Delete
[220] Fix | Delete
# Creates a new SyncEnumerator which enumerates rows of given
[221] Fix | Delete
# Enumerable objects.
[222] Fix | Delete
def initialize(*enums)
[223] Fix | Delete
@gens = enums.map { |e| Generator.new(e) }
[224] Fix | Delete
end
[225] Fix | Delete
[226] Fix | Delete
# Returns the number of enumerated Enumerable objects, i.e. the size
[227] Fix | Delete
# of each row.
[228] Fix | Delete
def size
[229] Fix | Delete
@gens.size
[230] Fix | Delete
end
[231] Fix | Delete
[232] Fix | Delete
# Returns the number of enumerated Enumerable objects, i.e. the size
[233] Fix | Delete
# of each row.
[234] Fix | Delete
def length
[235] Fix | Delete
@gens.length
[236] Fix | Delete
end
[237] Fix | Delete
[238] Fix | Delete
# Returns true if the given nth Enumerable object has reached the
[239] Fix | Delete
# end. If no argument is given, returns true if any of the
[240] Fix | Delete
# Enumerable objects has reached the end.
[241] Fix | Delete
def end?(i = nil)
[242] Fix | Delete
if i.nil?
[243] Fix | Delete
@gens.detect { |g| g.end? } ? true : false
[244] Fix | Delete
else
[245] Fix | Delete
@gens[i].end?
[246] Fix | Delete
end
[247] Fix | Delete
end
[248] Fix | Delete
[249] Fix | Delete
# Enumerates rows of the Enumerable objects.
[250] Fix | Delete
def each
[251] Fix | Delete
@gens.each { |g| g.rewind }
[252] Fix | Delete
[253] Fix | Delete
loop do
[254] Fix | Delete
count = 0
[255] Fix | Delete
[256] Fix | Delete
ret = @gens.map { |g|
[257] Fix | Delete
if g.end?
[258] Fix | Delete
count += 1
[259] Fix | Delete
nil
[260] Fix | Delete
else
[261] Fix | Delete
g.next
[262] Fix | Delete
end
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
if count == @gens.size
[266] Fix | Delete
break
[267] Fix | Delete
end
[268] Fix | Delete
[269] Fix | Delete
yield ret
[270] Fix | Delete
end
[271] Fix | Delete
[272] Fix | Delete
self
[273] Fix | Delete
end
[274] Fix | Delete
end
[275] Fix | Delete
[276] Fix | Delete
if $0 == __FILE__
[277] Fix | Delete
eval DATA.read, nil, $0, __LINE__+4
[278] Fix | Delete
end
[279] Fix | Delete
[280] Fix | Delete
__END__
[281] Fix | Delete
[282] Fix | Delete
require 'test/unit'
[283] Fix | Delete
[284] Fix | Delete
class TC_Generator < Test::Unit::TestCase
[285] Fix | Delete
def test_block1
[286] Fix | Delete
g = Generator.new { |g|
[287] Fix | Delete
# no yield's
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
assert_equal(0, g.pos)
[291] Fix | Delete
assert_raises(EOFError) { g.current }
[292] Fix | Delete
end
[293] Fix | Delete
[294] Fix | Delete
def test_block2
[295] Fix | Delete
g = Generator.new { |g|
[296] Fix | Delete
for i in 'A'..'C'
[297] Fix | Delete
g.yield i
[298] Fix | Delete
end
[299] Fix | Delete
[300] Fix | Delete
g.yield 'Z'
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
assert_equal(0, g.pos)
[304] Fix | Delete
assert_equal('A', g.current)
[305] Fix | Delete
[306] Fix | Delete
assert_equal(true, g.next?)
[307] Fix | Delete
assert_equal(0, g.pos)
[308] Fix | Delete
assert_equal('A', g.current)
[309] Fix | Delete
assert_equal(0, g.pos)
[310] Fix | Delete
assert_equal('A', g.next)
[311] Fix | Delete
[312] Fix | Delete
assert_equal(1, g.pos)
[313] Fix | Delete
assert_equal(true, g.next?)
[314] Fix | Delete
assert_equal(1, g.pos)
[315] Fix | Delete
assert_equal('B', g.current)
[316] Fix | Delete
assert_equal(1, g.pos)
[317] Fix | Delete
assert_equal('B', g.next)
[318] Fix | Delete
[319] Fix | Delete
assert_equal(g, g.rewind)
[320] Fix | Delete
[321] Fix | Delete
assert_equal(0, g.pos)
[322] Fix | Delete
assert_equal('A', g.current)
[323] Fix | Delete
[324] Fix | Delete
assert_equal(true, g.next?)
[325] Fix | Delete
assert_equal(0, g.pos)
[326] Fix | Delete
assert_equal('A', g.current)
[327] Fix | Delete
assert_equal(0, g.pos)
[328] Fix | Delete
assert_equal('A', g.next)
[329] Fix | Delete
[330] Fix | Delete
assert_equal(1, g.pos)
[331] Fix | Delete
assert_equal(true, g.next?)
[332] Fix | Delete
assert_equal(1, g.pos)
[333] Fix | Delete
assert_equal('B', g.current)
[334] Fix | Delete
assert_equal(1, g.pos)
[335] Fix | Delete
assert_equal('B', g.next)
[336] Fix | Delete
[337] Fix | Delete
assert_equal(2, g.pos)
[338] Fix | Delete
assert_equal(true, g.next?)
[339] Fix | Delete
assert_equal(2, g.pos)
[340] Fix | Delete
assert_equal('C', g.current)
[341] Fix | Delete
assert_equal(2, g.pos)
[342] Fix | Delete
assert_equal('C', g.next)
[343] Fix | Delete
[344] Fix | Delete
assert_equal(3, g.pos)
[345] Fix | Delete
assert_equal(true, g.next?)
[346] Fix | Delete
assert_equal(3, g.pos)
[347] Fix | Delete
assert_equal('Z', g.current)
[348] Fix | Delete
assert_equal(3, g.pos)
[349] Fix | Delete
assert_equal('Z', g.next)
[350] Fix | Delete
[351] Fix | Delete
assert_equal(4, g.pos)
[352] Fix | Delete
assert_equal(false, g.next?)
[353] Fix | Delete
assert_raises(EOFError) { g.next }
[354] Fix | Delete
end
[355] Fix | Delete
[356] Fix | Delete
def test_each
[357] Fix | Delete
a = [5, 6, 7, 8, 9]
[358] Fix | Delete
[359] Fix | Delete
g = Generator.new(a)
[360] Fix | Delete
[361] Fix | Delete
i = 0
[362] Fix | Delete
[363] Fix | Delete
g.each { |x|
[364] Fix | Delete
assert_equal(a[i], x)
[365] Fix | Delete
[366] Fix | Delete
i += 1
[367] Fix | Delete
[368] Fix | Delete
break if i == 3
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
assert_equal(3, i)
[372] Fix | Delete
[373] Fix | Delete
i = 0
[374] Fix | Delete
[375] Fix | Delete
g.each { |x|
[376] Fix | Delete
assert_equal(a[i], x)
[377] Fix | Delete
[378] Fix | Delete
i += 1
[379] Fix | Delete
}
[380] Fix | Delete
[381] Fix | Delete
assert_equal(5, i)
[382] Fix | Delete
end
[383] Fix | Delete
end
[384] Fix | Delete
[385] Fix | Delete
class TC_SyncEnumerator < Test::Unit::TestCase
[386] Fix | Delete
def test_each
[387] Fix | Delete
r = ['a'..'f', 1..10, 10..20]
[388] Fix | Delete
ra = r.map { |x| x.to_a }
[389] Fix | Delete
[390] Fix | Delete
a = (0...(ra.map {|x| x.size}.max)).map { |i| ra.map { |x| x[i] } }
[391] Fix | Delete
[392] Fix | Delete
s = SyncEnumerator.new(*r)
[393] Fix | Delete
[394] Fix | Delete
i = 0
[395] Fix | Delete
[396] Fix | Delete
s.each { |x|
[397] Fix | Delete
assert_equal(a[i], x)
[398] Fix | Delete
[399] Fix | Delete
i += 1
[400] Fix | Delete
[401] Fix | Delete
break if i == 3
[402] Fix | Delete
}
[403] Fix | Delete
[404] Fix | Delete
assert_equal(3, i)
[405] Fix | Delete
[406] Fix | Delete
i = 0
[407] Fix | Delete
[408] Fix | Delete
s.each { |x|
[409] Fix | Delete
assert_equal(a[i], x)
[410] Fix | Delete
[411] Fix | Delete
i += 1
[412] Fix | Delete
}
[413] Fix | Delete
[414] Fix | Delete
assert_equal(a.size, i)
[415] Fix | Delete
end
[416] Fix | Delete
end
[417] Fix | Delete
[418] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function