[#41918] [Feature #3647] Array#sample(n, replace=false) — Kenta Murata <redmine@...>

Feature #3647: Array#sample(n, replace=false)

11 messages 2010/08/03

[#41966] [Bug #3673] PTY.getpty with IO.pipe doesn't finish on FreeBSD — Yui NARUSE <redmine@...>

Bug #3673: PTY.getpty with IO.pipe doesn't finish on FreeBSD

9 messages 2010/08/10

[#41969] [Feature #3675] String#prepend, String#>> — Sora Harakami <redmine@...>

Feature #3675: String#prepend, String#>>

15 messages 2010/08/10
[#41976] Re: [Feature #3675] String#prepend, String#>> — Yukihiro Matsumoto <matz@...> 2010/08/10

まつもと ゆきひろです

[#41974] Re: [ruby-cvs:36157] Ruby:r28955 (trunk): * complex.c (nucomp_to_[ifr]): don't allow complex with in-exact — Yukihiro Matsumoto <matz@...>

まつもと ゆきひろです

7 messages 2010/08/10

[#42003] WEBrickに関するセキュリティ修正 (CVE-2010-0541) — Hideki Yamane <henrich@...>

12 messages 2010/08/11

[#42090] Math::atan2(0, 0) on ruby 1.9.2 — KUBO Takehiro <kubo@...>

久保です。

18 messages 2010/08/22
[#42092] Re: Math::atan2(0, 0) on ruby 1.9.2 — Kenta Murata <muraken@...> 2010/08/22

むらたです。

[#42166] Ruby'sライセンスの、BSDLとのデュアルライセンスへの変更 — "NARUSE, Yui" <naruse@...>

Ruby's ライセンスは BSDL と Ruby's のデュアルライセンスになります。

14 messages 2010/08/31

[ruby-dev:42072] Re: [Bug #3708] Array#permutation がおかしな結果を返す

From: Kenta Murata <muraken@...>
Date: 2010-08-18 10:35:48 UTC
List: ruby-dev #42072
むらたです。

On 2010/08/18, at 18:28, Yukihiro Matsumoto wrote:

> |diff --git a/array.c b/array.c
> |index 51d3ad2..ea4fe43 100644
> |--- a/array.c
> |+++ b/array.c
> |@@ -409,10 +409,7 @@ static VALUE
> | ary_make_shared(VALUE ary)
> | {
> |     assert(!ARY_EMBED_P(ary));
> |-    if (ARY_SHARED_P(ary)) {
> |-       return ARY_SHARED(ary);
> |-    }
> |-    else if (ARY_SHARED_ROOT_P(ary)) {
> |+    if (ARY_SHARED_P(ary) || ARY_SHARED_ROOT_P(ary)) {
> |        return ary;
> |     }
> |     else if (OBJ_FROZEN(ary)) {
> 
> なるほど。コミットして下さい。
> 


一旦これでコミットしたのですが、SEGV が発生することがあったため revert しました。

なかださんに ary_make_shared_copy を教えてもらったので、もう一度修正案を提示します。
repeated_permutation、repeated_combination、product も対象です。

diff --git a/array.c b/array.c
index 51d3ad2..bca7956 100644
--- a/array.c
+++ b/array.c
@@ -4010,7 +4010,7 @@ rb_ary_permutation(int argc, VALUE *argv, VALUE ary)
        long *p = (long*)RSTRING_PTR(t0);
        volatile VALUE t1 = tmpbuf(n,sizeof(char));
        char *used = (char*)RSTRING_PTR(t1);
-       VALUE ary0 = ary_make_substitution(ary); /* private defensive copy of ary */
+       VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
        RBASIC(ary0)->klass = 0;

        MEMZERO(used, char, n); /* initialize array */
@@ -4180,7 +4180,7 @@ rb_ary_repeated_permutation(VALUE ary, VALUE num)
     else {             /* this is the general case */
        volatile VALUE t0 = tmpbuf(r, sizeof(long));
        long *p = (long*)RSTRING_PTR(t0);
-       VALUE ary0 = ary_make_substitution(ary); /* private defensive copy of ary */
+       VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
        RBASIC(ary0)->klass = 0;

        rpermute0(n, r, p, 0, ary0); /* compute and yield repeated permutations */
@@ -4266,7 +4266,7 @@ rb_ary_repeated_combination(VALUE ary, VALUE num)
     else {
        volatile VALUE t0 = tmpbuf(n, sizeof(long));
        long *p = (long*)RSTRING_PTR(t0);
-       VALUE ary0 = ary_make_substitution(ary); /* private defensive copy of ary */
+       VALUE ary0 = ary_make_shared_copy(ary); /* private defensive copy of ary */
        RBASIC(ary0)->klass = 0;

        rcombinate0(len, n, p, 0, n, ary0); /* compute and yield repeated combinations */
@@ -4325,7 +4325,7 @@ rb_ary_product(int argc, VALUE *argv, VALUE ary)
        /* Make defensive copies of arrays; exit if any is empty */
        for (i = 0; i < n; i++) {
            if (RARRAY_LEN(arrays[i]) == 0) goto done;
-           arrays[i] = ary_make_substitution(arrays[i]);
+           arrays[i] = ary_make_shared_copy(arrays[i]);
        }
     }
     else {
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -1528,6 +1528,10 @@ class TestArray < Test::Unit::TestCase
     acc = [1,2].product(*[o]*10)
     assert_equal([1,2].product([3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4]),
                  acc)
+
+    a = []
+    [1, 2].product([0, 1, 2, 3, 4][1, 4]) {|x| a << x }
+    assert(a.all?{|x| !x.include?(0) })
   end

   def test_permutation
@@ -1574,6 +1578,9 @@ class TestArray < Test::Unit::TestCase
     a.repeated_permutation(4) {|x| b << x; a.replace(@cls[9, 8, 7, 6]) }
     assert_equal(@cls[9, 8, 7, 6], a)
     assert_equal(@cls[1, 2, 3, 4].repeated_permutation(4).to_a, b)
+
+    a = @cls[0, 1, 2, 3, 4][1, 4].repeated_permutation(2)
+    assert(a.all?{|x| !x.include?(0) })
   end

   def test_repeated_combination
@@ -1600,6 +1607,9 @@ class TestArray < Test::Unit::TestCase
     a.repeated_combination(4) {|x| b << x; a.replace(@cls[9, 8, 7, 6]) }
     assert_equal(@cls[9, 8, 7, 6], a)
     assert_equal(@cls[1, 2, 3, 4].repeated_combination(4).to_a, b)
+
+    a = @cls[0, 1, 2, 3, 4][1, 4].repeated_combination(2)
+    assert(a.all?{|x| !x.include?(0) })
   end

   def test_take


--
Kenta Murata
OpenPGP FP = 1D69 ADDE 081C 9CC2 2E54  98C1 CEFE 8AFB 6081 B062

本を書きました!!
『Ruby 逆引きレシピ』 http://www.amazon.co.jp/dp/4798119881/mrkn-22

E-mail: mrkn@mrkn.jp
twitter: http://twitter.com/mrkn/
blog: http://d.hatena.ne.jp/mrkn/

In This Thread

Prev Next