* enum.c (ary_inject_op): Extracted from enum_inject.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
fa153d7435
commit
85473c481e
@ -1,3 +1,7 @@
|
|||||||
|
Thu Mar 17 20:55:21 2016 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* enum.c (ary_inject_op): Extracted from enum_inject.
|
||||||
|
|
||||||
Thu Mar 17 18:39:04 2016 Tanaka Akira <akr@fsij.org>
|
Thu Mar 17 18:39:04 2016 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* enum.c (enum_inject): Implement the specialized code for sum of
|
* enum.c (enum_inject): Implement the specialized code for sum of
|
||||||
|
124
enum.c
124
enum.c
@ -628,6 +628,72 @@ inject_op_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, p))
|
|||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
ary_inject_op(VALUE ary, VALUE init, VALUE op)
|
||||||
|
{
|
||||||
|
ID id;
|
||||||
|
VALUE v;
|
||||||
|
long i;
|
||||||
|
|
||||||
|
if (RARRAY_LEN(ary) == 0)
|
||||||
|
return init == Qundef ? Qnil : init;
|
||||||
|
|
||||||
|
if (init == Qundef) {
|
||||||
|
v = RARRAY_AREF(ary, 0);
|
||||||
|
i = 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
v = init;
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
id = SYM2ID(op);
|
||||||
|
if (id == idPLUS) {
|
||||||
|
if (FIXNUM_P(v) &&
|
||||||
|
rb_method_basic_definition_p(rb_cFixnum, idPLUS)) {
|
||||||
|
long n = FIX2LONG(v);
|
||||||
|
while (i < RARRAY_LEN(ary)) {
|
||||||
|
VALUE e = RARRAY_AREF(ary, i);
|
||||||
|
if (!FIXNUM_P(e)) break;
|
||||||
|
n += FIX2LONG(e); /* should not overflow long type */
|
||||||
|
i++;
|
||||||
|
if (!FIXABLE(n)) break;
|
||||||
|
}
|
||||||
|
v = LONG2NUM(n);
|
||||||
|
}
|
||||||
|
if (i < RARRAY_LEN(ary) && (FIXNUM_P(v) || RB_TYPE_P(v, T_BIGNUM)) &&
|
||||||
|
rb_method_basic_definition_p(rb_cFixnum, idPLUS) &&
|
||||||
|
rb_method_basic_definition_p(rb_cBignum, idPLUS)) {
|
||||||
|
long n = 0;
|
||||||
|
while (i < RARRAY_LEN(ary)) {
|
||||||
|
VALUE e = RARRAY_AREF(ary, i);
|
||||||
|
if (FIXNUM_P(e)) {
|
||||||
|
n += FIX2LONG(e); /* should not overflow long type */
|
||||||
|
i++;
|
||||||
|
if (!FIXABLE(n)) {
|
||||||
|
v = rb_big_plus(LONG2NUM(n), v);
|
||||||
|
n = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (RB_TYPE_P(e, T_BIGNUM)) {
|
||||||
|
v = rb_big_plus(e, v);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (n != 0) {
|
||||||
|
v = rb_fix_plus(LONG2FIX(n), v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (; i<RARRAY_LEN(ary); i++) {
|
||||||
|
v = rb_funcall(v, id, 1, RARRAY_AREF(ary, i));
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* enum.inject(initial, sym) -> obj
|
* enum.inject(initial, sym) -> obj
|
||||||
@ -706,63 +772,7 @@ enum_inject(int argc, VALUE *argv, VALUE obj)
|
|||||||
SYMBOL_P(op) &&
|
SYMBOL_P(op) &&
|
||||||
RB_TYPE_P(obj, T_ARRAY) &&
|
RB_TYPE_P(obj, T_ARRAY) &&
|
||||||
rb_method_basic_definition_p(CLASS_OF(obj), id_each)) {
|
rb_method_basic_definition_p(CLASS_OF(obj), id_each)) {
|
||||||
VALUE v;
|
return ary_inject_op(obj, init, op);
|
||||||
long i;
|
|
||||||
if (RARRAY_LEN(obj) == 0)
|
|
||||||
return init == Qundef ? Qnil : init;
|
|
||||||
if (init == Qundef) {
|
|
||||||
v = RARRAY_AREF(obj, 0);
|
|
||||||
i = 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
v = init;
|
|
||||||
i = 0;
|
|
||||||
}
|
|
||||||
id = SYM2ID(op);
|
|
||||||
if (id == idPLUS) {
|
|
||||||
if (FIXNUM_P(v) &&
|
|
||||||
rb_method_basic_definition_p(rb_cFixnum, idPLUS)) {
|
|
||||||
long n = FIX2LONG(v);
|
|
||||||
while (i < RARRAY_LEN(obj)) {
|
|
||||||
VALUE e = RARRAY_AREF(obj, i);
|
|
||||||
if (!FIXNUM_P(e)) break;
|
|
||||||
n += FIX2LONG(e); /* should not overflow long type */
|
|
||||||
i++;
|
|
||||||
if (!FIXABLE(n)) break;
|
|
||||||
}
|
|
||||||
v = LONG2NUM(n);
|
|
||||||
}
|
|
||||||
if (i < RARRAY_LEN(obj) && (FIXNUM_P(v) || RB_TYPE_P(v, T_BIGNUM)) &&
|
|
||||||
rb_method_basic_definition_p(rb_cFixnum, idPLUS) &&
|
|
||||||
rb_method_basic_definition_p(rb_cBignum, idPLUS)) {
|
|
||||||
long n = 0;
|
|
||||||
while (i < RARRAY_LEN(obj)) {
|
|
||||||
VALUE e = RARRAY_AREF(obj, i);
|
|
||||||
if (FIXNUM_P(e)) {
|
|
||||||
n += FIX2LONG(e); /* should not overflow long type */
|
|
||||||
i++;
|
|
||||||
if (!FIXABLE(n)) {
|
|
||||||
v = rb_big_plus(LONG2NUM(n), v);
|
|
||||||
n = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (RB_TYPE_P(e, T_BIGNUM)) {
|
|
||||||
v = rb_big_plus(e, v);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (n != 0) {
|
|
||||||
v = rb_fix_plus(LONG2FIX(n), v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (; i<RARRAY_LEN(obj); i++) {
|
|
||||||
v = rb_funcall(v, id, 1, RARRAY_AREF(obj, i));
|
|
||||||
}
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
memo = MEMO_NEW(init, Qnil, op);
|
memo = MEMO_NEW(init, Qnil, op);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user