summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrajjwal Singh <sin@prajjwal.com>2019-10-07 10:42:29 +0530
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-10-07 15:59:12 +0900
commitc8542ab484efb6ee0009cd081789d9a68f482483 (patch)
treef8c00b416b4ff3c7fae2cb534338162066201b17
parentd4ec37f69e3bd6f88725fe1075daa2e5f58c2aec (diff)
Add: Array#intersection method
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2533
-rw-r--r--NEWS6
-rw-r--r--array.c31
-rw-r--r--test/ruby/test_array.rb17
3 files changed, 54 insertions, 0 deletions
diff --git a/NEWS b/NEWS
index 958385f6f9..d4e7e02710 100644
--- a/NEWS
+++ b/NEWS
@@ -129,6 +129,12 @@ sufficient information, see the ChangeLog file or Redmine
=== Core classes updates (outstanding ones only)
+Array::
+
+ New method::
+
+ * Added Array#intersection. [Feature #16155]
+
Complex::
New method::
diff --git a/array.c b/array.c
index 9235bf22e8..341edb5aaf 100644
--- a/array.c
+++ b/array.c
@@ -4668,6 +4668,36 @@ rb_ary_and(VALUE ary1, VALUE ary2)
return ary3;
}
+/*
+ * call-seq:
+ * ary.intersection(other_ary1, other_ary2, ...) -> new_ary
+ *
+ * Set Intersection --- Returns a new array containing unique elements common
+ * to +self+ and <code>other_ary</code>s. Order is preserved from the original
+ * array.
+ *
+ * It compares elements using their #hash and #eql? methods for efficiency.
+ *
+ * [ 1, 1, 3, 5 ].intersection([ 3, 2, 1 ]) # => [ 1, 3 ]
+ * [ "a", "b", "z" ].intersection([ "a", "b", "c" ], [ "b" ]) # => [ "b" ]
+ * [ "a" ].intersection #=> [ "a" ]
+ *
+ * See also Array#&.
+ */
+
+static VALUE
+rb_ary_intersection_multi(int argc, VALUE *argv, VALUE ary)
+{
+ VALUE result = rb_ary_dup(ary);
+ int i;
+
+ for (i = 0; i < argc; i++) {
+ result = rb_ary_and(result, argv[i]);
+ }
+
+ return result;
+}
+
static int
ary_hash_orset(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
{
@@ -6928,6 +6958,7 @@ Init_Array(void)
rb_define_method(rb_cArray, "concat", rb_ary_concat_multi, -1);
rb_define_method(rb_cArray, "union", rb_ary_union_multi, -1);
rb_define_method(rb_cArray, "difference", rb_ary_difference_multi, -1);
+ rb_define_method(rb_cArray, "intersection", rb_ary_intersection_multi, -1);
rb_define_method(rb_cArray, "<<", rb_ary_push, 1);
rb_define_method(rb_cArray, "push", rb_ary_push_m, -1);
rb_define_alias(rb_cArray, "append", "push");
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 955a00ad99..6d730db4ae 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -241,6 +241,23 @@ class TestArray < Test::Unit::TestCase
assert_equal(@cls[], @cls[ 1, 2, 3 ]*64 & @cls[ 4, 5, 6 ]*64)
end
+ def test_intersection
+ assert_equal(@cls[1, 2], @cls[1, 2, 3].intersection(@cls[1, 2]))
+ assert_equal(@cls[ ], @cls[1].intersection(@cls[ ]))
+ assert_equal(@cls[ ], @cls[ ].intersection(@cls[1]))
+ assert_equal(@cls[1], @cls[1, 2, 3].intersection(@cls[1, 2], @cls[1]))
+ assert_equal(@cls[ ], @cls[1, 2, 3].intersection(@cls[1, 2], @cls[3]))
+ assert_equal(@cls[ ], @cls[1, 2, 3].intersection(@cls[4, 5, 6]))
+ end
+
+ def test_intersection_big_array
+ assert_equal(@cls[1, 2], (@cls[1, 2, 3] * 64).intersection(@cls[1, 2] * 64))
+ assert_equal(@cls[ ], (@cls[1] * 64).intersection(@cls[ ]))
+ assert_equal(@cls[ ], @cls[ ].intersection(@cls[1] * 64))
+ assert_equal(@cls[1], (@cls[1, 2, 3] * 64).intersection((@cls[1, 2] * 64), (@cls[1] * 64)))
+ assert_equal(@cls[ ], (@cls[1, 2, 3] * 64).intersection(@cls[4, 5, 6] * 64))
+ end
+
def test_MUL # '*'
assert_equal(@cls[], @cls[]*3)
assert_equal(@cls[1, 1, 1], @cls[1]*3)