Ruby  2.0.0p353(2013-11-22revision43784)
enumerator.c
Go to the documentation of this file.
1 /************************************************
2 
3  enumerator.c - provides Enumerator class
4 
5  $Author: nagachika $
6 
7  Copyright (C) 2001-2003 Akinori MUSHA
8 
9  $Idaemons: /home/cvs/rb/enumerator/enumerator.c,v 1.1.1.1 2001/07/15 10:12:48 knu Exp $
10  $RoughId: enumerator.c,v 1.6 2003/07/27 11:03:24 nobu Exp $
11  $Id: enumerator.c 42935 2013-09-13 14:18:33Z nagachika $
12 
13 ************************************************/
14 
15 #include "ruby/ruby.h"
16 #include "node.h"
17 #include "internal.h"
18 
19 /*
20  * Document-class: Enumerator
21  *
22  * A class which allows both internal and external iteration.
23  *
24  * An Enumerator can be created by the following methods.
25  * - Kernel#to_enum
26  * - Kernel#enum_for
27  * - Enumerator.new
28  *
29  * Most methods have two forms: a block form where the contents
30  * are evaluated for each item in the enumeration, and a non-block form
31  * which returns a new Enumerator wrapping the iteration.
32  *
33  * enumerator = %w(one two three).each
34  * puts enumerator.class # => Enumerator
35  *
36  * enumerator.each_with_object("foo") do |item, obj|
37  * puts "#{obj}: #{item}"
38  * end
39  *
40  * # foo: one
41  * # foo: two
42  * # foo: three
43  *
44  * enum_with_obj = enumerator.each_with_object("foo")
45  * puts enum_with_obj.class # => Enumerator
46  *
47  * enum_with_obj.each do |item, obj|
48  * puts "#{obj}: #{item}"
49  * end
50  *
51  * # foo: one
52  * # foo: two
53  * # foo: three
54  *
55  * This allows you to chain Enumerators together. For example, you
56  * can map a list's elements to strings containing the index
57  * and the element as a string via:
58  *
59  * puts %w[foo bar baz].map.with_index { |w, i| "#{i}:#{w}" }
60  * # => ["0:foo", "1:bar", "2:baz"]
61  *
62  * An Enumerator can also be used as an external iterator.
63  * For example, Enumerator#next returns the next value of the iterator
64  * or raises StopIteration if the Enumerator is at the end.
65  *
66  * e = [1,2,3].each # returns an enumerator object.
67  * puts e.next # => 1
68  * puts e.next # => 2
69  * puts e.next # => 3
70  * puts e.next # raises StopIteration
71  *
72  * You can use this to implement an internal iterator as follows:
73  *
74  * def ext_each(e)
75  * while true
76  * begin
77  * vs = e.next_values
78  * rescue StopIteration
79  * return $!.result
80  * end
81  * y = yield(*vs)
82  * e.feed y
83  * end
84  * end
85  *
86  * o = Object.new
87  *
88  * def o.each
89  * puts yield
90  * puts yield(1)
91  * puts yield(1, 2)
92  * 3
93  * end
94  *
95  * # use o.each as an internal iterator directly.
96  * puts o.each {|*x| puts x; [:b, *x] }
97  * # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
98  *
99  * # convert o.each to an external iterator for
100  * # implementing an internal iterator.
101  * puts ext_each(o.to_enum) {|*x| puts x; [:b, *x] }
102  * # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
103  *
104  */
110 
112 
113 struct enumerator {
124 };
125 
127 
128 struct generator {
130 };
131 
132 struct yielder {
134 };
135 
136 static VALUE generator_allocate(VALUE klass);
137 static VALUE generator_init(VALUE obj, VALUE proc);
138 
139 /*
140  * Enumerator
141  */
142 static void
144 {
145  struct enumerator *ptr = p;
146  rb_gc_mark(ptr->obj);
147  rb_gc_mark(ptr->args);
148  rb_gc_mark(ptr->fib);
149  rb_gc_mark(ptr->dst);
150  rb_gc_mark(ptr->lookahead);
151  rb_gc_mark(ptr->feedvalue);
152  rb_gc_mark(ptr->stop_exc);
153  rb_gc_mark(ptr->size);
154 }
155 
156 #define enumerator_free RUBY_TYPED_DEFAULT_FREE
157 
158 static size_t
159 enumerator_memsize(const void *p)
160 {
161  return p ? sizeof(struct enumerator) : 0;
162 }
163 
165  "enumerator",
166  {
170  },
171 };
172 
173 static struct enumerator *
175 {
176  struct enumerator *ptr;
177 
179  if (!ptr || ptr->obj == Qundef) {
180  rb_raise(rb_eArgError, "uninitialized enumerator");
181  }
182  return ptr;
183 }
184 
185 /*
186  * call-seq:
187  * obj.to_enum(method = :each, *args) -> enum
188  * obj.enum_for(method = :each, *args) -> enum
189  * obj.to_enum(method = :each, *args) {|*args| block} -> enum
190  * obj.enum_for(method = :each, *args){|*args| block} -> enum
191  *
192  * Creates a new Enumerator which will enumerate by calling +method+ on
193  * +obj+, passing +args+ if any.
194  *
195  * If a block is given, it will be used to calculate the size of
196  * the enumerator without the need to iterate it (see Enumerator#size).
197  *
198  * === Examples
199  *
200  * str = "xyz"
201  *
202  * enum = str.enum_for(:each_byte)
203  * enum.each { |b| puts b }
204  * # => 120
205  * # => 121
206  * # => 122
207  *
208  * # protect an array from being modified by some_method
209  * a = [1, 2, 3]
210  * some_method(a.to_enum)
211  *
212  * It is typical to call to_enum when defining methods for
213  * a generic Enumerable, in case no block is passed.
214  *
215  * Here is such an example, with parameter passing and a sizing block:
216  *
217  * module Enumerable
218  * # a generic method to repeat the values of any enumerable
219  * def repeat(n)
220  * raise ArgumentError, "#{n} is negative!" if n < 0
221  * unless block_given?
222  * return to_enum(__method__, n) do # __method__ is :repeat here
223  * sz = size # Call size and multiply by n...
224  * sz * n if sz # but return nil if size itself is nil
225  * end
226  * end
227  * each do |*val|
228  * n.times { yield *val }
229  * end
230  * end
231  * end
232  *
233  * %i[hello world].repeat(2) { |w| puts w }
234  * # => Prints 'hello', 'hello', 'world', 'world'
235  * enum = (1..14).repeat(3)
236  * # => returns an Enumerator when called without a block
237  * enum.first(4) # => [1, 1, 1, 2]
238  * enum.size # => 42
239  */
240 static VALUE
242 {
244 
245  if (argc > 0) {
246  --argc;
247  meth = *argv++;
248  }
249  enumerator = rb_enumeratorize_with_size(obj, meth, argc, argv, 0);
250  if (rb_block_given_p()) {
251  enumerator_ptr(enumerator)->size = rb_block_proc();
252  }
253  return enumerator;
254 }
255 
256 static VALUE
258 {
259  struct enumerator *ptr;
260  VALUE enum_obj;
261 
262  enum_obj = TypedData_Make_Struct(klass, struct enumerator, &enumerator_data_type, ptr);
263  ptr->obj = Qundef;
264 
265  return enum_obj;
266 }
267 
268 static VALUE
270 {
271  struct enumerator *ptr;
272 
273  TypedData_Get_Struct(enum_obj, struct enumerator, &enumerator_data_type, ptr);
274 
275  if (!ptr) {
276  rb_raise(rb_eArgError, "unallocated enumerator");
277  }
278 
279  ptr->obj = obj;
280  ptr->meth = rb_to_id(meth);
281  if (argc) ptr->args = rb_ary_new4(argc, argv);
282  ptr->fib = 0;
283  ptr->dst = Qnil;
284  ptr->lookahead = Qundef;
285  ptr->feedvalue = Qundef;
286  ptr->stop_exc = Qfalse;
287  ptr->size = size;
288  ptr->size_fn = size_fn;
289 
290  return enum_obj;
291 }
292 
293 /*
294  * call-seq:
295  * Enumerator.new(size = nil) { |yielder| ... }
296  * Enumerator.new(obj, method = :each, *args)
297  *
298  * Creates a new Enumerator object, which can be used as an
299  * Enumerable.
300  *
301  * In the first form, iteration is defined by the given block, in
302  * which a "yielder" object, given as block parameter, can be used to
303  * yield a value by calling the +yield+ method (aliased as +<<+):
304  *
305  * fib = Enumerator.new do |y|
306  * a = b = 1
307  * loop do
308  * y << a
309  * a, b = b, a + b
310  * end
311  * end
312  *
313  * p fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
314  *
315  * The optional parameter can be used to specify how to calculate the size
316  * in a lazy fashion (see Enumerator#size). It can either be a value or
317  * a callable object.
318  *
319  * In the second, deprecated, form, a generated Enumerator iterates over the
320  * given object using the given method with the given arguments passed.
321  *
322  * Use of this form is discouraged. Use Kernel#enum_for or Kernel#to_enum
323  * instead.
324  *
325  * e = Enumerator.new(ObjectSpace, :each_object)
326  * #-> ObjectSpace.enum_for(:each_object)
327  *
328  * e.select { |obj| obj.is_a?(Class) } #=> array of all classes
329  *
330  */
331 static VALUE
332 enumerator_initialize(int argc, VALUE *argv, VALUE obj)
333 {
334  VALUE recv, meth = sym_each;
335  VALUE size = Qnil;
336 
337  if (rb_block_given_p()) {
338  rb_check_arity(argc, 0, 1);
340  if (argc) {
341  if (NIL_P(argv[0]) || rb_obj_is_proc(argv[0]) ||
342  (RB_TYPE_P(argv[0], T_FLOAT) && RFLOAT_VALUE(argv[0]) == INFINITY)) {
343  size = argv[0];
344  } else {
345  size = rb_to_int(argv[0]);
346  }
347  argc = 0;
348  }
349  }
350  else {
352  rb_warn("Enumerator.new without a block is deprecated; use Object#to_enum");
353  recv = *argv++;
354  if (--argc) {
355  meth = *argv++;
356  --argc;
357  }
358  }
359 
360  return enumerator_init(obj, recv, meth, argc, argv, 0, size);
361 }
362 
363 /* :nodoc: */
364 static VALUE
366 {
367  struct enumerator *ptr0, *ptr1;
368 
369  if (!OBJ_INIT_COPY(obj, orig)) return obj;
370  ptr0 = enumerator_ptr(orig);
371  if (ptr0->fib) {
372  /* Fibers cannot be copied */
373  rb_raise(rb_eTypeError, "can't copy execution context");
374  }
375 
377 
378  if (!ptr1) {
379  rb_raise(rb_eArgError, "unallocated enumerator");
380  }
381 
382  ptr1->obj = ptr0->obj;
383  ptr1->meth = ptr0->meth;
384  ptr1->args = ptr0->args;
385  ptr1->fib = 0;
386  ptr1->lookahead = Qundef;
387  ptr1->feedvalue = Qundef;
388  ptr1->size = ptr0->size;
389  ptr1->size_fn = ptr0->size_fn;
390 
391  return obj;
392 }
393 
394 /*
395  * For backwards compatibility; use rb_enumeratorize_with_size
396  */
397 VALUE
398 rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv)
399 {
400  return rb_enumeratorize_with_size(obj, meth, argc, argv, 0);
401 }
402 
403 static VALUE
404 lazy_to_enum_i(VALUE self, VALUE meth, int argc, VALUE *argv, VALUE (*size_fn)(ANYARGS));
405 
406 VALUE
408 {
409  /* Similar effect as calling obj.to_enum, i.e. dispatching to either
410  Kernel#to_enum vs Lazy#to_enum */
411  if (RTEST(rb_obj_is_kind_of(obj, rb_cLazy)))
412  return lazy_to_enum_i(obj, meth, argc, argv, size_fn);
413  else
415  obj, meth, argc, argv, size_fn, Qnil);
416 }
417 
418 static VALUE
420 {
421  int argc = 0;
422  VALUE *argv = 0;
423  const struct enumerator *e = enumerator_ptr(obj);
424  ID meth = e->meth;
425 
426  if (e->args) {
427  argc = RARRAY_LENINT(e->args);
428  argv = RARRAY_PTR(e->args);
429  }
430  return rb_block_call(e->obj, meth, argc, argv, func, arg);
431 }
432 
433 /*
434  * call-seq:
435  * enum.each {...}
436  *
437  * Iterates over the block according to how this Enumerable was constructed.
438  * If no block is given, returns self.
439  *
440  */
441 static VALUE
442 enumerator_each(int argc, VALUE *argv, VALUE obj)
443 {
444  if (argc > 0) {
445  struct enumerator *e = enumerator_ptr(obj = rb_obj_dup(obj));
446  VALUE args = e->args;
447  if (args) {
448  args = rb_ary_dup(args);
449  rb_ary_cat(args, argv, argc);
450  }
451  else {
452  args = rb_ary_new4(argc, argv);
453  }
454  e->args = args;
455  }
456  if (!rb_block_given_p()) return obj;
457  return enumerator_block_call(obj, 0, obj);
458 }
459 
460 static VALUE
462 {
463  VALUE idx;
464  VALUE *memo = (VALUE *)m;
465 
466  idx = INT2FIX(*memo);
467  ++*memo;
468 
469  if (argc <= 1)
470  return rb_yield_values(2, val, idx);
471 
472  return rb_yield_values(2, rb_ary_new4(argc, argv), idx);
473 }
474 
475 static VALUE
477 
478 /*
479  * call-seq:
480  * e.with_index(offset = 0) {|(*args), idx| ... }
481  * e.with_index(offset = 0)
482  *
483  * Iterates the given block for each element with an index, which
484  * starts from +offset+. If no block is given, returns a new Enumerator
485  * that includes the index, starting from +offset+
486  *
487  * +offset+:: the starting index to use
488  *
489  */
490 static VALUE
491 enumerator_with_index(int argc, VALUE *argv, VALUE obj)
492 {
493  VALUE memo;
494 
495  rb_scan_args(argc, argv, "01", &memo);
496  RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_size);
497  memo = NIL_P(memo) ? 0 : (VALUE)NUM2LONG(memo);
499 }
500 
501 /*
502  * call-seq:
503  * e.each_with_index {|(*args), idx| ... }
504  * e.each_with_index
505  *
506  * Same as Enumerator#with_index(0), i.e. there is no starting offset.
507  *
508  * If no block is given, a new Enumerator is returned that includes the index.
509  *
510  */
511 static VALUE
513 {
514  return enumerator_with_index(0, NULL, obj);
515 }
516 
517 static VALUE
519 {
520  if (argc <= 1)
521  return rb_yield_values(2, val, memo);
522 
523  return rb_yield_values(2, rb_ary_new4(argc, argv), memo);
524 }
525 
526 /*
527  * call-seq:
528  * e.with_object(obj) {|(*args), obj| ... }
529  * e.with_object(obj)
530  *
531  * Iterates the given block for each element with an arbitrary object, +obj+,
532  * and returns +obj+
533  *
534  * If no block is given, returns a new Enumerator.
535  *
536  * === Example
537  *
538  * to_three = Enumerator.new do |y|
539  * 3.times do |x|
540  * y << x
541  * end
542  * end
543  *
544  * to_three_with_string = to_three.with_object("foo")
545  * to_three_with_string.each do |x,string|
546  * puts "#{string}: #{x}"
547  * end
548  *
549  * # => foo:0
550  * # => foo:1
551  * # => foo:2
552  */
553 static VALUE
555 {
558 
559  return memo;
560 }
561 
562 static VALUE
563 next_ii(VALUE i, VALUE obj, int argc, VALUE *argv)
564 {
565  struct enumerator *e = enumerator_ptr(obj);
566  VALUE feedvalue = Qnil;
567  VALUE args = rb_ary_new4(argc, argv);
568  rb_fiber_yield(1, &args);
569  if (e->feedvalue != Qundef) {
570  feedvalue = e->feedvalue;
571  e->feedvalue = Qundef;
572  }
573  return feedvalue;
574 }
575 
576 static VALUE
577 next_i(VALUE curr, VALUE obj)
578 {
579  struct enumerator *e = enumerator_ptr(obj);
580  VALUE nil = Qnil;
581  VALUE result;
582 
583  result = rb_block_call(obj, id_each, 0, 0, next_ii, obj);
584  e->stop_exc = rb_exc_new2(rb_eStopIteration, "iteration reached an end");
585  rb_ivar_set(e->stop_exc, id_result, result);
586  return rb_fiber_yield(1, &nil);
587 }
588 
589 static void
590 next_init(VALUE obj, struct enumerator *e)
591 {
592  VALUE curr = rb_fiber_current();
593  e->dst = curr;
594  e->fib = rb_fiber_new(next_i, obj);
595  e->lookahead = Qundef;
596 }
597 
598 static VALUE
600 {
601  VALUE curr, vs;
602 
603  if (e->stop_exc)
605 
606  curr = rb_fiber_current();
607 
608  if (!e->fib || !rb_fiber_alive_p(e->fib)) {
609  next_init(obj, e);
610  }
611 
612  vs = rb_fiber_resume(e->fib, 1, &curr);
613  if (e->stop_exc) {
614  e->fib = 0;
615  e->dst = Qnil;
616  e->lookahead = Qundef;
617  e->feedvalue = Qundef;
619  }
620  return vs;
621 }
622 
623 /*
624  * call-seq:
625  * e.next_values -> array
626  *
627  * Returns the next object as an array in the enumerator, and move the
628  * internal position forward. When the position reached at the end,
629  * StopIteration is raised.
630  *
631  * This method can be used to distinguish <code>yield</code> and <code>yield
632  * nil</code>.
633  *
634  * === Example
635  *
636  * o = Object.new
637  * def o.each
638  * yield
639  * yield 1
640  * yield 1, 2
641  * yield nil
642  * yield [1, 2]
643  * end
644  * e = o.to_enum
645  * p e.next_values
646  * p e.next_values
647  * p e.next_values
648  * p e.next_values
649  * p e.next_values
650  * e = o.to_enum
651  * p e.next
652  * p e.next
653  * p e.next
654  * p e.next
655  * p e.next
656  *
657  * ## yield args next_values next
658  * # yield [] nil
659  * # yield 1 [1] 1
660  * # yield 1, 2 [1, 2] [1, 2]
661  * # yield nil [nil] nil
662  * # yield [1, 2] [[1, 2]] [1, 2]
663  *
664  * Note that +next_values+ does not affect other non-external enumeration
665  * methods unless underlying iteration method itself has side-effect, e.g.
666  * IO#each_line.
667  *
668  */
669 
670 static VALUE
672 {
673  struct enumerator *e = enumerator_ptr(obj);
674  VALUE vs;
675 
676  if (e->lookahead != Qundef) {
677  vs = e->lookahead;
678  e->lookahead = Qundef;
679  return vs;
680  }
681 
682  return get_next_values(obj, e);
683 }
684 
685 static VALUE
686 ary2sv(VALUE args, int dup)
687 {
688  if (!RB_TYPE_P(args, T_ARRAY))
689  return args;
690 
691  switch (RARRAY_LEN(args)) {
692  case 0:
693  return Qnil;
694 
695  case 1:
696  return RARRAY_PTR(args)[0];
697 
698  default:
699  if (dup)
700  return rb_ary_dup(args);
701  return args;
702  }
703 }
704 
705 /*
706  * call-seq:
707  * e.next -> object
708  *
709  * Returns the next object in the enumerator, and move the internal position
710  * forward. When the position reached at the end, StopIteration is raised.
711  *
712  * === Example
713  *
714  * a = [1,2,3]
715  * e = a.to_enum
716  * p e.next #=> 1
717  * p e.next #=> 2
718  * p e.next #=> 3
719  * p e.next #raises StopIteration
720  *
721  * Note that enumeration sequence by +next+ does not affect other non-external
722  * enumeration methods, unless the underlying iteration methods itself has
723  * side-effect, e.g. IO#each_line.
724  *
725  */
726 
727 static VALUE
729 {
730  VALUE vs = enumerator_next_values(obj);
731  return ary2sv(vs, 0);
732 }
733 
734 static VALUE
736 {
737  struct enumerator *e = enumerator_ptr(obj);
738 
739  if (e->lookahead == Qundef) {
740  e->lookahead = get_next_values(obj, e);
741  }
742  return e->lookahead;
743 }
744 
745 /*
746  * call-seq:
747  * e.peek_values -> array
748  *
749  * Returns the next object as an array, similar to Enumerator#next_values, but
750  * doesn't move the internal position forward. If the position is already at
751  * the end, StopIteration is raised.
752  *
753  * === Example
754  *
755  * o = Object.new
756  * def o.each
757  * yield
758  * yield 1
759  * yield 1, 2
760  * end
761  * e = o.to_enum
762  * p e.peek_values #=> []
763  * e.next
764  * p e.peek_values #=> [1]
765  * p e.peek_values #=> [1]
766  * e.next
767  * p e.peek_values #=> [1, 2]
768  * e.next
769  * p e.peek_values # raises StopIteration
770  *
771  */
772 
773 static VALUE
775 {
776  return rb_ary_dup(enumerator_peek_values(obj));
777 }
778 
779 /*
780  * call-seq:
781  * e.peek -> object
782  *
783  * Returns the next object in the enumerator, but doesn't move the internal
784  * position forward. If the position is already at the end, StopIteration
785  * is raised.
786  *
787  * === Example
788  *
789  * a = [1,2,3]
790  * e = a.to_enum
791  * p e.next #=> 1
792  * p e.peek #=> 2
793  * p e.peek #=> 2
794  * p e.peek #=> 2
795  * p e.next #=> 2
796  * p e.next #=> 3
797  * p e.next #raises StopIteration
798  *
799  */
800 
801 static VALUE
803 {
804  VALUE vs = enumerator_peek_values(obj);
805  return ary2sv(vs, 1);
806 }
807 
808 /*
809  * call-seq:
810  * e.feed obj -> nil
811  *
812  * Sets the value to be returned by the next yield inside +e+.
813  *
814  * If the value is not set, the yield returns nil.
815  *
816  * This value is cleared after being yielded.
817  *
818  * o = Object.new
819  * def o.each
820  * x = yield # (2) blocks
821  * p x # (5) => "foo"
822  * x = yield # (6) blocks
823  * p x # (8) => nil
824  * x = yield # (9) blocks
825  * p x # not reached w/o another e.next
826  * end
827  *
828  * e = o.to_enum
829  * e.next # (1)
830  * e.feed "foo" # (3)
831  * e.next # (4)
832  * e.next # (7)
833  * # (10)
834  */
835 
836 static VALUE
838 {
839  struct enumerator *e = enumerator_ptr(obj);
840 
841  if (e->feedvalue != Qundef) {
842  rb_raise(rb_eTypeError, "feed value already set");
843  }
844  e->feedvalue = v;
845 
846  return Qnil;
847 }
848 
849 /*
850  * call-seq:
851  * e.rewind -> e
852  *
853  * Rewinds the enumeration sequence to the beginning.
854  *
855  * If the enclosed object responds to a "rewind" method, it is called.
856  */
857 
858 static VALUE
860 {
861  struct enumerator *e = enumerator_ptr(obj);
862 
863  rb_check_funcall(e->obj, id_rewind, 0, 0);
864 
865  e->fib = 0;
866  e->dst = Qnil;
867  e->lookahead = Qundef;
868  e->feedvalue = Qundef;
869  e->stop_exc = Qfalse;
870  return obj;
871 }
872 
873 static VALUE
875 {
876  struct enumerator *e;
877  const char *cname;
878  VALUE eobj, eargs, str, method;
879  int tainted, untrusted;
880 
882 
883  cname = rb_obj_classname(obj);
884 
885  if (!e || e->obj == Qundef) {
886  return rb_sprintf("#<%s: uninitialized>", cname);
887  }
888 
889  if (recur) {
890  str = rb_sprintf("#<%s: ...>", cname);
891  OBJ_TAINT(str);
892  return str;
893  }
894 
895  eobj = rb_attr_get(obj, id_receiver);
896  if (NIL_P(eobj)) {
897  eobj = e->obj;
898  }
899 
900  tainted = OBJ_TAINTED(eobj);
901  untrusted = OBJ_UNTRUSTED(eobj);
902 
903  /* (1..100).each_cons(2) => "#<Enumerator: 1..100:each_cons(2)>" */
904  str = rb_sprintf("#<%s: ", cname);
905  rb_str_concat(str, rb_inspect(eobj));
906  method = rb_attr_get(obj, id_method);
907  if (NIL_P(method)) {
908  rb_str_buf_cat2(str, ":");
909  rb_str_buf_cat2(str, rb_id2name(e->meth));
910  }
911  else if (method != Qfalse) {
912  Check_Type(method, T_SYMBOL);
913  rb_str_buf_cat2(str, ":");
914  rb_str_buf_cat2(str, rb_id2name(SYM2ID(method)));
915  }
916 
917  eargs = rb_attr_get(obj, id_arguments);
918  if (NIL_P(eargs)) {
919  eargs = e->args;
920  }
921  if (eargs != Qfalse) {
922  long argc = RARRAY_LEN(eargs);
923  VALUE *argv = RARRAY_PTR(eargs);
924 
925  if (argc > 0) {
926  rb_str_buf_cat2(str, "(");
927 
928  while (argc--) {
929  VALUE arg = *argv++;
930 
931  rb_str_concat(str, rb_inspect(arg));
932  rb_str_buf_cat2(str, argc > 0 ? ", " : ")");
933 
934  if (OBJ_TAINTED(arg)) tainted = TRUE;
935  if (OBJ_UNTRUSTED(arg)) untrusted = TRUE;
936  }
937  }
938  }
939 
940  rb_str_buf_cat2(str, ">");
941 
942  if (tainted) OBJ_TAINT(str);
943  if (untrusted) OBJ_UNTRUST(str);
944  return str;
945 }
946 
947 /*
948  * call-seq:
949  * e.inspect -> string
950  *
951  * Creates a printable version of <i>e</i>.
952  */
953 
954 static VALUE
956 {
957  return rb_exec_recursive(inspect_enumerator, obj, 0);
958 }
959 
960 /*
961  * call-seq:
962  * e.size -> int, Float::INFINITY or nil
963  *
964  * Returns the size of the enumerator, or +nil+ if it can't be calculated lazily.
965  *
966  * (1..100).to_a.permutation(4).size # => 94109400
967  * loop.size # => Float::INFINITY
968  * (1..100).drop_while.size # => nil
969  */
970 
971 static VALUE
973 {
974  struct enumerator *e = enumerator_ptr(obj);
975 
976  if (e->size_fn) {
977  return (*e->size_fn)(e->obj, e->args, obj);
978  }
979  if (rb_obj_is_proc(e->size)) {
980  if (e->args)
981  return rb_proc_call(e->size, e->args);
982  else
983  return rb_proc_call_with_block(e->size, 0, 0, Qnil);
984  }
985  return e->size;
986 }
987 
988 /*
989  * Yielder
990  */
991 static void
992 yielder_mark(void *p)
993 {
994  struct yielder *ptr = p;
995  rb_gc_mark(ptr->proc);
996 }
997 
998 #define yielder_free RUBY_TYPED_DEFAULT_FREE
999 
1000 static size_t
1001 yielder_memsize(const void *p)
1002 {
1003  return p ? sizeof(struct yielder) : 0;
1004 }
1005 
1007  "yielder",
1008  {
1009  yielder_mark,
1010  yielder_free,
1012  },
1013 };
1014 
1015 static struct yielder *
1017 {
1018  struct yielder *ptr;
1019 
1020  TypedData_Get_Struct(obj, struct yielder, &yielder_data_type, ptr);
1021  if (!ptr || ptr->proc == Qundef) {
1022  rb_raise(rb_eArgError, "uninitialized yielder");
1023  }
1024  return ptr;
1025 }
1026 
1027 /* :nodoc: */
1028 static VALUE
1030 {
1031  struct yielder *ptr;
1032  VALUE obj;
1033 
1034  obj = TypedData_Make_Struct(klass, struct yielder, &yielder_data_type, ptr);
1035  ptr->proc = Qundef;
1036 
1037  return obj;
1038 }
1039 
1040 static VALUE
1042 {
1043  struct yielder *ptr;
1044 
1045  TypedData_Get_Struct(obj, struct yielder, &yielder_data_type, ptr);
1046 
1047  if (!ptr) {
1048  rb_raise(rb_eArgError, "unallocated yielder");
1049  }
1050 
1051  ptr->proc = proc;
1052 
1053  return obj;
1054 }
1055 
1056 /* :nodoc: */
1057 static VALUE
1059 {
1060  rb_need_block();
1061 
1062  return yielder_init(obj, rb_block_proc());
1063 }
1064 
1065 /* :nodoc: */
1066 static VALUE
1068 {
1069  struct yielder *ptr = yielder_ptr(obj);
1070 
1071  return rb_proc_call(ptr->proc, args);
1072 }
1073 
1074 /* :nodoc: */
1076 {
1077  yielder_yield(obj, args);
1078  return obj;
1079 }
1080 
1081 static VALUE
1082 yielder_yield_i(VALUE obj, VALUE memo, int argc, VALUE *argv)
1083 {
1084  return rb_yield_values2(argc, argv);
1085 }
1086 
1087 static VALUE
1089 {
1091 }
1092 
1093 /*
1094  * Generator
1095  */
1096 static void
1098 {
1099  struct generator *ptr = p;
1100  rb_gc_mark(ptr->proc);
1101 }
1102 
1103 #define generator_free RUBY_TYPED_DEFAULT_FREE
1104 
1105 static size_t
1106 generator_memsize(const void *p)
1107 {
1108  return p ? sizeof(struct generator) : 0;
1109 }
1110 
1112  "generator",
1113  {
1117  },
1118 };
1119 
1120 static struct generator *
1122 {
1123  struct generator *ptr;
1124 
1126  if (!ptr || ptr->proc == Qundef) {
1127  rb_raise(rb_eArgError, "uninitialized generator");
1128  }
1129  return ptr;
1130 }
1131 
1132 /* :nodoc: */
1133 static VALUE
1135 {
1136  struct generator *ptr;
1137  VALUE obj;
1138 
1139  obj = TypedData_Make_Struct(klass, struct generator, &generator_data_type, ptr);
1140  ptr->proc = Qundef;
1141 
1142  return obj;
1143 }
1144 
1145 static VALUE
1147 {
1148  struct generator *ptr;
1149 
1151 
1152  if (!ptr) {
1153  rb_raise(rb_eArgError, "unallocated generator");
1154  }
1155 
1156  ptr->proc = proc;
1157 
1158  return obj;
1159 }
1160 
1161 /* :nodoc: */
1162 static VALUE
1163 generator_initialize(int argc, VALUE *argv, VALUE obj)
1164 {
1165  VALUE proc;
1166 
1167  if (argc == 0) {
1168  rb_need_block();
1169 
1170  proc = rb_block_proc();
1171  }
1172  else {
1173  rb_scan_args(argc, argv, "1", &proc);
1174 
1175  if (!rb_obj_is_proc(proc))
1177  "wrong argument type %s (expected Proc)",
1178  rb_obj_classname(proc));
1179 
1180  if (rb_block_given_p()) {
1181  rb_warn("given block not used");
1182  }
1183  }
1184 
1185  return generator_init(obj, proc);
1186 }
1187 
1188 /* :nodoc: */
1189 static VALUE
1191 {
1192  struct generator *ptr0, *ptr1;
1193 
1194  if (!OBJ_INIT_COPY(obj, orig)) return obj;
1195 
1196  ptr0 = generator_ptr(orig);
1197 
1198  TypedData_Get_Struct(obj, struct generator, &generator_data_type, ptr1);
1199 
1200  if (!ptr1) {
1201  rb_raise(rb_eArgError, "unallocated generator");
1202  }
1203 
1204  ptr1->proc = ptr0->proc;
1205 
1206  return obj;
1207 }
1208 
1209 /* :nodoc: */
1210 static VALUE
1211 generator_each(int argc, VALUE *argv, VALUE obj)
1212 {
1213  struct generator *ptr = generator_ptr(obj);
1214  VALUE args = rb_ary_new2(argc + 1);
1215 
1216  rb_ary_push(args, yielder_new());
1217  if (argc > 0) {
1218  rb_ary_cat(args, argv, argc);
1219  }
1220 
1221  return rb_proc_call(ptr->proc, args);
1222 }
1223 
1224 /* Lazy Enumerator methods */
1225 static VALUE
1227 {
1228  VALUE r = rb_check_funcall(self, id_size, 0, 0);
1229  return (r == Qundef) ? Qnil : r;
1230 }
1231 
1232 static VALUE
1234 {
1235  return enum_size(rb_ivar_get(self, id_receiver));
1236 }
1237 
1238 static VALUE
1240 {
1241  return lazy_size(lazy);
1242 }
1243 
1244 static VALUE
1246 {
1247  VALUE result;
1248  if (argc == 1) {
1249  VALUE args[2];
1250  args[0] = m;
1251  args[1] = val;
1252  result = rb_yield_values2(2, args);
1253  }
1254  else {
1255  VALUE args;
1256  int len = rb_long2int((long)argc + 1);
1257 
1258  args = rb_ary_tmp_new(len);
1259  rb_ary_push(args, m);
1260  if (argc > 0) {
1261  rb_ary_cat(args, argv, argc);
1262  }
1263  result = rb_yield_values2(len, RARRAY_PTR(args));
1264  RB_GC_GUARD(args);
1265  }
1266  if (result == Qundef) rb_iter_break();
1267  return Qnil;
1268 }
1269 
1270 static VALUE
1271 lazy_init_block_i(VALUE val, VALUE m, int argc, VALUE *argv)
1272 {
1273  rb_block_call(m, id_each, argc-1, argv+1, lazy_init_iterator, val);
1274  return Qnil;
1275 }
1276 
1277 /*
1278  * call-seq:
1279  * Lazy.new(obj, size=nil) { |yielder, *values| ... }
1280  *
1281  * Creates a new Lazy enumerator. When the enumerator is actually enumerated
1282  * (e.g. by calling #force), +obj+ will be enumerated and each value passed
1283  * to the given block. The block can yield values back using +yielder+.
1284  * For example, to create a method +filter_map+ in both lazy and
1285  * non-lazy fashions:
1286  *
1287  * module Enumerable
1288  * def filter_map(&block)
1289  * map(&block).compact
1290  * end
1291  * end
1292  *
1293  * class Enumerator::Lazy
1294  * def filter_map
1295  * Lazy.new(self) do |yielder, *values|
1296  * result = yield *values
1297  * yielder << result if result
1298  * end
1299  * end
1300  * end
1301  *
1302  * (1..Float::INFINITY).lazy.filter_map{|i| i*i if i.even?}.first(5)
1303  * # => [4, 16, 36, 64, 100]
1304  */
1305 static VALUE
1306 lazy_initialize(int argc, VALUE *argv, VALUE self)
1307 {
1308  VALUE obj, size = Qnil;
1309  VALUE generator;
1310 
1311  rb_check_arity(argc, 1, 2);
1312  if (!rb_block_given_p()) {
1313  rb_raise(rb_eArgError, "tried to call lazy new without a block");
1314  }
1315  obj = argv[0];
1316  if (argc > 1) {
1317  size = argv[1];
1318  }
1319  generator = generator_allocate(rb_cGenerator);
1320  rb_block_call(generator, id_initialize, 0, 0, lazy_init_block_i, obj);
1321  enumerator_init(self, generator, sym_each, 0, 0, 0, size);
1322  rb_ivar_set(self, id_receiver, obj);
1323 
1324  return self;
1325 }
1326 
1327 static VALUE
1329 {
1330  ID id = rb_frame_this_func();
1331  struct enumerator *e = enumerator_ptr(lazy);
1332  rb_ivar_set(lazy, id_method, ID2SYM(id));
1333  if (NIL_P(args)) {
1334  /* Qfalse indicates that the arguments are empty */
1336  }
1337  else {
1338  rb_ivar_set(lazy, id_arguments, args);
1339  }
1340  e->size_fn = size_fn;
1341  return lazy;
1342 }
1343 
1344 /*
1345  * call-seq:
1346  * e.lazy -> lazy_enumerator
1347  *
1348  * Returns a lazy enumerator, whose methods map/collect,
1349  * flat_map/collect_concat, select/find_all, reject, grep, zip, take,
1350  * take_while, drop, and drop_while enumerate values only on an
1351  * as-needed basis. However, if a block is given to zip, values
1352  * are enumerated immediately.
1353  *
1354  * === Example
1355  *
1356  * The following program finds pythagorean triples:
1357  *
1358  * def pythagorean_triples
1359  * (1..Float::INFINITY).lazy.flat_map {|z|
1360  * (1..z).flat_map {|x|
1361  * (x..z).select {|y|
1362  * x**2 + y**2 == z**2
1363  * }.map {|y|
1364  * [x, y, z]
1365  * }
1366  * }
1367  * }
1368  * end
1369  * # show first ten pythagorean triples
1370  * p pythagorean_triples.take(10).force # take is lazy, so force is needed
1371  * p pythagorean_triples.first(10) # first is eager
1372  * # show pythagorean triples less than 100
1373  * p pythagorean_triples.take_while { |*, z| z < 100 }.force
1374  */
1375 static VALUE
1377 {
1378  VALUE result = lazy_to_enum_i(obj, sym_each, 0, 0, enum_size);
1379  /* Qfalse indicates that the Enumerator::Lazy has no method name */
1380  rb_ivar_set(result, id_method, Qfalse);
1381  return result;
1382 }
1383 
1384 static VALUE
1385 lazy_to_enum_i(VALUE obj, VALUE meth, int argc, VALUE *argv, VALUE (*size_fn)(ANYARGS))
1386 {
1388  obj, meth, argc, argv, size_fn, Qnil);
1389 }
1390 
1391 /*
1392  * call-seq:
1393  * lzy.to_enum(method = :each, *args) -> lazy_enum
1394  * lzy.enum_for(method = :each, *args) -> lazy_enum
1395  * lzy.to_enum(method = :each, *args) {|*args| block} -> lazy_enum
1396  * lzy.enum_for(method = :each, *args){|*args| block} -> lazy_enum
1397  *
1398  * Similar to Kernel#to_enum, except it returns a lazy enumerator.
1399  * This makes it easy to define Enumerable methods that will
1400  * naturally remain lazy if called from a lazy enumerator.
1401  *
1402  * For example, continuing from the example in Kernel#to_enum:
1403  *
1404  * # See Kernel#to_enum for the definition of repeat
1405  * r = 1..Float::INFINITY
1406  * r.repeat(2).first(5) # => [1, 1, 2, 2, 3]
1407  * r.repeat(2).class # => Enumerator
1408  * r.repeat(2).map{|n| n ** 2}.first(5) # => endless loop!
1409  * # works naturally on lazy enumerator:
1410  * r.lazy.repeat(2).class # => Enumerator::Lazy
1411  * r.lazy.repeat(2).map{|n| n ** 2}.first(5) # => [1, 1, 4, 4, 9]
1412  */
1413 
1414 static VALUE
1415 lazy_to_enum(int argc, VALUE *argv, VALUE self)
1416 {
1417  VALUE lazy, meth = sym_each;
1418 
1419  if (argc > 0) {
1420  --argc;
1421  meth = *argv++;
1422  }
1423  lazy = lazy_to_enum_i(self, meth, argc, argv, 0);
1424  if (rb_block_given_p()) {
1425  enumerator_ptr(lazy)->size = rb_block_proc();
1426  }
1427  return lazy;
1428 }
1429 
1430 static VALUE
1431 lazy_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
1432 {
1433  VALUE result = rb_yield_values2(argc - 1, &argv[1]);
1434 
1435  rb_funcall(argv[0], id_yield, 1, result);
1436  return Qnil;
1437 }
1438 
1439 static VALUE
1441 {
1442  if (!rb_block_given_p()) {
1443  rb_raise(rb_eArgError, "tried to call lazy map without a block");
1444  }
1445 
1446  return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
1447  lazy_map_func, 0),
1449 }
1450 
1451 static VALUE
1453 {
1454  return rb_funcall2(yielder, id_yield, argc, argv);
1455 }
1456 
1457 static VALUE
1459 {
1460  rb_block_call(obj, id_each, 0, 0, lazy_flat_map_i, yielder);
1461  return Qnil;
1462 }
1463 
1464 static VALUE
1466 {
1467  VALUE ary = rb_check_array_type(obj);
1468  if (NIL_P(ary)) {
1469  rb_funcall(yielder, id_yield, 1, obj);
1470  }
1471  else {
1472  long i;
1473  for (i = 0; i < RARRAY_LEN(ary); i++) {
1474  rb_funcall(yielder, id_yield, 1, RARRAY_PTR(ary)[i]);
1475  }
1476  }
1477  return Qnil;
1478 }
1479 
1480 static VALUE
1482 {
1483  VALUE result = rb_yield_values2(argc - 1, &argv[1]);
1484  if (RB_TYPE_P(result, T_ARRAY)) {
1485  long i;
1486  for (i = 0; i < RARRAY_LEN(result); i++) {
1487  rb_funcall(argv[0], id_yield, 1, RARRAY_PTR(result)[i]);
1488  }
1489  }
1490  else {
1491  if (rb_respond_to(result, id_force) && rb_respond_to(result, id_each)) {
1492  lazy_flat_map_each(result, argv[0]);
1493  }
1494  else {
1495  lazy_flat_map_to_ary(result, argv[0]);
1496  }
1497  }
1498  return Qnil;
1499 }
1500 
1501 /*
1502  * call-seq:
1503  * lazy.flat_map { |obj| block } -> a_lazy_enumerator
1504  *
1505  * Returns a new lazy enumerator with the concatenated results of running
1506  * <i>block</i> once for every element in <i>lazy</i>.
1507  *
1508  * ["foo", "bar"].lazy.flat_map {|i| i.each_char.lazy}.force
1509  * #=> ["f", "o", "o", "b", "a", "r"]
1510  *
1511  * A value <i>x</i> returned by <i>block</i> is decomposed if either of
1512  * the following conditions is true:
1513  *
1514  * a) <i>x</i> responds to both each and force, which means that
1515  * <i>x</i> is a lazy enumerator.
1516  * b) <i>x</i> is an array or responds to to_ary.
1517  *
1518  * Otherwise, <i>x</i> is contained as-is in the return value.
1519  *
1520  * [{a:1}, {b:2}].lazy.flat_map {|i| i}.force
1521  * #=> [{:a=>1}, {:b=>2}]
1522  */
1523 static VALUE
1525 {
1526  if (!rb_block_given_p()) {
1527  rb_raise(rb_eArgError, "tried to call lazy flat_map without a block");
1528  }
1529 
1530  return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
1531  lazy_flat_map_func, 0),
1532  Qnil, 0);
1533 }
1534 
1535 static VALUE
1536 lazy_select_func(VALUE val, VALUE m, int argc, VALUE *argv)
1537 {
1538  VALUE element = rb_enum_values_pack(argc - 1, argv + 1);
1539 
1540  if (RTEST(rb_yield(element))) {
1541  return rb_funcall(argv[0], id_yield, 1, element);
1542  }
1543  return Qnil;
1544 }
1545 
1546 static VALUE
1548 {
1549  if (!rb_block_given_p()) {
1550  rb_raise(rb_eArgError, "tried to call lazy select without a block");
1551  }
1552 
1553  return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
1554  lazy_select_func, 0),
1555  Qnil, 0);
1556 }
1557 
1558 static VALUE
1559 lazy_reject_func(VALUE val, VALUE m, int argc, VALUE *argv)
1560 {
1561  VALUE element = rb_enum_values_pack(argc - 1, argv + 1);
1562 
1563  if (!RTEST(rb_yield(element))) {
1564  return rb_funcall(argv[0], id_yield, 1, element);
1565  }
1566  return Qnil;
1567 }
1568 
1569 static VALUE
1571 {
1572  if (!rb_block_given_p()) {
1573  rb_raise(rb_eArgError, "tried to call lazy reject without a block");
1574  }
1575 
1576  return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
1577  lazy_reject_func, 0),
1578  Qnil, 0);
1579 }
1580 
1581 static VALUE
1582 lazy_grep_func(VALUE val, VALUE m, int argc, VALUE *argv)
1583 {
1584  VALUE i = rb_enum_values_pack(argc - 1, argv + 1);
1585  VALUE result = rb_funcall(m, id_eqq, 1, i);
1586 
1587  if (RTEST(result)) {
1588  rb_funcall(argv[0], id_yield, 1, i);
1589  }
1590  return Qnil;
1591 }
1592 
1593 static VALUE
1594 lazy_grep_iter(VALUE val, VALUE m, int argc, VALUE *argv)
1595 {
1596  VALUE i = rb_enum_values_pack(argc - 1, argv + 1);
1597  VALUE result = rb_funcall(m, id_eqq, 1, i);
1598 
1599  if (RTEST(result)) {
1600  rb_funcall(argv[0], id_yield, 1, rb_yield(i));
1601  }
1602  return Qnil;
1603 }
1604 
1605 static VALUE
1606 lazy_grep(VALUE obj, VALUE pattern)
1607 {
1608  return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
1609  rb_block_given_p() ?
1611  pattern),
1612  rb_ary_new3(1, pattern), 0);
1613 }
1614 
1615 static VALUE
1617 {
1618  return rb_funcall(obj, id_next, 0);
1619 }
1620 
1621 static VALUE
1623 {
1624  return Qnil;
1625 }
1626 
1627 static VALUE
1628 lazy_zip_arrays_func(VALUE val, VALUE arrays, int argc, VALUE *argv)
1629 {
1630  VALUE yielder, ary, memo;
1631  long i, count;
1632 
1633  yielder = argv[0];
1634  memo = rb_attr_get(yielder, id_memo);
1635  count = NIL_P(memo) ? 0 : NUM2LONG(memo);
1636 
1637  ary = rb_ary_new2(RARRAY_LEN(arrays) + 1);
1638  rb_ary_push(ary, argv[1]);
1639  for (i = 0; i < RARRAY_LEN(arrays); i++) {
1640  rb_ary_push(ary, rb_ary_entry(RARRAY_PTR(arrays)[i], count));
1641  }
1642  rb_funcall(yielder, id_yield, 1, ary);
1643  rb_ivar_set(yielder, id_memo, LONG2NUM(++count));
1644  return Qnil;
1645 }
1646 
1647 static VALUE
1648 lazy_zip_func(VALUE val, VALUE zip_args, int argc, VALUE *argv)
1649 {
1650  VALUE yielder, ary, arg, v;
1651  long i;
1652 
1653  yielder = argv[0];
1654  arg = rb_attr_get(yielder, id_memo);
1655  if (NIL_P(arg)) {
1656  arg = rb_ary_new2(RARRAY_LEN(zip_args));
1657  for (i = 0; i < RARRAY_LEN(zip_args); i++) {
1658  rb_ary_push(arg, rb_funcall(RARRAY_PTR(zip_args)[i], id_to_enum, 0));
1659  }
1660  rb_ivar_set(yielder, id_memo, arg);
1661  }
1662 
1663  ary = rb_ary_new2(RARRAY_LEN(arg) + 1);
1664  v = Qnil;
1665  if (--argc > 0) {
1666  ++argv;
1667  v = argc > 1 ? rb_ary_new4(argc, argv) : *argv;
1668  }
1669  rb_ary_push(ary, v);
1670  for (i = 0; i < RARRAY_LEN(arg); i++) {
1671  v = rb_rescue2(call_next, RARRAY_PTR(arg)[i], next_stopped, 0,
1672  rb_eStopIteration, (VALUE)0);
1673  rb_ary_push(ary, v);
1674  }
1675  rb_funcall(yielder, id_yield, 1, ary);
1676  return Qnil;
1677 }
1678 
1679 static VALUE
1680 lazy_zip(int argc, VALUE *argv, VALUE obj)
1681 {
1682  VALUE ary, v;
1683  long i;
1685 
1686  if (rb_block_given_p()) {
1687  return rb_call_super(argc, argv);
1688  }
1689 
1690  ary = rb_ary_new2(argc);
1691  for (i = 0; i < argc; i++) {
1692  v = rb_check_array_type(argv[i]);
1693  if (NIL_P(v)) {
1694  for (; i < argc; i++) {
1695  if (!rb_respond_to(argv[i], id_each)) {
1696  rb_raise(rb_eTypeError, "wrong argument type %s (must respond to :each)",
1697  rb_obj_classname(argv[i]));
1698  }
1699  }
1700  ary = rb_ary_new4(argc, argv);
1701  func = lazy_zip_func;
1702  break;
1703  }
1704  rb_ary_push(ary, v);
1705  }
1706 
1707  return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
1708  func, ary),
1709  ary, lazy_receiver_size);
1710 }
1711 
1712 static VALUE
1713 lazy_take_func(VALUE val, VALUE args, int argc, VALUE *argv)
1714 {
1715  long remain;
1716  VALUE memo = rb_attr_get(argv[0], id_memo);
1717  if (NIL_P(memo)) {
1718  memo = args;
1719  }
1720 
1721  rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
1722  if ((remain = NUM2LONG(memo)-1) == 0) {
1723  return Qundef;
1724  }
1725  else {
1726  rb_ivar_set(argv[0], id_memo, LONG2NUM(remain));
1727  return Qnil;
1728  }
1729 }
1730 
1731 static VALUE
1733 {
1734  VALUE receiver = lazy_size(lazy);
1735  long len = NUM2LONG(RARRAY_PTR(rb_ivar_get(lazy, id_arguments))[0]);
1736  if (NIL_P(receiver) || (FIXNUM_P(receiver) && FIX2LONG(receiver) < len))
1737  return receiver;
1738  return LONG2NUM(len);
1739 }
1740 
1741 static VALUE
1743 {
1744  long len = NUM2LONG(n);
1745  VALUE lazy;
1746 
1747  if (len < 0) {
1748  rb_raise(rb_eArgError, "attempt to take negative size");
1749  }
1750  if (len == 0) {
1751  VALUE len = INT2NUM(0);
1752  lazy = lazy_to_enum_i(obj, sym_cycle, 1, &len, 0);
1753  }
1754  else {
1755  lazy = rb_block_call(rb_cLazy, id_new, 1, &obj,
1756  lazy_take_func, n);
1757  }
1758  return lazy_set_method(lazy, rb_ary_new3(1, n), lazy_take_size);
1759 }
1760 
1761 static VALUE
1762 lazy_take_while_func(VALUE val, VALUE args, int argc, VALUE *argv)
1763 {
1764  VALUE result = rb_yield_values2(argc - 1, &argv[1]);
1765  if (!RTEST(result)) return Qundef;
1766  rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
1767  return Qnil;
1768 }
1769 
1770 static VALUE
1772 {
1773  if (!rb_block_given_p()) {
1774  rb_raise(rb_eArgError, "tried to call lazy take_while without a block");
1775  }
1776  return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
1778  Qnil, 0);
1779 }
1780 
1781 static VALUE
1783 {
1784  long len = NUM2LONG(RARRAY_PTR(rb_ivar_get(lazy, id_arguments))[0]);
1785  VALUE receiver = lazy_size(lazy);
1786  if (NIL_P(receiver))
1787  return receiver;
1788  if (FIXNUM_P(receiver)) {
1789  len = FIX2LONG(receiver) - len;
1790  return LONG2FIX(len < 0 ? 0 : len);
1791  }
1792  return rb_funcall(receiver, '-', 1, LONG2NUM(len));
1793 }
1794 
1795 static VALUE
1796 lazy_drop_func(VALUE val, VALUE args, int argc, VALUE *argv)
1797 {
1798  long remain;
1799  VALUE memo = rb_attr_get(argv[0], id_memo);
1800  if (NIL_P(memo)) {
1801  memo = args;
1802  }
1803  if ((remain = NUM2LONG(memo)) == 0) {
1804  rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
1805  }
1806  else {
1807  rb_ivar_set(argv[0], id_memo, LONG2NUM(--remain));
1808  }
1809  return Qnil;
1810 }
1811 
1812 static VALUE
1814 {
1815  long len = NUM2LONG(n);
1816 
1817  if (len < 0) {
1818  rb_raise(rb_eArgError, "attempt to drop negative size");
1819  }
1820  return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
1821  lazy_drop_func, n),
1822  rb_ary_new3(1, n), lazy_drop_size);
1823 }
1824 
1825 static VALUE
1826 lazy_drop_while_func(VALUE val, VALUE args, int argc, VALUE *argv)
1827 {
1828  VALUE memo = rb_attr_get(argv[0], id_memo);
1829  if (NIL_P(memo) && !RTEST(rb_yield_values2(argc - 1, &argv[1]))) {
1830  rb_ivar_set(argv[0], id_memo, memo = Qtrue);
1831  }
1832  if (memo == Qtrue) {
1833  rb_funcall2(argv[0], id_yield, argc - 1, argv + 1);
1834  }
1835  return Qnil;
1836 }
1837 
1838 static VALUE
1840 {
1841  if (!rb_block_given_p()) {
1842  rb_raise(rb_eArgError, "tried to call lazy drop_while without a block");
1843  }
1844  return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
1846  Qnil, 0);
1847 }
1848 
1849 static VALUE
1850 lazy_super(int argc, VALUE *argv, VALUE lazy)
1851 {
1852  return enumerable_lazy(rb_call_super(argc, argv));
1853 }
1854 
1855 static VALUE
1857 {
1858  return obj;
1859 }
1860 
1861 /*
1862  * Document-class: StopIteration
1863  *
1864  * Raised to stop the iteration, in particular by Enumerator#next. It is
1865  * rescued by Kernel#loop.
1866  *
1867  * loop do
1868  * puts "Hello"
1869  * raise StopIteration
1870  * puts "World"
1871  * end
1872  * puts "Done!"
1873  *
1874  * <em>produces:</em>
1875  *
1876  * Hello
1877  * Done!
1878  */
1879 
1880 /*
1881  * call-seq:
1882  * result -> value
1883  *
1884  * Returns the return value of the iterator.
1885  *
1886  * o = Object.new
1887  * def o.each
1888  * yield 1
1889  * yield 2
1890  * yield 3
1891  * 100
1892  * end
1893  *
1894  * e = o.to_enum
1895  *
1896  * puts e.next #=> 1
1897  * puts e.next #=> 2
1898  * puts e.next #=> 3
1899  *
1900  * begin
1901  * e.next
1902  * rescue StopIteration => ex
1903  * puts ex.result #=> 100
1904  * end
1905  *
1906  */
1907 
1908 static VALUE
1910 {
1911  return rb_attr_get(self, id_result);
1912 }
1913 
1914 void
1916 {
1917  rb_define_method(rb_mKernel, "to_enum", obj_to_enum, -1);
1918  rb_define_method(rb_mKernel, "enum_for", obj_to_enum, -1);
1919 
1920  rb_cEnumerator = rb_define_class("Enumerator", rb_cObject);
1922 
1925  rb_define_method(rb_cEnumerator, "initialize_copy", enumerator_init_copy, 1);
1928  rb_define_method(rb_cEnumerator, "each_with_object", enumerator_with_object, 1);
1939 
1940  /* Lazy */
1943  rb_define_method(rb_cLazy, "initialize", lazy_initialize, -1);
1944  rb_define_method(rb_cLazy, "to_enum", lazy_to_enum, -1);
1945  rb_define_method(rb_cLazy, "enum_for", lazy_to_enum, -1);
1946  rb_define_method(rb_cLazy, "map", lazy_map, 0);
1947  rb_define_method(rb_cLazy, "collect", lazy_map, 0);
1948  rb_define_method(rb_cLazy, "flat_map", lazy_flat_map, 0);
1949  rb_define_method(rb_cLazy, "collect_concat", lazy_flat_map, 0);
1950  rb_define_method(rb_cLazy, "select", lazy_select, 0);
1951  rb_define_method(rb_cLazy, "find_all", lazy_select, 0);
1952  rb_define_method(rb_cLazy, "reject", lazy_reject, 0);
1953  rb_define_method(rb_cLazy, "grep", lazy_grep, 1);
1954  rb_define_method(rb_cLazy, "zip", lazy_zip, -1);
1955  rb_define_method(rb_cLazy, "take", lazy_take, 1);
1956  rb_define_method(rb_cLazy, "take_while", lazy_take_while, 0);
1957  rb_define_method(rb_cLazy, "drop", lazy_drop, 1);
1958  rb_define_method(rb_cLazy, "drop_while", lazy_drop_while, 0);
1959  rb_define_method(rb_cLazy, "lazy", lazy_lazy, 0);
1960  rb_define_method(rb_cLazy, "chunk", lazy_super, -1);
1961  rb_define_method(rb_cLazy, "slice_before", lazy_super, -1);
1962 
1963  rb_define_alias(rb_cLazy, "force", "to_a");
1964 
1965  rb_eStopIteration = rb_define_class("StopIteration", rb_eIndexError);
1967 
1968  /* Generator */
1973  rb_define_method(rb_cGenerator, "initialize_copy", generator_init_copy, 1);
1975 
1976  /* Yielder */
1979  rb_define_method(rb_cYielder, "initialize", yielder_initialize, 0);
1982 
1983  rb_provide("enumerator.so"); /* for backward compatibility */
1984 }
1985 
1986 void
1988 {
1989  id_rewind = rb_intern("rewind");
1990  id_each = rb_intern("each");
1991  id_call = rb_intern("call");
1992  id_size = rb_intern("size");
1993  id_yield = rb_intern("yield");
1994  id_new = rb_intern("new");
1995  id_initialize = rb_intern("initialize");
1996  id_next = rb_intern("next");
1997  id_result = rb_intern("result");
1998  id_lazy = rb_intern("lazy");
1999  id_eqq = rb_intern("===");
2000  id_receiver = rb_intern("receiver");
2001  id_arguments = rb_intern("arguments");
2002  id_memo = rb_intern("memo");
2003  id_method = rb_intern("method");
2004  id_force = rb_intern("force");
2005  id_to_enum = rb_intern("to_enum");
2006  sym_each = ID2SYM(id_each);
2007  sym_cycle = ID2SYM(rb_intern("cycle"));
2008 
2009  InitVM(Enumerator);
2010 }
static VALUE lazy_drop(VALUE obj, VALUE n)
Definition: enumerator.c:1813
#define T_SYMBOL
Definition: ruby.h:502
static VALUE lazy_zip_func(VALUE val, VALUE zip_args, int argc, VALUE *argv)
Definition: enumerator.c:1648
VALUE size
Definition: enumerator.c:122
static void enumerator_mark(void *p)
Definition: enumerator.c:143
VALUE rb_ary_new4(long n, const VALUE *elts)
Definition: array.c:451
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1101
#define RARRAY_LEN(a)
Definition: ruby.h:899
static VALUE next_i(VALUE curr, VALUE obj)
Definition: enumerator.c:577
static VALUE rb_cGenerator
Definition: enumerator.c:126
static VALUE sym_cycle
Definition: enumerator.c:109
static VALUE enumerator_rewind(VALUE obj)
Definition: enumerator.c:859
#define INT2NUM(x)
Definition: ruby.h:1178
static VALUE lazy_drop_while_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1826
static VALUE lazy_reject(VALUE obj)
Definition: enumerator.c:1570
int i
Definition: win32ole.c:784
static VALUE lazy_grep(VALUE obj, VALUE pattern)
Definition: enumerator.c:1606
VALUE rb_yield_values(int n,...)
Definition: vm_eval.c:945
int count
Definition: encoding.c:51
static VALUE call_next(VALUE obj)
Definition: enumerator.c:1616
static void generator_mark(void *p)
Definition: enumerator.c:1097
static VALUE enumerator_peek_values(VALUE obj)
Definition: enumerator.c:735
static VALUE yielder_new(void)
Definition: enumerator.c:1088
static VALUE generator_init_copy(VALUE obj, VALUE orig)
Definition: enumerator.c:1190
static size_t generator_memsize(const void *p)
Definition: enumerator.c:1106
VALUE feedvalue
Definition: enumerator.c:120
#define InitVM(ext)
Definition: ruby.h:1651
#define Qtrue
Definition: ruby.h:434
static VALUE yielder_allocate(VALUE klass)
Definition: enumerator.c:1029
VALUE rb_proc_call_with_block(VALUE, int argc, VALUE *argv, VALUE)
Definition: proc.c:605
#define OBJ_INIT_COPY(obj, orig)
Definition: intern.h:268
#define TypedData_Get_Struct(obj, type, data_type, sval)
Definition: ruby.h:1030
static VALUE lazy_size(VALUE self)
Definition: enumerator.c:1233
static VALUE yielder_init(VALUE obj, VALUE proc)
Definition: enumerator.c:1041
VALUE(* size_fn)(ANYARGS)
Definition: enumerator.c:123
ID rb_frame_this_func(void)
Definition: eval.c:902
VALUE rb_eTypeError
Definition: error.c:511
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:822
#define rb_long2int(n)
Definition: ruby.h:325
static VALUE enumerator_with_object_i(VALUE val, VALUE memo, int argc, VALUE *argv)
Definition: enumerator.c:518
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:2162
#define SYM2ID(x)
Definition: ruby.h:364
VALUE rb_fiber_yield(int argc, VALUE *argv)
Definition: cont.c:1394
static const rb_data_type_t generator_data_type
Definition: enumerator.c:1111
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:465
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
static VALUE enumerator_init(VALUE enum_obj, VALUE obj, VALUE meth, int argc, VALUE *argv, VALUE(*size_fn)(ANYARGS), VALUE size)
Definition: enumerator.c:269
static VALUE enumerator_next_values(VALUE obj)
Definition: enumerator.c:671
VALUE rb_cEnumerator
Definition: enumerator.c:105
VALUE rb_fiber_alive_p(VALUE fibval)
Definition: cont.c:1422
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:545
VALUE rb_to_int(VALUE)
Definition: object.c:2431
#define Check_Type(v, t)
Definition: ruby.h:539
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1780
VALUE obj
Definition: enumerator.c:114
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1116
VALUE rb_fiber_current(void)
Definition: cont.c:1230
static const rb_data_type_t yielder_data_type
Definition: enumerator.c:1006
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4852
#define RB_GC_GUARD(v)
Definition: ruby.h:530
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE proc
Definition: enumerator.c:129
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:582
static VALUE enum_size(VALUE self)
Definition: enumerator.c:1226
#define generator_free
Definition: enumerator.c:1103
VALUE rb_ary_new3(long n,...)
Definition: array.c:432
static VALUE enumerator_next(VALUE obj)
Definition: enumerator.c:728
static VALUE lazy_to_enum_i(VALUE self, VALUE meth, int argc, VALUE *argv, VALUE(*size_fn)(ANYARGS))
Definition: enumerator.c:1385
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:695
static VALUE enumerator_block_call(VALUE obj, rb_block_call_func *func, VALUE arg)
Definition: enumerator.c:419
void rb_gc_mark(VALUE ptr)
Definition: gc.c:2598
static ID id_lazy
Definition: enumerator.c:108
#define T_ARRAY
Definition: ruby.h:492
static VALUE lazy_to_enum(int argc, VALUE *argv, VALUE self)
Definition: enumerator.c:1415
VALUE rb_block_call(VALUE, ID, int, VALUE *, VALUE(*)(ANYARGS), VALUE)
Definition: vm_eval.c:1131
static VALUE lazy_drop_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1796
#define FIXNUM_P(f)
Definition: ruby.h:355
static VALUE yielder_yield(VALUE obj, VALUE args)
Definition: enumerator.c:1067
static VALUE generator_initialize(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:1163
static VALUE generator_allocate(VALUE klass)
Definition: enumerator.c:1134
static VALUE yielder_yield_i(VALUE obj, VALUE memo, int argc, VALUE *argv)
Definition: enumerator.c:1082
#define OBJ_TAINTED(x)
Definition: ruby.h:1153
static ID id_memo
Definition: enumerator.c:108
const char * rb_obj_classname(VALUE)
Definition: variable.c:396
static VALUE lazy_init_iterator(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1245
static VALUE enumerator_with_object(VALUE obj, VALUE memo)
Definition: enumerator.c:554
void InitVM_Enumerator(void)
Definition: enumerator.c:1915
static VALUE lazy_flat_map(VALUE obj)
Definition: enumerator.c:1524
Win32OLEIDispatch * p
Definition: win32ole.c:786
VALUE rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv)
Definition: enumerator.c:398
void rb_exc_raise(VALUE mesg)
Definition: eval.c:527
static VALUE lazy_map(VALUE obj)
Definition: enumerator.c:1440
static ID id_initialize
Definition: enumerator.c:107
static struct enumerator * enumerator_ptr(VALUE obj)
Definition: enumerator.c:174
int args
Definition: win32ole.c:785
VALUE rb_obj_dup(VALUE)
Definition: object.c:338
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1537
VALUE rb_enumeratorize_with_size(VALUE obj, VALUE meth, int argc, VALUE *argv, VALUE(*size_fn)(ANYARGS))
Definition: enumerator.c:407
static VALUE enumerable_lazy(VALUE obj)
Definition: enumerator.c:1376
void rb_iter_break(void)
Definition: vm.c:960
static ID id_to_enum
Definition: enumerator.c:107
static VALUE get_next_values(VALUE obj, struct enumerator *e)
Definition: enumerator.c:599
VALUE rb_fiber_resume(VALUE fibval, int argc, VALUE *argv)
Definition: cont.c:1378
static VALUE lazy_flat_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1481
static VALUE enumerator_each(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:442
static VALUE enumerator_with_index(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:491
int rb_block_given_p(void)
Definition: eval.c:672
static VALUE obj_to_enum(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:241
VALUE rb_ary_cat(VALUE ary, const VALUE *ptr, long len)
Definition: array.c:846
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1426
VALUE rb_str_buf_cat2(VALUE, const char *)
Definition: string.c:1957
static ID id_next
Definition: enumerator.c:108
RUBY_EXTERN VALUE rb_mKernel
Definition: ruby.h:1414
static VALUE enumerator_peek_values_m(VALUE obj)
Definition: enumerator.c:774
#define NIL_P(v)
Definition: ruby.h:446
static VALUE enumerator_size(VALUE obj)
Definition: enumerator.c:972
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:499
static void next_init(VALUE obj, struct enumerator *e)
Definition: enumerator.c:590
static VALUE lazy_take_size(VALUE generator, VALUE args, VALUE lazy)
Definition: enumerator.c:1732
VALUE proc
Definition: enumerator.c:133
VALUE rb_fiber_new(VALUE(*func)(ANYARGS), VALUE obj)
Definition: cont.c:1120
#define T_FLOAT
Definition: ruby.h:489
static VALUE stop_result(VALUE self)
Definition: enumerator.c:1909
#define OBJ_UNTRUST(x)
Definition: ruby.h:1156
int argc
Definition: ruby.c:130
#define Qfalse
Definition: ruby.h:433
static VALUE enumerator_with_index_i(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:461
static VALUE lazy_take_while(VALUE obj)
Definition: enumerator.c:1771
static VALUE lazy_select(VALUE obj)
Definition: enumerator.c:1547
VALUE rb_cLazy
Definition: enumerator.c:106
static ID id_result
Definition: enumerator.c:108
static VALUE generator_init(VALUE obj, VALUE proc)
Definition: enumerator.c:1146
VALUE lookahead
Definition: enumerator.c:119
VALUE rb_eIndexError
Definition: error.c:513
static VALUE lazy_receiver_size(VALUE generator, VALUE args, VALUE lazy)
Definition: enumerator.c:1239
void rb_need_block(void)
Definition: eval.c:693
static VALUE yielder_yield_push(VALUE obj, VALUE args)
Definition: enumerator.c:1075
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1539
#define enumerator_free
Definition: enumerator.c:156
VALUE rb_yield(VALUE)
Definition: vm_eval.c:934
static VALUE lazy_super(int argc, VALUE *argv, VALUE lazy)
Definition: enumerator.c:1850
static ID id_yield
Definition: enumerator.c:107
#define TRUE
Definition: nkf.h:175
static VALUE lazy_init_block_i(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1271
VALUE rb_obj_is_proc(VALUE)
Definition: proc.c:91
VALUE rb_check_funcall(VALUE, ID, int, VALUE *)
Definition: vm_eval.c:408
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
Definition: vm_eval.c:805
#define OBJ_UNTRUSTED(x)
Definition: ruby.h:1155
VALUE rb_mEnumerable
Definition: enum.c:20
static VALUE lazy_drop_size(VALUE generator, VALUE args, VALUE lazy)
Definition: enumerator.c:1782
static VALUE lazy_take(VALUE obj, VALUE n)
Definition: enumerator.c:1742
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1270
#define const
Definition: strftime.c:102
static VALUE enumerator_each_with_index(VALUE obj)
Definition: enumerator.c:512
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1128
unsigned long ID
Definition: ruby.h:105
static VALUE lazy_grep_iter(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1594
VALUE fib
Definition: enumerator.c:117
#define Qnil
Definition: ruby.h:435
VALUE rb_exc_new2(VALUE etype, const char *s)
Definition: error.c:542
VALUE rb_eStopIteration
Definition: enumerator.c:111
static ID id_eqq
Definition: enumerator.c:108
#define OBJ_TAINT(x)
Definition: ruby.h:1154
unsigned long VALUE
Definition: ruby.h:104
static VALUE result
Definition: nkf.c:40
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
Definition: intern.h:215
static size_t enumerator_memsize(const void *p)
Definition: enumerator.c:159
VALUE rb_rescue2(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2,...)
Definition: eval.c:701
VALUE stop_exc
Definition: enumerator.c:121
VALUE rb_call_super(int, const VALUE *)
Definition: vm_eval.c:273
#define INFINITY
Definition: missing.h:138
VALUE rb_proc_call(VALUE, VALUE)
Definition: proc.c:592
static VALUE enumerator_initialize(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:332
VALUE rb_block_call_func(VALUE, VALUE, int, VALUE *)
Definition: ruby.h:1393
#define RARRAY_LENINT(ary)
Definition: ruby.h:908
static VALUE generator_each(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:1211
static VALUE lazy_lazy(VALUE obj)
Definition: enumerator.c:1856
static VALUE lazy_take_while_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1762
static VALUE lazy_initialize(int argc, VALUE *argv, VALUE self)
Definition: enumerator.c:1306
#define LONG2NUM(x)
Definition: ruby.h:1199
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:1564
#define recur(fmt)
static ID id_receiver
Definition: enumerator.c:108
VALUE rb_enum_values_pack(int argc, VALUE *argv)
Definition: enum.c:33
VALUE dst
Definition: enumerator.c:118
#define RFLOAT_VALUE(v)
Definition: ruby.h:836
int size
Definition: encoding.c:52
VALUE rb_yield_values2(int n, const VALUE *argv)
Definition: vm_eval.c:967
#define rb_check_arity(argc, min, max)
Definition: intern.h:277
#define INT2FIX(i)
Definition: ruby.h:241
#define UNLIMITED_ARGUMENTS
Definition: intern.h:54
static VALUE sym_each
Definition: enumerator.c:109
static VALUE inspect_enumerator(VALUE obj, VALUE dummy, int recur)
Definition: enumerator.c:874
static struct generator * generator_ptr(VALUE obj)
Definition: enumerator.c:1121
VALUE rb_block_proc(void)
Definition: proc.c:479
static VALUE lazy_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1431
#define ANYARGS
Definition: defines.h:57
static VALUE next_stopped(VALUE obj)
Definition: enumerator.c:1622
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:557
#define RARRAY_PTR(a)
Definition: ruby.h:904
static void yielder_mark(void *p)
Definition: enumerator.c:992
#define LONG2FIX(i)
Definition: ruby.h:242
#define RTEST(v)
Definition: ruby.h:445
VALUE rb_proc_new(VALUE(*)(ANYARGS), VALUE)
Definition: proc.c:2018
static ID id_arguments
Definition: enumerator.c:108
v
Definition: win32ole.c:798
static ID id_force
Definition: enumerator.c:108
void Init_Enumerator(void)
Definition: enumerator.c:1987
static ID id_size
Definition: enumerator.c:107
static VALUE next_ii(VALUE i, VALUE obj, int argc, VALUE *argv)
Definition: enumerator.c:563
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1019
VALUE rb_ary_dup(VALUE ary)
Definition: array.c:1778
static VALUE enumerator_allocate(VALUE klass)
Definition: enumerator.c:257
static VALUE lazy_zip_arrays_func(VALUE val, VALUE arrays, int argc, VALUE *argv)
Definition: enumerator.c:1628
static VALUE enumerator_init_copy(VALUE obj, VALUE orig)
Definition: enumerator.c:365
static VALUE lazy_flat_map_to_ary(VALUE obj, VALUE yielder)
Definition: enumerator.c:1465
VALUE rb_ary_new2(long capa)
Definition: array.c:417
static const rb_data_type_t enumerator_data_type
Definition: enumerator.c:164
static ID id_call
Definition: enumerator.c:107
#define ID2SYM(x)
Definition: ruby.h:363
static VALUE yielder_initialize(VALUE obj)
Definition: enumerator.c:1058
const char * rb_id2name(ID id)
Definition: ripper.c:17005
static VALUE ary2sv(VALUE args, int dup)
Definition: enumerator.c:686
VALUE args
Definition: enumerator.c:116
VALUE rb_inspect(VALUE)
Definition: object.c:402
static VALUE lazy_flat_map_i(VALUE i, VALUE yielder, int argc, VALUE *argv)
Definition: enumerator.c:1452
static ID id_new
Definition: enumerator.c:107
static VALUE enumerator_peek(VALUE obj)
Definition: enumerator.c:802
static VALUE lazy_set_method(VALUE lazy, VALUE args, VALUE(*size_fn)(ANYARGS))
Definition: enumerator.c:1328
static size_t yielder_memsize(const void *p)
Definition: enumerator.c:1001
static VALUE lazy_drop_while(VALUE obj)
Definition: enumerator.c:1839
static VALUE enumerator_inspect(VALUE obj)
Definition: enumerator.c:955
static ID id_rewind
Definition: enumerator.c:107
static VALUE lazy_zip(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:1680
#define yielder_free
Definition: enumerator.c:998
#define rb_intern(str)
static VALUE enumerator_feed(VALUE obj, VALUE v)
Definition: enumerator.c:837
#define NULL
Definition: _sdbm.c:103
#define FIX2LONG(x)
Definition: ruby.h:353
#define Qundef
Definition: ruby.h:436
static VALUE lazy_reject_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1559
static VALUE rb_cYielder
Definition: enumerator.c:126
static VALUE lazy_grep_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1582
static struct yielder * yielder_ptr(VALUE obj)
Definition: enumerator.c:1016
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
static ID id_each
Definition: enumerator.c:107
void rb_provide(const char *)
Definition: load.c:566
void rb_warn(const char *fmt,...)
Definition: error.c:216
ID rb_to_id(VALUE)
Definition: string.c:8154
VALUE rb_eArgError
Definition: error.c:512
static ID id_method
Definition: enumerator.c:108
static VALUE lazy_flat_map_each(VALUE obj, VALUE yielder)
Definition: enumerator.c:1458
#define NUM2LONG(x)
Definition: ruby.h:592
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1122
char ** argv
Definition: ruby.c:131
static VALUE lazy_take_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1713
static VALUE lazy_select_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1536