ruby-changes:47574
From: nobu <ko1@a...>
Date: Wed, 30 Aug 2017 14:37:05 +0900 (JST)
Subject: [ruby-changes:47574] nobu:r59689 (trunk): array.c: integer calculations
nobu 2017-08-30 14:36:59 +0900 (Wed, 30 Aug 2017) New Revision: 59689 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59689 Log: 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. Modified files: trunk/array.c trunk/test/ruby/test_array.rb Index: test/ruby/test_array.rb =================================================================== --- test/ruby/test_array.rb (revision 59688) +++ test/ruby/test_array.rb (revision 59689) @@ -1838,12 +1838,22 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1838 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1885 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1929 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1970 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2529 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 Index: array.c =================================================================== --- array.c (revision 59688) +++ array.c (revision 59689) @@ -25,8 +25,6 @@ https://github.com/ruby/ruby/blob/trunk/array.c#L25 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 https://github.com/ruby/ruby/blob/trunk/array.c#L4953 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 https://github.com/ruby/ruby/blob/trunk/array.c#L5082 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 siz https://github.com/ruby/ruby/blob/trunk/array.c#L5099 } 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 a https://github.com/ruby/ruby/blob/trunk/array.c#L5304 { 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) https://github.com/ruby/ruby/blob/trunk/array.c#L6261 rb_define_method(rb_cArray, "sum", rb_ary_sum, -1); id_random = rb_intern("random"); - id_div = rb_intern("div"); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/