* enum.c (enum_inject): Implement the specialized code for sum of
integers including Bignums. * internal.h (rb_fix_plus): Declared to be usable from enum_inject. * numeric.c (rb_fix_plus): Defined. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54147 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
fe9817618d
commit
fa153d7435
@ -1,3 +1,12 @@
|
|||||||
|
Thu Mar 17 18:39:04 2016 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* enum.c (enum_inject): Implement the specialized code for sum of
|
||||||
|
integers including Bignums.
|
||||||
|
|
||||||
|
* internal.h (rb_fix_plus): Declared to be usable from enum_inject.
|
||||||
|
|
||||||
|
* numeric.c (rb_fix_plus): Defined.
|
||||||
|
|
||||||
Thu Mar 17 17:20:28 2016 Anton Davydov <antondavydov.o@gmail.com>
|
Thu Mar 17 17:20:28 2016 Anton Davydov <antondavydov.o@gmail.com>
|
||||||
|
|
||||||
* thread_sync.c: [DOC] Update documentation for Queue class
|
* thread_sync.c: [DOC] Update documentation for Queue class
|
||||||
|
48
enum.c
48
enum.c
@ -719,17 +719,45 @@ enum_inject(int argc, VALUE *argv, VALUE obj)
|
|||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
id = SYM2ID(op);
|
id = SYM2ID(op);
|
||||||
if (id == idPLUS && FIXNUM_P(v) &&
|
if (id == idPLUS) {
|
||||||
rb_method_basic_definition_p(rb_cFixnum, idPLUS)) {
|
if (FIXNUM_P(v) &&
|
||||||
long n = FIX2LONG(v);
|
rb_method_basic_definition_p(rb_cFixnum, idPLUS)) {
|
||||||
while (i < RARRAY_LEN(obj)) {
|
long n = FIX2LONG(v);
|
||||||
VALUE e = RARRAY_AREF(obj, i);
|
while (i < RARRAY_LEN(obj)) {
|
||||||
if (!FIXNUM_P(e)) break;
|
VALUE e = RARRAY_AREF(obj, i);
|
||||||
n += FIX2LONG(e); /* should not overflow long type */
|
if (!FIXNUM_P(e)) break;
|
||||||
i++;
|
n += FIX2LONG(e); /* should not overflow long type */
|
||||||
if (!FIXABLE(n)) break;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
v = LONG2NUM(n);
|
|
||||||
}
|
}
|
||||||
for (; i<RARRAY_LEN(obj); i++) {
|
for (; i<RARRAY_LEN(obj); i++) {
|
||||||
v = rb_funcall(v, id, 1, RARRAY_AREF(obj, i));
|
v = rb_funcall(v, id, 1, RARRAY_AREF(obj, i));
|
||||||
|
@ -958,6 +958,7 @@ int rb_num_negative_p(VALUE);
|
|||||||
VALUE rb_int_succ(VALUE num);
|
VALUE rb_int_succ(VALUE num);
|
||||||
VALUE rb_int_pred(VALUE num);
|
VALUE rb_int_pred(VALUE num);
|
||||||
VALUE rb_dbl_hash(double d);
|
VALUE rb_dbl_hash(double d);
|
||||||
|
VALUE rb_fix_plus(VALUE x, VALUE y);
|
||||||
|
|
||||||
#if USE_FLONUM
|
#if USE_FLONUM
|
||||||
#define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n)))
|
#define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n)))
|
||||||
|
@ -2990,6 +2990,12 @@ fix_plus(VALUE x, VALUE y)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_fix_plus(VALUE x, VALUE y)
|
||||||
|
{
|
||||||
|
return fix_plus(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* fix - numeric -> numeric_result
|
* fix - numeric -> numeric_result
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
EnvUtil.suppress_warning {require 'continuation'}
|
EnvUtil.suppress_warning {require 'continuation'}
|
||||||
require 'stringio'
|
require 'stringio'
|
||||||
|
require "rbconfig/sizeof"
|
||||||
|
|
||||||
class TestEnumerable < Test::Unit::TestCase
|
class TestEnumerable < Test::Unit::TestCase
|
||||||
def setup
|
def setup
|
||||||
@ -184,7 +185,33 @@ class TestEnumerable < Test::Unit::TestCase
|
|||||||
assert_equal(nil, @empty.inject() {9})
|
assert_equal(nil, @empty.inject() {9})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
FIXNUM_MIN = -(1 << (8 * RbConfig::SIZEOF['long'] - 2))
|
||||||
|
FIXNUM_MAX = (1 << (8 * RbConfig::SIZEOF['long'] - 2)) - 1
|
||||||
|
|
||||||
|
def test_inject_array_mul
|
||||||
|
assert_equal(nil, [].inject(:*))
|
||||||
|
assert_equal(5, [5].inject(:*))
|
||||||
|
assert_equal(35, [5, 7].inject(:*))
|
||||||
|
assert_equal(3, [].inject(3, :*))
|
||||||
|
assert_equal(15, [5].inject(3, :*))
|
||||||
|
assert_equal(105, [5, 7].inject(3, :*))
|
||||||
|
end
|
||||||
|
|
||||||
def test_inject_array_plus
|
def test_inject_array_plus
|
||||||
|
assert_equal(3, [3].inject(:+))
|
||||||
|
assert_equal(8, [3, 5].inject(:+))
|
||||||
|
assert_equal(15, [3, 5, 7].inject(:+))
|
||||||
|
assert_equal(15.0, [3, 5, 7.0].inject(:+))
|
||||||
|
assert_equal(2*FIXNUM_MAX, Array.new(2, FIXNUM_MAX).inject(:+))
|
||||||
|
assert_equal(2*(FIXNUM_MAX+1), Array.new(2, FIXNUM_MAX+1).inject(:+))
|
||||||
|
assert_equal(10*FIXNUM_MAX, Array.new(10, FIXNUM_MAX).inject(:+))
|
||||||
|
assert_equal(0, ([FIXNUM_MAX, 1, -FIXNUM_MAX, -1]*10).inject(:+))
|
||||||
|
assert_equal(FIXNUM_MAX*10, ([FIXNUM_MAX+1, -1]*10).inject(:+))
|
||||||
|
assert_equal(2*FIXNUM_MIN, Array.new(2, FIXNUM_MIN).inject(:+))
|
||||||
|
assert_equal((FIXNUM_MAX+1).to_f, [FIXNUM_MAX, 1, 0.0].inject(:+))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_inject_array_plus_redefined
|
||||||
assert_separately([], <<-"end;")
|
assert_separately([], <<-"end;")
|
||||||
class Fixnum
|
class Fixnum
|
||||||
undef :+
|
undef :+
|
||||||
@ -194,6 +221,15 @@ class TestEnumerable < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
assert_equal(0, [1,2,3].inject(:+), "[ruby-dev:49510] [Bug#12178]")
|
assert_equal(0, [1,2,3].inject(:+), "[ruby-dev:49510] [Bug#12178]")
|
||||||
end;
|
end;
|
||||||
|
assert_separately([], <<-"end;")
|
||||||
|
class Bignum
|
||||||
|
undef :+
|
||||||
|
def +(x)
|
||||||
|
0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(0, [#{FIXNUM_MAX},1,1].inject(:+))
|
||||||
|
end;
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_partition
|
def test_partition
|
||||||
|
Loading…
x
Reference in New Issue
Block a user