Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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 */
105 VALUE rb_cEnumerator;
106 VALUE rb_cLazy;
107 static ID id_rewind, id_each, id_new, id_initialize, id_yield, id_call, id_size, id_to_enum;
108 static ID id_eqq, id_next, id_result, id_lazy, id_receiver, id_arguments, id_memo, id_method, id_force;
109 static VALUE sym_each, sym_cycle;
110
111 VALUE rb_eStopIteration;
112
113 struct enumerator {
114 VALUE obj;
115 ID meth;
116 VALUE args;
117 VALUE fib;
118 VALUE dst;
119 VALUE lookahead;
120 VALUE feedvalue;
121 VALUE stop_exc;
122 VALUE size;
123 VALUE (*size_fn)(ANYARGS);
124 };
125
126 static VALUE rb_cGenerator, rb_cYielder;
127
128 struct generator {
129 VALUE proc;
130 };
131
132 struct yielder {
133 VALUE proc;
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
143 enumerator_mark(void *p)
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
164 static const rb_data_type_t enumerator_data_type = {
165 "enumerator",
166 {
167 enumerator_mark,
168 enumerator_free,
169 enumerator_memsize,
170 },
171 };
172
173 static struct enumerator *
174 enumerator_ptr(VALUE obj)
175 {
176 struct enumerator *ptr;
177
178 TypedData_Get_Struct(obj, struct enumerator, &enumerator_data_type, ptr);
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
241 obj_to_enum(int argc, VALUE *argv, VALUE obj)
242 {
243 VALUE enumerator, meth = sym_each;
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
257 enumerator_allocate(VALUE klass)
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
269 enumerator_init(VALUE enum_obj, VALUE obj, VALUE meth, int argc, VALUE *argv, VALUE (*size_fn)(ANYARGS), VALUE size)
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);
339 recv = generator_init(generator_allocate(rb_cGenerator), rb_block_proc());
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 {
351 rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
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
365 enumerator_init_copy(VALUE obj, VALUE orig)
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
376 TypedData_Get_Struct(obj, struct enumerator, &enumerator_data_type, ptr1);
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
407 rb_enumeratorize_with_size(VALUE obj, VALUE meth, int argc, VALUE *argv, VALUE (*size_fn)(ANYARGS))
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
414 return enumerator_init(enumerator_allocate(rb_cEnumerator),
415 obj, meth, argc, argv, size_fn, Qnil);
416 }
417
418 static VALUE
419 enumerator_block_call(VALUE obj, rb_block_call_func *func, VALUE arg)
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
461 enumerator_with_index_i(VALUE val, VALUE m, int argc, VALUE *argv)
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
476 enumerator_size(VALUE obj);
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);
498 return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)&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
512 enumerator_each_with_index(VALUE obj)
513 {
514 return enumerator_with_index(0, NULL, obj);
515 }
516
517 static VALUE
518 enumerator_with_object_i(VALUE val, VALUE memo, int argc, VALUE *argv)
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
554 enumerator_with_object(VALUE obj, VALUE memo)
555 {
556 RETURN_SIZED_ENUMERATOR(obj, 1, &memo, enumerator_size);
557 enumerator_block_call(obj, enumerator_with_object_i, memo);
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
599 get_next_values(VALUE obj, struct enumerator *e)
600 {
601 VALUE curr, vs;
602
603 if (e->stop_exc)
604 rb_exc_raise(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;
618 rb_exc_raise(e->stop_exc);
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
671 enumerator_next_values(VALUE obj)
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
728 enumerator_next(VALUE obj)
729 {
730 VALUE vs = enumerator_next_values(obj);
731 return ary2sv(vs, 0);
732 }
733
734 static VALUE
735 enumerator_peek_values(VALUE obj)
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
774 enumerator_peek_values_m(VALUE obj)
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
802 enumerator_peek(VALUE obj)
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
837 enumerator_feed(VALUE obj, VALUE v)
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
859 enumerator_rewind(VALUE obj)
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
874 inspect_enumerator(VALUE obj, VALUE dummy, int recur)
875 {
876 struct enumerator *e;
877 const char *cname;
878 VALUE eobj, eargs, str, method;
879 int tainted, untrusted;
880
881 TypedData_Get_Struct(obj, struct enumerator, &enumerator_data_type, e);
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
955 enumerator_inspect(VALUE obj)
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
972 enumerator_size(VALUE obj)
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
1006 static const rb_data_type_t yielder_data_type = {
1007 "yielder",
1008 {
1009 yielder_mark,
1010 yielder_free,
1011 yielder_memsize,
1012 },
1013 };
1014
1015 static struct yielder *
1016 yielder_ptr(VALUE obj)
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
1029 yielder_allocate(VALUE klass)
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
1041 yielder_init(VALUE obj, VALUE proc)
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
1058 yielder_initialize(VALUE obj)
1059 {
1060 rb_need_block();
1061
1062 return yielder_init(obj, rb_block_proc());
1063 }
1064
1065 /* :nodoc: */
1066 static VALUE
1067 yielder_yield(VALUE obj, VALUE args)
1068 {
1069 struct yielder *ptr = yielder_ptr(obj);
1070
1071 return rb_proc_call(ptr->proc, args);
1072 }
1073
1074 /* :nodoc: */
1075 static VALUE yielder_yield_push(VALUE obj, VALUE args)
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
1088 yielder_new(void)
1089 {
1090 return yielder_init(yielder_allocate(rb_cYielder), rb_proc_new(yielder_yield_i, 0));
1091 }
1092
1093 /*
1094 * Generator
1095 */
1096 static void
1097 generator_mark(void *p)
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
1111 static const rb_data_type_t generator_data_type = {
1112 "generator",
1113 {
1114 generator_mark,
1115 generator_free,
1116 generator_memsize,
1117 },
1118 };
1119
1120 static struct generator *
1121 generator_ptr(VALUE obj)
1122 {
1123 struct generator *ptr;
1124
1125 TypedData_Get_Struct(obj, struct generator, &generator_data_type, ptr);
1126 if (!ptr || ptr->proc == Qundef) {
1127 rb_raise(rb_eArgError, "uninitialized generator");
1128 }
1129 return ptr;
1130 }
1131
1132 /* :nodoc: */
1133 static VALUE
1134 generator_allocate(VALUE klass)
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
1146 generator_init(VALUE obj, VALUE proc)
1147 {
1148 struct generator *ptr;
1149
1150 TypedData_Get_Struct(obj, struct generator, &generator_data_type, ptr);
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))
1176 rb_raise(rb_eTypeError,
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
1190 generator_init_copy(VALUE obj, VALUE orig)
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
1226 enum_size(VALUE self)
1227 {
1228 VALUE r = rb_check_funcall(self, id_size, 0, 0);
1229 return (r == Qundef) ? Qnil : r;
1230 }
1231
1232 static VALUE
1233 lazy_size(VALUE self)
1234 {
1235 return enum_size(rb_ivar_get(self, id_receiver));
1236 }
1237
1238 static VALUE
1239 lazy_receiver_size(VALUE generator, VALUE args, VALUE lazy)
1240 {
1241 return lazy_size(lazy);
1242 }
1243
1244 static VALUE
1245 lazy_init_iterator(VALUE val, VALUE m, int argc, VALUE *argv)
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
1328 lazy_set_method(VALUE lazy, VALUE args, VALUE (*size_fn)(ANYARGS))
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 */
1335 rb_ivar_set(lazy, id_arguments, Qfalse);
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
1376 enumerable_lazy(VALUE obj)
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 {
1387 return enumerator_init(enumerator_allocate(rb_cLazy),
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
1440 lazy_map(VALUE obj)
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),
1448 Qnil, lazy_receiver_size);
1449 }
1450
1451 static VALUE
1452 lazy_flat_map_i(VALUE i, VALUE yielder, int argc, VALUE *argv)
1453 {
1454 return rb_funcall2(yielder, id_yield, argc, argv);
1455 }
1456
1457 static VALUE
1458 lazy_flat_map_each(VALUE obj, VALUE yielder)
1459 {
1460 rb_block_call(obj, id_each, 0, 0, lazy_flat_map_i, yielder);
1461 return Qnil;
1462 }
1463
1464 static VALUE
1465 lazy_flat_map_to_ary(VALUE obj, VALUE yielder)
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
1481 lazy_flat_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
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
1524 lazy_flat_map(VALUE obj)
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
1547 lazy_select(VALUE obj)
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
1570 lazy_reject(VALUE obj)
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() ?
1610 lazy_grep_iter : lazy_grep_func,
1611 pattern),
1612 rb_ary_new3(1, pattern), 0);
1613 }
1614
1615 static VALUE
1616 call_next(VALUE obj)
1617 {
1618 return rb_funcall(obj, id_next, 0);
1619 }
1620
1621 static VALUE
1622 next_stopped(VALUE obj)
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;
1684 rb_block_call_func *func = lazy_zip_arrays_func;
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
1732 lazy_take_size(VALUE generator, VALUE args, VALUE lazy)
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
1742 lazy_take(VALUE obj, VALUE n)
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
1771 lazy_take_while(VALUE obj)
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,
1777 lazy_take_while_func, 0),
1778 Qnil, 0);
1779 }
1780
1781 static VALUE
1782 lazy_drop_size(VALUE generator, VALUE args, VALUE lazy)
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
1813 lazy_drop(VALUE obj, VALUE n)
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
1839 lazy_drop_while(VALUE obj)
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,
1845 lazy_drop_while_func, 0),
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
1856 lazy_lazy(VALUE obj)
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
1909 stop_result(VALUE self)
1910 {
1911 return rb_attr_get(self, id_result);
1912 }
1913
1914 void
1915 InitVM_Enumerator(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);
1921 rb_include_module(rb_cEnumerator, rb_mEnumerable);
1922
1923 rb_define_alloc_func(rb_cEnumerator, enumerator_allocate);
1924 rb_define_method(rb_cEnumerator, "initialize", enumerator_initialize, -1);
1925 rb_define_method(rb_cEnumerator, "initialize_copy", enumerator_init_copy, 1);
1926 rb_define_method(rb_cEnumerator, "each", enumerator_each, -1);
1927 rb_define_method(rb_cEnumerator, "each_with_index", enumerator_each_with_index, 0);
1928 rb_define_method(rb_cEnumerator, "each_with_object", enumerator_with_object, 1);
1929 rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, -1);
1930 rb_define_method(rb_cEnumerator, "with_object", enumerator_with_object, 1);
1931 rb_define_method(rb_cEnumerator, "next_values", enumerator_next_values, 0);
1932 rb_define_method(rb_cEnumerator, "peek_values", enumerator_peek_values_m, 0);
1933 rb_define_method(rb_cEnumerator, "next", enumerator_next, 0);
1934 rb_define_method(rb_cEnumerator, "peek", enumerator_peek, 0);
1935 rb_define_method(rb_cEnumerator, "feed", enumerator_feed, 1);
1936 rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);
1937 rb_define_method(rb_cEnumerator, "inspect", enumerator_inspect, 0);
1938 rb_define_method(rb_cEnumerator, "size", enumerator_size, 0);
1939
1940 /* Lazy */
1941 rb_cLazy = rb_define_class_under(rb_cEnumerator, "Lazy", rb_cEnumerator);
1942 rb_define_method(rb_mEnumerable, "lazy", enumerable_lazy, 0);
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);
1966 rb_define_method(rb_eStopIteration, "result", stop_result, 0);
1967
1968 /* Generator */
1969 rb_cGenerator = rb_define_class_under(rb_cEnumerator, "Generator", rb_cObject);
1970 rb_include_module(rb_cGenerator, rb_mEnumerable);
1971 rb_define_alloc_func(rb_cGenerator, generator_allocate);
1972 rb_define_method(rb_cGenerator, "initialize", generator_initialize, -1);
1973 rb_define_method(rb_cGenerator, "initialize_copy", generator_init_copy, 1);
1974 rb_define_method(rb_cGenerator, "each", generator_each, -1);
1975
1976 /* Yielder */
1977 rb_cYielder = rb_define_class_under(rb_cEnumerator, "Yielder", rb_cObject);
1978 rb_define_alloc_func(rb_cYielder, yielder_allocate);
1979 rb_define_method(rb_cYielder, "initialize", yielder_initialize, 0);
1980 rb_define_method(rb_cYielder, "yield", yielder_yield, -2);
1981 rb_define_method(rb_cYielder, "<<", yielder_yield_push, -2);
1982
1983 rb_provide("enumerator.so"); /* for backward compatibility */
1984 }
1985
1986 void
1987 Init_Enumerator(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 }
lazy_drop
static VALUE lazy_drop(VALUE obj, VALUE n)
Definition: enumerator.c:1813
RB_TYPE_P
#define RB_TYPE_P(obj, type)
RARRAY_PTR
RARRAY_PTR(q->result)[0]
rb_to_int
VALUE rb_to_int(VALUE)
Definition: object.c:2431
lazy_zip_func
static VALUE lazy_zip_func(VALUE val, VALUE zip_args, int argc, VALUE *argv)
Definition: enumerator.c:1648
enumerator::size
VALUE size
Definition: enumerator.c:122
rb_block_call_func
VALUE rb_block_call_func(VALUE, VALUE, int, VALUE *)
Definition: ripper.y:1393
n
ssize_t n
Definition: bigdecimal.c:5655
ary
volatile VALUE ary
Definition: tcltklib.c:9713
enumerator_mark
static void enumerator_mark(void *p)
Definition: enumerator.c:143
rb_ary_new4
VALUE rb_ary_new4(long n, const VALUE *elts)
Definition: array.c:451
rb_ary_entry
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1101
next_i
static VALUE next_i(VALUE curr, VALUE obj)
Definition: enumerator.c:577
rb_cGenerator
static VALUE rb_cGenerator
Definition: enumerator.c:126
sym_cycle
static VALUE sym_cycle
Definition: enumerator.c:109
enumerator_rewind
static VALUE enumerator_rewind(VALUE obj)
Definition: enumerator.c:859
lazy_drop_while_func
static VALUE lazy_drop_while_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1826
enumerator
Definition: enumerator.c:113
lazy_reject
static VALUE lazy_reject(VALUE obj)
Definition: enumerator.c:1570
lazy_grep
static VALUE lazy_grep(VALUE obj, VALUE pattern)
Definition: enumerator.c:1606
rb_obj_classname
const char * rb_obj_classname(VALUE)
Definition: variable.c:396
p
Win32OLEIDispatch * p
Definition: win32ole.c:786
UNLIMITED_ARGUMENTS
#define UNLIMITED_ARGUMENTS
count
int count
Definition: encoding.c:51
call_next
static VALUE call_next(VALUE obj)
Definition: enumerator.c:1616
generator_mark
static void generator_mark(void *p)
Definition: enumerator.c:1097
node.h
enumerator_peek_values
static VALUE enumerator_peek_values(VALUE obj)
Definition: enumerator.c:735
yielder_new
static VALUE yielder_new(void)
Definition: enumerator.c:1088
generator_init_copy
static VALUE generator_init_copy(VALUE obj, VALUE orig)
Definition: enumerator.c:1190
ruby.h
generator_memsize
static size_t generator_memsize(const void *p)
Definition: enumerator.c:1106
enumerator::feedvalue
VALUE feedvalue
Definition: enumerator.c:120
proc
VALUE proc
Definition: tcltklib.c:2959
func
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
yielder_allocate
static VALUE yielder_allocate(VALUE klass)
Definition: enumerator.c:1029
i
ssize_t i
Definition: bigdecimal.c:5655
lazy_size
static VALUE lazy_size(VALUE self)
Definition: enumerator.c:1233
yielder_init
static VALUE yielder_init(VALUE obj, VALUE proc)
Definition: enumerator.c:1041
RFLOAT_VALUE
#define RFLOAT_VALUE(v)
rb_frame_this_func
ID rb_frame_this_func(void)
Definition: eval.c:902
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:511
rb_obj_dup
VALUE rb_obj_dup(VALUE)
Definition: object.c:338
rb_define_alloc_func
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
OBJ_TAINTED
#define OBJ_TAINTED(x)
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:822
enumerator_with_object_i
static VALUE enumerator_with_object_i(VALUE val, VALUE memo, int argc, VALUE *argv)
Definition: enumerator.c:518
rb_yield_values2
VALUE rb_yield_values2(int n, const VALUE *argv)
Definition: vm_eval.c:967
rb_fiber_yield
VALUE rb_fiber_yield(int argc, VALUE *argv)
Definition: cont.c:1394
rb_yield_values
VALUE rb_yield_values(int n,...)
Definition: vm_eval.c:945
generator_data_type
static const rb_data_type_t generator_data_type
Definition: enumerator.c:1111
rb_ary_tmp_new
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:465
NIL_P
NIL_P(eventloop_thread)
Definition: tcltklib.c:4068
T_ARRAY
#define T_ARRAY
enumerator_init
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
rb_str_buf_cat2
VALUE rb_str_buf_cat2(VALUE, const char *)
Definition: string.c:1957
enumerator_next_values
static VALUE enumerator_next_values(VALUE obj)
Definition: enumerator.c:671
rb_cEnumerator
VALUE rb_cEnumerator
Definition: enumerator.c:105
rb_fiber_alive_p
VALUE rb_fiber_alive_p(VALUE fibval)
Definition: cont.c:1422
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:545
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1780
enumerator::obj
VALUE obj
Definition: enumerator.c:114
rb_proc_new
VALUE rb_proc_new(VALUE(*)(ANYARGS), VALUE)
Definition: proc.c:2018
rb_fiber_current
VALUE rb_fiber_current(void)
Definition: cont.c:1230
Qtrue
return Qtrue
Definition: tcltklib.c:9610
yielder_data_type
static const rb_data_type_t yielder_data_type
Definition: enumerator.c:1006
generator::proc
VALUE proc
Definition: enumerator.c:129
enum_size
static VALUE enum_size(VALUE self)
Definition: enumerator.c:1226
generator_free
#define generator_free
Definition: enumerator.c:1103
rb_ary_new3
VALUE rb_ary_new3(long n,...)
Definition: array.c:432
enumerator_next
static VALUE enumerator_next(VALUE obj)
Definition: enumerator.c:728
lazy_to_enum_i
static VALUE lazy_to_enum_i(VALUE self, VALUE meth, int argc, VALUE *argv, VALUE(*size_fn)(ANYARGS))
Definition: enumerator.c:1385
receiver
receiver
Definition: tcltklib.c:3576
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:695
enumerator_block_call
static VALUE enumerator_block_call(VALUE obj, rb_block_call_func *func, VALUE arg)
Definition: enumerator.c:419
id_lazy
static ID id_lazy
Definition: enumerator.c:108
r
r
Definition: bigdecimal.c:1196
lazy_to_enum
static VALUE lazy_to_enum(int argc, VALUE *argv, VALUE self)
Definition: enumerator.c:1415
InitVM
#define InitVM(ext)
lazy_drop_func
static VALUE lazy_drop_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1796
ID2SYM
#define ID2SYM(x)
yielder_yield
static VALUE yielder_yield(VALUE obj, VALUE args)
Definition: enumerator.c:1067
T_FLOAT
#define T_FLOAT
args
VALUE VALUE args
Definition: tcltklib.c:2561
generator_initialize
static VALUE generator_initialize(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:1163
LONG2NUM
#define LONG2NUM(x)
generator_allocate
static VALUE generator_allocate(VALUE klass)
Definition: enumerator.c:1134
yielder_yield_i
static VALUE yielder_yield_i(VALUE obj, VALUE memo, int argc, VALUE *argv)
Definition: enumerator.c:1082
rb_ivar_get
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1116
rb_str_concat
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:2162
id_memo
static ID id_memo
Definition: enumerator.c:108
generator
Definition: enumerator.c:128
lazy_init_iterator
static VALUE lazy_init_iterator(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1245
enumerator_with_object
static VALUE enumerator_with_object(VALUE obj, VALUE memo)
Definition: enumerator.c:554
InitVM_Enumerator
void InitVM_Enumerator(void)
Definition: enumerator.c:1915
lazy_flat_map
static VALUE lazy_flat_map(VALUE obj)
Definition: enumerator.c:1524
rb_enumeratorize
VALUE rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv)
Definition: enumerator.c:398
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Definition: eval.c:527
lazy_map
static VALUE lazy_map(VALUE obj)
Definition: enumerator.c:1440
id_initialize
static ID id_initialize
Definition: enumerator.c:107
enumerator_ptr
static struct enumerator * enumerator_ptr(VALUE obj)
Definition: enumerator.c:174
rb_enumeratorize_with_size
VALUE rb_enumeratorize_with_size(VALUE obj, VALUE meth, int argc, VALUE *argv, VALUE(*size_fn)(ANYARGS))
Definition: enumerator.c:407
rb_provide
void rb_provide(const char *)
Definition: load.c:566
enumerable_lazy
static VALUE enumerable_lazy(VALUE obj)
Definition: enumerator.c:1376
rb_iter_break
void rb_iter_break(void)
Definition: vm.c:960
id_to_enum
static ID id_to_enum
Definition: enumerator.c:107
get_next_values
static VALUE get_next_values(VALUE obj, struct enumerator *e)
Definition: enumerator.c:599
rb_block_proc
VALUE rb_block_proc(void)
Definition: proc.c:479
rb_fiber_resume
VALUE rb_fiber_resume(VALUE fibval, int argc, VALUE *argv)
Definition: cont.c:1378
lazy_flat_map_func
static VALUE lazy_flat_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1481
m
BDIGIT m
Definition: bigdecimal.c:5085
enumerator_each
static VALUE enumerator_each(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:442
enumerator_with_index
static VALUE enumerator_with_index(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:491
FIXNUM_P
#define FIXNUM_P(f)
Qfalse
return Qfalse
Definition: tcltklib.c:6779
TypedData_Get_Struct
#define TypedData_Get_Struct(obj, type, data_type, sval)
rb_block_given_p
int rb_block_given_p(void)
Definition: eval.c:672
obj_to_enum
static VALUE obj_to_enum(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:241
RARRAY_LEN
#define RARRAY_LEN(a)
rb_ary_cat
VALUE rb_ary_cat(VALUE ary, const VALUE *ptr, long len)
Definition: array.c:846
Qnil
#define Qnil
Definition: tcltklib.c:1896
val
#define val
Definition: tcltklib.c:1949
rb_mKernel
RUBY_EXTERN VALUE rb_mKernel
Definition: ripper.y:1414
str
static VALUE char * str
Definition: tcltklib.c:3547
Check_Type
#define Check_Type(v, t)
ID
unsigned long ID
Definition: ripper.y:105
id_next
static ID id_next
Definition: enumerator.c:108
rb_gc_mark
void rb_gc_mark(VALUE)
Definition: gc.c:2598
enumerator_peek_values_m
static VALUE enumerator_peek_values_m(VALUE obj)
Definition: enumerator.c:774
enumerator_size
static VALUE enumerator_size(VALUE obj)
Definition: enumerator.c:972
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:499
obj
static VALUE VALUE obj
Definition: tcltklib.c:3158
next_init
static void next_init(VALUE obj, struct enumerator *e)
Definition: enumerator.c:590
INT2FIX
#define INT2FIX(i)
lazy_take_size
static VALUE lazy_take_size(VALUE generator, VALUE args, VALUE lazy)
Definition: enumerator.c:1732
idx
int idx
Definition: tcltklib.c:9716
yielder::proc
VALUE proc
Definition: enumerator.c:133
FIX2LONG
#define FIX2LONG(x)
ANYARGS
#define ANYARGS
rb_fiber_new
VALUE rb_fiber_new(VALUE(*func)(ANYARGS), VALUE obj)
Definition: cont.c:1120
stop_result
static VALUE stop_result(VALUE self)
Definition: enumerator.c:1909
OBJ_INIT_COPY
#define OBJ_INIT_COPY(obj, orig)
method
volatile ID method
Definition: tcltklib.c:3599
rb_call_super
VALUE rb_call_super(int, const VALUE *)
Definition: vm_eval.c:273
enumerator_with_index_i
static VALUE enumerator_with_index_i(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:461
lazy_take_while
static VALUE lazy_take_while(VALUE obj)
Definition: enumerator.c:1771
lazy_select
static VALUE lazy_select(VALUE obj)
Definition: enumerator.c:1547
rb_cLazy
VALUE rb_cLazy
Definition: enumerator.c:106
id_result
static ID id_result
Definition: enumerator.c:108
generator_init
static VALUE generator_init(VALUE obj, VALUE proc)
Definition: enumerator.c:1146
enumerator::lookahead
VALUE lookahead
Definition: enumerator.c:119
rb_check_funcall
VALUE rb_check_funcall(VALUE, ID, int, VALUE *)
Definition: vm_eval.c:408
rb_eIndexError
VALUE rb_eIndexError
Definition: error.c:513
lazy_receiver_size
static VALUE lazy_receiver_size(VALUE generator, VALUE args, VALUE lazy)
Definition: enumerator.c:1239
rb_need_block
void rb_need_block(void)
Definition: eval.c:693
rb_long2int
#define rb_long2int(n)
yielder_yield_push
static VALUE yielder_yield_push(VALUE obj, VALUE args)
Definition: enumerator.c:1075
argv
VALUE * argv
Definition: tcltklib.c:1971
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1539
rb_yield
VALUE rb_yield(VALUE)
Definition: vm_eval.c:934
RTEST
#define RTEST(v)
enumerator_free
#define enumerator_free
Definition: enumerator.c:156
lazy_super
static VALUE lazy_super(int argc, VALUE *argv, VALUE lazy)
Definition: enumerator.c:1850
id_yield
static ID id_yield
Definition: enumerator.c:107
TRUE
#define TRUE
Definition: nkf.h:175
lazy_init_block_i
static VALUE lazy_init_block_i(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1271
result
q result
Definition: tcltklib.c:7070
rb_mEnumerable
VALUE rb_mEnumerable
Definition: enum.c:20
lazy_drop_size
static VALUE lazy_drop_size(VALUE generator, VALUE args, VALUE lazy)
Definition: enumerator.c:1782
lazy_take
static VALUE lazy_take(VALUE obj, VALUE n)
Definition: enumerator.c:1742
rb_sprintf
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1270
const
#define const
Definition: strftime.c:102
rb_proc_call_with_block
VALUE rb_proc_call_with_block(VALUE, int argc, VALUE *argv, VALUE)
Definition: proc.c:605
enumerator_each_with_index
static VALUE enumerator_each_with_index(VALUE obj)
Definition: enumerator.c:512
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
rb_block_call
VALUE rb_block_call(VALUE, ID, int, VALUE *, VALUE(*)(ANYARGS), VALUE)
Definition: vm_eval.c:1131
internal.h
lazy_grep_iter
static VALUE lazy_grep_iter(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1594
enumerator::size_fn
VALUE(* size_fn)(ANYARGS)
Definition: enumerator.c:123
enumerator::fib
VALUE fib
Definition: enumerator.c:117
rb_exc_new2
VALUE rb_exc_new2(VALUE etype, const char *s)
Definition: error.c:542
rb_eStopIteration
VALUE rb_eStopIteration
Definition: enumerator.c:111
id_eqq
static ID id_eqq
Definition: enumerator.c:108
RB_GC_GUARD
#define RB_GC_GUARD(v)
argc
int argc
Definition: tcltklib.c:1970
enumerator_memsize
static size_t enumerator_memsize(const void *p)
Definition: enumerator.c:159
rb_rescue2
VALUE rb_rescue2(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2,...)
Definition: eval.c:701
enumerator::stop_exc
VALUE stop_exc
Definition: enumerator.c:121
rb_attr_get
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1122
INFINITY
#define INFINITY
Definition: missing.h:138
enumerator_initialize
static VALUE enumerator_initialize(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:332
generator_each
static VALUE generator_each(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:1211
lazy_lazy
static VALUE lazy_lazy(VALUE obj)
Definition: enumerator.c:1856
ptr
return ptr
Definition: tcltklib.c:784
lazy_take_while_func
static VALUE lazy_take_while_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1762
lazy_initialize
static VALUE lazy_initialize(int argc, VALUE *argv, VALUE self)
Definition: enumerator.c:1306
rb_obj_is_proc
VALUE rb_obj_is_proc(VALUE)
Definition: proc.c:91
rb_to_id
ID rb_to_id(VALUE)
Definition: string.c:8154
recur
#define recur(fmt)
Definition: date_strptime.c:147
rb_ivar_set
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1128
id_receiver
static ID id_receiver
Definition: enumerator.c:108
arg
arg
Definition: ripper.y:1315
rb_enum_values_pack
VALUE rb_enum_values_pack(int argc, VALUE *argv)
Definition: enum.c:33
T_SYMBOL
#define T_SYMBOL
enumerator::dst
VALUE dst
Definition: enumerator.c:118
RETURN_SIZED_ENUMERATOR
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
size
int size
Definition: encoding.c:52
NUM2LONG
#define NUM2LONG(x)
sym_each
static VALUE sym_each
Definition: enumerator.c:109
inspect_enumerator
static VALUE inspect_enumerator(VALUE obj, VALUE dummy, int recur)
Definition: enumerator.c:874
generator_ptr
static struct generator * generator_ptr(VALUE obj)
Definition: enumerator.c:1121
Qundef
#define Qundef
rb_obj_is_kind_of
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:582
rb_proc_call
VALUE rb_proc_call(VALUE, VALUE)
Definition: proc.c:592
lazy_map_func
static VALUE lazy_map_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1431
next_stopped
static VALUE next_stopped(VALUE obj)
Definition: enumerator.c:1622
rb_data_type_struct
Definition: ripper.y:960
rb_check_array_type
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:557
TypedData_Make_Struct
#define TypedData_Make_Struct(klass, type, data_type, sval)
yielder_mark
static void yielder_mark(void *p)
Definition: enumerator.c:992
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ripper.y:1426
LONG2FIX
#define LONG2FIX(i)
RARRAY_LENINT
#define RARRAY_LENINT(ary)
klass
klass
Definition: tcltklib.c:3504
INT2NUM
#define INT2NUM(x)
id_arguments
static ID id_arguments
Definition: enumerator.c:108
id_force
static ID id_force
Definition: enumerator.c:108
Init_Enumerator
void Init_Enumerator(void)
Definition: enumerator.c:1987
rb_respond_to
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:1564
id_size
static ID id_size
Definition: enumerator.c:107
next_ii
static VALUE next_ii(VALUE i, VALUE obj, int argc, VALUE *argv)
Definition: enumerator.c:563
rb_ary_dup
VALUE rb_ary_dup(VALUE ary)
Definition: array.c:1778
enumerator_allocate
static VALUE enumerator_allocate(VALUE klass)
Definition: enumerator.c:257
lazy_zip_arrays_func
static VALUE lazy_zip_arrays_func(VALUE val, VALUE arrays, int argc, VALUE *argv)
Definition: enumerator.c:1628
rb_exec_recursive
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:4852
enumerator_init_copy
static VALUE enumerator_init_copy(VALUE obj, VALUE orig)
Definition: enumerator.c:365
lazy_flat_map_to_ary
static VALUE lazy_flat_map_to_ary(VALUE obj, VALUE yielder)
Definition: enumerator.c:1465
rb_ary_new2
VALUE rb_ary_new2(long capa)
Definition: array.c:417
OBJ_UNTRUST
#define OBJ_UNTRUST(x)
enumerator_data_type
static const rb_data_type_t enumerator_data_type
Definition: enumerator.c:164
id_call
static ID id_call
Definition: enumerator.c:107
yielder_initialize
static VALUE yielder_initialize(VALUE obj)
Definition: enumerator.c:1058
rb_id2name
const char * rb_id2name(ID id)
Definition: ripper.c:17005
rb_check_arity
#define rb_check_arity(argc, min, max)
OBJ_UNTRUSTED
#define OBJ_UNTRUSTED(x)
ary2sv
static VALUE ary2sv(VALUE args, int dup)
Definition: enumerator.c:686
enumerator::args
VALUE args
Definition: enumerator.c:116
e
BDIGIT e
Definition: bigdecimal.c:5085
lazy_flat_map_i
static VALUE lazy_flat_map_i(VALUE i, VALUE yielder, int argc, VALUE *argv)
Definition: enumerator.c:1452
rb_funcall2
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
Definition: vm_eval.c:805
VALUE
unsigned long VALUE
Definition: ripper.y:104
id_new
static ID id_new
Definition: enumerator.c:107
enumerator_peek
static VALUE enumerator_peek(VALUE obj)
Definition: enumerator.c:802
lazy_set_method
static VALUE lazy_set_method(VALUE lazy, VALUE args, VALUE(*size_fn)(ANYARGS))
Definition: enumerator.c:1328
yielder_memsize
static size_t yielder_memsize(const void *p)
Definition: enumerator.c:1001
lazy_drop_while
static VALUE lazy_drop_while(VALUE obj)
Definition: enumerator.c:1839
enumerator_inspect
static VALUE enumerator_inspect(VALUE obj)
Definition: enumerator.c:955
id_rewind
static ID id_rewind
Definition: enumerator.c:107
OBJ_TAINT
#define OBJ_TAINT(x)
lazy_zip
static VALUE lazy_zip(int argc, VALUE *argv, VALUE obj)
Definition: enumerator.c:1680
yielder_free
#define yielder_free
Definition: enumerator.c:998
rb_intern
#define rb_intern(str)
v
BDIGIT v
Definition: bigdecimal.c:5656
yielder
Definition: enumerator.c:132
enumerator_feed
static VALUE enumerator_feed(VALUE obj, VALUE v)
Definition: enumerator.c:837
NULL
#define NULL
Definition: _sdbm.c:103
lazy_reject_func
static VALUE lazy_reject_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1559
rb_cYielder
static VALUE rb_cYielder
Definition: enumerator.c:126
lazy_grep_func
static VALUE lazy_grep_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1582
yielder_ptr
static struct yielder * yielder_ptr(VALUE obj)
Definition: enumerator.c:1016
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
id_each
static ID id_each
Definition: enumerator.c:107
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:216
SYM2ID
#define SYM2ID(x)
rb_eArgError
VALUE rb_eArgError
Definition: error.c:512
id_method
static ID id_method
Definition: enumerator.c:108
lazy_flat_map_each
static VALUE lazy_flat_map_each(VALUE obj, VALUE yielder)
Definition: enumerator.c:1458
dummy
int dummy
Definition: tcltklib.c:4483
rb_inspect
VALUE rb_inspect(VALUE)
Definition: object.c:402
enumerator::meth
ID meth
Definition: enumerator.c:115
lazy_take_func
static VALUE lazy_take_func(VALUE val, VALUE args, int argc, VALUE *argv)
Definition: enumerator.c:1713
lazy_select_func
static VALUE lazy_select_func(VALUE val, VALUE m, int argc, VALUE *argv)
Definition: enumerator.c:1536
len
size_t len
Definition: tcltklib.c:3568