headius (owner)

Revisions

gist: 213603 Download_button fork
public
Public Clone URL: git://gist.github.com/213603.git
Embed All Files: show embed
Diff #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
diff --git a/lib/ruby/site_ruby/shared/generator.rb b/lib/ruby/site_ruby/shared/generator.rb
index 1678c49..9aaf5ee 100644
--- a/lib/ruby/site_ruby/shared/generator.rb
+++ b/lib/ruby/site_ruby/shared/generator.rb
@@ -59,121 +59,165 @@ require 'thread'
 class Generator
   include Enumerable
 
- # marks the end of enumeration
- END_MARKER = Object.new
- def END_MARKER.inspect ; "END_MARKER" ; end
-
- # a queue which emulates the producer-side interface of a generator
- class ProducerQueue < SizedQueue
- def initialize
- super(1)
+ # Returns true if the generator has not reached the end yet.
+ def next?()
+ !end?
+ end
+
+ # Returns the current index (position) counting from zero.
+ def index()
+ @index
+ end
+ alias pos index
+
+ # Construct a new generator; defaults to the threaded impl
+ def self.new(*args, &block)
+ generator = (self == Generator) ? Threaded : self
+
+ gen = generator.allocate
+ gen.send :initialize, *args, &block
+ gen
+ end
+
+ class Indexed < Generator
+ SIMPLE_INDEXER = proc {|ary, i| ary[i]}
+ def initialize(ary, &indexer)
+ @ary = ary
+ @index = 0
+ @indexer = indexer || SIMPLE_INDEXER
     end
 
- alias yield push
+ def end?
+ @index >= @ary.size
+ end
 
- def _run_enum(enum)
- _run { enum.each { |x| self.yield(x) } }
+ def next
+ obj, @index = current, @index + 1
+ obj
     end
 
- def _run
- # caller manages thread, to avoid circularity
- Thread.new do
- begin
- yield self
- ensure
- self.yield(END_MARKER)
- end
- end
+ def current
+ raise EOFError, "no more elements available" if end?
+ @indexer.call(@ary, @index)
     end
- end
 
- # 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.
- def initialize(enum = nil, &block)
- @thread.kill if @thread
-
- @queue = ProducerQueue.new
- @got_next_element = false
- @next_element = nil
- @index = 0
-
- @enum = enum
- @block = block
- if enum
- @thread = @queue._run_enum(enum)
- else
- @thread = @queue._run(&block)
+ def rewind
+ @index = 0
     end
 
- self
+ def each
+ for i in 0...@ary.size
+ yield @indexer.call(i)
+ end
+ end
   end
 
- # Yields an element to the generator.
- def yield(value)
- @queue.yield(value)
- self
- end
+ class Threaded < Generator
+ # marks the end of enumeration
+ END_MARKER = Object.new
+ def END_MARKER.inspect ; "END_MARKER" ; end
+
+ # a queue which emulates the producer-side interface of a generator
+ class ProducerQueue < SizedQueue
+ def initialize
+ super(1)
+ end
 
- # gets the next element; may block
- def _next_element
- unless @got_next_element
- @next_element = @queue.pop
- @got_next_element = true
+ alias yield push
+
+ def _run_enum(enum)
+ _run { enum.each { |x| self.yield(x) } }
+ end
+
+ def _run
+ # caller manages thread, to avoid circularity
+ Thread.new do
+ begin
+ yield self
+ ensure
+ self.yield(END_MARKER)
+ end
+ end
+ end
     end
- @next_element
- end
 
- # Returns true if the generator has reached the end.
- def end?()
- END_MARKER.equal? _next_element
- end
+ # 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.
+ def initialize(enum = nil, &block)
+ @thread.kill if @thread
+
+ @queue = ProducerQueue.new
+ @got_next_element = false
+ @next_element = nil
+ @index = 0
+
+ @enum = enum
+ @block = block
+ if enum
+ @thread = @queue._run_enum(enum)
+ else
+ @thread = @queue._run(&block)
+ end
 
- # Returns true if the generator has not reached the end yet.
- def next?()
- !end?
- end
+ self
+ end
 
- # Returns the current index (position) counting from zero.
- def index()
- @index
- end
- alias pos index
+ # Yields an element to the generator.
+ def yield(value)
+ @queue.yield(value)
+ self
+ end
 
- # Returns the element at the current position and moves forward.
- def next()
- result = current
- @index += 1
- @got_next_element = false
- @next_element = nil
- result
- end
+ # gets the next element; may block
+ def _next_element
+ unless @got_next_element
+ @next_element = @queue.pop
+ @got_next_element = true
+ end
+ @next_element
+ end
 
