Project

General

Profile

Feature #15976 ยป 2260.patch

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

View differences:

array.c
4631 4631
    return ary3;
4632 4632
}
4633 4633

  
4634
/*
4635
 *  call-seq:
4636
 *     ary.overlaps?(ary2)   -> true or false
4637
 *
4638
 *  Returns +true+ if the intersection of +ary+ and +ary2+ is non empty, else returns false. 
4639
 *
4640
 *  This is equivalent to (ary & ary2).any?
4641
 *
4642
 *     [ "a", "b", "c" ].overlaps?( [ "c", "d", "a" ] )    #=> true
4643
 *     [ "a" ].overlaps?( ["e", "b"] )                     #=> false
4644
 *     [ "a" ].overlaps?( [] )                             #=> false
4645
 *
4646
 *  See also Array#&.
4647
 */
4648

  
4649
static VALUE
4650
rb_ary_and_p(VALUE ary1, VALUE ary2)
4651
{
4652
    VALUE hash, result, v;
4653
    st_data_t vv;
4654
    long i;
4655

  
4656
    ary2 = to_ary(ary2);
4657
    if (RARRAY_LEN(ary1) == 0 || RARRAY_LEN(ary2) == 0) return Qfalse;
4658

  
4659
    if (RARRAY_LEN(ary1) <= SMALL_ARRAY_LEN && RARRAY_LEN(ary2) <= SMALL_ARRAY_LEN) {
4660
        for (i=0; i<RARRAY_LEN(ary1); i++) {
4661
            v = RARRAY_AREF(ary1, i);
4662
            if (rb_ary_includes_by_eql(ary2, v)) {
4663
                return Qtrue;
4664
            }
4665
        }
4666
        return Qfalse;
4667
    }
4668

  
4669
    hash = ary_make_hash(ary2);
4670
    result = Qfalse;
4671
    for (i=0; i<RARRAY_LEN(ary1); i++) {
4672
        v = RARRAY_AREF(ary1, i);
4673
        vv = (st_data_t)v;
4674
        if (rb_hash_stlike_delete(hash, &vv, 0)) {
4675
            result = Qtrue;
4676
            break;
4677
        }
4678
    }
4679
    ary_recycle_hash(hash);
4680

  
4681
    return result;
4682
}
4683

  
4634 4684
static int
4635 4685
ary_hash_orset(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
4636 4686
{
......
6898 6948

  
6899 6949
    rb_define_method(rb_cArray, "-", rb_ary_diff, 1);
6900 6950
    rb_define_method(rb_cArray, "&", rb_ary_and, 1);
6951
    rb_define_method(rb_cArray, "overlaps?", rb_ary_and_p, 1);
6952

  
6901 6953
    rb_define_method(rb_cArray, "|", rb_ary_or, 1);
6902 6954

  
6903 6955
    rb_define_method(rb_cArray, "max", rb_ary_max, -1);
test/ruby/test_array.rb
241 241
    assert_equal(@cls[],     @cls[ 1, 2, 3 ]*64 & @cls[ 4, 5, 6 ]*64)
242 242
  end
243 243

  
244
  def test_overlaps
245
    assert_equal(true,      @cls[ 1, 1, 3, 5 ].overlaps?( @cls[ 1, 2, 3 ]))
246
    assert_equal(false,     @cls[ 1, 1, 3, 5 ].overlaps?( @cls[ ]))
247
    assert_equal(false,     @cls[  ].overlaps?(           @cls[ 1, 2, 3 ]))
248
    assert_equal(false,     @cls[ 1, 2, 3 ].overlaps?(    @cls[ 4, 5, 6 ]))
249
  end
250

  
251
  def test_overlaps_big_array 
252
    assert_equal(true,      (@cls[ 1, 1, 3, 5 ]*64).overlaps?(@cls[ 1, 2, 3 ]*64))
253
    assert_equal(false,     (@cls[ 1, 1, 3, 5 ]*64).overlaps?(@cls[ ]))
254
    assert_equal(false,     (@cls[  ]).overlaps?(@cls[ 1, 2, 3 ]*64))
255
    assert_equal(false,     (@cls[ 1, 2, 3 ]*64).overlaps?(@cls[ 4, 5, 6 ]*64))
256
  end
257

  
244 258
  def test_MUL # '*'
245 259
    assert_equal(@cls[], @cls[]*3)
246 260
    assert_equal(@cls[1, 1, 1], @cls[1]*3)
array.c
4633 4633

  
4634 4634
/*
4635 4635
 *  call-seq:
4636
 *     ary.overlaps?(ary2)   -> true or false
4636
 *     ary.overlap?(ary2)   -> true or false
4637 4637
 *
4638 4638
 *  Returns +true+ if the intersection of +ary+ and +ary2+ is non empty, else returns false. 
4639 4639
 *
4640 4640
 *  This is equivalent to (ary & ary2).any?
4641 4641
 *
4642
 *     [ "a", "b", "c" ].overlaps?( [ "c", "d", "a" ] )    #=> true
4643
 *     [ "a" ].overlaps?( ["e", "b"] )                     #=> false
4644
 *     [ "a" ].overlaps?( [] )                             #=> false
4642
 *     [ "a", "b", "c" ].overlap?( [ "c", "d", "a" ] )    #=> true
4643
 *     [ "a" ].overlap?( ["e", "b"] )                     #=> false
4644
 *     [ "a" ].overlap?( [] )                             #=> false
4645 4645
 *
4646 4646
 *  See also Array#&.
4647 4647
 */
......
6948 6948

  
6949 6949
    rb_define_method(rb_cArray, "-", rb_ary_diff, 1);
6950 6950
    rb_define_method(rb_cArray, "&", rb_ary_and, 1);
6951
    rb_define_method(rb_cArray, "overlaps?", rb_ary_and_p, 1);
6951
    rb_define_method(rb_cArray, "overlap?", rb_ary_and_p, 1);
6952 6952

  
6953 6953
    rb_define_method(rb_cArray, "|", rb_ary_or, 1);
6954 6954

  
test/ruby/test_array.rb
241 241
    assert_equal(@cls[],     @cls[ 1, 2, 3 ]*64 & @cls[ 4, 5, 6 ]*64)
242 242
  end
243 243

  
244
  def test_overlaps
245
    assert_equal(true,      @cls[ 1, 1, 3, 5 ].overlaps?( @cls[ 1, 2, 3 ]))
246
    assert_equal(false,     @cls[ 1, 1, 3, 5 ].overlaps?( @cls[ ]))
247
    assert_equal(false,     @cls[  ].overlaps?(           @cls[ 1, 2, 3 ]))
248
    assert_equal(false,     @cls[ 1, 2, 3 ].overlaps?(    @cls[ 4, 5, 6 ]))
249
  end
250

  
251
  def test_overlaps_big_array 
252
    assert_equal(true,      (@cls[ 1, 1, 3, 5 ]*64).overlaps?(@cls[ 1, 2, 3 ]*64))
253
    assert_equal(false,     (@cls[ 1, 1, 3, 5 ]*64).overlaps?(@cls[ ]))
254
    assert_equal(false,     (@cls[  ]).overlaps?(@cls[ 1, 2, 3 ]*64))
255
    assert_equal(false,     (@cls[ 1, 2, 3 ]*64).overlaps?(@cls[ 4, 5, 6 ]*64))
244
  def test_overlap
245
    assert_equal(true,      @cls[ 1, 1, 3, 5 ].overlap?( @cls[ 1, 2, 3 ]))
246
    assert_equal(false,     @cls[ 1, 1, 3, 5 ].overlap?( @cls[ ]))
247
    assert_equal(false,     @cls[  ].overlap?(           @cls[ 1, 2, 3 ]))
248
    assert_equal(false,     @cls[ 1, 2, 3 ].overlap?(    @cls[ 4, 5, 6 ]))
249
  end
250

  
251
  def test_overlap_big_array 
252
    assert_equal(true,      (@cls[ 1, 1, 3, 5 ]*64).overlap?(@cls[ 1, 2, 3 ]*64))
253
    assert_equal(false,     (@cls[ 1, 1, 3, 5 ]*64).overlap?(@cls[ ]))
254
    assert_equal(false,     (@cls[  ]).overlap?(@cls[ 1, 2, 3 ]*64))
255
    assert_equal(false,     (@cls[ 1, 2, 3 ]*64).overlap?(@cls[ 4, 5, 6 ]*64))
256 256
  end
257 257

  
258 258
  def test_MUL # '*'
array.c
4637 4637
 *
4638 4638
 *  Returns +true+ if the intersection of +ary+ and +ary2+ is non empty, else returns false. 
4639 4639
 *
4640
 *  This is equivalent to (ary & ary2).any?
4640
 *  This is equivalent to !(ary & ary2).empty?
4641 4641
 *
4642 4642
 *     [ "a", "b", "c" ].overlap?( [ "c", "d", "a" ] )    #=> true
4643 4643
 *     [ "a" ].overlap?( ["e", "b"] )                     #=> false
test/ruby/test_array.rb
242 242
  end
243 243

  
244 244
  def test_overlap
245
    assert_equal(true,      @cls[ nil ].overlap?( @cls[ nil ]))
245 246
    assert_equal(true,      @cls[ 1, 1, 3, 5 ].overlap?( @cls[ 1, 2, 3 ]))
246 247
    assert_equal(false,     @cls[ 1, 1, 3, 5 ].overlap?( @cls[ ]))
247 248
    assert_equal(false,     @cls[  ].overlap?(           @cls[ 1, 2, 3 ]))