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

ruby-changes:31936

From: nobu <ko1@a...>
Date: Thu, 5 Dec 2013 21:53:38 +0900 (JST)
Subject: [ruby-changes:31936] nobu:r44015 (trunk): array.c: prefer lhs elements

nobu	2013-12-05 21:53:31 +0900 (Thu, 05 Dec 2013)

  New Revision: 44015

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

  Log:
    array.c: prefer lhs elements
    
    * array.c (rb_ary_or): lhs elements are prefered, so should not
      replace with rhs elements.
    * test/ruby/test_array.rb (test_OR_in_order): import the test failed
      by r43969 from rubyspec/core/array/union_spec.rb.

  Modified files:
    trunk/ChangeLog
    trunk/array.c
    trunk/test/ruby/test_array.rb
Index: array.c
===================================================================
--- array.c	(revision 44014)
+++ array.c	(revision 44015)
@@ -4025,6 +4025,14 @@ rb_ary_and(VALUE ary1, VALUE ary2) https://github.com/ruby/ruby/blob/trunk/array.c#L4025
     return ary3;
 }
 
+static int
+ary_hash_orset(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
+{
+    if (existing) return ST_STOP;
+    *key = *value = (VALUE)arg;
+    return ST_CONTINUE;
+}
+
 /*
  *  call-seq:
  *     ary | other_ary     -> new_ary
@@ -4043,9 +4051,17 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L4051
 rb_ary_or(VALUE ary1, VALUE ary2)
 {
     VALUE hash, ary3;
+    long i;
 
     ary2 = to_ary(ary2);
-    hash = ary_add_hash(ary_make_hash(ary1), ary2);
+    hash = ary_make_hash(ary1);
+
+    for (i=0; i<RARRAY_LEN(ary2); i++) {
+	VALUE elt = RARRAY_AREF(ary2, i);
+	if (!st_update(RHASH_TBL(hash), (st_data_t)elt, ary_hash_orset, (st_data_t)elt)) {
+	    OBJ_WRITTEN(hash, Qundef, elt);
+	}
+    }
     ary3 = rb_hash_values(hash);
     ary_recycle_hash(hash);
     return ary3;
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44014)
+++ ChangeLog	(revision 44015)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Dec  5 21:53:29 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* array.c (rb_ary_or): lhs elements are prefered, so should not
+	  replace with rhs elements.
+
+	* test/ruby/test_array.rb (test_OR_in_order): import the test failed
+	  by r43969 from rubyspec/core/array/union_spec.rb.
+
 Thu Dec  5 21:05:42 2013  Koichi Sasada  <ko1@a...>
 
 	* gc.c (gc_info_decode): fix to avoid syntax error on VS2012.
Index: test/ruby/test_array.rb
===================================================================
--- test/ruby/test_array.rb	(revision 44014)
+++ test/ruby/test_array.rb	(revision 44015)
@@ -1662,6 +1662,18 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1662
     assert(c.none?(&:frozen?))
   end
 
+  def test_OR_in_order
+    obj1, obj2 = Class.new do
+      attr_reader :name
+      def initialize(name) @name = name; end
+      def inspect; "test_OR_in_order(#{@name})"; end
+      def hash; 0; end
+      def eql?(a) true; end
+      break [new("1"), new("2")]
+    end
+    assert_equal([obj1], [obj1]|[obj2])
+  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)

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

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