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);
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)
636 rb_ary_push(ary, rb_enum_values_pack(argc, argv));
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);
678 tmp = rb_check_array_type(i);
684 rb_ary_concat(ary, tmp);
709enum_flat_map(
VALUE obj)
733 VALUE ary = rb_ary_new();
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))) {
1106 rb_ary_push(ary, i);
1139enum_partition(
VALUE obj)
1145 memo = MEMO_NEW(rb_ary_new(), rb_ary_new(), 0);
1148 return rb_assoc_new(memo->v1, memo->v2);
1159 group = enum_yield(argc, i);
1160 values = rb_hash_aref(hash, group);
1163 rb_hash_aset(hash, group, values);
1166 rb_ary_push(values, i);
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;
1225 rb_hash_stlike_update(hash, group, tally_up, (st_data_t)hash);
1233 rb_enum_tally_up(hash, i);
1288 hash = rb_to_hash_type(argv[0]);
1289 rb_check_frozen(hash);
1292 hash = rb_hash_new();
1295 return enum_hashify_into(obj, 0, 0, tally_i, hash);
1302 struct MEMO *memo = MEMO_CAST(params);
1305 MEMO_V1_SET(memo, i);
1344 return enum_take(obj, argv[0]);
1347 memo = MEMO_NEW(
Qnil, 0, 0);
1388 return rb_ary_sort_bang(enum_to_a(0, 0, obj));
1391#define SORT_BY_BUFSIZE 16
1392#define SORT_BY_UNIFORMED(num, flo, fix) (((num&1)<<2)|((flo&1)<<1)|fix)
1397 uint8_t primitive_uniformed;
1404 VALUE ary = data->ary;
1409 v = enum_yield(argc, i);
1411 if (
RBASIC(ary)->klass) {
1414 if (
RARRAY_LEN(data->buf) != SORT_BY_BUFSIZE*2) {
1418 if (data->primitive_uniformed) {
1426 if (data->n == SORT_BY_BUFSIZE) {
1427 rb_ary_concat(ary, data->buf);
1434sort_by_cmp(
const void *ap,
const void *bp,
void *data)
1440 if (
RBASIC(ary)->klass) {
1447 return OPTIMIZED_CMP(a, b);
1455#define uless rb_uniform_is_less
1456#define UNIFORM_SWAP(a,b)\
1457 do{struct rb_uniform_sort_data tmp = a; a = b; b = tmp;} while(0)
1473 return rb_float_cmp(b, a) > 0;
1477 return rb_float_cmp(a, b) < 0;
1490 return rb_float_cmp(b, a) < 0;
1494 return rb_float_cmp(a, b) > 0;
1498#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))
1504 if ((ptr_end - ptr_begin) < 2)
return;
1506 *index = ptr_begin+1;
1507 for (; index < ptr_end; index++) {
1510 if (uless(tmp.v, ptr_begin->v)) {
1511 while (ptr_begin < j) {
1517 while (uless(tmp.v, (--k)->v)) {
1528 size_t offset,
size_t len)
1532 while ((c = (offset<<1)+1) <=
len) {
1533 if (c <
len && uless(ptr_begin[c].v, ptr_begin[c+1].v)) {
1536 if (!uless(tmp.v, ptr_begin[c].v))
break;
1537 ptr_begin[offset] = ptr_begin[c];
1540 ptr_begin[offset] = tmp;
1547 size_t n = ptr_end - ptr_begin;
1550 for (
size_t offset = n>>1; offset > 0;) {
1551 rb_uniform_heap_down_2(ptr_begin, --offset, n-1);
1553 for (
size_t offset = n-1; offset > 0;) {
1554 UNIFORM_SWAP(*ptr_begin, ptr_begin[offset]);
1555 rb_uniform_heap_down_2(ptr_begin, 0, --offset);
1565 if (ptr_end - ptr_begin <= 16) {
1566 rb_uniform_insertionsort_2(ptr_begin, ptr_end);
1570 rb_uniform_heapsort_2(ptr_begin, ptr_end);
1574 VALUE x = med3_val(ptr_begin->v,
1575 ptr_begin[(ptr_end - ptr_begin)>>1].v,
1581 while (uless(i->v, x)) i++;
1582 while (uless(x, j->v)) j--;
1584 UNIFORM_SWAP(*i, *j);
1590 if (ptr_end - j > 1) rb_uniform_quicksort_intro_2(j, ptr_end, d-1);
1591 if (i - ptr_begin > 1) rb_uniform_quicksort_intro_2(ptr_begin, i, d-1);
1603 size_t n = ptr_end - ptr_begin;
1604 size_t d = CHAR_BIT *
sizeof(n) - nlz_intptr(n) - 1;
1605 bool sorted_flag =
true;
1608 if (rb_uniform_is_larger((ptr-1)->v, (ptr)->v)) {
1609 sorted_flag =
false;
1617 rb_uniform_quicksort_intro_2(ptr_begin, ptr_end, d<<1);
1706enum_sort_by(
VALUE obj)
1721 RBASIC_CLEAR_CLASS(ary);
1722 buf = rb_ary_hidden_new(SORT_BY_BUFSIZE*2);
1723 rb_ary_store(buf, SORT_BY_BUFSIZE*2-1,
Qnil);
1724 memo = MEMO_NEW(0, 0, 0);
1729 data->primitive_uniformed = SORT_BY_UNIFORMED((CMP_OPTIMIZABLE(FLOAT) && CMP_OPTIMIZABLE(INTEGER)),
1730 CMP_OPTIMIZABLE(FLOAT),
1731 CMP_OPTIMIZABLE(INTEGER));
1736 rb_ary_resize(buf, data->n*2);
1737 rb_ary_concat(ary, buf);
1740 if (data->primitive_uniformed) {
1748 sort_by_cmp, (
void *)ary));
1751 if (
RBASIC(ary)->klass) {
1763#define ENUMFUNC(name) argc ? name##_eqq : rb_block_given_p() ? name##_iter_i : name##_i
1765#define ENUM_BLOCK_CALL(name) \
1766 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);
1768#define MEMO_ENUM_NEW(v1) (rb_check_arity(argc, 0, 1), MEMO_NEW((v1), (argc ? *argv : 0), 0))
1770#define DEFINE_ENUMFUNCS(name) \
1771static VALUE enum_##name##_func(VALUE result, struct MEMO *memo); \
1774name##_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1776 return enum_##name##_func(rb_enum_values_pack(argc, argv), MEMO_CAST(memo)); \
1780name##_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1782 return enum_##name##_func(rb_yield_values2(argc, argv), MEMO_CAST(memo)); \
1786name##_eqq(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
1788 ENUM_WANT_SVALUE(); \
1789 return enum_##name##_func(rb_funcallv(MEMO_CAST(memo)->v2, id_eqq, 1, &i), MEMO_CAST(memo)); \
1793enum_##name##_func(VALUE result, struct MEMO *memo)
1795#define WARN_UNUSED_BLOCK(argc) do { \
1796 if ((argc) > 0 && rb_block_given_p()) { \
1797 rb_warn("given block not used"); \
1801DEFINE_ENUMFUNCS(all)
1803 if (!
RTEST(result)) {
1804 MEMO_V1_SET(memo,
Qfalse);
1859 struct MEMO *memo = MEMO_ENUM_NEW(
Qtrue);
1860 WARN_UNUSED_BLOCK(argc);
1861 ENUM_BLOCK_CALL(all);
1865DEFINE_ENUMFUNCS(any)
1867 if (
RTEST(result)) {
1868 MEMO_V1_SET(memo,
Qtrue);
1922 WARN_UNUSED_BLOCK(argc);
1923 ENUM_BLOCK_CALL(any);
1927DEFINE_ENUMFUNCS(one)
1929 if (
RTEST(result)) {
1930 if (UNDEF_P(memo->v1)) {
1931 MEMO_V1_SET(memo,
Qtrue);
1933 else if (memo->v1 ==
Qtrue) {
1934 MEMO_V1_SET(memo,
Qfalse);
1947 int (*cmpfunc)(
const void *,
const void *,
void *);
1955 if (
RBASIC(data->buf)->klass) {
1957 data->rev ?
"max" :
"min",
1958 data->by ?
"_by" :
"");
1964nmin_cmp(
const void *ap,
const void *bp,
void *_data)
1968#define rb_cmpint(cmp, a, b) rb_cmpint(cmpint_reenter_check(data, (cmp)), a, b)
1969 return OPTIMIZED_CMP(a, b);
1974nmin_block_cmp(
const void *ap,
const void *bp,
void *_data)
1979 cmpint_reenter_check(data, cmp);
1980 return rb_cmpint(cmp, a, b);
1996 if (data->curlen <= data->n)
2001 eltsize = data->by ? 2 : 1;
2002 numelts = data->curlen;
2007#define GETPTR(i) (beg+(i)*eltsize)
2009#define SWAP(i, j) do { \
2011 memcpy(tmp, GETPTR(i), sizeof(VALUE)*eltsize); \
2012 memcpy(GETPTR(i), GETPTR(j), sizeof(VALUE)*eltsize); \
2013 memcpy(GETPTR(j), tmp, sizeof(VALUE)*eltsize); \
2017 long pivot_index = left + (right-left)/2;
2018 long num_pivots = 1;
2020 SWAP(pivot_index, right);
2021 pivot_index = right;
2025 while (i <= right-num_pivots) {
2026 int c = data->cmpfunc(GETPTR(i), GETPTR(pivot_index), data);
2030 SWAP(i, right-num_pivots);
2035 SWAP(i, store_index);
2041 for (i = right; right-num_pivots < i; i--) {
2048 if (store_index <= n && n <= store_index+num_pivots)
2051 if (n < store_index) {
2052 right = store_index-1;
2055 left = store_index+num_pivots;
2061 data->limit =
RARRAY_AREF(data->buf, store_index*eltsize);
2062 data->curlen = data->n;
2063 rb_ary_resize(data->buf, data->n * eltsize);
2075 cmpv = enum_yield(argc, i);
2079 if (!UNDEF_P(data->limit)) {
2080 int c = data->cmpfunc(&cmpv, &data->limit, data);
2088 rb_ary_push(data->buf, cmpv);
2089 rb_ary_push(data->buf, i);
2093 if (data->curlen == data->bufmax) {
2101rb_nmin_run(
VALUE obj,
VALUE num,
int by,
int rev,
int ary)
2108 rb_raise(rb_eArgError,
"negative size (%ld)", data.n);
2111 if (LONG_MAX/4/(by ? 2 : 1) < data.n)
2112 rb_raise(rb_eArgError,
"too big size");
2113 data.bufmax = data.n * 4;
2115 data.buf = rb_ary_hidden_new(data.bufmax * (by ? 2 : 1));
2117 data.cmpfunc = by ? nmin_cmp :
2141 data.cmpfunc, (
void *)&data);
2151 data.cmpfunc, (
void *)&data);
2155 rb_ary_reverse(result);
2211 WARN_UNUSED_BLOCK(argc);
2212 ENUM_BLOCK_CALL(one);
2214 if (UNDEF_P(result))
return Qfalse;
2218DEFINE_ENUMFUNCS(none)
2220 if (
RTEST(result)) {
2221 MEMO_V1_SET(memo,
Qfalse);
2270 struct MEMO *memo = MEMO_ENUM_NEW(
Qtrue);
2272 WARN_UNUSED_BLOCK(argc);
2273 ENUM_BLOCK_CALL(none);
2284 struct min_t *memo = MEMO_FOR(
struct min_t, args);
2288 if (UNDEF_P(memo->min)) {
2292 if (OPTIMIZED_CMP(i, memo->min) < 0) {
2303 struct min_t *memo = MEMO_FOR(
struct min_t, args);
2307 if (UNDEF_P(memo->min)) {
2312 if (rb_cmpint(cmp, i, memo->min) < 0) {
2381 struct min_t *m = NEW_MEMO_FOR(
struct min_t, memo);
2386 return rb_nmin_run(obj, num, 0, 0, 0);
2396 if (UNDEF_P(result))
return Qnil;
2407 struct max_t *memo = MEMO_FOR(
struct max_t, args);
2411 if (UNDEF_P(memo->max)) {
2415 if (OPTIMIZED_CMP(i, memo->max) > 0) {
2425 struct max_t *memo = MEMO_FOR(
struct max_t, args);
2430 if (UNDEF_P(memo->max)) {
2435 if (rb_cmpint(cmp, i, memo->max) > 0) {
2503 struct max_t *m = NEW_MEMO_FOR(
struct max_t, memo);
2508 return rb_nmin_run(obj, num, 0, 1, 0);
2518 if (UNDEF_P(result))
return Qnil;
2533 if (UNDEF_P(memo->min)) {
2538 n = OPTIMIZED_CMP(i, memo->min);
2542 n = OPTIMIZED_CMP(j, memo->max);
2558 if (UNDEF_P(memo->last)) {
2565 n = OPTIMIZED_CMP(j, i);
2575 minmax_i_update(i, j, memo);
2585 if (UNDEF_P(memo->min)) {
2610 if (UNDEF_P(memo->last)) {
2627 minmax_ii_update(i, j, memo);
2664enum_minmax(
VALUE obj)
2673 if (!UNDEF_P(m->last))
2674 minmax_ii_update(m->last, m->last, m);
2678 if (!UNDEF_P(m->last))
2679 minmax_i_update(m->last, m->last, m);
2681 if (!UNDEF_P(m->min)) {
2682 return rb_assoc_new(m->min, m->max);
2690 struct MEMO *memo = MEMO_CAST(args);
2695 v = enum_yield(argc, i);
2696 if (UNDEF_P(memo->v1)) {
2697 MEMO_V1_SET(memo, v);
2698 MEMO_V2_SET(memo, i);
2700 else if (OPTIMIZED_CMP(v, memo->v1) < 0) {
2701 MEMO_V1_SET(memo, v);
2702 MEMO_V2_SET(memo, i);
2753 if (argc && !
NIL_P(num = argv[0]))
2754 return rb_nmin_run(obj, num, 1, 0, 0);
2764 struct MEMO *memo = MEMO_CAST(args);
2769 v = enum_yield(argc, i);
2770 if (UNDEF_P(memo->v1)) {
2771 MEMO_V1_SET(memo, v);
2772 MEMO_V2_SET(memo, i);
2774 else if (OPTIMIZED_CMP(v, memo->v1) > 0) {
2775 MEMO_V1_SET(memo, v);
2776 MEMO_V2_SET(memo, i);
2827 if (argc && !
NIL_P(num = argv[0]))
2828 return rb_nmin_run(obj, num, 1, 1, 0);
2847 if (UNDEF_P(memo->min_bv)) {
2854 if (OPTIMIZED_CMP(v1, memo->min_bv) < 0) {
2858 if (OPTIMIZED_CMP(v2, memo->max_bv) > 0) {
2874 vi = enum_yield(argc, i);
2876 if (UNDEF_P(memo->last_bv)) {
2885 n = OPTIMIZED_CMP(vj, vi);
2900 minmax_by_i_update(vi, vj, i, j, memo);
2929enum_minmax_by(
VALUE obj)
2943 if (!UNDEF_P(m->last_bv))
2944 minmax_by_i_update(m->last_bv, m->last_bv, m->last, m->last, m);
2946 return rb_assoc_new(m->min, m->max);
2952 struct MEMO *memo = MEMO_CAST(args);
2954 if (
rb_equal(rb_enum_values_pack(argc, argv), memo->v1)) {
2955 MEMO_V2_SET(memo,
Qtrue);
2981 struct MEMO *memo = MEMO_NEW(val,
Qfalse, 0);
2990 struct vm_ifunc *ifunc = rb_current_ifunc();
2991 ifunc->data = (
const void *)rb_int_succ(index);
3025enum_each_with_index(
int argc,
VALUE *argv,
VALUE obj)
3061enum_reverse_each(
int argc,
VALUE *argv,
VALUE obj)
3068 ary = enum_to_a(argc, argv, obj);
3088 enum_yield(argc, i);
3131enum_each_entry(
int argc,
VALUE *argv,
VALUE obj)
3139add_int(
VALUE x,
long n)
3143 return rb_funcallv(x,
'+', 1, &y);
3147div_int(
VALUE x,
long n)
3151 return rb_funcallv(x, id_div, 1, &y);
3154#define dont_recycle_block_arg(arity) ((arity) == 1 || (arity) < 0)
3159 struct MEMO *memo = MEMO_CAST(m);
3160 VALUE ary = memo->v1;
3162 long size = memo->u3.cnt;
3165 rb_ary_push(ary, i);
3188 if (slice_size <= 0) rb_raise(rb_eArgError,
"invalid slice size");
3190 size = enum_size(obj, 0, 0);
3196 n = add_int(size, slice_size-1);
3197 return div_int(n, slice_size);
3228 if (size <= 0) rb_raise(rb_eArgError,
"invalid slice size");
3230 size = limit_by_enum_size(obj, size);
3232 arity = rb_block_arity();
3233 memo = MEMO_NEW(ary, dont_recycle_block_arg(arity), size);
3244 struct MEMO *memo = MEMO_CAST(args);
3245 VALUE ary = memo->v1;
3247 long size = memo->u3.cnt;
3253 rb_ary_push(ary, i);
3256 ary = rb_ary_dup(ary);
3269 if (cons_size <= 0) rb_raise(rb_eArgError,
"invalid size");
3271 size = enum_size(obj, 0, 0);
3274 n = add_int(size, 1 - cons_size);
3275 return (OPTIMIZED_CMP(n, zero) == -1) ? zero : n;
3305 if (size <= 0) rb_raise(rb_eArgError,
"invalid size");
3307 arity = rb_block_arity();
3308 if (enum_size_over_p(obj, size))
return obj;
3309 memo = MEMO_NEW(
rb_ary_new2(size), dont_recycle_block_arg(arity), size);
3344 rb_block_call(obj, id_each, 0, 0, each_with_object_i, memo);
3352 struct MEMO *memo = (
struct MEMO *)memoval;
3353 VALUE result = memo->v1;
3354 VALUE args = memo->v2;
3355 long n = memo->u3.cnt++;
3360 rb_ary_store(tmp, 0, rb_enum_values_pack(argc, argv));
3365 rb_ary_push(tmp,
Qnil);
3371 if (
NIL_P(result)) {
3372 enum_yield_array(tmp);
3375 rb_ary_push(result, tmp);
3387 return v[0] = rb_funcallv(v[1], id_next, 0, 0);
3400 struct MEMO *memo = (
struct MEMO *)memoval;
3401 VALUE result = memo->v1;
3402 VALUE args = memo->v2;
3407 rb_ary_store(tmp, 0, rb_enum_values_pack(argc, argv));
3410 rb_ary_push(tmp,
Qnil);
3417 if (UNDEF_P(v[0])) {
3421 rb_ary_push(tmp, v[0]);
3424 if (
NIL_P(result)) {
3425 enum_yield_array(tmp);
3428 rb_ary_push(result, tmp);
3514 for (i=0; i<argc; i++) {
3515 VALUE ary = rb_check_array_type(argv[i]);
3523 static const VALUE sym_each = STATIC_ID2SYM(id_each);
3525 for (i=0; i<argc; i++) {
3527 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (must respond to :each)",
3530 argv[i] = rb_funcallv(argv[i], conv, 1, &sym_each);
3534 result = rb_ary_new();
3538 memo = MEMO_NEW(result, args, 0);
3547 struct MEMO *memo = MEMO_CAST(args);
3548 rb_ary_push(memo->v1, rb_enum_values_pack(argc, argv));
3576 rb_raise(rb_eArgError,
"attempt to take negative size");
3581 memo = MEMO_NEW(result, 0,
len);
3591 rb_ary_push(ary, rb_enum_values_pack(argc, argv));
3615enum_take_while(
VALUE obj)
3628 struct MEMO *memo = MEMO_CAST(args);
3629 if (memo->u3.cnt == 0) {
3630 rb_ary_push(memo->v1, rb_enum_values_pack(argc, argv));
3665 rb_raise(rb_eArgError,
"attempt to drop negative size");
3668 result = rb_ary_new();
3669 memo = MEMO_NEW(result, 0,
len);
3678 struct MEMO *memo = MEMO_CAST(args);
3681 if (!memo->u3.state && !
RTEST(enum_yield(argc, i))) {
3682 memo->u3.state = TRUE;
3684 if (memo->u3.state) {
3685 rb_ary_push(memo->v1, i);
3721enum_drop_while(
VALUE obj)
3727 result = rb_ary_new();
3728 memo = MEMO_NEW(result, 0, FALSE);
3738 rb_ary_push(ary, argc > 1 ? i : rb_ary_new_from_values(argc, argv));
3739 enum_yield(argc, i);
3755 size = enum_size(self, args, 0);
3756 if (
NIL_P(size) || FIXNUM_ZERO_P(size))
return size;
3759 if (mul <= 0)
return INT2FIX(0);
3761 return rb_funcallv(size,
'*', 1, &n);
3801 if (!argc ||
NIL_P(nv = argv[0])) {
3806 if (n <= 0)
return Qnil;
3809 RBASIC_CLEAR_CLASS(ary);
3813 while (n < 0 || 0 < --n) {
3814 for (i=0; i<
len; i++) {
3838 v = rb_funcallv(argp->categorize, id_call, 1, &i);
3841 if (!
NIL_P(argp->prev_value)) {
3842 s = rb_assoc_new(argp->prev_value, argp->prev_elts);
3843 rb_funcallv(argp->yielder, id_lshift, 1, &s);
3844 argp->prev_value = argp->prev_elts =
Qnil;
3847 rb_funcallv(argp->yielder, id_lshift, 1, &v);
3849 else if (
NIL_P(v) || v == separator) {
3850 if (!
NIL_P(argp->prev_value)) {
3851 v = rb_assoc_new(argp->prev_value, argp->prev_elts);
3852 rb_funcallv(argp->yielder, id_lshift, 1, &v);
3853 argp->prev_value = argp->prev_elts =
Qnil;
3857 rb_raise(
rb_eRuntimeError,
"symbols beginning with an underscore are reserved");
3860 if (
NIL_P(argp->prev_value)) {
3861 argp->prev_value = v;
3865 if (
rb_equal(argp->prev_value, v)) {
3866 rb_ary_push(argp->prev_elts, i);
3869 s = rb_assoc_new(argp->prev_value, argp->prev_elts);
3870 rb_funcallv(argp->yielder, id_lshift, 1, &s);
3871 argp->prev_value = v;
3888 memo->prev_value =
Qnil;
3889 memo->prev_elts =
Qnil;
3894 if (!
NIL_P(memo->prev_elts)) {
3895 arg = rb_assoc_new(memo->prev_value, memo->prev_elts);
3896 rb_funcallv(memo->yielder, id_lshift, 1, &arg);
4006enum_chunk(
VALUE enumerable)
4035 if (!
NIL_P(argp->sep_pat))
4036 header_p = rb_funcallv(argp->sep_pat, id_eqq, 1, &i);
4038 header_p = rb_funcallv(argp->sep_pred, id_call, 1, &i);
4039 if (
RTEST(header_p)) {
4040 if (!
NIL_P(argp->prev_elts))
4041 rb_funcallv(argp->yielder, id_lshift, 1, &argp->prev_elts);
4045 if (
NIL_P(argp->prev_elts))
4048 rb_ary_push(argp->prev_elts, i);
4062 memo->sep_pred = rb_attr_get(
enumerator, id_slicebefore_sep_pred);
4064 memo->prev_elts =
Qnil;
4067 rb_block_call(enumerable, id_each, 0, 0, slicebefore_ii, arg);
4069 if (!
NIL_P(memo->prev_elts))
4070 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4235enum_slice_before(
int argc,
VALUE *argv,
VALUE enumerable)
4241 rb_error_arity(argc, 0, 0);
4267#define UPDATE_MEMO ((void)(memo = MEMO_FOR(struct sliceafter_arg, _memo)))
4274 if (
NIL_P(memo->prev_elts)) {
4278 rb_ary_push(memo->prev_elts, i);
4281 if (
NIL_P(memo->pred)) {
4282 split_p =
RTEST(rb_funcallv(memo->pat, id_eqq, 1, &i));
4286 split_p =
RTEST(rb_funcallv(memo->pred, id_call, 1, &i));
4291 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4293 memo->prev_elts =
Qnil;
4309 memo->pred = rb_attr_get(
enumerator, id_sliceafter_pred);
4310 memo->prev_elts =
Qnil;
4313 rb_block_call(enumerable, id_each, 0, 0, sliceafter_ii, arg);
4315 if (!
NIL_P(memo->prev_elts))
4316 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4357enum_slice_after(
int argc,
VALUE *argv,
VALUE enumerable)
4364 rb_raise(rb_eArgError,
"both pattern and block are given");
4391#define UPDATE_MEMO ((void)(memo = MEMO_FOR(struct slicewhen_arg, _memo)))
4398 if (UNDEF_P(memo->prev_elt)) {
4405 args[0] = memo->prev_elt;
4407 split_p =
RTEST(rb_funcallv(memo->pred, id_call, 2, args));
4414 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4419 rb_ary_push(memo->prev_elts, i);
4438 memo->pred = rb_attr_get(
enumerator, id_slicewhen_pred);
4440 memo->prev_elts =
Qnil;
4442 memo->inverted =
RTEST(rb_attr_get(
enumerator, id_slicewhen_inverted));
4444 rb_block_call(enumerable, id_each, 0, 0, slicewhen_ii, arg);
4446 if (!
NIL_P(memo->prev_elts))
4447 rb_funcallv(memo->yielder, id_lshift, 1, &memo->prev_elts);
4514enum_slice_when(
VALUE enumerable)
4580enum_chunk_while(
VALUE enumerable)
4608 memo->v = rb_fix_plus(
LONG2FIX(memo->n), memo->v);
4611 switch (
TYPE(memo->r)) {
4612 case T_RATIONAL: memo->v = rb_rational_plus(memo->r, memo->v);
break;
4624 memo->v = rb_big_plus(
LONG2NUM(memo->n), memo->v);
4632 memo->v = rb_big_plus(i, memo->v);
4638 if (UNDEF_P(memo->r)) {
4642 memo->r = rb_rational_plus(memo->r, i);
4649 memo->v = rb_funcallv(memo->v, idPLUS, 1, &i);
4664 case T_BIGNUM: x = rb_big2dbl(i);
break;
4668 memo->float_value = 0;
4669 sum_iter_some_value(i, memo);
4678 else if (! isfinite(x)) {
4679 if (isinf(x) && isinf(f) && signbit(x) != signbit(f)) {
4687 else if (isinf(f)) {
4694 if (fabs(f) >= fabs(x)) {
4710 if (memo->block_given) {
4714 if (memo->float_value) {
4715 sum_iter_Kahan_Babuska(i, memo);
4717 else switch (
TYPE(memo->v)) {
4718 default: sum_iter_some_value(i, memo);
return;
4724 case T_FIXNUM: sum_iter_fixnum(i, memo);
return;
4725 case T_BIGNUM: sum_iter_bignum(i, memo);
return;
4726 case T_RATIONAL: sum_iter_rational(i, memo);
return;
4728 sum_iter_normalize_memo(memo);
4731 memo->float_value = 1;
4732 sum_iter_Kahan_Babuska(i, memo);
4735 sum_iter_normalize_memo(memo);
4736 sum_iter_some_value(i, memo);
4753 sum_iter(rb_assoc_new(key, value), (
struct enum_sum_memo *) arg);
4773 end = rb_big_minus(end,
LONG2FIX(1));
4776 if (rb_int_ge(end, beg)) {
4778 a = rb_int_plus(rb_int_minus(end, beg),
LONG2FIX(1));
4779 a = rb_int_mul(a, rb_int_plus(end, beg));
4781 return rb_int_plus(init, a);
4840 if (!memo.block_given && !memo.float_value &&
4841 (
FIXNUM_P(beg) || RB_BIGNUM_TYPE_P(beg)) &&
4842 (
FIXNUM_P(end) || RB_BIGNUM_TYPE_P(end))) {
4843 return int_range_sum(beg, end, excl, memo.v);
4848 rb_method_basic_definition_p(
CLASS_OF(obj), id_each))
4849 hash_sum(obj, &memo);
4853 if (memo.float_value) {
4854 return DBL2NUM(memo.f + memo.c);
4858 memo.v = rb_fix_plus(
LONG2FIX(memo.n), memo.v);
4859 if (!UNDEF_P(memo.r)) {
4860 memo.v = rb_rational_plus(memo.r, memo.v);
4870 rb_hash_add_new_element(hash, i, i);
4912 ret = rb_hash_values(hash);
4913 rb_hash_clear(hash);
4923 rb_ary_push(ary, i);
4940enum_compact(
VALUE obj)
5201Init_Enumerable(
void)
5273 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 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_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.
#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.