summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-08-30 05:36:59 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-08-30 05:36:59 (GMT)
commit56ce54608ca53c02c125e609d1e9eeea6025c4d9 (patch)
treef9bdd55efbfe727ea2b6f836fd3760b4b8e6e44b
parent4b9aeef1fa2f32a4555f88c6e83539374a1bd8d1 (diff)
array.c: integer calculations
* array.c (rb_ary_cycle_size, descending_factorial): use rb_int_mul instead of rb_funcallv. * array.c (binomial_coefficient): use rb_int_idiv instead of rb_funcallv. * array.c (rb_ary_repeated_permutation_size): use rb_int_positive_pow. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59689 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--array.c17
-rw-r--r--test/ruby/test_array.rb49
2 files changed, 50 insertions, 16 deletions
diff --git a/array.c b/array.c
index 5a3a49f..674e354 100644
--- a/array.c
+++ b/array.c
@@ -25,8 +25,6 @@
VALUE rb_cArray;
-static ID id_div;
-
/* for OPTIMIZED_CMP: */
#define id_cmp idCmp
@@ -4955,7 +4953,7 @@ rb_ary_cycle_size(VALUE self, VALUE args, VALUE eobj)
mul = NUM2LONG(n);
if (mul <= 0) return INT2FIX(0);
n = LONG2FIX(mul);
- return rb_funcallv(rb_ary_length(self), '*', 1, &n);
+ return rb_fix_mul_fix(rb_ary_length(self), n);
}
/*
@@ -5084,7 +5082,7 @@ descending_factorial(long from, long how_many)
VALUE cnt = LONG2FIX(how_many >= 0);
while (how_many-- > 0) {
VALUE v = LONG2FIX(from--);
- cnt = rb_funcallv(cnt, '*', 1, &v);
+ cnt = rb_int_mul(cnt, v);
}
return cnt;
}
@@ -5101,7 +5099,7 @@ binomial_coefficient(long comb, long size)
}
r = descending_factorial(size, comb);
v = descending_factorial(comb, comb);
- return rb_funcallv(r, id_div, 1, &v);
+ return rb_int_idiv(r, v);
}
static VALUE
@@ -5306,14 +5304,14 @@ rb_ary_repeated_permutation_size(VALUE ary, VALUE args, VALUE eobj)
{
long n = RARRAY_LEN(ary);
long k = NUM2LONG(RARRAY_AREF(args, 0));
- VALUE v;
if (k < 0) {
return LONG2FIX(0);
}
-
- v = LONG2NUM(k);
- return rb_funcallv(LONG2NUM(n), idPow, 1, &v);
+ if (n <= 0) {
+ return LONG2FIX(!k);
+ }
+ return rb_int_positive_pow(n, (unsigned long)k);
}
/*
@@ -6263,5 +6261,4 @@ Init_Array(void)
rb_define_method(rb_cArray, "sum", rb_ary_sum, -1);
id_random = rb_intern("random");
- id_div = rb_intern("div");
}
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index e7e654d..172ae5a 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -1838,12 +1838,22 @@ class TestArray < Test::Unit::TestCase
end
def test_combination
- assert_equal(@cls[[]], @cls[1,2,3,4].combination(0).to_a)
- assert_equal(@cls[[1],[2],[3],[4]], @cls[1,2,3,4].combination(1).to_a)
- assert_equal(@cls[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], @cls[1,2,3,4].combination(2).to_a)
- assert_equal(@cls[[1,2,3],[1,2,4],[1,3,4],[2,3,4]], @cls[1,2,3,4].combination(3).to_a)
- assert_equal(@cls[[1,2,3,4]], @cls[1,2,3,4].combination(4).to_a)
- assert_equal(@cls[], @cls[1,2,3,4].combination(5).to_a)
+ a = @cls[]
+ assert_equal(1, a.combination(0).size)
+ assert_equal(0, a.combination(1).size)
+ a = @cls[1,2,3,4]
+ assert_equal(1, a.combination(0).size)
+ assert_equal(4, a.combination(1).size)
+ assert_equal(6, a.combination(2).size)
+ assert_equal(4, a.combination(3).size)
+ assert_equal(1, a.combination(4).size)
+ assert_equal(0, a.combination(5).size)
+ assert_equal(@cls[[]], a.combination(0).to_a)
+ assert_equal(@cls[[1],[2],[3],[4]], a.combination(1).to_a)
+ assert_equal(@cls[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], a.combination(2).to_a)
+ assert_equal(@cls[[1,2,3],[1,2,4],[1,3,4],[2,3,4]], a.combination(3).to_a)
+ assert_equal(@cls[[1,2,3,4]], a.combination(4).to_a)
+ assert_equal(@cls[], a.combination(5).to_a)
end
def test_product
@@ -1875,7 +1885,16 @@ class TestArray < Test::Unit::TestCase
end
def test_permutation
+ a = @cls[]
+ assert_equal(1, a.permutation(0).size)
+ assert_equal(0, a.permutation(1).size)
a = @cls[1,2,3]
+ assert_equal(1, a.permutation(0).size)
+ assert_equal(3, a.permutation(1).size)
+ assert_equal(6, a.permutation(2).size)
+ assert_equal(6, a.permutation(3).size)
+ assert_equal(0, a.permutation(4).size)
+ assert_equal(6, a.permutation.size)
assert_equal(@cls[[]], a.permutation(0).to_a)
assert_equal(@cls[[1],[2],[3]], a.permutation(1).to_a.sort)
assert_equal(@cls[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]],
@@ -1910,7 +1929,14 @@ class TestArray < Test::Unit::TestCase
end
def test_repeated_permutation
+ a = @cls[]
+ assert_equal(1, a.repeated_permutation(0).size)
+ assert_equal(0, a.repeated_permutation(1).size)
a = @cls[1,2]
+ assert_equal(1, a.repeated_permutation(0).size)
+ assert_equal(2, a.repeated_permutation(1).size)
+ assert_equal(4, a.repeated_permutation(2).size)
+ assert_equal(8, a.repeated_permutation(3).size)
assert_equal(@cls[[]], a.repeated_permutation(0).to_a)
assert_equal(@cls[[1],[2]], a.repeated_permutation(1).to_a.sort)
assert_equal(@cls[[1,1],[1,2],[2,1],[2,2]],
@@ -1944,7 +1970,15 @@ class TestArray < Test::Unit::TestCase
end
def test_repeated_combination
+ a = @cls[]
+ assert_equal(1, a.repeated_combination(0).size)
+ assert_equal(0, a.repeated_combination(1).size)
a = @cls[1,2,3]
+ assert_equal(1, a.repeated_combination(0).size)
+ assert_equal(3, a.repeated_combination(1).size)
+ assert_equal(6, a.repeated_combination(2).size)
+ assert_equal(10, a.repeated_combination(3).size)
+ assert_equal(15, a.repeated_combination(4).size)
assert_equal(@cls[[]], a.repeated_combination(0).to_a)
assert_equal(@cls[[1],[2],[3]], a.repeated_combination(1).to_a.sort)
assert_equal(@cls[[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]],
@@ -2495,6 +2529,9 @@ class TestArray < Test::Unit::TestCase
a = []
[0, 1, 2].cycle(3) {|i| a << i }
assert_equal([0, 1, 2, 0, 1, 2, 0, 1, 2], a)
+
+ assert_equal(Float::INFINITY, a.cycle.size)
+ assert_equal(27, a.cycle(3).size)
end
def test_reverse_each2