[前][次][番号順一覧][スレッド一覧]

ruby-changes:34337

From: nobu <ko1@a...>
Date: Fri, 13 Jun 2014 17:34:26 +0900 (JST)
Subject: [ruby-changes:34337] nobu:r46418 (trunk): array.c: combination on a shared copy

nobu	2014-06-13 17:34:17 +0900 (Fri, 13 Jun 2014)

  New Revision: 46418

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=46418

  Log:
    array.c: combination on a shared copy
    
    * array.c (rb_ary_combination): iterate on a shared copy, and use
      array of indexes instead of array of chosen objects.
      [ruby-core:63149] [Bug #9939]

  Modified files:
    trunk/ChangeLog
    trunk/array.c
    trunk/test/ruby/test_array.rb
Index: array.c
===================================================================
--- array.c	(revision 46417)
+++ array.c	(revision 46418)
@@ -4938,21 +4938,19 @@ rb_ary_combination(VALUE ary, VALUE num) https://github.com/ruby/ruby/blob/trunk/array.c#L4938
 	}
     }
     else {
-	volatile VALUE t0 = tmpbuf(n+1, sizeof(long));
-	long *stack = (long*)RSTRING_PTR(t0);
-	volatile VALUE cc = tmpary(n);
-	VALUE *chosen = RARRAY_PTR(cc);
+	VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
+	volatile VALUE t0;
+	long *stack = ALLOCV_N(long, t0, n+1);
 	long lev = 0;
 
-	MEMZERO(stack, long, n);
+	RBASIC_CLEAR_CLASS(ary0);
+	MEMZERO(stack+1, long, n);
 	stack[0] = -1;
 	for (;;) {
-	    chosen[lev] = RARRAY_AREF(ary, stack[lev+1]);
 	    for (lev++; lev < n; lev++) {
-		chosen[lev] = RARRAY_AREF(ary, stack[lev+1] = stack[lev]+1);
+		stack[lev+1] = stack[lev]+1;
 	    }
-	    rb_yield(rb_ary_new4(n, chosen));
-	    if (RBASIC(t0)->klass) {
+	    if (!yield_indexed_values(ary0, n, stack+1)) {
 		rb_raise(rb_eRuntimeError, "combination reentered");
 	    }
 	    do {
@@ -4961,8 +4959,8 @@ rb_ary_combination(VALUE ary, VALUE num) https://github.com/ruby/ruby/blob/trunk/array.c#L4959
 	    } while (stack[lev+1]+n == len+lev+1);
 	}
     done:
-	tmpbuf_discard(t0);
-	tmpary_discard(cc);
+	ALLOCV_END(t0);
+	RBASIC_SET_CLASS_RAW(ary0, rb_cArray);
     }
     return ary;
 }
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46417)
+++ ChangeLog	(revision 46418)
@@ -1,4 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
-Fri Jun 13 17:32:56 2014  Nobuyoshi Nakada  <nobu@r...>
+Fri Jun 13 17:33:14 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* array.c (rb_ary_combination): iterate on a shared copy, and use
+	  array of indexes instead of array of chosen objects.
+	  [ruby-core:63149] [Bug #9939]
 
 	* array.c (yield_indexed_values): extract from permute0(),
 	  rpermute0(), and rcombinate0().
Index: test/ruby/test_array.rb
===================================================================
--- test/ruby/test_array.rb	(revision 46417)
+++ test/ruby/test_array.rb	(revision 46418)
@@ -2291,6 +2291,15 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2291
     assert_equal(:called, (0..100).to_a.combination(50) { break :called }, "[ruby-core:29240] ... must be yielded even if 100C50 > signed integer")
   end
 
+  def test_combination_clear
+    bug9939 = '[ruby-core:63149] [Bug #9939]'
+    assert_separately([], <<-'end;')
+      100_000.times {Array.new(1000)}
+      a = [*0..100]
+      a.combination(3) {|*,x| a.clear}
+    end;
+  end
+
   def test_product2
     a = (0..100).to_a
     assert_raise(RangeError) do

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]