Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-40333] Ruby 3.1 Array#intersect? #2834

Merged
merged 5 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Compatibility:
* Fix execution order of `END` blocks and `at_exit` callbacks (#2818, @andrykonchin).
* Fix `String#casecmp?` for empty strings of different encodings (#2826, @eregon).
* Implement `Enumerable#compact` and `Enumerator::Lazy#compact` (#2733, @andrykonchin).
* Implement `Array#intersect?` (#2831, @nirvdrum).

Performance:

Expand Down
53 changes: 51 additions & 2 deletions spec/ruby/core/array/intersect_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,66 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe 'Array#intersect?' do
ruby_version_is '3.1' do # https://bugs.ruby-lang.org/issues/15198
describe 'when at least one element in two Arrays is the same' do
it 'returns true' do
[1, 2].intersect?([2, 3]).should == true
[1, 2].intersect?([2, 3, 4]).should == true
[2, 3, 4].intersect?([1, 2]).should == true
end
end

describe 'when there are no elements in common between two Arrays' do
it 'returns false' do
[1, 2].intersect?([3, 4]).should == false
[0, 1, 2].intersect?([3, 4]).should == false
[3, 4].intersect?([0, 1, 2]).should == false
[3, 4].intersect?([]).should == false
[].intersect?([0, 1, 2]).should == false
end
end

it "tries to convert the passed argument to an Array using #to_ary" do
obj = mock('[1,2,3]')
obj.should_receive(:to_ary).and_return([1, 2, 3])

[1, 2].intersect?(obj).should == true
end

it "determines equivalence between elements in the sense of eql?" do
obj1 = mock('1')
obj2 = mock('2')
obj1.stub!(:hash).and_return(0)
obj2.stub!(:hash).and_return(0)
obj1.stub!(:eql?).and_return(true)
obj2.stub!(:eql?).and_return(true)

[obj1].intersect?([obj2]).should == true

obj1 = mock('3')
obj2 = mock('4')
obj1.stub!(:hash).and_return(0)
obj2.stub!(:hash).and_return(0)
obj1.stub!(:eql?).and_return(false)
obj2.stub!(:eql?).and_return(false)

[obj1].intersect?([obj2]).should == false
end

it "does not call to_ary on array subclasses" do
[5, 6].intersect?(ArraySpecs::ToAryArray[1, 2, 5, 6]).should == true
end

it "properly handles an identical item even when its #eql? isn't reflexive" do
x = mock('x')
x.stub!(:hash).and_return(42)
x.stub!(:eql?).and_return(false) # Stubbed for clarity and latitude in implementation; not actually sent by MRI.

[x].intersect?([x]).should == true
end

it "has semantic of !(a & b).empty?" do
[].intersect?([]).should == false
[nil].intersect?([nil]).should == true
end
end
end
3 changes: 2 additions & 1 deletion spec/ruby/core/array/shared/intersection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
end

it "creates an array with elements in order they are first encountered" do
[ 1, 2, 3, 2, 5 ].send(@method, [ 5, 2, 3, 4 ]).should == [2, 3, 5]
[ 1, 2, 3, 2, 5, 6, 7, 8 ].send(@method, [ 5, 2, 3, 4 ]).should == [2, 3, 5] # array > other
[ 5, 2, 3, 4 ].send(@method, [ 1, 2, 3, 2, 5, 6, 7, 8 ]).should == [5, 2, 3] # array < other
end

it "does not modify the original Array" do
Expand Down
2 changes: 0 additions & 2 deletions spec/tags/core/array/intersect_tags.txt

This file was deleted.

16 changes: 16 additions & 0 deletions src/main/ruby/truffleruby/core/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,22 @@ def inspect
end
alias_method :to_s, :inspect

def intersect?(other)
other = Truffle::Type.coerce_to other, Array, :to_ary

shorter, longer = size > other.size ? [other, self] : [self, other]
return false if shorter.empty?

shorter_set = {}
shorter.each { |e| shorter_set[e] = true }

longer.each do |e|
return true if shorter_set.include?(e)
end

false
end

def intersection(*others)
return self & others.first if others.size == 1

Expand Down
5 changes: 5 additions & 0 deletions src/main/ruby/truffleruby/core/truffle/versioned_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ def inspect(*args, &block)
copy.inspect(*args, &block)
end

def intersect?(*args, &block)
copy = TruffleRuby.synchronized(@lock) { Array.new self }
copy.intersect?(*args, &block)
end

def intersection(*args, &block)
copy = TruffleRuby.synchronized(@lock) { Array.new self }
copy.intersection(*args, &block)
Expand Down