PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/jruby-1.1.6RC1/lib/ruby/1.8/generator.rb

https://bitbucket.org/nicksieger/advent-jruby
Ruby | 274 lines | 140 code | 54 blank | 80 comment | 11 complexity | 49d12a5e4e78ebd699d7b18c4d2104d2 MD5 | raw file
Possible License(s): CPL-1.0, AGPL-1.0, LGPL-2.1, JSON
  1. #!/usr/bin/env ruby
  2. #--
  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 5479 2008-01-03 21:39:44Z headius $
  6. #++
  7. #
  8. # = generator.rb: convert an internal iterator to an external one
  9. #
  10. # Copyright (c) 2001,2003 Akinori MUSHA <knu@iDaemons.org>
  11. #
  12. # All rights reserved. You can redistribute and/or modify it under
  13. # the same terms as Ruby.
  14. #
  15. # == Overview
  16. #
  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.
  20. #
  21. # The SyncEnumerator class, which is implemented using Generator,
  22. # makes it easy to roll many Enumerable objects synchronously.
  23. #
  24. # See the respective classes for examples of usage.
  25. #
  26. # Generator converts an internal iterator (i.e. an Enumerable object)
  27. # to an external iterator.
  28. #
  29. # Note that it is not very fast since it is implemented using
  30. # continuations, which are currently slow.
  31. #
  32. # == Example
  33. #
  34. # require 'generator'
  35. #
  36. # # Generator from an Enumerable object
  37. # g = Generator.new(['A', 'B', 'C', 'Z'])
  38. #
  39. # while g.next?
  40. # puts g.next
  41. # end
  42. #
  43. # # Generator from a block
  44. # g = Generator.new { |g|
  45. # for i in 'A'..'C'
  46. # g.yield i
  47. # end
  48. #
  49. # g.yield 'Z'
  50. # }
  51. #
  52. # # The same result as above
  53. # while g.next?
  54. # puts g.next
  55. # end
  56. #
  57. require 'generator_internal'
  58. #
  59. # SyncEnumerator creates an Enumerable object from multiple Enumerable
  60. # objects and enumerates them synchronously.
  61. #
  62. # == Example
  63. #
  64. # require 'generator'
  65. #
  66. # s = SyncEnumerator.new([1,2,3], ['a', 'b', 'c'])
  67. #
  68. # # Yields [1, 'a'], [2, 'b'], and [3,'c']
  69. # s.each { |row| puts row.join(', ') }
  70. #
  71. class SyncEnumerator
  72. include Enumerable
  73. # Creates a new SyncEnumerator which enumerates rows of given
  74. # Enumerable objects.
  75. def initialize(*enums)
  76. @gens = enums.map { |e| Generator.new(e) }
  77. end
  78. # Returns the number of enumerated Enumerable objects, i.e. the size
  79. # of each row.
  80. def size
  81. @gens.size
  82. end
  83. # Returns the number of enumerated Enumerable objects, i.e. the size
  84. # of each row.
  85. def length
  86. @gens.length
  87. end
  88. # Returns true if the given nth Enumerable object has reached the
  89. # end. If no argument is given, returns true if any of the
  90. # Enumerable objects has reached the end.
  91. def end?(i = nil)
  92. if i.nil?
  93. @gens.detect { |g| g.end? } ? true : false
  94. else
  95. @gens[i].end?
  96. end
  97. end
  98. # Enumerates rows of the Enumerable objects.
  99. def each
  100. @gens.each { |g| g.rewind }
  101. loop do
  102. count = 0
  103. ret = @gens.map { |g|
  104. if g.end?
  105. count += 1
  106. nil
  107. else
  108. g.next
  109. end
  110. }
  111. if count == @gens.size
  112. break
  113. end
  114. yield ret
  115. end
  116. self
  117. end
  118. end
  119. if $0 == __FILE__
  120. eval DATA.read, nil, $0, __LINE__+4
  121. end
  122. __END__
  123. require 'test/unit'
  124. class TC_Generator < Test::Unit::TestCase
  125. def test_block1
  126. g = Generator.new { |g|
  127. # no yield's
  128. }
  129. assert_equal(0, g.pos)
  130. assert_raises(EOFError) { g.current }
  131. end
  132. def test_block2
  133. g = Generator.new { |g|
  134. for i in 'A'..'C'
  135. g.yield i
  136. end
  137. g.yield 'Z'
  138. }
  139. assert_equal(0, g.pos)
  140. assert_equal('A', g.current)
  141. assert_equal(true, g.next?)
  142. assert_equal(0, g.pos)
  143. assert_equal('A', g.current)
  144. assert_equal(0, g.pos)
  145. assert_equal('A', g.next)
  146. assert_equal(1, g.pos)
  147. assert_equal(true, g.next?)
  148. assert_equal(1, g.pos)
  149. assert_equal('B', g.current)
  150. assert_equal(1, g.pos)
  151. assert_equal('B', g.next)
  152. assert_equal(g, g.rewind)
  153. assert_equal(0, g.pos)
  154. assert_equal('A', g.current)
  155. assert_equal(true, g.next?)
  156. assert_equal(0, g.pos)
  157. assert_equal('A', g.current)
  158. assert_equal(0, g.pos)
  159. assert_equal('A', g.next)
  160. assert_equal(1, g.pos)
  161. assert_equal(true, g.next?)
  162. assert_equal(1, g.pos)
  163. assert_equal('B', g.current)
  164. assert_equal(1, g.pos)
  165. assert_equal('B', g.next)
  166. assert_equal(2, g.pos)
  167. assert_equal(true, g.next?)
  168. assert_equal(2, g.pos)
  169. assert_equal('C', g.current)
  170. assert_equal(2, g.pos)
  171. assert_equal('C', g.next)
  172. assert_equal(3, g.pos)
  173. assert_equal(true, g.next?)
  174. assert_equal(3, g.pos)
  175. assert_equal('Z', g.current)
  176. assert_equal(3, g.pos)
  177. assert_equal('Z', g.next)
  178. assert_equal(4, g.pos)
  179. assert_equal(false, g.next?)
  180. assert_raises(EOFError) { g.next }
  181. end
  182. def test_each
  183. a = [5, 6, 7, 8, 9]
  184. g = Generator.new(a)
  185. i = 0
  186. g.each { |x|
  187. assert_equal(a[i], x)
  188. i += 1
  189. break if i == 3
  190. }
  191. assert_equal(3, i)
  192. i = 0
  193. g.each { |x|
  194. assert_equal(a[i], x)
  195. i += 1
  196. }
  197. assert_equal(5, i)
  198. end
  199. end
  200. class TC_SyncEnumerator < Test::Unit::TestCase
  201. def test_each
  202. r = ['a'..'f', 1..10, 10..20]
  203. ra = r.map { |x| x.to_a }
  204. a = (0...(ra.map {|x| x.size}.max)).map { |i| ra.map { |x| x[i] } }
  205. s = SyncEnumerator.new(*r)
  206. i = 0
  207. s.each { |x|
  208. assert_equal(a[i], x)
  209. i += 1
  210. break if i == 3
  211. }
  212. assert_equal(3, i)
  213. i = 0
  214. s.each { |x|
  215. assert_equal(a[i], x)
  216. i += 1
  217. }
  218. assert_equal(a.size, i)
  219. end
  220. end