14#include "internal/compar.h"
15#include "internal/enum.h"
16#include "internal/hash.h"
17#include "internal/imemo.h"
18#include "internal/numeric.h"
19#include "internal/object.h"
20#include "internal/proc.h"
21#include "internal/rational.h"
22#include "internal/re.h"
24#include "ruby_assert.h"
31static ID id__separator;
32static ID id_chunk_categorize;
33static ID id_chunk_enumerable;
34static ID id_sliceafter_enum;
35static ID id_sliceafter_pat;
36static ID id_sliceafter_pred;
37static ID id_slicebefore_enumerable;
38static ID id_slicebefore_sep_pat;
39static ID id_slicebefore_sep_pred;
40static ID id_slicewhen_enum;
41static ID id_slicewhen_inverted;
42static ID id_slicewhen_pred;
48#define id_lshift idLTLT
53rb_enum_values_pack(
int argc,
const VALUE *argv)
55 if (argc == 0)
return Qnil;
56 if (argc == 1)
return argv[0];
60#define ENUM_WANT_SVALUE() do { \
61 i = rb_enum_values_pack(argc, argv); \
65enum_yield(
int argc,
VALUE ary)
68 return rb_yield_force_blockarg(ary);
75enum_yield_array(
VALUE ary)
80 return rb_yield_force_blockarg(ary);
89 struct MEMO *memo = MEMO_CAST(args);
92 if (
RTEST(rb_funcallv(memo->v1, id_eqq, 1, &i)) ==
RTEST(memo->u3.value)) {
93 rb_ary_push(memo->v2, i);
101 struct MEMO *memo = MEMO_CAST(args);
102 VALUE converted_element, match;
107 match =
NIL_P(converted_element) ?
Qfalse : rb_reg_match_p(memo->v1, i, 0);
108 if (match == memo->u3.value) {
109 rb_ary_push(memo->v2, i);
117 struct MEMO *memo = MEMO_CAST(args);
120 if (
RTEST(rb_funcallv(memo->v1, id_eqq, 1, &i)) ==
RTEST(memo->u3.value)) {
121 rb_ary_push(memo->v2, enum_yield(argc, i));
129 VALUE ary = rb_ary_new();
130 struct MEMO *memo = MEMO_NEW(pat, ary, test);
135 else if (RB_TYPE_P(pat,
T_REGEXP) &&
136 LIKELY(rb_method_basic_definition_p(
CLASS_OF(pat), idEqq))) {
175 return enum_grep0(obj, pat,
Qtrue);
207 return enum_grep0(obj, pat,
Qfalse);
210#define COUNT_BIGNUM IMEMO_FL_USER0
211#define MEMO_V3_SET(m, v) RB_OBJ_WRITE((m), &(m)->u3.value, (v))
214imemo_count_up(
struct MEMO *memo)
216 if (memo->flags & COUNT_BIGNUM) {
217 MEMO_V3_SET(memo, rb_int_succ(memo->u3.value));
219 else if (++memo->u3.cnt == 0) {
221 unsigned long buf[2] = {0, 1};
222 MEMO_V3_SET(memo, rb_big_unpack(buf, 2));
223 memo->flags |= COUNT_BIGNUM;
228imemo_count_value(
struct MEMO *memo)
230 if (memo->flags & COUNT_BIGNUM) {
231 return memo->u3.value;
241 struct MEMO *memo = MEMO_CAST(memop);
246 imemo_count_up(memo);
254 struct MEMO *memo = MEMO_CAST(memop);
257 imemo_count_up(memo);
265 struct MEMO *memo = MEMO_CAST(memop);
267 imemo_count_up(memo);
315 rb_warn(
"given block not used");
320 memo = MEMO_NEW(item, 0, 0);
322 return imemo_count_value(memo);
330 if (
RTEST(enum_yield(argc, i))) {
331 struct MEMO *memo = MEMO_CAST(memop);
332 MEMO_V1_SET(memo, i);
368 memo = MEMO_NEW(
Qundef, 0, 0);
373 if (!
NIL_P(if_none)) {
374 return rb_funcallv(if_none, id_call, 0, 0);
382 struct MEMO *memo = MEMO_CAST(memop);
387 MEMO_V1_SET(memo, imemo_count_value(memo));
390 imemo_count_up(memo);
397 struct MEMO *memo = MEMO_CAST(memop);
400 MEMO_V1_SET(memo, imemo_count_value(memo));
403 imemo_count_up(memo);
432enum_find_index(
int argc,
VALUE *argv,
VALUE obj)
440 func = find_index_iter_i;
445 rb_warn(
"given block not used");
450 memo = MEMO_NEW(
Qnil, condition_value, 0);
460 if (
RTEST(enum_yield(argc, i))) {
469 return rb_check_funcall_default(self, id_size, 0, 0,
Qnil);
473limit_by_enum_size(
VALUE obj,
long n)
479 return ((
unsigned long)n > limit) ? (long)limit : n;
483enum_size_over_p(
VALUE obj,
long n)
487 return ((
unsigned long)n >
FIX2ULONG(size));
509enum_find_all(
VALUE obj)
550enum_filter_map(
VALUE obj)
568 if (!
RTEST(enum_yield(argc, i))) {
593enum_reject(
VALUE obj)
616 rb_ary_push(ary, rb_enum_values_pack(argc, argv));
638enum_collect(
VALUE obj)
641 int min_argc, max_argc;
646 min_argc = rb_block_min_max_arity(&max_argc);
647 rb_lambda_call(obj, id_each, 0, 0, collect_i, min_argc, max_argc, ary);
658 tmp = rb_check_array_type(i);
664 rb_ary_concat(ary, tmp);
689enum_flat_map(
VALUE obj)
713 VALUE ary = rb_ary_new();
730 return enum_hashify_into(obj, argc, argv, iter, rb_hash_new());
737 return rb_hash_set_pair(hash, i);
771 return enum_hashify(obj, argc, argv, iter);
777 struct MEMO *memo = MEMO_CAST(p);
781 if (UNDEF_P(memo->v1)) {
782 MEMO_V1_SET(memo, i);
793 struct MEMO *memo = MEMO_CAST(p);
798 if (UNDEF_P(memo->v1)) {
799 MEMO_V1_SET(memo, i);
801 else if (
SYMBOL_P(name = memo->u3.value)) {
809 MEMO_V1_SET(memo, rb_f_send(numberof(args), args, memo->v1));
822 return UNDEF_P(init) ?
Qnil : init;
838 rb_method_basic_definition_p(
rb_cInteger, idPLUS) &&
850 else if (RB_BIGNUM_TYPE_P(e))
851 v = rb_big_plus(e, v);
1029 op =
id ?
ID2SYM(
id) : init;
1043 if (iter == inject_op_i &&
1046 rb_method_basic_definition_p(
CLASS_OF(obj), id_each)) {
1047 return ary_inject_op(obj, init, op);
1050 memo = MEMO_NEW(init,
Qnil, op);
1052 if (UNDEF_P(memo->v1))
return Qnil;
1059 struct MEMO *memo = MEMO_CAST(arys);
1063 if (
RTEST(enum_yield(argc, i))) {
1069 rb_ary_push(ary, i);
1102enum_partition(
VALUE obj)
1108 memo = MEMO_NEW(rb_ary_new(), rb_ary_new(), 0);
1111 return rb_assoc_new(memo->v1, memo->v2);
1122 group = enum_yield(argc, i);
1123 values = rb_hash_aref(hash, group);
1124 if (!RB_TYPE_P(values,
T_ARRAY)) {
1126 rb_hash_aset(hash, group, values);
1129 rb_ary_push(values, i);
1157enum_group_by(
VALUE obj)
1161 return enum_hashify(obj, 0, 0, group_by_i);
1165tally_up(st_data_t *group, st_data_t *value, st_data_t arg,
int existing)
1173 tally +=
INT2FIX(1) & ~FIXNUM_FLAG;
1177 tally = rb_big_plus(tally,
INT2FIX(1));
1180 *value = (st_data_t)tally;
1188 rb_hash_stlike_update(hash, group, tally_up, (st_data_t)hash);
1196 rb_enum_tally_up(hash, i);
1233 hash = rb_to_hash_type(argv[0]);
1237 hash = rb_hash_new();
1240 return enum_hashify_into(obj, 0, 0, tally_i, hash);
1247 struct MEMO *memo = MEMO_CAST(params);
1250 MEMO_V1_SET(memo, i);
1289 return enum_take(obj, argv[0]);
1292 memo = MEMO_NEW(
Qnil, 0, 0);
1333 return rb_ary_sort_bang(enum_to_a(0, 0, obj));
1336#define SORT_BY_BUFSIZE 16
1337#define SORT_BY_UNIFORMED(num, flo, fix) (((num&1)<<2)|((flo&1)<<1)|fix)
1342 uint8_t primitive_uniformed;
1349 VALUE ary = data->ary;
1354 v = enum_yield(argc, i);
1356 if (
RBASIC(ary)->klass) {
1359 if (
RARRAY_LEN(data->buf) != SORT_BY_BUFSIZE*2) {
1363 if (data->primitive_uniformed) {
1371 if (data->n == SORT_BY_BUFSIZE) {
1372 rb_ary_concat(ary, data->buf);
1379sort_by_cmp(
const void *ap,
const void *bp,
void *data)
1385 if (
RBASIC(ary)->klass) {
1392 return OPTIMIZED_CMP(a, b);
1400#define uless rb_uniform_is_less
1401#define UNIFORM_SWAP(a,b)\
1402 do{struct rb_uniform_sort_data tmp = a; a = b; b = tmp;} while(0)
1418 return rb_float_cmp(b, a) > 0;
1422 return rb_float_cmp(a, b) < 0;
1435 return rb_float_cmp(b, a) < 0;
1439 return rb_float_cmp(a, b) > 0;
1443#define med3_val(a,b,c) (uless(a,b)?(uless(b,c)?b:uless(c,a)?a:c):(uless(c,b)?b:uless(a,c)?a:c))
1449 if ((ptr_end - ptr_begin) < 2)
return;
1451 *index = ptr_begin+1;
1452 for (; index < ptr_end; index++) {
1455 if (uless(tmp.v, ptr_begin->v)) {
1456 while (ptr_begin < j) {
1462 while (uless(tmp.v, (--k)->v)) {
1473 size_t offset,
size_t len)
1477 while ((c = (offset<<1)+1) <=
len) {
1478 if (c <
len && uless(ptr_begin[c].v, ptr_begin[c+1].v)) {
1481 if (!uless(tmp.v, ptr_begin[c].v))
break;
1482 ptr_begin[offset] = ptr_begin[c];
1485 ptr_begin[offset] = tmp;
1492 size_t n = ptr_end - ptr_begin;
1495 for (
size_t offset = n>>1; offset > 0;) {
1496 rb_uniform_heap_down_2(ptr_begin, --offset, n-1);
1498 for (
size_t offset = n-1; offset > 0;) {
1499 UNIFORM_SWAP(*ptr_begin, ptr_begin[offset]);
1500 rb_uniform_heap_down_2(ptr_begin, 0, --offset);
1510 if (ptr_end - ptr_begin <= 16) {
1511 rb_uniform_insertionsort_2(ptr_begin, ptr_end);
1515 rb_uniform_heapsort_2(ptr_begin, ptr_end);
1519 VALUE x = med3_val(ptr_begin->v,
1520 ptr_begin[(ptr_end - ptr_begin)>>1].v,
1526 while (uless(i->v, x)) i++;
1527 while (uless(x, j->v)) j--;
1529 UNIFORM_SWAP(*i, *j);
1535 if (ptr_end - j > 1) rb_uniform_quicksort_intro_2(j, ptr_end, d-1);
1536 if (i - ptr_begin > 1) rb_uniform_quicksort_intro_2(ptr_begin, i, d-1);
1548 size_t n = ptr_end - ptr_begin;
1549 size_t d = CHAR_BIT *
sizeof(n) - nlz_intptr(n) - 1;
1550 bool sorted_flag =
true;
1553 if (rb_uniform_is_larger((ptr-1)->v, (ptr)->v)) {
1554 sorted_flag =
false;
1562 rb_uniform_quicksort_intro_2(ptr_begin, ptr_end, d<<1);
1651enum_sort_by(
VALUE obj)
1666 RBASIC_CLEAR_CLASS(ary);
1667 buf = rb_ary_hidden_new(SORT_BY_BUFSIZE*2);
1668 rb_ary_store(buf, SORT_BY_BUFSIZE*2-1,
Qnil);
1669 memo = MEMO_NEW(0, 0, 0);
1674 data->primitive_uniformed = SORT_BY_UNIFORMED((CMP_OPTIMIZABLE(FLOAT) && CMP_OPTIMIZABLE(INTEGER)),
1675 CMP_OPTIMIZABLE(FLOAT),
1676 CMP_OPTIMIZABLE(INTEGER));
1681 rb_ary_resize(buf, data->n*2);
1682 rb_ary_concat(ary, buf);
1685 if (data->primitive_uniformed) {
1693 sort_by_cmp, (
void *)ary));
1696 if (
RBASIC(ary)->klass) {
1708#define ENUMFUNC(name) argc ? name##_eqq : rb_block_given_p() ? name##_iter_i : name##_i
1710#define MEMO_ENUM_NEW(v1) (rb_check_arity(argc, 0, 1), MEMO_NEW((v1), (argc ? *argv : 0), 0))
1712#define DEFINE_ENUMFUNCS(name) \
1713static VALUE enum_##name##_func(VALUE result, struct MEMO *memo); \
1716name##_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1718 return enum_##name##_func(rb_enum_values_pack(argc, argv), MEMO_CAST(memo)); \
1722name##_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1724 return enum_##name##_func(rb_yield_values2(argc, argv), MEMO_CAST(memo)); \
1728name##_eqq(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1730 ENUM_WANT_SVALUE(); \
1731 return enum_##name##_func(rb_funcallv(MEMO_CAST(memo)->v2, id_eqq, 1, &i), MEMO_CAST(memo)); \
1735enum_##name##_func(VALUE result, struct MEMO *memo)
1737#define WARN_UNUSED_BLOCK(argc) do { \
1738 if ((argc) > 0 && rb_block_given_p()) { \
1739 rb_warn("given block not used"); \
1743DEFINE_ENUMFUNCS(all)
1745 if (!
RTEST(result)) {
1746 MEMO_V1_SET(memo,
Qfalse);
1801 struct MEMO *memo = MEMO_ENUM_NEW(
Qtrue);
1802 WARN_UNUSED_BLOCK(argc);
1807DEFINE_ENUMFUNCS(any)
1809 if (
RTEST(result)) {
1810 MEMO_V1_SET(memo,
Qtrue);
1864 WARN_UNUSED_BLOCK(argc);
1869DEFINE_ENUMFUNCS(one)
1871 if (
RTEST(result)) {
1872 if (UNDEF_P(memo->v1)) {
1873 MEMO_V1_SET(memo,
Qtrue);
1875 else if (memo->v1 ==
Qtrue) {
1876 MEMO_V1_SET(memo,
Qfalse);
1889 int (*cmpfunc)(
const void *,
const void *,
void *);
1897 if (
RBASIC(data->buf)->klass) {
1899 data->rev ?
"max" :
"min",
1900 data->by ?
"_by" :
"");
1906nmin_cmp(
const void *ap,
const void *bp,
void *_data)
1910#define rb_cmpint(cmp, a, b) rb_cmpint(cmpint_reenter_check(data, (cmp)), a, b)
1911 return OPTIMIZED_CMP(a, b);
1916nmin_block_cmp(
const void *ap,
const void *bp,
void *_data)
1921 cmpint_reenter_check(data, cmp);
1922 return rb_cmpint(cmp, a, b);
1938 if (data->curlen <= data->n)
1943 eltsize = data->by ? 2 : 1;
1944 numelts = data->curlen;
1949#define GETPTR(i) (beg+(i)*eltsize)
1951#define SWAP(i, j) do { \
1953 memcpy(tmp, GETPTR(i), sizeof(VALUE)*eltsize); \
1954 memcpy(GETPTR(i), GETPTR(j), sizeof(VALUE)*eltsize); \
1955 memcpy(GETPTR(j), tmp, sizeof(VALUE)*eltsize); \
1959 long pivot_index = left + (right-left)/2;
1960 long num_pivots = 1;
1962 SWAP(pivot_index, right);
1963 pivot_index = right;
1967 while (i <= right-num_pivots) {
1968 int c = data->cmpfunc(GETPTR(i), GETPTR(pivot_index), data);
1972 SWAP(i, right-num_pivots);
1977 SWAP(i, store_index);
1983 for (i = right; right-num_pivots < i; i--) {
1990 if (store_index <= n && n <= store_index+num_pivots)
1993 if (n < store_index) {
1994 right = store_index-1;
1997 left = store_index+num_pivots;
2003 data->limit =
RARRAY_AREF(data->buf, store_index*eltsize);
2004 data->curlen = data->n;
2005 rb_ary_resize(data->buf, data->n * eltsize);
2017 cmpv = enum_yield(argc, i);
2021 if (!UNDEF_P(data->limit)) {
2022 int c = data->cmpfunc(&cmpv, &data->limit, data);
2030 rb_ary_push(data->buf, cmpv);
2031 rb_ary_push(data->buf, i);
2035 if (data->curlen == data->bufmax) {
2043rb_nmin_run(
VALUE obj,
VALUE num,
int by,
int rev,
int ary)
2050 rb_raise(rb_eArgError,
"negative size (%ld)", data.n);
2053 if (LONG_MAX/4/(by ? 2 : 1) < data.n)
2054 rb_raise(rb_eArgError,
"too big size");
2055 data.bufmax = data.n * 4;
2057 data.buf = rb_ary_hidden_new(data.bufmax * (by ? 2 : 1));
2059 data.cmpfunc = by ? nmin_cmp :
2083 data.cmpfunc, (
void *)&data);
2093 data.cmpfunc, (
void *)&data);
2097 rb_ary_reverse(result);
2153 WARN_UNUSED_BLOCK(argc);
2156 if (UNDEF_P(result))
return Qfalse;
2160DEFINE_ENUMFUNCS(none)
2162 if (
RTEST(result)) {
2163 MEMO_V1_SET(memo,
Qfalse);
2212 struct MEMO *memo = MEMO_ENUM_NEW(
Qtrue);
2214 WARN_UNUSED_BLOCK(argc);
2226 struct min_t *memo = MEMO_FOR(
struct min_t, args);
2230 if (UNDEF_P(memo->min)) {
2234 if (OPTIMIZED_CMP(i, memo->min) < 0) {
2245 struct min_t *memo = MEMO_FOR(
struct min_t, args);
2249 if (UNDEF_P(memo->min)) {
2254 if (rb_cmpint(cmp, i, memo->min) < 0) {
2323 struct min_t *m = NEW_MEMO_FOR(
struct min_t, memo);
2328 return rb_nmin_run(obj, num, 0, 0, 0);
2338 if (UNDEF_P(result))
return Qnil;
2349 struct max_t *memo = MEMO_FOR(
struct max_t, args);
2353 if (UNDEF_P(memo->max)) {
2357 if (OPTIMIZED_CMP(i, memo->max) > 0) {
2367 struct max_t *memo = MEMO_FOR(
struct max_t, args);
2372 if (UNDEF_P(memo->max)) {
2377 if (rb_cmpint(cmp, i, memo->max) > 0) {
2445 struct max_t *m = NEW_MEMO_FOR(
struct max_t, memo);
2450 return rb_nmin_run(obj, num, 0, 1, 0);
2460 if (UNDEF_P(result))
return Qnil;
2475 if (UNDEF_P(memo->min)) {
2480 n = OPTIMIZED_CMP(i, memo->min);
2484 n = OPTIMIZED_CMP(j, memo->max);
2500 if (UNDEF_P(memo->last)) {
2507 n = OPTIMIZED_CMP(j, i);
2517 minmax_i_update(i, j, memo);
2527 if (UNDEF_P(memo->min)) {
2552 if (UNDEF_P(memo->last)) {
2569 minmax_ii_update(i, j, memo);
2606enum_minmax(
VALUE obj)
2615 if (!UNDEF_P(m->last))
2616 minmax_ii_update(m->last, m->last, m);
2620 if (!UNDEF_P(m->last))
2621 minmax_i_update(m->last, m->last, m);
2623 if (!UNDEF_P(m->min)) {
2624 return rb_assoc_new(m->min, m->max);
2632 struct MEMO *memo = MEMO_CAST(args);
2637 v = enum_yield(argc, i);
2638 if (UNDEF_P(memo->v1)) {
2639 MEMO_V1_SET(memo, v);
2640 MEMO_V2_SET(memo, i);
2642 else if (OPTIMIZED_CMP(v, memo->v1) < 0) {
2643 MEMO_V1_SET(memo, v);
2644 MEMO_V2_SET(memo, i);
2695 if (argc && !
NIL_P(num = argv[0]))
2696 return rb_nmin_run(obj, num, 1, 0, 0);
2706 struct MEMO *memo = MEMO_CAST(args);
2711 v = enum_yield(argc, i);
2712 if (UNDEF_P(memo->v1)) {
2713 MEMO_V1_SET(memo, v);
2714 MEMO_V2_SET(memo, i);
2716 else if (OPTIMIZED_CMP(v, memo->v1) > 0) {
2717 MEMO_V1_SET(memo, v);
2718 MEMO_V2_SET(memo, i);
2769 if (argc && !
NIL_P(num = argv[0]))
2770 return rb_nmin_run(obj, num, 1, 1, 0);
2789 if (UNDEF_P(memo->min_bv)) {
2796 if (OPTIMIZED_CMP(v1, memo->min_bv) < 0) {
2800 if (OPTIMIZED_CMP(v2, memo->max_bv) > 0) {
2816 vi = enum_yield(argc, i);
2818 if (UNDEF_P(memo->last_bv)) {
2827 n = OPTIMIZED_CMP(vj, vi);
2842 minmax_by_i_update(vi, vj, i, j, memo);
2871enum_minmax_by(
VALUE obj)
2885 if (!UNDEF_P(m->last_bv))
2886 minmax_by_i_update(m->last_bv, m->last_bv, m->last, m->last, m);
2888 return rb_assoc_new(m->min, m->max);
2894 struct MEMO *memo = MEMO_CAST(args);
2896 if (
rb_equal(rb_enum_values_pack(argc, argv), memo->v1)) {
2897 MEMO_V2_SET(memo,
Qtrue);
2923 struct MEMO *memo = MEMO_NEW(val,
Qfalse, 0);
2932 struct MEMO *m = MEMO_CAST(memo);
2933 VALUE n = imemo_count_value(m);
2967enum_each_with_index(
int argc,
VALUE *argv,
VALUE obj)
2973 memo = MEMO_NEW(0, 0, 0);
3006enum_reverse_each(
int argc,
VALUE *argv,
VALUE obj)
3013 ary = enum_to_a(argc, argv, obj);
3033 enum_yield(argc, i);
3076enum_each_entry(
int argc,
VALUE *argv,
VALUE obj)
3084add_int(
VALUE x,
long n)
3088 return rb_funcallv(x,
'+', 1, &y);
3092div_int(
VALUE x,
long n)
3096 return rb_funcallv(x, id_div, 1, &y);
3099#define dont_recycle_block_arg(arity) ((arity) == 1 || (arity) < 0)
3104 struct MEMO *memo = MEMO_CAST(m);
3105 VALUE ary = memo->v1;
3107 long size = memo->u3.cnt;
3110 rb_ary_push(ary, i);
3133 if (slice_size <= 0) rb_raise(rb_eArgError,
"invalid slice size");
3135 size = enum_size(obj, 0, 0);
3141 n = add_int(size, slice_size-1);
3142 return div_int(n, slice_size);
3173 if (size <= 0) rb_raise(rb_eArgError,
"invalid slice size");
3175 size = limit_by_enum_size(obj, size);
3177 arity = rb_block_arity();
3178 memo = MEMO_NEW(ary, dont_recycle_block_arg(arity), size);
3189 struct MEMO *memo = MEMO_CAST(args);
3190 VALUE ary = memo->v1;
3192 long size = memo->u3.cnt;
3198 rb_ary_push(ary, i);
3201 ary = rb_ary_dup(ary);
3214 if (cons_size <= 0) rb_raise(rb_eArgError,
"invalid size");
3216 size = enum_size(obj, 0, 0);
3219 n = add_int(size, 1 - cons_size);
3220 return (OPTIMIZED_CMP(n, zero) == -1) ? zero : n;
3250 if (size <= 0) rb_raise(rb_eArgError,
"invalid size");
3252 arity = rb_block_arity();
3253 if (enum_size_over_p(obj, size))
return obj;
3254 memo = MEMO_NEW(
rb_ary_new2(size), dont_recycle_block_arg(arity), size);
3289 rb_block_call(obj, id_each, 0, 0, each_with_object_i, memo);
3297 struct MEMO *memo = (
struct MEMO *)memoval;
3298 VALUE result = memo->v1;
3299 VALUE args = memo->v2;
3300 long n = memo->u3.cnt++;
3305 rb_ary_store(tmp, 0, rb_enum_values_pack(argc, argv));
3310 rb_ary_push(tmp,
Qnil);
3316 if (
NIL_P(result)) {
3317 enum_yield_array(tmp);
3320 rb_ary_push(result, tmp);
3332 return v[0] = rb_funcallv(v[1], id_next, 0, 0);
3345 struct MEMO *memo = (
struct MEMO *)memoval;
3346 VALUE result = memo->v1;
3347 VALUE args = memo->v2;
3352 rb_ary_store(tmp, 0, rb_enum_values_pack(argc, argv));
3355 rb_ary_push(tmp,
Qnil);
3362 if (UNDEF_P(v[0])) {
3366 rb_ary_push(tmp, v[0]);
3369 if (
NIL_P(result)) {
3370 enum_yield_array(tmp);
3373 rb_ary_push(result, tmp);
3459 for (i=0; i<argc; i++) {
3460 VALUE ary = rb_check_array_type(argv[i]);
3468 static const VALUE sym_each = STATIC_ID2SYM(id_each);
3470 for (i=0; i<argc; i++) {
3472 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (must respond to :each)",
3475 argv[i] = rb_funcallv(argv[i], conv, 1, &sym_each);
3479 result = rb_ary_new();
3483 memo = MEMO_NEW(result, args, 0);
3492 struct MEMO *memo = MEMO_CAST(args);
3493 rb_ary_push(memo->v1, rb_enum_values_pack(argc, argv));
3521 rb_raise(rb_eArgError,
"attempt to take negative size");
3526 memo = MEMO_NEW(result, 0,
len);
3536 rb_ary_push(ary, rb_enum_values_pack(argc, argv));
3560enum_take_while(
VALUE obj)
3573 struct MEMO *memo = MEMO_CAST(args);
3574 if (memo->u3.cnt == 0) {
3575 rb_ary_push(memo->v1, rb_enum_values_pack(argc, argv));
3610 rb_raise(rb_eArgError,
"attempt to drop negative size");
3613 result = rb_ary_new();
3614 memo = MEMO_NEW(result, 0,
len);
3623 struct MEMO *memo = MEMO_CAST(args);
3626 if (!memo->u3.state && !
RTEST(enum_yield(argc, i))) {
3627 memo->u3.state = TRUE;
3629 if (memo->u3.state) {
3630 rb_ary_push(memo->v1, i);
3655enum_drop_while(
VALUE obj)
3661 result = rb_ary_new();
3662 memo = MEMO_NEW(result, 0, FALSE);
3672 rb_ary_push(ary, argc > 1 ? i : rb_ary_new_from_values(argc, argv));
3673 enum_yield(argc, i);
3689 size = enum_size(self, args, 0);
3690 if (
NIL_P(size) || FIXNUM_ZERO_P(size))
return size;
3693 if (mul <= 0)
return INT2FIX(0);
3695 return rb_funcallv(size,
'*', 1, &n);
3735 if (!argc ||
NIL_P(nv = argv[0])) {
3740 if (n <= 0)
return Qnil;
3743 RBASIC_CLEAR_CLASS(ary);
3747 while (n < 0 || 0 < --n) {
3748 for (i=0; i<
len; i++) {
3772 v = rb_funcallv(argp->categorize, id_call, 1, &i);
3775 if (!
NIL_P(argp->prev_value)) {
3776 s = rb_assoc_new(argp->prev_value, argp->prev_elts);
3777 rb_funcallv(argp->yielder, id_lshift, 1, &s);
3778 argp->prev_value = argp->prev_elts =
Qnil;
3781 rb_funcallv(argp->yielder, id_lshift, 1, &v);
3783 else if (
NIL_P(v) || v == separator) {
3784 if (!
NIL_P(argp->prev_value)) {
3785 v = rb_assoc_new(argp->prev_value, argp->prev_elts);
3786 rb_funcallv(argp->yielder, id_lshift, 1, &v);
3787 argp->prev_value = argp->prev_elts =
Qnil;
3791 rb_raise(
rb_eRuntimeError,
"symbols beginning with an underscore are reserved");
3794 if (
NIL_P(argp->prev_value)) {
3795 argp->prev_value = v;
3799 if (
rb_equal(argp->prev_value, v)) {
3800 rb_ary_push(argp->prev_elts, i);
3803 s = rb_assoc_new(argp->prev_value, argp->prev_elts);
3804 rb_funcallv(argp->yielder, id_lshift, 1, &s);
3805 argp->prev_value = v;
3822 memo->prev_value =
Qnil;
3823 memo->prev_elts =
Qnil;
3828 if (!
NIL_P(memo->prev_elts)) {
3829 arg = rb_assoc_new(memo->prev_value, memo->prev_elts);
3830 rb_funcallv(memo->yielder, id_lshift, 1, &arg);
3940enum_chunk(
VALUE enumerable)
3969 if (!
NIL_P(argp->sep_pat))
3970 header_p = rb_funcallv(argp->sep_pat, id_eqq, 1, &i);
3972 header_p = rb_funcallv(argp->sep_pred, id_call, 1, &i);
3973 if (
RTEST(header_p)) {
3974 if (!
NIL_P(argp->prev_elts))
3975 rb_funcallv(argp->yielder, id_lshift, 1, &argp->prev_elts);
3979 if (
NIL_P(argp->prev_elts))
3982 rb_ary_push(argp->prev_elts, i);
3996 memo->sep_pred = rb_attr_get(
enumerator, id_slicebefore_sep_pred);
3998 memo->prev_elts =
Qnil;
4001 rb_block_call(enumerable, id_each, 0, 0, slicebefore_ii, arg);
4003 if (!
NIL_P(memo->prev_elts))
4004 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4169enum_slice_before(
int argc,
VALUE *argv,
VALUE enumerable)
4175 rb_error_arity(argc, 0, 0);
4201#define UPDATE_MEMO ((void)(memo = MEMO_FOR(struct sliceafter_arg, _memo)))
4208 if (
NIL_P(memo->prev_elts)) {
4212 rb_ary_push(memo->prev_elts, i);
4215 if (
NIL_P(memo->pred)) {
4216 split_p =
RTEST(rb_funcallv(memo->pat, id_eqq, 1, &i));
4220 split_p =
RTEST(rb_funcallv(memo->pred, id_call, 1, &i));
4225 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4227 memo->prev_elts =
Qnil;
4243 memo->pred = rb_attr_get(
enumerator, id_sliceafter_pred);
4244 memo->prev_elts =
Qnil;
4247 rb_block_call(enumerable, id_each, 0, 0, sliceafter_ii, arg);
4249 if (!
NIL_P(memo->prev_elts))
4250 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4291enum_slice_after(
int argc,
VALUE *argv,
VALUE enumerable)
4298 rb_raise(rb_eArgError,
"both pattern and block are given");
4325#define UPDATE_MEMO ((void)(memo = MEMO_FOR(struct slicewhen_arg, _memo)))
4332 if (UNDEF_P(memo->prev_elt)) {
4339 args[0] = memo->prev_elt;
4341 split_p =
RTEST(rb_funcallv(memo->pred, id_call, 2, args));
4348 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4353 rb_ary_push(memo->prev_elts, i);
4372 memo->pred = rb_attr_get(
enumerator, id_slicewhen_pred);
4374 memo->prev_elts =
Qnil;
4376 memo->inverted =
RTEST(rb_attr_get(
enumerator, id_slicewhen_inverted));
4378 rb_block_call(enumerable, id_each, 0, 0, slicewhen_ii, arg);
4380 if (!
NIL_P(memo->prev_elts))
4381 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4448enum_slice_when(
VALUE enumerable)
4514enum_chunk_while(
VALUE enumerable)
4542 memo->v = rb_fix_plus(
LONG2FIX(memo->n), memo->v);
4545 switch (
TYPE(memo->r)) {
4546 case T_RATIONAL: memo->v = rb_rational_plus(memo->r, memo->v);
break;
4558 memo->v = rb_big_plus(
LONG2NUM(memo->n), memo->v);
4566 memo->v = rb_big_plus(i, memo->v);
4572 if (UNDEF_P(memo->r)) {
4576 memo->r = rb_rational_plus(memo->r, i);
4583 memo->v = rb_funcallv(memo->v, idPLUS, 1, &i);
4598 case T_BIGNUM: x = rb_big2dbl(i);
break;
4602 memo->float_value = 0;
4603 sum_iter_some_value(i, memo);
4612 else if (! isfinite(x)) {
4613 if (isinf(x) && isinf(f) && signbit(x) != signbit(f)) {
4621 else if (isinf(f)) {
4628 if (fabs(f) >= fabs(x)) {
4643 assert(memo != NULL);
4644 if (memo->block_given) {
4648 if (memo->float_value) {
4649 sum_iter_Kahan_Babuska(i, memo);
4651 else switch (
TYPE(memo->v)) {
4652 default: sum_iter_some_value(i, memo);
return;
4653 case T_FLOAT: sum_iter_Kahan_Babuska(i, memo);
return;
4658 case T_FIXNUM: sum_iter_fixnum(i, memo);
return;
4659 case T_BIGNUM: sum_iter_bignum(i, memo);
return;
4660 case T_RATIONAL: sum_iter_rational(i, memo);
return;
4662 sum_iter_normalize_memo(memo);
4665 memo->float_value = 1;
4666 sum_iter_Kahan_Babuska(i, memo);
4669 sum_iter_normalize_memo(memo);
4670 sum_iter_some_value(i, memo);
4687 sum_iter(rb_assoc_new(key, value), (
struct enum_sum_memo *) arg);
4694 assert(RB_TYPE_P(hash,
T_HASH));
4695 assert(memo != NULL);
4707 end = rb_big_minus(end,
LONG2FIX(1));
4710 if (rb_int_ge(end, beg)) {
4712 a = rb_int_plus(rb_int_minus(end, beg),
LONG2FIX(1));
4713 a = rb_int_mul(a, rb_int_plus(end, beg));
4715 return rb_int_plus(init, a);
4774 if (!memo.block_given && !memo.float_value &&
4775 (
FIXNUM_P(beg) || RB_BIGNUM_TYPE_P(beg)) &&
4776 (
FIXNUM_P(end) || RB_BIGNUM_TYPE_P(end))) {
4777 return int_range_sum(beg, end, excl, memo.v);
4781 if (RB_TYPE_P(obj,
T_HASH) &&
4782 rb_method_basic_definition_p(
CLASS_OF(obj), id_each))
4783 hash_sum(obj, &memo);
4787 if (memo.float_value) {
4788 return DBL2NUM(memo.f + memo.c);
4792 memo.v = rb_fix_plus(
LONG2FIX(memo.n), memo.v);
4793 if (!UNDEF_P(memo.r)) {
4794 memo.v = rb_rational_plus(memo.r, memo.v);
4804 rb_hash_add_new_element(hash, i, i);
4844 hash = rb_obj_hide(rb_hash_new());
4846 ret = rb_hash_values(hash);
4847 rb_hash_clear(hash);
4857 rb_ary_push(ary, i);
4874enum_compact(
VALUE obj)
5059Init_Enumerable(
void)
5131 id_slicebefore_enumerable =
rb_intern_const(
"slicebefore_enumerable");
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
VALUE rb_define_module(const char *name)
Defines a top-level module.
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_block_given_p(void)
Determines if the current method is given a block.
#define TYPE(_)
Old name of rb_type.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SYM2ID
Old name of RB_SYM2ID.
#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 LONG2FIX
Old name of RB_INT2FIX.
#define FIX2ULONG
Old name of RB_FIX2ULONG.
#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 T_UNDEF
Old name of RUBY_T_UNDEF.
#define Qtrue
Old name of RUBY_Qtrue.
#define FIXNUM_MAX
Old name of RUBY_FIXNUM_MAX.
#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 DBL2NUM
Old name of rb_float_new.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
void rb_iter_break(void)
Breaks from a block.
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 unless $VERBOSE is nil.
void rb_warning(const char *fmt,...)
Issues a warning.
VALUE rb_cArray
Array class.
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_cEnumerator
Enumerator class.
VALUE rb_cInteger
Module class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
double rb_num2dbl(VALUE num)
Converts an instance of rb_cNumeric into C's double.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv_public(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it only takes public methods into account.
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
#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.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
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.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
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.
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
int len
Length of the buffer.
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
Reentrant implementation of quick sort.
#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.
rb_block_call_func * rb_block_call_func_t
Shorthand type that represents an iterator-written-in-C function pointer.
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_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
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 void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
static VALUE * RARRAY_PTR(VALUE ary)
Wild use of a C pointer.
#define RARRAY_AREF(a, i)
#define RBASIC(obj)
Convenient casting macro.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
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.