15#include "ruby/internal/config.h"
23#include "internal/enumerator.h"
24#include "internal/error.h"
25#include "internal/hash.h"
26#include "internal/imemo.h"
27#include "internal/numeric.h"
28#include "internal/range.h"
29#include "internal/rational.h"
127static VALUE rb_cLazy;
128static ID id_rewind, id_new, id_to_enum;
129static ID id_next, id_result, id_receiver, id_arguments, id_memo, id_method, id_force;
130static ID id_begin, id_end, id_step, id_exclude_end;
131static VALUE sym_each, sym_cycle, sym_yield;
133static VALUE lazy_use_super_method;
135#define id_call idCall
136#define id_each idEach
138#define id_initialize idInitialize
139#define id_size idSize
158static VALUE rb_cGenerator, rb_cYielder, rb_cEnumProducer;
177 lazyenum_proc_func *proc;
178 lazyenum_size_func *size;
190static VALUE rb_cEnumChain;
203enumerator_mark(
void *p)
206 rb_gc_mark_movable(ptr->obj);
207 rb_gc_mark_movable(ptr->args);
208 rb_gc_mark_movable(ptr->fib);
209 rb_gc_mark_movable(ptr->dst);
210 rb_gc_mark_movable(ptr->lookahead);
211 rb_gc_mark_movable(ptr->feedvalue);
212 rb_gc_mark_movable(ptr->stop_exc);
213 rb_gc_mark_movable(ptr->size);
214 rb_gc_mark_movable(ptr->procs);
218enumerator_compact(
void *p)
221 ptr->obj = rb_gc_location(ptr->obj);
222 ptr->args = rb_gc_location(ptr->args);
223 ptr->fib = rb_gc_location(ptr->fib);
224 ptr->dst = rb_gc_location(ptr->dst);
225 ptr->lookahead = rb_gc_location(ptr->lookahead);
226 ptr->feedvalue = rb_gc_location(ptr->feedvalue);
227 ptr->stop_exc = rb_gc_location(ptr->stop_exc);
228 ptr->size = rb_gc_location(ptr->size);
229 ptr->procs = rb_gc_location(ptr->procs);
232#define enumerator_free RUBY_TYPED_DEFAULT_FREE
235enumerator_memsize(
const void *p)
248 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
252enumerator_ptr(
VALUE obj)
257 if (!ptr || ptr->obj ==
Qundef) {
264proc_entry_mark(
void *p)
267 rb_gc_mark_movable(ptr->proc);
268 rb_gc_mark_movable(ptr->memo);
272proc_entry_compact(
void *p)
275 ptr->proc = rb_gc_location(ptr->proc);
276 ptr->memo = rb_gc_location(ptr->memo);
279#define proc_entry_free RUBY_TYPED_DEFAULT_FREE
282proc_entry_memsize(
const void *p)
377 enumerator = rb_enumeratorize_with_size(obj, meth, argc, argv, 0);
385enumerator_allocate(
VALUE klass)
417 ptr->size_fn = size_fn;
418 ptr->kw_splat = kw_splat;
424convert_to_feasible_size_value(
VALUE obj)
466enumerator_initialize(
int argc,
VALUE *argv,
VALUE obj)
469 VALUE recv = generator_init(generator_allocate(rb_cGenerator), iter);
471 VALUE size = convert_to_feasible_size_value(arg0);
473 return enumerator_init(obj, recv, sym_each, 0, 0, 0, size,
false);
483 ptr0 = enumerator_ptr(orig);
495 ptr1->obj = ptr0->obj;
496 ptr1->meth = ptr0->meth;
497 ptr1->args = ptr0->args;
501 ptr1->size = ptr0->size;
502 ptr1->size_fn = ptr0->size_fn;
513 return rb_enumeratorize_with_size(obj, meth, argc, argv, 0);
525 base_class = rb_cLazy;
528 obj = enumerator_init(enumerator_allocate(
rb_cEnumerator), obj, sym_each, 0, 0, 0,
Qnil,
false);
531 return enumerator_init(enumerator_allocate(base_class),
532 obj, meth, argc, argv, size_fn,
Qnil, kw_splat);
538 return rb_enumeratorize_with_size_kw(obj, meth, argc, argv, size_fn,
rb_keyword_given_p());
545 const VALUE *argv = 0;
546 const struct enumerator *e = enumerator_ptr(obj);
593enumerator_each(
int argc,
VALUE *argv,
VALUE obj)
597 VALUE args = e->args;
599#if SIZEOF_INT < SIZEOF_LONG
603 args = rb_ary_dup(args);
604 rb_ary_cat(args, argv, argc);
614 return enumerator_block_call(obj, 0, obj);
620 struct MEMO *memo = (
struct MEMO *)m;
621 VALUE idx = memo->v1;
622 MEMO_V1_SET(memo, rb_int_succ(idx));
631enumerator_size(
VALUE obj);
636 return enumerator_size(obj);
652enumerator_with_index(
int argc,
VALUE *argv,
VALUE obj)
659 return enumerator_block_call(obj, enumerator_with_index_i, (
VALUE)MEMO_NEW(memo, 0, 0));
673enumerator_each_with_index(
VALUE obj)
675 return enumerator_with_index(0, NULL, obj);
720 enumerator_block_call(obj, enumerator_with_object_i, memo);
731 rb_fiber_yield(1, &args);
732 if (e->feedvalue !=
Qundef) {
733 feedvalue = e->feedvalue;
749 return rb_fiber_yield(1, &nil);
755 VALUE curr = rb_fiber_current();
769 curr = rb_fiber_current();
771 if (!e->fib || !rb_fiber_alive_p(e->fib)) {
775 vs = rb_fiber_resume(e->fib, 1, &curr);
832enumerator_next_values(
VALUE obj)
837 if (e->lookahead !=
Qundef) {
843 return get_next_values(obj, e);
847ary2sv(
VALUE args,
int dup)
861 return rb_ary_dup(args);
887enumerator_next(
VALUE obj)
889 VALUE vs = enumerator_next_values(obj);
890 return ary2sv(vs, 0);
894enumerator_peek_values(
VALUE obj)
898 if (e->lookahead ==
Qundef) {
899 e->lookahead = get_next_values(obj, e);
935enumerator_peek_values_m(
VALUE obj)
937 return rb_ary_dup(enumerator_peek_values(obj));
965enumerator_peek(
VALUE obj)
967 VALUE vs = enumerator_peek_values(obj);
968 return ary2sv(vs, 1);
1022 if (e->feedvalue !=
Qundef) {
1040enumerator_rewind(
VALUE obj)
1058inspect_enumerator(
VALUE obj,
VALUE dummy,
int recur)
1061 VALUE eobj, str, cname;
1067 if (!e || e->obj ==
Qundef) {
1079 eobj = generator_ptr(e->obj)->obj;
1088 str =
rb_sprintf(
"#<%"PRIsVALUE
": %"PRIsVALUE, cname, str);
1089 append_method(
RARRAY_AREF(e->procs, i), str, e->meth, e->args);
1102 append_method(obj, str, e->meth, e->args);
1112 if (
SYMBOL_P(key))
return ST_CONTINUE;
1113 *(
int *)arg = FALSE;
1121 rb_str_catf(str,
"% "PRIsVALUE
": %"PRIsVALUE
", ", key, val);
1128 VALUE method, eargs;
1132 if (!
NIL_P(method)) {
1137 method = rb_id2str(default_method);
1145 eargs = default_args;
1159 if (all_key) kwds = argv[--argc];
1163 VALUE arg = *argv++;
1187enumerator_inspect(
VALUE obj)
1204enumerator_size(
VALUE obj)
1208 const VALUE *argv = NULL;
1212 struct generator *g = generator_ptr(e->obj);
1218 struct proc_entry *entry = proc_entry_ptr(proc);
1219 lazyenum_size_func *size_fn = entry->fn->size;
1223 receiver = (*size_fn)(proc, receiver);
1229 return (*e->size_fn)(e->obj, e->args, obj);
1236 if (size !=
Qundef)
return size;
1244yielder_mark(
void *p)
1247 rb_gc_mark_movable(ptr->proc);
1251yielder_compact(
void *p)
1254 ptr->proc = rb_gc_location(ptr->proc);
1257#define yielder_free RUBY_TYPED_DEFAULT_FREE
1260yielder_memsize(
const void *p)
1262 return sizeof(
struct yielder);
1273 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1277yielder_ptr(
VALUE obj)
1282 if (!ptr || ptr->proc ==
Qundef) {
1290yielder_allocate(
VALUE klass)
1319yielder_initialize(
VALUE obj)
1330 struct yielder *ptr = yielder_ptr(obj);
1339 struct yielder *ptr = yielder_ptr(obj);
1359yielder_to_proc(
VALUE obj)
1375 return yielder_init(yielder_allocate(rb_cYielder),
rb_proc_new(yielder_yield_i, 0));
1382generator_mark(
void *p)
1385 rb_gc_mark_movable(ptr->proc);
1386 rb_gc_mark_movable(ptr->obj);
1390generator_compact(
void *p)
1393 ptr->proc = rb_gc_location(ptr->proc);
1394 ptr->obj = rb_gc_location(ptr->obj);
1397#define generator_free RUBY_TYPED_DEFAULT_FREE
1400generator_memsize(
const void *p)
1413 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1417generator_ptr(
VALUE obj)
1422 if (!ptr || ptr->proc ==
Qundef) {
1430generator_allocate(
VALUE klass)
1460generator_initialize(
int argc,
VALUE *argv,
VALUE obj)
1474 "wrong argument type %"PRIsVALUE
" (expected Proc)",
1478 rb_warn(
"given block not used");
1482 return generator_init(obj, proc);
1493 ptr0 = generator_ptr(orig);
1501 ptr1->proc = ptr0->proc;
1508generator_each(
int argc,
VALUE *argv,
VALUE obj)
1510 struct generator *ptr = generator_ptr(obj);
1513 rb_ary_push(args, yielder_new());
1515 rb_ary_cat(args, argv, argc);
1523enum_size(
VALUE self)
1532 return enum_size(self);
1535#define lazy_receiver_size lazy_map_size
1566 rb_block_call(m, id_each, argc-1, argv+1, lazy_init_iterator, val);
1570#define memo_value v2
1571#define memo_flags u3.state
1572#define LAZY_MEMO_BREAK 1
1573#define LAZY_MEMO_PACKED 2
1574#define LAZY_MEMO_BREAK_P(memo) ((memo)->memo_flags & LAZY_MEMO_BREAK)
1575#define LAZY_MEMO_PACKED_P(memo) ((memo)->memo_flags & LAZY_MEMO_PACKED)
1576#define LAZY_MEMO_SET_BREAK(memo) ((memo)->memo_flags |= LAZY_MEMO_BREAK)
1577#define LAZY_MEMO_RESET_BREAK(memo) ((memo)->memo_flags &= ~LAZY_MEMO_BREAK)
1578#define LAZY_MEMO_SET_VALUE(memo, value) MEMO_V2_SET(memo, value)
1579#define LAZY_MEMO_SET_PACKED(memo) ((memo)->memo_flags |= LAZY_MEMO_PACKED)
1580#define LAZY_MEMO_RESET_PACKED(memo) ((memo)->memo_flags &= ~LAZY_MEMO_PACKED)
1590 struct MEMO *result;
1592 result = MEMO_NEW(m, rb_enum_values_pack(argc, argv),
1593 argc > 1 ? LAZY_MEMO_PACKED : 0);
1594 return lazy_yielder_result(result,
yielder, procs_array, memos, 0);
1598lazy_yielder_yield(
struct MEMO *result,
long memo_index,
int argc,
const VALUE *argv)
1600 VALUE m = result->v1;
1604 LAZY_MEMO_SET_VALUE(result, rb_enum_values_pack(argc, argv));
1606 LAZY_MEMO_SET_PACKED(result);
1608 LAZY_MEMO_RESET_PACKED(result);
1609 return lazy_yielder_result(result,
yielder, procs_array, memos, memo_index);
1619 struct proc_entry *entry = proc_entry_ptr(proc);
1620 if (!(*entry->fn->proc)(proc, result, memos, i)) {
1629 if (LAZY_MEMO_BREAK_P(result)) {
1632 return result->memo_value;
1655 struct generator *old_gen_ptr = generator_ptr(e->obj);
1656 obj = old_gen_ptr->obj;
1662 generator = generator_allocate(rb_cGenerator);
1763lazy_initialize(
int argc,
VALUE *argv,
VALUE self)
1776 generator = generator_allocate(rb_cGenerator);
1778 enumerator_init(self,
generator, sym_each, 0, 0, 0, size, 0);
1801 ID id = rb_frame_this_func();
1817 lazy_set_args(lazy, args);
1818 e->size_fn = size_fn;
1829 VALUE new_generator;
1834 &proc_entry_data_type, entry);
1841 lazy_set_args(entry_obj, memo);
1843 new_procs =
RTEST(e->procs) ? rb_ary_dup(e->procs) : rb_ary_new();
1844 new_generator = lazy_generator_init(obj, new_procs);
1845 rb_ary_push(new_procs, entry_obj);
1847 new_obj = enumerator_init_copy(enumerator_allocate(rb_cLazy), obj);
1849 new_e->obj = new_generator;
1850 new_e->procs = new_procs;
1857 new_e->meth = id_each;
1893enumerable_lazy(
VALUE obj)
1904 return enumerator_init(enumerator_allocate(rb_cLazy),
1905 obj, meth, argc, argv, size_fn,
Qnil, kw_splat);
1932lazy_to_enum(
int argc,
VALUE *argv,
VALUE self)
1934 VALUE lazy, meth = sym_each, super_meth;
1940 if (
RTEST((super_meth = rb_hash_aref(lazy_use_super_method, meth)))) {
1953 return enum_size(self);
1964lazy_eager(
VALUE self)
1967 self, sym_each, 0, 0, lazy_eager_size,
Qnil, 0);
1982 const VALUE *argv = &result->memo_value;
1983 if (LAZY_MEMO_PACKED_P(result)) {
1984 const VALUE args = *argv;
1995 LAZY_MEMO_SET_VALUE(result, value);
1996 LAZY_MEMO_RESET_PACKED(result);
2007 lazy_map_proc, lazy_map_size,
2030 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_map_funcs);
2034 struct MEMO *result;
2043 return lazy_yielder_yield(arg->result, arg->index, argc, argv);
2051 const long proc_index = memo_index + 1;
2052 int break_p = LAZY_MEMO_BREAK_P(result);
2058 struct flat_map_i_arg arg = {.result = result, .index = proc_index};
2059 LAZY_MEMO_RESET_BREAK(result);
2061 if (break_p) LAZY_MEMO_SET_BREAK(result);
2065 if (ary || !
NIL_P(ary = rb_check_array_type(value))) {
2067 LAZY_MEMO_RESET_BREAK(result);
2070 lazy_yielder_yield(result, proc_index, 1, &argv);
2072 if (break_p) LAZY_MEMO_SET_BREAK(result);
2076 LAZY_MEMO_SET_VALUE(result, value);
2077 LAZY_MEMO_RESET_PACKED(result);
2082 lazy_flat_map_proc, 0,
2109lazy_flat_map(
VALUE obj)
2115 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_flat_map_funcs);
2122 if (!
RTEST(chain))
return 0;
2127 lazy_select_proc, 0,
2139lazy_select(
VALUE obj)
2145 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_select_funcs);
2152 if (!
RTEST(value))
return 0;
2153 LAZY_MEMO_SET_VALUE(result, value);
2154 LAZY_MEMO_RESET_PACKED(result);
2159 lazy_filter_map_proc, 0,
2173lazy_filter_map(
VALUE obj)
2179 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_filter_map_funcs);
2186 if (
RTEST(chain))
return 0;
2191 lazy_reject_proc, 0,
2202lazy_reject(
VALUE obj)
2208 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_reject_funcs);
2216 if (!
RTEST(chain))
return 0;
2224 VALUE value, chain =
rb_funcall(entry->memo, id_eqq, 1, result->memo_value);
2226 if (!
RTEST(chain))
return 0;
2228 LAZY_MEMO_SET_VALUE(result, value);
2229 LAZY_MEMO_RESET_PACKED(result);
2235 lazy_grep_iter_proc, 0,
2254 &lazy_grep_iter_funcs : &lazy_grep_funcs;
2255 return lazy_add_method(obj, 0, 0, pattern,
rb_ary_new3(1, pattern), funcs);
2263 if (
RTEST(chain))
return 0;
2271 VALUE value, chain =
rb_funcall(entry->memo, id_eqq, 1, result->memo_value);
2273 if (
RTEST(chain))
return 0;
2275 LAZY_MEMO_SET_VALUE(result, value);
2276 LAZY_MEMO_RESET_PACKED(result);
2282 lazy_grep_v_iter_proc, 0,
2286 lazy_grep_v_proc, 0,
2301 &lazy_grep_v_iter_funcs : &lazy_grep_v_funcs;
2302 return lazy_add_method(obj, 0, 0, pattern,
rb_ary_new3(1, pattern), funcs);
2321 VALUE ary, arrays = entry->memo;
2322 VALUE memo = rb_ary_entry(memos, memo_index);
2326 rb_ary_push(ary, result->memo_value);
2328 rb_ary_push(ary, rb_ary_entry(
RARRAY_AREF(arrays, i), count));
2330 LAZY_MEMO_SET_VALUE(result, ary);
2331 LAZY_MEMO_SET_PACKED(result);
2332 rb_ary_store(memos, memo_index,
LONG2NUM(++count));
2340 VALUE arg = rb_ary_entry(memos, memo_index);
2341 VALUE zip_args = entry->memo;
2350 rb_ary_store(memos, memo_index, arg);
2354 rb_ary_push(ary, result->memo_value);
2358 rb_ary_push(ary, v);
2360 LAZY_MEMO_SET_VALUE(result, ary);
2361 LAZY_MEMO_SET_PACKED(result);
2366 {lazy_zip_func, lazy_receiver_size,},
2367 {lazy_zip_arrays_func, lazy_receiver_size,},
2390 for (i = 0; i < argc; i++) {
2391 v = rb_check_array_type(argv[i]);
2393 for (; i < argc; i++) {
2400 funcs = &lazy_zip_funcs[0];
2403 rb_ary_push(ary, v);
2406 return lazy_add_method(obj, 0, 0, ary, ary, funcs);
2414 VALUE memo = rb_ary_entry(memos, memo_index);
2422 LAZY_MEMO_SET_BREAK(result);
2425 if (--remain == 0) LAZY_MEMO_SET_BREAK(result);
2426 rb_ary_store(memos, memo_index,
LONG2NUM(remain));
2441 lazy_take_proc, lazy_take_size,
2463 argv[0] = sym_cycle;
2468 return lazy_add_method(obj, argc, argv, n,
rb_ary_new3(1, n), &lazy_take_funcs);
2476 LAZY_MEMO_SET_BREAK(result);
2483 lazy_take_while_proc, 0,
2494lazy_take_while(
VALUE obj)
2500 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_take_while_funcs);
2507 if (
NIL_P(receiver))
2511 return LONG2FIX(len < 0 ? 0 : len);
2521 VALUE memo = rb_ary_entry(memos, memo_index);
2529 rb_ary_store(memos, memo_index,
LONG2NUM(remain));
2537 lazy_drop_proc, lazy_drop_size,
2559 return lazy_add_method(obj, 2, argv, n,
rb_ary_new3(1, n), &lazy_drop_funcs);
2566 VALUE memo = rb_ary_entry(memos, memo_index);
2574 if (
RTEST(drop))
return 0;
2575 rb_ary_store(memos, memo_index,
Qtrue);
2581 lazy_drop_while_proc, 0,
2592lazy_drop_while(
VALUE obj)
2598 return lazy_add_method(obj, 0, 0,
Qfalse,
Qnil, &lazy_drop_while_funcs);
2602lazy_uniq_check(
VALUE chain,
VALUE memos,
long memo_index)
2604 VALUE hash = rb_ary_entry(memos, memo_index);
2608 rb_ary_store(memos, memo_index, hash);
2611 return rb_hash_add_new_element(hash, chain,
Qfalse);
2617 if (lazy_uniq_check(result->memo_value, memos, memo_index))
return 0;
2626 if (lazy_uniq_check(chain, memos, memo_index))
return 0;
2631 lazy_uniq_iter_proc, 0,
2651 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, funcs);
2657 if (
NIL_P(result->memo_value))
return 0;
2662 lazy_compact_proc, 0,
2673lazy_compact(
VALUE obj)
2675 return lazy_add_method(obj, 0, 0,
Qnil,
Qnil, &lazy_compact_funcs);
2682 VALUE memo = rb_ary_entry(memos, memo_index);
2689 argv[0] = result->memo_value;
2693 LAZY_MEMO_RESET_PACKED(result);
2696 LAZY_MEMO_SET_VALUE(result, rb_ary_new_from_values(2, argv));
2697 LAZY_MEMO_SET_PACKED(result);
2704lazy_with_index_size(
VALUE proc,
VALUE receiver)
2710 lazy_with_index_proc, lazy_with_index_size,
2731lazy_with_index(
int argc,
VALUE *argv,
VALUE obj)
2739 return lazy_add_method(obj, 0, 0, memo, rb_ary_new_from_values(1, &memo), &lazy_with_index_funcs);
2864stop_result(
VALUE self)
2874producer_mark(
void *p)
2877 rb_gc_mark_movable(ptr->init);
2878 rb_gc_mark_movable(ptr->proc);
2882producer_compact(
void *p)
2885 ptr->init = rb_gc_location(ptr->init);
2886 ptr->proc = rb_gc_location(ptr->proc);
2889#define producer_free RUBY_TYPED_DEFAULT_FREE
2892producer_memsize(
const void *p)
2905 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2909producer_ptr(
VALUE obj)
2914 if (!ptr || ptr->proc ==
Qundef) {
2922producer_allocate(
VALUE klass)
2957NORETURN(
static VALUE producer_each_i(
VALUE obj));
2960producer_each_i(
VALUE obj)
2963 VALUE init, proc, curr;
2965 ptr = producer_ptr(obj);
2987producer_each(
VALUE obj)
3036enumerator_s_produce(
int argc,
VALUE *argv,
VALUE klass)
3062enum_chain_mark(
void *p)
3065 rb_gc_mark_movable(ptr->enums);
3069enum_chain_compact(
void *p)
3072 ptr->enums = rb_gc_location(ptr->enums);
3075#define enum_chain_free RUBY_TYPED_DEFAULT_FREE
3078enum_chain_memsize(
const void *p)
3091 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3095enum_chain_ptr(
VALUE obj)
3100 if (!ptr || ptr->enums ==
Qundef) {
3108enum_chain_allocate(
VALUE klass)
3148new_enum_chain(
VALUE enums)
3151 VALUE obj = enum_chain_initialize(enum_chain_allocate(rb_cEnumChain), enums);
3155 return enumerable_lazy(obj);
3169 ptr0 = enum_chain_ptr(orig);
3175 ptr1->enums = ptr0->enums;
3176 ptr1->pos = ptr0->pos;
3182enum_chain_total_size(
VALUE enums)
3213enum_chain_size(
VALUE obj)
3215 return enum_chain_total_size(enum_chain_ptr(obj)->enums);
3221 return enum_chain_size(obj);
3243enum_chain_each(
int argc,
VALUE *argv,
VALUE obj)
3251 objptr = enum_chain_ptr(obj);
3252 enums = objptr->enums;
3272enum_chain_rewind(
VALUE obj)
3274 struct enum_chain *objptr = enum_chain_ptr(obj);
3275 VALUE enums = objptr->enums;
3278 for (i = objptr->pos; 0 <= i && i <
RARRAY_LEN(enums); objptr->pos = --i) {
3286inspect_enum_chain(
VALUE obj,
VALUE dummy,
int recur)
3293 if (!ptr || ptr->enums ==
Qundef) {
3311enum_chain_inspect(
VALUE obj)
3329 VALUE enums = rb_ary_new_from_values(1, &obj);
3330 rb_ary_cat(enums, argv, argc);
3331 return new_enum_chain(enums);
3347 return new_enum_chain(rb_ary_new_from_args(2, obj, eobj));
3367 VALUE aseq = enumerator_init(enumerator_allocate(rb_cArithSeq),
3383arith_seq_begin(
VALUE self)
3394arith_seq_end(
VALUE self)
3406arith_seq_step(
VALUE self)
3417arith_seq_exclude_end(
VALUE self)
3423arith_seq_exclude_end_p(
VALUE self)
3425 return RTEST(arith_seq_exclude_end(self));
3432 component->
begin = arith_seq_begin(obj);
3433 component->
end = arith_seq_end(obj);
3434 component->
step = arith_seq_step(obj);
3435 component->
exclude_end = arith_seq_exclude_end_p(obj);
3447rb_arithmetic_sequence_beg_len_step(
VALUE obj,
long *begp,
long *lenp,
long *stepp,
long len,
int err)
3449 RBIMPL_NONNULL_ARG(begp);
3450 RBIMPL_NONNULL_ARG(lenp);
3451 RBIMPL_NONNULL_ARG(stepp);
3454 if (!rb_arithmetic_sequence_extract(obj, &aseq)) {
3467 if (err == 0 && (step < -1 || step > 1)) {
3477 return rb_range_component_beg_len(aseq.
begin, aseq.
end, aseq.
exclude_end, begp, lenp, len, err);
3494arith_seq_first(
int argc,
VALUE *argv,
VALUE self)
3502 b = arith_seq_begin(self);
3503 e = arith_seq_end(self);
3504 s = arith_seq_step(self);
3529 return rb_ary_new_capa(0);
3532 x = arith_seq_exclude_end_p(self);
3536 ary = rb_ary_new_capa(n);
3545 rb_ary_push(ary, b);
3546 b = rb_big_plus(b, s);
3562 if (len < 0) len = 0;
3563 ary = rb_ary_new_capa((n < len) ? n : len);
3564 while (n > 0 && i < end) {
3566 if (i + unit < i)
break;
3575 if (len < 0) len = 0;
3576 ary = rb_ary_new_capa((n < len) ? n : len);
3577 while (n > 0 && i > end) {
3579 if (i + unit > i)
break;
3591 double end =
NIL_P(e) ? (unit < 0 ? -1 : 1)*HUGE_VAL :
NUM2DBL(e);
3592 double len = ruby_float_step_size(beg, end, unit, x);
3600 ary = rb_ary_new_capa(1);
3601 rb_ary_push(ary,
DBL2NUM(beg));
3604 ary = rb_ary_new_capa(0);
3607 else if (unit == 0) {
3609 ary = rb_ary_new_capa(n);
3610 for (i = 0; i < len; ++i) {
3611 rb_ary_push(ary, val);
3615 ary = rb_ary_new_capa(n);
3616 for (i = 0; i < n; ++i) {
3617 double d = i*unit+beg;
3618 if (unit >= 0 ? end < d : d < end) d = end;
3633 return rb_int_plus(a, b);
3636 return rb_float_plus(a, b);
3639 return rb_rational_plus(a, b);
3642 return rb_funcallv(a,
'+', 1, &b);
3650 return rb_int_minus(a, b);
3653 return rb_float_minus(a, b);
3656 return rb_rational_minus(a, b);
3659 return rb_funcallv(a,
'-', 1, &b);
3667 return rb_int_mul(a, b);
3670 return rb_float_mul(a, b);
3673 return rb_rational_mul(a, b);
3676 return rb_funcallv(a,
'*', 1, &b);
3685 q = rb_int_idiv(a, b);
3688 q = rb_float_div(a, b);
3691 q = rb_rational_div(a, b);
3694 q = rb_funcallv(a, idDiv, 1, &b);
3701 return rb_float_floor(q, 0);
3704 return rb_rational_floor(q, 0);
3720arith_seq_last(
int argc,
VALUE *argv,
VALUE self)
3722 VALUE b, e, s, len_1, len, last, nv, ary;
3723 int last_is_adjusted;
3726 e = arith_seq_end(self);
3729 "cannot get the last element of endless arithmetic sequence");
3732 b = arith_seq_begin(self);
3733 s = arith_seq_step(self);
3735 len_1 = num_idiv(num_minus(e, b), s);
3736 if (rb_num_negative_int_p(len_1)) {
3740 return rb_ary_new_capa(0);
3743 last = num_plus(b, num_mul(s, len_1));
3744 if ((last_is_adjusted = arith_seq_exclude_end_p(self) &&
rb_equal(last, e))) {
3745 last = num_minus(last, s);
3752 if (last_is_adjusted) {
3756 len = rb_int_plus(len_1,
INT2FIX(1));
3763 if (
RTEST(rb_int_gt(nv, len))) {
3771 ary = rb_ary_new_capa(n);
3772 b = rb_int_minus(last, rb_int_mul(s, nv));
3774 b = rb_int_plus(b, s);
3775 rb_ary_push(ary, b);
3789arith_seq_inspect(
VALUE self)
3792 VALUE eobj, str, eargs;
3803 str =
rb_sprintf(
"(%s%"PRIsVALUE
"%s.", range_p ?
"(" :
"", eobj, range_p ?
")" :
"");
3823 if (all_key) kwds = argv[--argc];
3827 VALUE arg = *argv++;
3859 if (!
rb_equal(arith_seq_begin(self), arith_seq_begin(other))) {
3863 if (!
rb_equal(arith_seq_end(self), arith_seq_end(other))) {
3867 if (!
rb_equal(arith_seq_step(self), arith_seq_step(other))) {
3871 if (arith_seq_exclude_end_p(self) != arith_seq_exclude_end_p(other)) {
3889arith_seq_hash(
VALUE self)
3895 v = rb_hash(arith_seq_begin(self));
3897 v = rb_hash(arith_seq_end(self));
3899 v = rb_hash(arith_seq_step(self));
3906#define NUM_GE(x, y) RTEST(rb_num_coerce_relop((x), (y), idGE))
3921arith_seq_each(
VALUE self)
3923 VALUE c, e, s, len_1, last;
3928 c = arith_seq_begin(self);
3929 e = arith_seq_end(self);
3930 s = arith_seq_step(self);
3931 x = arith_seq_exclude_end_p(self);
3940 c = rb_int_plus(c, s);
3954 len_1 = num_idiv(num_minus(e, c), s);
3955 last = num_plus(c, num_mul(s, len_1));
3957 last = num_minus(last, s);
3960 if (rb_num_negative_int_p(s)) {
3961 while (NUM_GE(c, last)) {
3967 while (NUM_GE(last, c)) {
3984arith_seq_size(
VALUE self)
3986 VALUE b, e, s, len_1, len, last;
3989 b = arith_seq_begin(self);
3990 e = arith_seq_end(self);
3991 s = arith_seq_step(self);
3992 x = arith_seq_exclude_end_p(self);
3998 if (rb_num_negative_int_p(s)) {
4010 if (isinf(n))
return DBL2NUM(n);
4012 return rb_dbl2big(n);
4027 len_1 = rb_int_idiv(rb_int_minus(e, b), s);
4028 if (rb_num_negative_int_p(len_1)) {
4032 last = rb_int_plus(b, rb_int_mul(s, len_1));
4037 len = rb_int_plus(len_1,
INT2FIX(1));
4043#define sym(name) ID2SYM(rb_intern_const(name))
4045InitVM_Enumerator(
void)
4081 rb_define_alias(rb_cLazy,
"_enumerable_collect_concat",
"collect_concat");
4097 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_map"));
4098 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_collect"));
4099 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_flat_map"));
4100 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_collect_concat"));
4101 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_select"));
4102 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_find_all"));
4103 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_filter"));
4104 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_filter_map"));
4105 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_reject"));
4106 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_grep"));
4107 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_grep_v"));
4108 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_zip"));
4109 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_take"));
4110 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_take_while"));
4111 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_drop"));
4112 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_drop_while"));
4113 rb_funcall(rb_cLazy, id_private, 1, sym(
"_enumerable_uniq"));
4145 lazy_use_super_method = rb_hash_new_with_size(18);
4146 rb_hash_aset(lazy_use_super_method, sym(
"map"), sym(
"_enumerable_map"));
4147 rb_hash_aset(lazy_use_super_method, sym(
"collect"), sym(
"_enumerable_collect"));
4148 rb_hash_aset(lazy_use_super_method, sym(
"flat_map"), sym(
"_enumerable_flat_map"));
4149 rb_hash_aset(lazy_use_super_method, sym(
"collect_concat"), sym(
"_enumerable_collect_concat"));
4150 rb_hash_aset(lazy_use_super_method, sym(
"select"), sym(
"_enumerable_select"));
4151 rb_hash_aset(lazy_use_super_method, sym(
"find_all"), sym(
"_enumerable_find_all"));
4152 rb_hash_aset(lazy_use_super_method, sym(
"filter"), sym(
"_enumerable_filter"));
4153 rb_hash_aset(lazy_use_super_method, sym(
"filter_map"), sym(
"_enumerable_filter_map"));
4154 rb_hash_aset(lazy_use_super_method, sym(
"reject"), sym(
"_enumerable_reject"));
4155 rb_hash_aset(lazy_use_super_method, sym(
"grep"), sym(
"_enumerable_grep"));
4156 rb_hash_aset(lazy_use_super_method, sym(
"grep_v"), sym(
"_enumerable_grep_v"));
4157 rb_hash_aset(lazy_use_super_method, sym(
"zip"), sym(
"_enumerable_zip"));
4158 rb_hash_aset(lazy_use_super_method, sym(
"take"), sym(
"_enumerable_take"));
4159 rb_hash_aset(lazy_use_super_method, sym(
"take_while"), sym(
"_enumerable_take_while"));
4160 rb_hash_aset(lazy_use_super_method, sym(
"drop"), sym(
"_enumerable_drop"));
4161 rb_hash_aset(lazy_use_super_method, sym(
"drop_while"), sym(
"_enumerable_drop_while"));
4162 rb_hash_aset(lazy_use_super_method, sym(
"uniq"), sym(
"_enumerable_uniq"));
4163 rb_hash_aset(lazy_use_super_method, sym(
"with_index"), sym(
"_enumerable_with_index"));
4165 rb_gc_register_mark_object(lazy_use_super_method);
4185 rb_define_method(rb_cGenerator,
"initialize_copy", generator_init_copy, 1);
4206 rb_define_method(rb_cEnumChain,
"initialize_copy", enum_chain_init_copy, 1);
4240Init_Enumerator(
void)
4256 sym_each =
ID2SYM(id_each);
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_need_block(void)
Declares that the current method needs a block.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
int rb_keyword_given_p(void)
Determines if the current method is given a keyword argument.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define ID2SYM
Old name of RB_ID2SYM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define CLASS_OF
Old name of rb_class_of.
#define rb_ary_new4
Old name of rb_ary_new_from_values.
#define FIXABLE
Old name of RB_FIXABLE.
#define rb_exc_new2
Old name of rb_exc_new_cstr.
#define LONG2FIX
Old name of RB_INT2FIX.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_HASH
Old name of RUBY_T_HASH.
#define NUM2DBL
Old name of rb_num2dbl.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define POSFIXABLE
Old name of RB_POSFIXABLE.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_iter_break(void)
Breaks from a block.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
VALUE rb_eStopIteration
StopIteration exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports always regardless of runtime -W flag.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_eIndexError
IndexError exception.
VALUE rb_mKernel
Kernel module.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_cEnumerator
Enumerator class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_cNumeric
Numeric class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_cRange
Range class.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcall_with_block(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE procval)
Identical to rb_funcallv_public(), except you can pass a block.
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
VALUE rb_enumerator_size_func(VALUE recv, VALUE argv, VALUE eobj)
This is the type of functions that rb_enumeratorize_with_size() expects.
#define rb_check_frozen
Just another name of rb_check_frozen.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
VALUE rb_num_coerce_cmp(VALUE lhs, VALUE rhs, ID op)
Identical to rb_num_coerce_bin(), except for return values.
VALUE rb_obj_method(VALUE recv, VALUE mid)
Creates a method object.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_proc_call_with_block(VALUE recv, int argc, const VALUE *argv, VALUE proc)
Identical to rb_proc_call(), except you can additionally pass another proc object,...
VALUE rb_proc_call_kw(VALUE recv, VALUE args, int kw_splat)
Identical to rb_proc_call(), except you can specify how to handle the last element of the given array...
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
#define rb_hash_end(h)
Just another name of st_hash_end.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
VALUE rb_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
VALUE rb_check_funcall_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_check_funcall(), except you can specify how to handle the last element of the given a...
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
ID rb_to_id(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
VALUE rb_yield_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
VALUE rb_yield(VALUE val)
Yields the block.
VALUE rb_yield_values_kw(int n, const VALUE *argv, int kw_splat)
Identical to rb_yield_values2(), except you can specify how to handle the last element of the given a...
VALUE rb_block_call_func(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
This is the type of a function that the interpreter expect for C-backended blocks.
VALUE rb_block_call_kw(VALUE obj, ID mid, int argc, const VALUE *argv, rb_block_call_func_t proc, VALUE data2, int kw_splat)
Identical to rb_funcallv_kw(), except it additionally passes a function as a block.
#define rb_long2int
Just another name of rb_long2int_inline.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
VALUE rb_proc_new(type *q, VALUE w)
Creates a rb_cProc instance.
VALUE rb_fiber_new(type *q, VALUE w)
Creates a rb_cFiber instance.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
VALUE rb_rescue2(type *q, VALUE w, type *e, VALUE r,...)
An equivalent of rescue clause.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define DATA_PTR(obj)
Convenient getter macro.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define InitVM(ext)
This macro is for internal use.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
#define RB_NO_KEYWORDS
Do not pass keywords.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Decomposed Enumerator::ArithmeicSequence.
int exclude_end
Whether the endpoint is open or closed.
VALUE end
"Right" or "highest" endpoint of the sequence.
VALUE step
Step between a sequence.
VALUE begin
"Left" or "lowest" endpoint of the sequence.
This is the struct that holds necessary info for a struct.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.