From 77a6c82eabe113802a52bf77b8b8a0bcfe1f28f4 Mon Sep 17 00:00:00 2001 From: matz Date: Tue, 2 Oct 2007 03:33:53 +0000 Subject: [PATCH] * array.c (rb_ary_product): generalized product, now takes arbitrary number of arrays. a patch from David Flanagan . [ruby-core:12346] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13598 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++ array.c | 72 +++++++++++++++++++++++++++++++++-------- test/ruby/test_array.rb | 6 ++++ 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index d3c1358828..50094435d6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue Oct 2 12:30:40 2007 Yukihiro Matsumoto + + * array.c (rb_ary_product): generalized product, now takes + arbitrary number of arrays. a patch from David Flanagan + . [ruby-core:12346] + Tue Oct 2 08:25:50 2007 Yukihiro Matsumoto * array.c (rb_ary_permutation): implementation contributed from diff --git a/array.c b/array.c index 801f2e6c2d..2d99d14956 100644 --- a/array.c +++ b/array.c @@ -2967,7 +2967,8 @@ rb_ary_cycle(VALUE ary) * values: the Ruby array that holds the actual values to permute */ static void -permute0(long n, long r, long p[], long index, int used[], VALUE values) { +permute0(long n, long r, long *p, long index, int *used, VALUE values) +{ long i,j; for(i = 0; i < n; i++) { if (used[i] == 0) { @@ -3132,30 +3133,73 @@ rb_ary_combination(VALUE ary, VALUE num) /* * call-seq: - * ary.product(ary2) + * ary.product(other_ary, ...) * - * Returns an array of all combinations of elements from both arrays. - * - * [1,2,3].product([4,5]) #=> [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]] - * [1,2].product([1,2]) #=> [[1,1],[1,2],[2,1],[2,2]] + * Returns an array of all combinations of elements from all arrays. + * The length of the returned array is the product of the length + * of ary and the argument arrays * + * [1,2,3].product([4,5]) # => [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]] + * [1,2].product([1,2]) # => [[1,1],[1,2],[2,1],[2,2]] + * [1,2].product([3,4],[5,6]) # => [[1,3,5],[1,3,6],[1,4,5],[1,4,6], + * # [2,3,5],[2,3,6],[2,4,5],[2,4,6]] + * [1,2].product() # => [[1],[2]] + * [1,2].product([]) # => [] */ static VALUE -rb_ary_product(VALUE ary, VALUE a2) +rb_ary_product(int argc, VALUE *argv, VALUE ary) { - VALUE result = rb_ary_new2(RARRAY_LEN(ary)); - long i, j; + int n = argc+1; /* How many arrays we're operating on */ + VALUE arrays[n]; /* The arrays we're computing the product of */ + int counters[n]; /* The current position in each one */ + VALUE result; /* The array we'll be returning */ + long i,j; - for (i=0; i= 0 && counters[m] == RARRAY_LEN(arrays[m])) { + counters[m] = 0; + m--; + counters[m]++; } } + return result; } + + /* Arrays are ordered, integer-indexed collections of any object. * Array indexing starts at 0, as in C or Java. A negative index is * assumed to be relative to the end of the array---that is, an index of -1 @@ -3256,7 +3300,7 @@ Init_Array(void) rb_define_method(rb_cArray, "cycle", rb_ary_cycle, 0); rb_define_method(rb_cArray, "permutation", rb_ary_permutation, 1); rb_define_method(rb_cArray, "combination", rb_ary_combination, 1); - rb_define_method(rb_cArray, "product", rb_ary_product, 1); + rb_define_method(rb_cArray, "product", rb_ary_product, -1); id_cmp = rb_intern("<=>"); } diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 2e9c6fca57..6787d9c99c 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1190,6 +1190,12 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]], @cls[1,2,3].product([4,5])) assert_equal(@cls[[1,1],[1,2],[2,1],[2,2]], @cls[1,2].product([1,2])) + + assert_equal(@cls[[1,3,5],[1,3,6],[1,4,5],[1,4,6], + [2,3,5],[2,3,6],[2,4,5],[2,4,6]], + @cls[1,2].product([3,4],[5,6])) + assert_equal(@cls[[1],[2]], @cls[1,2].product) + assert_equal(@cls[], @cls[1,2].product([])) end def test_permutation