Skip to content

Commit

Permalink
[GR-59866] Add Range#overlap?
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4451
  • Loading branch information
andrykonchin committed Jan 15, 2025
2 parents 9e605e0 + ccb20a8 commit e3618dc
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Compatibility:
* `Kernel#lambda` with now raises `ArgumentError` when given a non-lambda, non-literal block (#3681, @Th3-M4jor).
* Add `rb_data_define()` to define Data (#3681, @andrykonchin).
* Add `Refinement#target` (#3681, @andrykonchin).
* Add `Range#overlap?` (#3681, @andrykonchin).

Performance:

Expand Down
89 changes: 89 additions & 0 deletions spec/ruby/core/range/overlap_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require_relative '../../spec_helper'

ruby_version_is '3.3' do
describe "Range#overlap?" do
it "returns true if other Range overlaps self" do
(0..2).overlap?(1..3).should == true
(1..3).overlap?(0..2).should == true
(0..2).overlap?(0..2).should == true
(0..3).overlap?(1..2).should == true
(1..2).overlap?(0..3).should == true

('a'..'c').overlap?('b'..'d').should == true
end

it "returns false if other Range does not overlap self" do
(0..2).overlap?(3..4).should == false
(0..2).overlap?(-4..-1).should == false

('a'..'c').overlap?('d'..'f').should == false
end

it "raises TypeError when called with non-Range argument" do
-> {
(0..2).overlap?(1)
}.should raise_error(TypeError, "wrong argument type Integer (expected Range)")
end

it "returns true when beginningless and endless Ranges overlap" do
(0..2).overlap?(..3).should == true
(0..2).overlap?(..1).should == true
(0..2).overlap?(..0).should == true

(..3).overlap?(0..2).should == true
(..1).overlap?(0..2).should == true
(..0).overlap?(0..2).should == true

(0..2).overlap?(-1..).should == true
(0..2).overlap?(1..).should == true
(0..2).overlap?(2..).should == true

(-1..).overlap?(0..2).should == true
(1..).overlap?(0..2).should == true
(2..).overlap?(0..2).should == true

(0..).overlap?(2..).should == true
(..0).overlap?(..2).should == true
end

it "returns false when beginningless and endless Ranges do not overlap" do
(0..2).overlap?(..-1).should == false
(0..2).overlap?(3..).should == false

(..-1).overlap?(0..2).should == false
(3..).overlap?(0..2).should == false
end

it "returns false when Ranges are not compatible" do
(0..2).overlap?('a'..'d').should == false
end

it "return false when self is empty" do
(2..0).overlap?(1..3).should == false
(2...2).overlap?(1..3).should == false
(1...1).overlap?(1...1).should == false
(2..0).overlap?(2..0).should == false

('c'..'a').overlap?('b'..'d').should == false
('a'...'a').overlap?('b'..'d').should == false
('b'...'b').overlap?('b'...'b').should == false
('c'...'a').overlap?('c'...'a').should == false
end

it "return false when other Range is empty" do
(1..3).overlap?(2..0).should == false
(1..3).overlap?(2...2).should == false

('b'..'d').overlap?('c'..'a').should == false
('b'..'d').overlap?('c'...'c').should == false
end

it "takes into account exclusive end" do
(0...2).overlap?(2..4).should == false
(2..4).overlap?(0...2).should == false

('a'...'c').overlap?('c'..'e').should == false
('c'..'e').overlap?('a'...'c').should == false
end
end
end
14 changes: 14 additions & 0 deletions src/main/ruby/truffleruby/core/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,20 @@ def minmax(&block)
end
end

def overlap?(other)
Truffle::Type.rb_check_type(other, Range)

# ranges don't overlap
return false if Truffle::RangeOperations.greater_than?(self.begin, other.end, other.exclude_end?)
return false if Truffle::RangeOperations.greater_than?(other.begin, self.end, self.exclude_end?)

# empty range doesn't overlap any other range
return false if Truffle::RangeOperations.greater_than?(self.begin, self.end, self.exclude_end?)
return false if Truffle::RangeOperations.greater_than?(other.begin, other.end, other.exclude_end?)

true
end

def %(n)
Truffle::RangeOperations.step_no_block(self, n)
end
Expand Down
13 changes: 13 additions & 0 deletions src/main/ruby/truffleruby/core/truffle/range_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,18 @@ def self.cover?(range, value)

true
end

# MRI: empty_region_p
def self.greater_than?(from, to, to_exclusive)
return false if Primitive.nil?(from)
return false if Primitive.nil?(to)

cmp = from <=> to

return true if Primitive.nil?(cmp)
return true if cmp == 0 && to_exclusive

cmp > 0 # that's from > to
end
end
end

0 comments on commit e3618dc

Please sign in to comment.