- # Returns the element at the current position.
- def current()
- raise EOFError, "no more elements available" if end?
- _next_element
- end
+ # Returns true if the generator has reached the end.
+ def end?()
+ END_MARKER.equal? _next_element
+ end
 
- # Rewinds the generator.
- def rewind()
- initialize(@enum, &@block) if @index.nonzero?
- self
- end
+ # Returns the element at the current position and moves forward.
+ def next()
+ result = current
+ @index += 1
+ @got_next_element = false
+ @next_element = nil
+ result
+ end
 
- # Rewinds the generator and enumerates the elements.
- def each
- rewind
+ # Returns the element at the current position.
+ def current()
+ raise EOFError, "no more elements available" if end?
+ _next_element
+ end
 
- until end?
- yield self.next
+ # Rewinds the generator.
+ def rewind()
+ initialize(@enum, &@block) if @index.nonzero?
+ self
     end
 
- self
+ # Rewinds the generator and enumerates the elements.
+ def each
+ rewind
+
+ until end?
+ yield self.next
+ end
+
+ self
+ end
   end
 
   if RUBY_VERSION =~ /^1\.9/
@@ -182,11 +226,63 @@ class Generator
     Enumerator = Enumerable::Enumerator
   end
 
+ module Iterators
+ module ClassMethods
+ def indexed_enum(method, &block)
+ extenum_method = :"enum_for_#{method}"
+
+ define_method extenum_method do
+ Generator::Indexed.new(self, &block)
+ end
+
+ iterators[method] = extenum_method
+ end
+
+ def iterators
+ @iterators ||= {}
+ end
+ end
+ def self.included(cls)
+ cls.extend ClassMethods
+ end
+
+ def to_enum(method = :each, *args)
+ if extenum_method = self.class.iterators[method]
+ send extenum_method, *args
+ else
+ super(method, *args)
+ end
+ end
+ end
+
+ class ::Array
+ include Iterators
+ indexed_enum(:each)
+ indexed_enum(:each_with_index) {|a,i| [ a[i], i ]}
+ indexed_enum(:each_index) {|a,i| i}
+ indexed_enum(:reverse_each) {|a,i| a[a.size - i - 1]}
+ end
+
+ class ::Hash
+ include Iterators
+ # these are inefficient and need a place to store 'keys'
+ indexed_enum(:each) {|h,i| keys = k.keys; [ keys[i], h[keys[i]] ]}
+ indexed_enum(:each_with_index) {|h,i| keys = k.keys; [ [ keys[i], h[keys[i]] ], i]}
+ end
+
   class Enumerator
     def __generator
- @generator ||= Generator.new(self)
+ @generator ||= __choose_generator
+ end
+
+ def __choose_generator
+ enum_for_method = :"enum_for_#{@__method__}"
+ if @__object__.respond_to? enum_for_method
+ @__object__.send enum_for_method
+ end
+ return Generator::Threaded.new(self)
     end
- private :__generator
+ private :__generator, :__choose_generator
 
     # call-seq:
     # e.next => object
diff --git a/src/org/jruby/RubyEnumerator.java b/src/org/jruby/RubyEnumerator.java
index bbd05fa..ce3fe45 100644
--- a/src/org/jruby/RubyEnumerator.java
+++ b/src/org/jruby/RubyEnumerator.java
@@ -84,13 +84,12 @@ public class RubyEnumerator extends RubyObject {
     private RubyEnumerator(Ruby runtime, RubyClass type) {
         super(runtime, type);
         object = runtime.getNil();
+ initialize(runtime.getNil(), RubyString.newEmptyString(runtime), IRubyObject.NULL_ARRAY);
     }
 
     private RubyEnumerator(Ruby runtime, IRubyObject object, IRubyObject method, IRubyObject[]args) {
         super(runtime, runtime.getEnumerator());
- this.object = object;
- this.method = method.asJavaString();
- this.methodArgs = args;
+ initialize(object, method, args);
     }
 
     static IRubyObject enumeratorize(Ruby runtime, IRubyObject object, String method) {
@@ -124,6 +123,9 @@ public class RubyEnumerator extends RubyObject {
         this.object = object;
         this.method = method.asJavaString();
         this.methodArgs = methodArgs;
+ setInstanceVariable("@__object__", object);
+ setInstanceVariable("@__method__", method);
+ setInstanceVariable("@__args__", RubyArray.newArrayNoCopyLight(getRuntime(), methodArgs));
         return this;
     }