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)) {
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) {
117 struct MEMO *memo = MEMO_CAST(args);
120 if (
RTEST(rb_funcallv(memo->v1, id_eqq, 1, &i)) ==
RTEST(memo->u3.value)) {
130 struct MEMO *memo = MEMO_NEW(pat, ary, test);
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);
325NORETURN(
static void found(
VALUE i,
VALUE memop));
329 struct MEMO *memo = MEMO_CAST(memop);
330 MEMO_V1_SET(memo, i);
350 if (
RTEST(enum_yield(argc, i))) {
385 memo = MEMO_NEW(
Qundef, 0, 0);
386 if (rb_block_pair_yield_optimizable())
387 rb_block_call2(obj, id_each, 0, 0, find_i_fast, (
VALUE)memo, RB_BLOCK_NO_USE_PACKED_ARGS);
389 rb_block_call2(obj, id_each, 0, 0, find_i, (
VALUE)memo, RB_BLOCK_NO_USE_PACKED_ARGS);
393 if (!
NIL_P(if_none)) {
394 return rb_funcallv(if_none, id_call, 0, 0);
402 struct MEMO *memo = MEMO_CAST(memop);
407 MEMO_V1_SET(memo, imemo_count_value(memo));
410 imemo_count_up(memo);
417 struct MEMO *memo = MEMO_CAST(memop);
420 MEMO_V1_SET(memo, imemo_count_value(memo));
423 imemo_count_up(memo);
452enum_find_index(
int argc,
VALUE *argv,
VALUE obj)
460 func = find_index_iter_i;
465 rb_warn(
"given block not used");
470 memo = MEMO_NEW(
Qnil, condition_value, 0);
480 if (
RTEST(enum_yield(argc, i))) {
489 return rb_check_funcall_default(self, id_size, 0, 0,
Qnil);
493limit_by_enum_size(
VALUE obj,
long n)
499 return ((
unsigned long)n > limit) ? (long)limit : n;
503enum_size_over_p(
VALUE obj,
long n)
507 return ((
unsigned long)n >
FIX2ULONG(size));
529enum_find_all(
VALUE obj)
570enum_filter_map(
VALUE obj)
588 if (!
RTEST(enum_yield(argc, i))) {
613enum_reject(
VALUE obj)
658enum_collect(
VALUE obj)
661 int min_argc, max_argc;
666 min_argc = rb_block_min_max_arity(&max_argc);
667 rb_lambda_call(obj, id_each, 0, 0, collect_i, min_argc, max_argc, ary);
709enum_flat_map(
VALUE obj)
750 return enum_hashify_into(obj, argc, argv, iter, rb_hash_new());
757 return rb_hash_set_pair(hash, i);
791 return enum_hashify(obj, argc, argv, iter);
797 struct MEMO *memo = MEMO_CAST(p);
801 if (UNDEF_P(memo->v1)) {
802 MEMO_V1_SET(memo, i);
813 struct MEMO *memo = MEMO_CAST(p);
818 if (UNDEF_P(memo->v1)) {
819 MEMO_V1_SET(memo, i);
821 else if (
SYMBOL_P(name = memo->u3.value)) {
829 MEMO_V1_SET(memo, rb_f_send(numberof(args), args, memo->v1));
842 return UNDEF_P(init) ?
Qnil : init;
858 rb_method_basic_definition_p(
rb_cInteger, idPLUS) &&
870 else if (RB_BIGNUM_TYPE_P(e))
871 v = rb_big_plus(e, v);
1066 op =
id ?
ID2SYM(
id) : init;
1080 if (iter == inject_op_i &&
1083 rb_method_basic_definition_p(
CLASS_OF(obj), id_each)) {
1084 return ary_inject_op(obj, init, op);
1087 memo = MEMO_NEW(init,
Qnil, op);
1089 if (UNDEF_P(memo->v1))
return Qnil;
1096 struct MEMO *memo = MEMO_CAST(arys);
1100 if (
RTEST(enum_yield(argc, i))) {
1139enum_partition(
VALUE obj)
1159 group = enum_yield(argc, i);
1160 values = rb_hash_aref(hash, group);
1163 rb_hash_aset(hash, group, values);
1194enum_group_by(
VALUE obj)
1198 return enum_hashify(obj, 0, 0, group_by_i);
1202tally_up(st_data_t *group, st_data_t *value, st_data_t arg,
int existing)
1210 tally +=
INT2FIX(1) & ~FIXNUM_FLAG;
1214 tally = rb_big_plus(tally,
INT2FIX(1));
1217 *value = (st_data_t)tally;
1224 if (!rb_hash_stlike_update(hash, group, tally_up, (st_data_t)hash)) {
1234 rb_enum_tally_up(hash, i);
1289 hash = rb_to_hash_type(argv[0]);
1290 rb_check_frozen(hash);
1293 hash = rb_hash_new();
1296 return enum_hashify_into(obj, 0, 0, tally_i, hash);
1303 struct MEMO *memo = MEMO_CAST(params);
1306 MEMO_V1_SET(memo, i);
1345 return enum_take(obj, argv[0]);
1348 memo = MEMO_NEW(
Qnil, 0, 0);
1392#define SORT_BY_BUFSIZE 16
1393#define SORT_BY_UNIFORMED(num, flo, fix) (((num&1)<<2)|((flo&1)<<1)|fix)
1398 uint8_t primitive_uniformed;
1405 VALUE ary = data->ary;
1410 v = enum_yield(argc, i);
1412 if (
RBASIC(ary)->klass) {
1415 if (
RARRAY_LEN(data->buf) != SORT_BY_BUFSIZE*2) {
1419 if (data->primitive_uniformed) {
1427 if (data->n == SORT_BY_BUFSIZE) {
1435sort_by_cmp(
const void *ap,
const void *bp,
void *data)
1441 if (
RBASIC(ary)->klass) {
1448 return OPTIMIZED_CMP(a, b);
1456#define uless rb_uniform_is_less
1457#define UNIFORM_SWAP(a,b)\
1458 do{struct rb_uniform_sort_data tmp = a; a = b; b = tmp;} while(0)
1474 return rb_float_cmp(b, a) > 0;
1478 return rb_float_cmp(a, b) < 0;
1491 return rb_float_cmp(b, a) < 0;
1495 return rb_float_cmp(a, b) > 0;
1499#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))
1505 if ((ptr_end - ptr_begin) < 2)
return;
1507 *index = ptr_begin+1;
1508 for (; index < ptr_end; index++) {
1511 if (uless(tmp.v, ptr_begin->v)) {
1512 while (ptr_begin < j) {
1518 while (uless(tmp.v, (--k)->v)) {
1529 size_t offset,
size_t len)
1533 while ((c = (offset<<1)+1) <=
len) {
1534 if (c <
len && uless(ptr_begin[c].v, ptr_begin[c+1].v)) {
1537 if (!uless(tmp.v, ptr_begin[c].v))
break;
1538 ptr_begin[offset] = ptr_begin[c];
1541 ptr_begin[offset] = tmp;
1548 size_t n = ptr_end - ptr_begin;
1551 for (
size_t offset = n>>1; offset > 0;) {
1552 rb_uniform_heap_down_2(ptr_begin, --offset, n-1);
1554 for (
size_t offset = n-1; offset > 0;) {
1555 UNIFORM_SWAP(*ptr_begin, ptr_begin[offset]);
1556 rb_uniform_heap_down_2(ptr_begin, 0, --offset);
1566 if (ptr_end - ptr_begin <= 16) {
1567 rb_uniform_insertionsort_2(ptr_begin, ptr_end);
1571 rb_uniform_heapsort_2(ptr_begin, ptr_end);
1575 VALUE x = med3_val(ptr_begin->v,
1576 ptr_begin[(ptr_end - ptr_begin)>>1].v,
1582 while (uless(i->v, x)) i++;
1583 while (uless(x, j->v)) j--;
1585 UNIFORM_SWAP(*i, *j);
1591 if (ptr_end - j > 1) rb_uniform_quicksort_intro_2(j, ptr_end, d-1);
1592 if (i - ptr_begin > 1) rb_uniform_quicksort_intro_2(ptr_begin, i, d-1);
1604 size_t n = ptr_end - ptr_begin;
1605 size_t d = CHAR_BIT *
sizeof(n) - nlz_intptr(n) - 1;
1606 bool sorted_flag =
true;
1609 if (rb_uniform_is_larger((ptr-1)->v, (ptr)->v)) {
1610 sorted_flag =
false;
1618 rb_uniform_quicksort_intro_2(ptr_begin, ptr_end, d<<1);
1707enum_sort_by(
VALUE obj)
1722 RBASIC_CLEAR_CLASS(ary);
1725 memo = MEMO_NEW(0, 0, 0);
1730 data->primitive_uniformed = SORT_BY_UNIFORMED((CMP_OPTIMIZABLE(FLOAT) && CMP_OPTIMIZABLE(INTEGER)),
1731 CMP_OPTIMIZABLE(FLOAT),
1732 CMP_OPTIMIZABLE(INTEGER));
1741 if (data->primitive_uniformed) {
1749 sort_by_cmp, (
void *)ary));
1752 if (
RBASIC(ary)->klass) {
1764#define ENUMFUNC(name) argc ? name##_eqq : rb_block_given_p() ? name##_iter_i : name##_i
1766#define ENUM_BLOCK_CALL(name) \
1767 rb_block_call2(obj, id_each, 0, 0, ENUMFUNC(name), (VALUE)memo, rb_block_given_p() && rb_block_pair_yield_optimizable() ? RB_BLOCK_NO_USE_PACKED_ARGS : 0);
1769#define MEMO_ENUM_NEW(v1) (rb_check_arity(argc, 0, 1), MEMO_NEW((v1), (argc ? *argv : 0), 0))
1771#define DEFINE_ENUMFUNCS(name) \
1772static VALUE enum_##name##_func(VALUE result, struct MEMO *memo); \
1775name##_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1777 return enum_##name##_func(rb_enum_values_pack(argc, argv), MEMO_CAST(memo)); \
1781name##_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1783 return enum_##name##_func(rb_yield_values2(argc, argv), MEMO_CAST(memo)); \
1787name##_eqq(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1789 ENUM_WANT_SVALUE(); \
1790 return enum_##name##_func(rb_funcallv(MEMO_CAST(memo)->v2, id_eqq, 1, &i), MEMO_CAST(memo)); \
1794enum_##name##_func(VALUE result, struct MEMO *memo)
1796#define WARN_UNUSED_BLOCK(argc) do { \
1797 if ((argc) > 0 && rb_block_given_p()) { \
1798 rb_warn("given block not used"); \
1802DEFINE_ENUMFUNCS(all)
1804 if (!
RTEST(result)) {
1805 MEMO_V1_SET(memo,
Qfalse);
1860 struct MEMO *memo = MEMO_ENUM_NEW(
Qtrue);
1861 WARN_UNUSED_BLOCK(argc);
1862 ENUM_BLOCK_CALL(all);
1866DEFINE_ENUMFUNCS(any)
1868 if (
RTEST(result)) {
1869 MEMO_V1_SET(memo,
Qtrue);
1923 WARN_UNUSED_BLOCK(argc);
1924 ENUM_BLOCK_CALL(any);
1928DEFINE_ENUMFUNCS(one)
1930 if (
RTEST(result)) {
1931 if (UNDEF_P(memo->v1)) {
1932 MEMO_V1_SET(memo,
Qtrue);
1934 else if (memo->v1 ==
Qtrue) {
1935 MEMO_V1_SET(memo,
Qfalse);
1948 int (*cmpfunc)(
const void *,
const void *,
void *);
1956 if (
RBASIC(data->buf)->klass) {
1958 data->rev ?
"max" :
"min",
1959 data->by ?
"_by" :
"");
1965nmin_cmp(
const void *ap,
const void *bp,
void *_data)
1969#define rb_cmpint(cmp, a, b) rb_cmpint(cmpint_reenter_check(data, (cmp)), a, b)
1970 return OPTIMIZED_CMP(a, b);
1975nmin_block_cmp(
const void *ap,
const void *bp,
void *_data)
1980 cmpint_reenter_check(data, cmp);
1981 return rb_cmpint(cmp, a, b);
1997 if (data->curlen <= data->n)
2002 eltsize = data->by ? 2 : 1;
2003 numelts = data->curlen;
2008#define GETPTR(i) (beg+(i)*eltsize)
2010#define SWAP(i, j) do { \
2012 memcpy(tmp, GETPTR(i), sizeof(VALUE)*eltsize); \
2013 memcpy(GETPTR(i), GETPTR(j), sizeof(VALUE)*eltsize); \
2014 memcpy(GETPTR(j), tmp, sizeof(VALUE)*eltsize); \
2018 long pivot_index = left + (right-left)/2;
2019 long num_pivots = 1;
2021 SWAP(pivot_index, right);
2022 pivot_index = right;
2026 while (i <= right-num_pivots) {
2027 int c = data->cmpfunc(GETPTR(i), GETPTR(pivot_index), data);
2031 SWAP(i, right-num_pivots);
2036 SWAP(i, store_index);
2042 for (i = right; right-num_pivots < i; i--) {
2049 if (store_index <= n && n <= store_index+num_pivots)
2052 if (n < store_index) {
2053 right = store_index-1;
2056 left = store_index+num_pivots;
2062 data->limit =
RARRAY_AREF(data->buf, store_index*eltsize);
2063 data->curlen = data->n;
2076 cmpv = enum_yield(argc, i);
2080 if (!UNDEF_P(data->limit)) {
2081 int c = data->cmpfunc(&cmpv, &data->limit, data);
2094 if (data->curlen == data->bufmax) {
2102rb_nmin_run(
VALUE obj,
VALUE num,
int by,
int rev,
int ary)
2109 rb_raise(rb_eArgError,
"negative size (%ld)", data.n);
2112 if (LONG_MAX/4/(by ? 2 : 1) < data.n)
2113 rb_raise(rb_eArgError,
"too big size");
2114 data.bufmax = data.n * 4;
2118 data.cmpfunc = by ? nmin_cmp :
2142 data.cmpfunc, (
void *)&data);
2152 data.cmpfunc, (
void *)&data);
2212 WARN_UNUSED_BLOCK(argc);
2213 ENUM_BLOCK_CALL(one);
2215 if (UNDEF_P(result))
return Qfalse;
2219DEFINE_ENUMFUNCS(none)
2221 if (
RTEST(result)) {
2222 MEMO_V1_SET(memo,
Qfalse);
2271 struct MEMO *memo = MEMO_ENUM_NEW(
Qtrue);
2273 WARN_UNUSED_BLOCK(argc);
2274 ENUM_BLOCK_CALL(none);
2285 struct min_t *memo = MEMO_FOR(
struct min_t, args);
2289 if (UNDEF_P(memo->min)) {
2293 if (OPTIMIZED_CMP(i, memo->min) < 0) {
2304 struct min_t *memo = MEMO_FOR(
struct min_t, args);
2308 if (UNDEF_P(memo->min)) {
2313 if (rb_cmpint(cmp, i, memo->min) < 0) {
2382 struct min_t *m = NEW_MEMO_FOR(
struct min_t, memo);
2387 return rb_nmin_run(obj, num, 0, 0, 0);
2397 if (UNDEF_P(result))
return Qnil;
2408 struct max_t *memo = MEMO_FOR(
struct max_t, args);
2412 if (UNDEF_P(memo->max)) {
2416 if (OPTIMIZED_CMP(i, memo->max) > 0) {
2426 struct max_t *memo = MEMO_FOR(
struct max_t, args);
2431 if (UNDEF_P(memo->max)) {
2436 if (rb_cmpint(cmp, i, memo->max) > 0) {
2504 struct max_t *m = NEW_MEMO_FOR(
struct max_t, memo);
2509 return rb_nmin_run(obj, num, 0, 1, 0);
2519 if (UNDEF_P(result))
return Qnil;
2534 if (UNDEF_P(memo->min)) {
2539 n = OPTIMIZED_CMP(i, memo->min);
2543 n = OPTIMIZED_CMP(j, memo->max);
2559 if (UNDEF_P(memo->last)) {
2566 n = OPTIMIZED_CMP(j, i);
2576 minmax_i_update(i, j, memo);
2586 if (UNDEF_P(memo->min)) {
2611 if (UNDEF_P(memo->last)) {
2628 minmax_ii_update(i, j, memo);
2665enum_minmax(
VALUE obj)
2674 if (!UNDEF_P(m->last))
2675 minmax_ii_update(m->last, m->last, m);
2679 if (!UNDEF_P(m->last))
2680 minmax_i_update(m->last, m->last, m);
2682 if (!UNDEF_P(m->min)) {
2691 struct MEMO *memo = MEMO_CAST(args);
2696 v = enum_yield(argc, i);
2697 if (UNDEF_P(memo->v1)) {
2698 MEMO_V1_SET(memo, v);
2699 MEMO_V2_SET(memo, i);
2701 else if (OPTIMIZED_CMP(v, memo->v1) < 0) {
2702 MEMO_V1_SET(memo, v);
2703 MEMO_V2_SET(memo, i);
2754 if (argc && !
NIL_P(num = argv[0]))
2755 return rb_nmin_run(obj, num, 1, 0, 0);
2765 struct MEMO *memo = MEMO_CAST(args);
2770 v = enum_yield(argc, i);
2771 if (UNDEF_P(memo->v1)) {
2772 MEMO_V1_SET(memo, v);
2773 MEMO_V2_SET(memo, i);
2775 else if (OPTIMIZED_CMP(v, memo->v1) > 0) {
2776 MEMO_V1_SET(memo, v);
2777 MEMO_V2_SET(memo, i);
2828 if (argc && !
NIL_P(num = argv[0]))
2829 return rb_nmin_run(obj, num, 1, 1, 0);
2848 if (UNDEF_P(memo->min_bv)) {
2855 if (OPTIMIZED_CMP(v1, memo->min_bv) < 0) {
2859 if (OPTIMIZED_CMP(v2, memo->max_bv) > 0) {
2875 vi = enum_yield(argc, i);
2877 if (UNDEF_P(memo->last_bv)) {
2886 n = OPTIMIZED_CMP(vj, vi);
2901 minmax_by_i_update(vi, vj, i, j, memo);
2930enum_minmax_by(
VALUE obj)
2944 if (!UNDEF_P(m->last_bv))
2945 minmax_by_i_update(m->last_bv, m->last_bv, m->last, m->last, m);
2953 struct MEMO *memo = MEMO_CAST(args);
2955 if (
rb_equal(rb_enum_values_pack(argc, argv), memo->v1)) {
2956 MEMO_V2_SET(memo,
Qtrue);
2982 struct MEMO *memo = MEMO_NEW(val,
Qfalse, 0);
2991 struct vm_ifunc *ifunc = rb_current_ifunc();
2992 ifunc->data = (
const void *)rb_int_succ(index);
3026enum_each_with_index(
int argc,
VALUE *argv,
VALUE obj)
3062enum_reverse_each(
int argc,
VALUE *argv,
VALUE obj)
3069 ary = enum_to_a(argc, argv, obj);
3089 enum_yield(argc, i);
3132enum_each_entry(
int argc,
VALUE *argv,
VALUE obj)
3140add_int(
VALUE x,
long n)
3144 return rb_funcallv(x,
'+', 1, &y);
3148div_int(
VALUE x,
long n)
3152 return rb_funcallv(x, id_div, 1, &y);
3155#define dont_recycle_block_arg(arity) ((arity) == 1 || (arity) < 0)
3160 struct MEMO *memo = MEMO_CAST(m);
3161 VALUE ary = memo->v1;
3163 long size = memo->u3.cnt;
3189 if (slice_size <= 0) rb_raise(rb_eArgError,
"invalid slice size");
3191 size = enum_size(obj, 0, 0);
3197 n = add_int(size, slice_size-1);
3198 return div_int(n, slice_size);
3229 if (size <= 0) rb_raise(rb_eArgError,
"invalid slice size");
3231 size = limit_by_enum_size(obj, size);
3233 arity = rb_block_arity();
3234 memo = MEMO_NEW(ary, dont_recycle_block_arg(arity), size);
3245 struct MEMO *memo = MEMO_CAST(args);
3246 VALUE ary = memo->v1;
3248 long size = memo->u3.cnt;
3270 if (cons_size <= 0) rb_raise(rb_eArgError,
"invalid size");
3272 size = enum_size(obj, 0, 0);
3275 n = add_int(size, 1 - cons_size);
3276 return (OPTIMIZED_CMP(n, zero) == -1) ? zero : n;
3306 if (size <= 0) rb_raise(rb_eArgError,
"invalid size");
3308 arity = rb_block_arity();
3309 if (enum_size_over_p(obj, size))
return obj;
3310 memo = MEMO_NEW(
rb_ary_new2(size), dont_recycle_block_arg(arity), size);
3345 rb_block_call(obj, id_each, 0, 0, each_with_object_i, memo);
3353 struct MEMO *memo = (
struct MEMO *)memoval;
3354 VALUE result = memo->v1;
3355 VALUE args = memo->v2;
3356 long n = memo->u3.cnt++;
3372 if (
NIL_P(result)) {
3373 enum_yield_array(tmp);
3388 return v[0] = rb_funcallv(v[1], id_next, 0, 0);
3401 struct MEMO *memo = (
struct MEMO *)memoval;
3402 VALUE result = memo->v1;
3403 VALUE args = memo->v2;
3418 if (UNDEF_P(v[0])) {
3425 if (
NIL_P(result)) {
3426 enum_yield_array(tmp);
3515 for (i=0; i<argc; i++) {
3524 static const VALUE sym_each = STATIC_ID2SYM(id_each);
3526 for (i=0; i<argc; i++) {
3528 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (must respond to :each)",
3531 argv[i] = rb_funcallv(argv[i], conv, 1, &sym_each);
3539 memo = MEMO_NEW(result, args, 0);
3548 struct MEMO *memo = MEMO_CAST(args);
3549 rb_ary_push(memo->v1, rb_enum_values_pack(argc, argv));
3577 rb_raise(rb_eArgError,
"attempt to take negative size");
3582 memo = MEMO_NEW(result, 0,
len);
3592 rb_ary_push(ary, rb_enum_values_pack(argc, argv));
3616enum_take_while(
VALUE obj)
3629 struct MEMO *memo = MEMO_CAST(args);
3630 if (memo->u3.cnt == 0) {
3631 rb_ary_push(memo->v1, rb_enum_values_pack(argc, argv));
3666 rb_raise(rb_eArgError,
"attempt to drop negative size");
3670 memo = MEMO_NEW(result, 0,
len);
3679 struct MEMO *memo = MEMO_CAST(args);
3682 if (!memo->u3.state && !
RTEST(enum_yield(argc, i))) {
3683 memo->u3.state = TRUE;
3685 if (memo->u3.state) {
3722enum_drop_while(
VALUE obj)
3729 memo = MEMO_NEW(result, 0, FALSE);
3740 enum_yield(argc, i);
3756 size = enum_size(self, args, 0);
3757 if (
NIL_P(size) || FIXNUM_ZERO_P(size))
return size;
3760 if (mul <= 0)
return INT2FIX(0);
3762 return rb_funcallv(size,
'*', 1, &n);
3802 if (!argc ||
NIL_P(nv = argv[0])) {
3807 if (n <= 0)
return Qnil;
3810 RBASIC_CLEAR_CLASS(ary);
3814 while (n < 0 || 0 < --n) {
3815 for (i=0; i<
len; i++) {
3839 v = rb_funcallv(argp->categorize, id_call, 1, &i);
3842 if (!
NIL_P(argp->prev_value)) {
3844 rb_funcallv(argp->yielder, id_lshift, 1, &s);
3845 argp->prev_value = argp->prev_elts =
Qnil;
3848 rb_funcallv(argp->yielder, id_lshift, 1, &v);
3850 else if (
NIL_P(v) || v == separator) {
3851 if (!
NIL_P(argp->prev_value)) {
3853 rb_funcallv(argp->yielder, id_lshift, 1, &v);
3854 argp->prev_value = argp->prev_elts =
Qnil;
3858 rb_raise(
rb_eRuntimeError,
"symbols beginning with an underscore are reserved");
3861 if (
NIL_P(argp->prev_value)) {
3862 argp->prev_value = v;
3866 if (
rb_equal(argp->prev_value, v)) {
3871 rb_funcallv(argp->yielder, id_lshift, 1, &s);
3872 argp->prev_value = v;
3889 memo->prev_value =
Qnil;
3890 memo->prev_elts =
Qnil;
3895 if (!
NIL_P(memo->prev_elts)) {
3897 rb_funcallv(memo->yielder, id_lshift, 1, &arg);
4007enum_chunk(
VALUE enumerable)
4036 if (!
NIL_P(argp->sep_pat))
4037 header_p = rb_funcallv(argp->sep_pat, id_eqq, 1, &i);
4039 header_p = rb_funcallv(argp->sep_pred, id_call, 1, &i);
4040 if (
RTEST(header_p)) {
4041 if (!
NIL_P(argp->prev_elts))
4042 rb_funcallv(argp->yielder, id_lshift, 1, &argp->prev_elts);
4046 if (
NIL_P(argp->prev_elts))
4063 memo->sep_pred = rb_attr_get(
enumerator, id_slicebefore_sep_pred);
4065 memo->prev_elts =
Qnil;
4068 rb_block_call(enumerable, id_each, 0, 0, slicebefore_ii, arg);
4070 if (!
NIL_P(memo->prev_elts))
4071 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4236enum_slice_before(
int argc,
VALUE *argv,
VALUE enumerable)
4242 rb_error_arity(argc, 0, 0);
4268#define UPDATE_MEMO ((void)(memo = MEMO_FOR(struct sliceafter_arg, _memo)))
4275 if (
NIL_P(memo->prev_elts)) {
4282 if (
NIL_P(memo->pred)) {
4283 split_p =
RTEST(rb_funcallv(memo->pat, id_eqq, 1, &i));
4287 split_p =
RTEST(rb_funcallv(memo->pred, id_call, 1, &i));
4292 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4294 memo->prev_elts =
Qnil;
4310 memo->pred = rb_attr_get(
enumerator, id_sliceafter_pred);
4311 memo->prev_elts =
Qnil;
4314 rb_block_call(enumerable, id_each, 0, 0, sliceafter_ii, arg);
4316 if (!
NIL_P(memo->prev_elts))
4317 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4358enum_slice_after(
int argc,
VALUE *argv,
VALUE enumerable)
4365 rb_raise(rb_eArgError,
"both pattern and block are given");
4392#define UPDATE_MEMO ((void)(memo = MEMO_FOR(struct slicewhen_arg, _memo)))
4399 if (UNDEF_P(memo->prev_elt)) {
4406 args[0] = memo->prev_elt;
4408 split_p =
RTEST(rb_funcallv(memo->pred, id_call, 2, args));
4415 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4439 memo->pred = rb_attr_get(
enumerator, id_slicewhen_pred);
4441 memo->prev_elts =
Qnil;
4443 memo->inverted =
RTEST(rb_attr_get(
enumerator, id_slicewhen_inverted));
4445 rb_block_call(enumerable, id_each, 0, 0, slicewhen_ii, arg);
4447 if (!
NIL_P(memo->prev_elts))
4448 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4515enum_slice_when(
VALUE enumerable)
4581enum_chunk_while(
VALUE enumerable)
4609 memo->v = rb_fix_plus(
LONG2FIX(memo->n), memo->v);
4612 switch (
TYPE(memo->r)) {
4613 case T_RATIONAL: memo->v = rb_rational_plus(memo->r, memo->v);
break;
4625 memo->v = rb_big_plus(
LONG2NUM(memo->n), memo->v);
4633 memo->v = rb_big_plus(i, memo->v);
4639 if (UNDEF_P(memo->r)) {
4643 memo->r = rb_rational_plus(memo->r, i);
4650 memo->v = rb_funcallv(memo->v, idPLUS, 1, &i);
4665 case T_BIGNUM: x = rb_big2dbl(i);
break;
4669 memo->float_value = 0;
4670 sum_iter_some_value(i, memo);
4679 else if (! isfinite(x)) {
4680 if (isinf(x) && isinf(f) && signbit(x) != signbit(f)) {
4688 else if (isinf(f)) {
4695 if (fabs(f) >= fabs(x)) {
4711 if (memo->block_given) {
4715 if (memo->float_value) {
4716 sum_iter_Kahan_Babuska(i, memo);
4718 else switch (
TYPE(memo->v)) {
4719 default: sum_iter_some_value(i, memo);
return;
4725 case T_FIXNUM: sum_iter_fixnum(i, memo);
return;
4726 case T_BIGNUM: sum_iter_bignum(i, memo);
return;
4727 case T_RATIONAL: sum_iter_rational(i, memo);
return;
4729 sum_iter_normalize_memo(memo);
4732 memo->float_value = 1;
4733 sum_iter_Kahan_Babuska(i, memo);
4736 sum_iter_normalize_memo(memo);
4737 sum_iter_some_value(i, memo);
4774 end = rb_big_minus(end,
LONG2FIX(1));
4777 if (rb_int_ge(end, beg)) {
4779 a = rb_int_plus(rb_int_minus(end, beg),
LONG2FIX(1));
4780 a = rb_int_mul(a, rb_int_plus(end, beg));
4782 return rb_int_plus(init, a);
4841 if (!memo.block_given && !memo.float_value &&
4842 (
FIXNUM_P(beg) || RB_BIGNUM_TYPE_P(beg)) &&
4843 (
FIXNUM_P(end) || RB_BIGNUM_TYPE_P(end))) {
4844 return int_range_sum(beg, end, excl, memo.v);
4849 rb_method_basic_definition_p(
CLASS_OF(obj), id_each))
4850 hash_sum(obj, &memo);
4854 if (memo.float_value) {
4855 return DBL2NUM(memo.f + memo.c);
4859 memo.v = rb_fix_plus(
LONG2FIX(memo.n), memo.v);
4860 if (!UNDEF_P(memo.r)) {
4861 memo.v = rb_rational_plus(memo.r, memo.v);
4871 rb_hash_add_new_element(hash, i, i);
4913 ret = rb_hash_values(hash);
4914 rb_hash_clear(hash);
4941enum_compact(
VALUE obj)
5202Init_Enumerable(
void)
5274 id_slicebefore_enumerable =
rb_intern_const(
"slicebefore_enumerable");
#define RUBY_ASSERT(...)
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 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_hide(VALUE obj)
Make the object invisible from Ruby code.
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.
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_ary_concat(VALUE lhs, VALUE rhs)
Destructively appends the contents of latter into the end of former.
VALUE rb_ary_reverse(VALUE ary)
Destructively reverses the passed array in-place.
VALUE rb_ary_shift(VALUE ary)
Destructively deletes an element from the beginning of the passed array and returns what was deleted.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_resize(VALUE ary, long len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_sort_bang(VALUE ary)
Destructively sorts the passed array in-place, according to each elements' <=> result.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#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.
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 symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
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.
IFUNC (Internal FUNCtion)
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.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.