24 #define id_each idEach 27 #define id_lshift idLTLT 28 #define id_call idCall 29 #define id_size idSize 34 if (argc == 0)
return Qnil;
35 if (argc == 1)
return argv[0];
39 #define ENUM_WANT_SVALUE() do { \ 40 i = rb_enum_values_pack(argc, argv); \ 54 enum_yield_array(
VALUE ary)
213 rb_warn(
"given block not used");
274 if (!
NIL_P(if_none)) {
336 func = find_index_iter_i;
341 rb_warn(
"given block not used");
369 limit_by_enum_size(
VALUE obj,
long n)
375 return ((
unsigned long)n > limit) ? (long)limit : n;
379 enum_size_over_p(
VALUE obj,
long n)
383 return ((
unsigned long)n >
FIX2ULONG(size));
407 enum_find_all(
VALUE obj)
448 enum_reject(
VALUE obj)
495 enum_collect(
VALUE obj)
498 int min_argc, max_argc;
544 enum_flat_map(
VALUE obj)
583 VALUE key_value_pair;
587 if (
NIL_P(key_value_pair)) {
778 op =
id ?
ID2SYM(
id) : init;
792 if (iter == inject_op_i &&
796 return ary_inject_op(obj, init, op);
838 enum_partition(
VALUE obj)
858 group = enum_yield(
argc, i);
886 enum_group_by(
VALUE obj)
936 return enum_take(obj,
argv[0]);
976 #define SORT_BY_BUFSIZE 16 992 v = enum_yield(
argc, i);
1012 sort_by_cmp(
const void *ap,
const void *
bp,
void *data)
1019 if (
RBASIC(ary)->klass) {
1104 enum_sort_by(
VALUE obj)
1138 sort_by_cmp, (
void *)ary));
1140 if (
RBASIC(ary)->klass) {
1153 #define ENUMFUNC(name) rb_block_given_p() ? name##_iter_i : name##_i 1155 #define DEFINE_ENUMFUNCS(name) \ 1156 static VALUE enum_##name##_func(VALUE result, struct MEMO *memo); \ 1159 name##_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \ 1161 return enum_##name##_func(rb_enum_values_pack(argc, argv), MEMO_CAST(memo)); \ 1165 name##_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \ 1167 return enum_##name##_func(rb_yield_values2(argc, argv), MEMO_CAST(memo)); \ 1171 enum_##name##_func(VALUE result, struct MEMO *memo) 1175 if (!
RTEST(result)) {
1210 if (
RTEST(result)) {
1245 if (
RTEST(result)) {
1246 if (memo->v1 ==
Qundef) {
1249 else if (memo->v1 ==
Qtrue) {
1263 int (*cmpfunc)(
const void *,
const void *,
void *);
1279 nmin_cmp(
const void *ap,
const void *bp,
void *_data)
1284 #define rb_cmpint(cmp, a, b) rb_cmpint(cmpint_reenter_check(data, (cmp)), a, b) 1290 nmin_block_cmp(
const void *ap,
const void *bp,
void *_data)
1295 cmpint_reenter_check(data, cmp);
1317 eltsize = data->
by ? 2 : 1;
1323 #define GETPTR(i) (beg+(i)*eltsize) 1325 #define SWAP(i, j) do { \ 1327 memcpy(tmp, GETPTR(i), sizeof(VALUE)*eltsize); \ 1328 memcpy(GETPTR(i), GETPTR(j), sizeof(VALUE)*eltsize); \ 1329 memcpy(GETPTR(j), tmp, sizeof(VALUE)*eltsize); \ 1333 long pivot_index = left + (right-left)/2;
1334 long num_pivots = 1;
1336 SWAP(pivot_index, right);
1337 pivot_index = right;
1341 while (i <= right-num_pivots) {
1346 SWAP(i, right-num_pivots);
1351 SWAP(i, store_index);
1357 for (i = right; right-num_pivots < i; i--) {
1364 if (store_index <= n && n <= store_index+num_pivots)
1367 if (n < store_index) {
1368 right = store_index-1;
1371 left = store_index+num_pivots;
1391 cmpv = enum_yield(argc, i);
1433 data.
cmpfunc = by ? nmin_cmp :
1438 data.
method = rev ? (by ?
"max_by" :
"max")
1439 : (by ?
"min_by" :
"min");
1445 nmin_i(obj, (
VALUE*)&data, 1, args);
1507 if (
RTEST(result)) {
1531 enum_none(
VALUE obj)
1781 minmax_i_update(i, j, memo);
1833 minmax_ii_update(i, j, memo);
1854 enum_minmax(
VALUE obj)
1866 minmax_ii_update(m->
last, m->
last, m);
1871 minmax_i_update(m->
last, m->
last, m);
1888 v = enum_yield(argc, i);
1924 enum_min_by(
int argc,
VALUE *argv,
VALUE obj)
1950 v = enum_yield(argc, i);
2031 enum_max_by(
int argc,
VALUE *argv,
VALUE obj)
2090 vi = enum_yield(argc, i);
2116 minmax_by_i_update(vi, vj, i, j, memo);
2137 enum_minmax_by(
VALUE obj)
2221 enum_each_with_index(
int argc,
VALUE *argv,
VALUE obj)
2252 enum_reverse_each(
int argc,
VALUE *argv,
VALUE obj)
2259 ary = enum_to_a(argc, argv, obj);
2273 enum_yield(argc, i);
2307 enum_each_entry(
int argc,
VALUE *argv,
VALUE obj)
2315 add_int(
VALUE x,
long n)
2323 div_int(
VALUE x,
long n)
2330 #define dont_recycle_block_arg(arity) ((arity) == 1 || (arity) < 0) 2364 size = enum_size(obj, 0, 0);
2367 n = add_int(size, slice_size-1);
2368 return div_int(n, slice_size);
2397 size = limit_by_enum_size(obj, size);
2439 size = enum_size(obj, 0, 0);
2442 n = add_int(size, 1 - cons_size);
2477 if (enum_size_over_p(obj, size))
return Qnil;
2518 struct MEMO *memo = (
struct MEMO *)memoval;
2521 long n = memo->
u3.
cnt++;
2537 if (
NIL_P(result)) {
2538 enum_yield_array(tmp);
2564 struct MEMO *memo = (
struct MEMO *)memoval;
2588 if (
NIL_P(result)) {
2589 enum_yield_array(tmp);
2639 for (i=0; i<
argc; i++) {
2650 for (i=0; i<
argc; i++) {
2655 argv[i] =
rb_funcallv(argv[i], conv, 1, &sym_each);
2733 enum_take_while(
VALUE obj)
2747 if (memo->
u3.
cnt == 0) {
2818 enum_drop_while(
VALUE obj)
2836 enum_yield(argc, i);
2854 if (mul <= 0)
return INT2FIX(0);
2895 if (n <= 0)
return Qnil;
2901 if (len == 0)
return Qnil;
2902 while (n < 0 || 0 < --n) {
2903 for (i=0; i<
len; i++) {
2938 else if (
NIL_P(v) || v == separator) {
3079 enum_chunk(
VALUE enumerable)
3088 rb_block_call(enumerator, idInitialize, 0, 0, chunk_i, enumerator);
3112 if (
RTEST(header_p)) {
3292 enum_slice_before(
int argc,
VALUE *argv,
VALUE enumerable)
3309 rb_block_call(enumerator, idInitialize, 0, 0, slicebefore_i, enumerator);
3324 #define UPDATE_MEMO ((void)(memo = MEMO_FOR(struct sliceafter_arg, _memo))) 3414 enum_slice_after(
int argc,
VALUE *argv,
VALUE enumerable)
3433 rb_block_call(enumerator, idInitialize, 0, 0, sliceafter_i, enumerator);
3448 #define UPDATE_MEMO ((void)(memo = MEMO_FOR(struct slicewhen_arg, _memo))) 3571 enum_slice_when(
VALUE enumerable)
3583 rb_block_call(enumerator, idInitialize, 0, 0, slicewhen_i, enumerator);
3637 enum_chunk_while(
VALUE enumerable)
3649 rb_block_call(enumerator, idInitialize, 0, 0, slicewhen_i, enumerator);
3664 const int unused = (
assert(memo !=
NULL), 0);
3742 if (fabs(f) >= fabs(x))
3858 return int_range_sum(beg, end, excl, memo.
v);
3864 hash_sum(obj, &memo);
3868 if (memo.float_value) {
3869 return DBL2NUM(memo.f + memo.c);
3914 enum_uniq(
VALUE obj)
3942 #define rb_intern(str) rb_intern_const(str) #define NEW_PARTIAL_MEMO_FOR(type, value, member)
VALUE rb_int_plus(VALUE x, VALUE y)
#define RBASIC_CLEAR_CLASS(obj)
int(* cmpfunc)(const void *, const void *, void *)
ID rb_check_id(volatile VALUE *)
Returns ID for the given name if it is interned already, or 0.
#define MEMO_FOR(type, value)
void rb_warn(const char *fmt,...)
int rb_block_min_max_arity(int *max)
VALUE rb_yield_values(int n,...)
int rb_block_given_p(void)
Determines if the current method is given a block.
VALUE rb_yield_force_blockarg(VALUE values)
#define NEW_MEMO_FOR(type, value)
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_ary_shift(VALUE ary)
int rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val)
VALUE rb_big_plus(VALUE x, VALUE y)
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
VALUE rb_ary_push(VALUE ary, VALUE item)
VALUE rb_ary_tmp_new(long capa)
VALUE rb_int_minus(VALUE x, VALUE y)
struct cmp_opt_data cmp_opt
#define RBASIC_SET_CLASS(obj, cls)
VALUE rb_int_mul(VALUE x, VALUE y)
VALUE rb_ivar_get(VALUE, ID)
VALUE rb_ary_clear(VALUE ary)
#define OPTIMIZED_CMP(a, b, data)
#define DEFINE_ENUMFUNCS(name)
VALUE rb_obj_alloc(VALUE)
Allocates an instance of klass.
VALUE rb_block_call(VALUE, ID, int, const VALUE *, rb_block_call_func_t, VALUE)
struct cmp_opt_data cmp_opt
double rb_big2dbl(VALUE x)
#define MEMO_NEW(a, b, c)
VALUE rb_rescue2(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2,...)
An equivalent of rescue clause.
#define MEMO_V2_SET(m, v)
void Init_Enumerable(void)
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
#define ENUM_WANT_SVALUE()
#define RBASIC_SET_CLASS_RAW(obj, cls)
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
#define RB_TYPE_P(obj, type)
VALUE rb_block_call_func(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
VALUE rb_int_idiv(VALUE x, VALUE y)
#define dont_recycle_block_arg(arity)
VALUE rb_equal(VALUE, VALUE)
call-seq: obj === other -> true or false
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
VALUE rb_int_ge(VALUE x, VALUE y)
void rb_ary_store(VALUE ary, long idx, VALUE val)
VALUE rb_nmin_run(VALUE obj, VALUE num, int by, int rev, int ary)
VALUE rb_big_minus(VALUE x, VALUE y)
VALUE rb_funcallv_public(VALUE, ID, int, const VALUE *)
Calls a method.
#define RARRAY_CONST_PTR(a)
#define RARRAY_PTR_USE(ary, ptr_name, expr)
#define MEMO_V1_SET(m, v)
int rb_obj_respond_to(VALUE, ID, int)
VALUE rb_hash_values(VALUE hash)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
VALUE rb_ivar_set(VALUE, ID, VALUE)
VALUE rb_assoc_new(VALUE car, VALUE cdr)
VALUE rb_fix_plus(VALUE x, VALUE y)
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
VALUE rb_hash_clear(VALUE hash)
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
RUBY_EXTERN VALUE rb_cInteger
VALUE rb_f_send(int argc, VALUE *argv, VALUE recv)
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
#define rb_cmpint(cmp, a, b)
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
#define RB_FLOAT_TYPE_P(obj)
const char * rb_builtin_class_name(VALUE x)
int rb_respond_to(VALUE, ID)
register unsigned int len
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
struct cmp_opt_data cmp_opt
VALUE rb_rational_plus(VALUE self, VALUE other)
#define RB_OBJ_WRITE(a, slot, b)
VALUE rb_ary_resize(VALUE ary, long len)
expands or shrinks ary to len elements.
#define RARRAY_ASET(a, i, v)
VALUE rb_yield_values2(int n, const VALUE *argv)
VALUE rb_enum_values_pack(int argc, const VALUE *argv)
#define RARRAY_AREF(a, i)
#define NEW_CMP_OPT_MEMO(type, value)
VALUE rb_block_proc(void)
VALUE rb_check_array_type(VALUE ary)
VALUE rb_hash_aref(VALUE hash, VALUE key)
void rb_error_arity(int argc, int min, int max)
void rb_thread_check_ints(void)
void rb_warning(const char *fmt,...)
#define STATIC_ID2SYM(id)
int rb_method_basic_definition_p(VALUE, ID)
VALUE rb_ary_sort_bang(VALUE ary)
VALUE rb_ary_dup(VALUE ary)
VALUE rb_ary_concat(VALUE x, VALUE y)
VALUE rb_lambda_call(VALUE obj, ID mid, int argc, const VALUE *argv, rb_block_call_func_t bl_proc, int min_argc, int max_argc, VALUE data2)
#define RETURN_ENUMERATOR(obj, argc, argv)
double rb_num2dbl(VALUE)
Converts a Numeric object to double.
VALUE rb_ary_reverse(VALUE ary)
#define CONST_ID(var, str)
VALUE rb_define_module(const char *name)
#define RB_INTEGER_TYPE_P(obj)
VALUE rb_check_funcall_default(VALUE, ID, int, const VALUE *, VALUE)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_attr_get(VALUE, ID)