Project

General

Profile

Feature #15976 ยป 2260.patch

fcheung (Frederick Cheung), 07/02/2019 09:02 PM

View differences:

array.c
return ary3;
}
/*
* call-seq:
* ary.overlaps?(ary2) -> true or false
*
* Returns +true+ if the intersection of +ary+ and +ary2+ is non empty, else returns false.
*
* This is equivalent to (ary & ary2).any?
*
* [ "a", "b", "c" ].overlaps?( [ "c", "d", "a" ] ) #=> true
* [ "a" ].overlaps?( ["e", "b"] ) #=> false
* [ "a" ].overlaps?( [] ) #=> false
*
* See also Array#&.
*/
static VALUE
rb_ary_and_p(VALUE ary1, VALUE ary2)
{
VALUE hash, result, v;
st_data_t vv;
long i;
ary2 = to_ary(ary2);
if (RARRAY_LEN(ary1) == 0 || RARRAY_LEN(ary2) == 0) return Qfalse;
if (RARRAY_LEN(ary1) <= SMALL_ARRAY_LEN && RARRAY_LEN(ary2) <= SMALL_ARRAY_LEN) {
for (i=0; i<RARRAY_LEN(ary1); i++) {
v = RARRAY_AREF(ary1, i);
if (rb_ary_includes_by_eql(ary2, v)) {
return Qtrue;
}
}
return Qfalse;
}
hash = ary_make_hash(ary2);
result = Qfalse;
for (i=0; i<RARRAY_LEN(ary1); i++) {
v = RARRAY_AREF(ary1, i);
vv = (st_data_t)v;
if (rb_hash_stlike_delete(hash, &vv, 0)) {
result = Qtrue;
break;
}
}
ary_recycle_hash(hash);
return result;
}
static int
ary_hash_orset(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
{
......
rb_define_method(rb_cArray, "-", rb_ary_diff, 1);
rb_define_method(rb_cArray, "&", rb_ary_and, 1);
rb_define_method(rb_cArray, "overlaps?", rb_ary_and_p, 1);
rb_define_method(rb_cArray, "|", rb_ary_or, 1);
rb_define_method(rb_cArray, "max", rb_ary_max, -1);
test/ruby/test_array.rb
assert_equal(@cls[], @cls[ 1, 2, 3 ]*64 & @cls[ 4, 5, 6 ]*64)
end
def test_overlaps
assert_equal(true, @cls[ 1, 1, 3, 5 ].overlaps?( @cls[ 1, 2, 3 ]))
assert_equal(false, @cls[ 1, 1, 3, 5 ].overlaps?( @cls[ ]))
assert_equal(false, @cls[ ].overlaps?( @cls[ 1, 2, 3 ]))
assert_equal(false, @cls[ 1, 2, 3 ].overlaps?( @cls[ 4, 5, 6 ]))
end
def test_overlaps_big_array
assert_equal(true, (@cls[ 1, 1, 3, 5 ]*64).overlaps?(@cls[ 1, 2, 3 ]*64))
assert_equal(false, (@cls[ 1, 1, 3, 5 ]*64).overlaps?(@cls[ ]))
assert_equal(false, (@cls[ ]).overlaps?(@cls[ 1, 2, 3 ]*64))
assert_equal(false, (@cls[ 1, 2, 3 ]*64).overlaps?(@cls[ 4, 5, 6 ]*64))
end
def test_MUL # '*'
assert_equal(@cls[], @cls[]*3)
assert_equal(@cls[1, 1, 1], @cls[1]*3)
array.c
/*
* call-seq:
* ary.overlaps?(ary2) -> true or false
* ary.overlap?(ary2) -> true or false
*
* Returns +true+ if the intersection of +ary+ and +ary2+ is non empty, else returns false.
*
* This is equivalent to (ary & ary2).any?
*
* [ "a", "b", "c" ].overlaps?( [ "c", "d", "a" ] ) #=> true
* [ "a" ].overlaps?( ["e", "b"] ) #=> false
* [ "a" ].overlaps?( [] ) #=> false
* [ "a", "b", "c" ].overlap?( [ "c", "d", "a" ] ) #=> true
* [ "a" ].overlap?( ["e", "b"] ) #=> false
* [ "a" ].overlap?( [] ) #=> false
*
* See also Array#&.
*/
......
rb_define_method(rb_cArray, "-", rb_ary_diff, 1);
rb_define_method(rb_cArray, "&", rb_ary_and, 1);
rb_define_method(rb_cArray, "overlaps?", rb_ary_and_p, 1);
rb_define_method(rb_cArray, "overlap?", rb_ary_and_p, 1);
rb_define_method(rb_cArray, "|", rb_ary_or, 1);
test/ruby/test_array.rb
assert_equal(@cls[], @cls[ 1, 2, 3 ]*64 & @cls[ 4, 5, 6 ]*64)
end
def test_overlaps
assert_equal(true, @cls[ 1, 1, 3, 5 ].overlaps?( @cls[ 1, 2, 3 ]))
assert_equal(false, @cls[ 1, 1, 3, 5 ].overlaps?( @cls[ ]))
assert_equal(false, @cls[ ].overlaps?( @cls[ 1, 2, 3 ]))
assert_equal(false, @cls[ 1, 2, 3 ].overlaps?( @cls[ 4, 5, 6 ]))
end
def test_overlaps_big_array
assert_equal(true, (@cls[ 1, 1, 3, 5 ]*64).overlaps?(@cls[ 1, 2, 3 ]*64))
assert_equal(false, (@cls[ 1, 1, 3, 5 ]*64).overlaps?(@cls[ ]))
assert_equal(false, (@cls[ ]).overlaps?(@cls[ 1, 2, 3 ]*64))
assert_equal(false, (@cls[ 1, 2, 3 ]*64).overlaps?(@cls[ 4, 5, 6 ]*64))
def test_overlap
assert_equal(true, @cls[ 1, 1, 3, 5 ].overlap?( @cls[ 1, 2, 3 ]))
assert_equal(false, @cls[ 1, 1, 3, 5 ].overlap?( @cls[ ]))
assert_equal(false, @cls[ ].overlap?( @cls[ 1, 2, 3 ]))
assert_equal(false, @cls[ 1, 2, 3 ].overlap?( @cls[ 4, 5, 6 ]))
end
def test_overlap_big_array
assert_equal(true, (@cls[ 1, 1, 3, 5 ]*64).overlap?(@cls[ 1, 2, 3 ]*64))
assert_equal(false, (@cls[ 1, 1, 3, 5 ]*64).overlap?(@cls[ ]))
assert_equal(false, (@cls[ ]).overlap?(@cls[ 1, 2, 3 ]*64))
assert_equal(false, (@cls[ 1, 2, 3 ]*64).overlap?(@cls[ 4, 5, 6 ]*64))
end
def test_MUL # '*'
array.c
*
* Returns +true+ if the intersection of +ary+ and +ary2+ is non empty, else returns false.
*
* This is equivalent to (ary & ary2).any?
* This is equivalent to !(ary & ary2).empty?
*
* [ "a", "b", "c" ].overlap?( [ "c", "d", "a" ] ) #=> true
* [ "a" ].overlap?( ["e", "b"] ) #=> false
test/ruby/test_array.rb
end
def test_overlap
assert_equal(true, @cls[ nil ].overlap?( @cls[ nil ]))
assert_equal(true, @cls[ 1, 1, 3, 5 ].overlap?( @cls[ 1, 2, 3 ]))
assert_equal(false, @cls[ 1, 1, 3, 5 ].overlap?( @cls[ ]))
assert_equal(false, @cls[ ].overlap?( @cls[ 1, 2, 3 ]))
    (1-1/1)