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

union, intersection and overlaps? for Ranges #15106

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 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
83 changes: 83 additions & 0 deletions spec/std/range_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,89 @@ describe "Range" do
(1...5).includes?(5).should be_false
end

it "does union" do
# Int ranges
(1..5).union(3..7).should eq(1..7) # Overlapping integer ranges
(1..5).union(6..10).should eq(1..10) # Adjacent ranges
(1..5).union(10..15).should eq(nil) # Disjoint integer ranges

# Float ranges
(1.0..5.5).union(3.2..7.8).should eq(1.0..7.8) # Overlapping float ranges
(1.0..2.5).union(3.0..4.0).should eq(nil) # Non-overlapping float ranges

# String ranges
('a'..'e').union('c'..'g').should eq('a'..'g') # Overlapping string ranges
('a'..'c').union('e'..'f').should eq(nil) # Non-overlapping string ranges
('a'..'c').union('d'..'f').should eq('a'..'f') # Adjacent string ranges

# Time ranges
t1 = Time.local(2024, 10, 1)
t2 = Time.local(2024, 10, 5)
t3 = Time.local(2024, 10, 10)
t4 = Time.local(2024, 10, 15)
(t1..t2).union(t2..t3).should eq(t1..t3) # Adjacent time ranges
(t1..t2).union(t3..t4).should eq(nil) # Disjoint time ranges

# Exclusive ranges
(1...5).union(5...10).should eq(1...10) # Adjacent exclusive integer ranges
(1.0...5.5).union(3.2...7.8).should eq(1.0...7.8) # Overlapping exclusive float ranges
('a'...'e').union('c'...'g').should eq('a'...'g') # Overlapping exclusive string ranges
(t1...t2).union(t2...t3).should eq(t1...t3) # Adjacent exclusive time ranges

end

it "does intersection" do
# Int ranges
(1..5).intersection(3..7).should eq(3..5) # Overlapping integer ranges
(1..5).intersection(6..10).should eq(nil) # Non-overlapping integer ranges
(1..5).intersection(5..10).should eq(5..5)
# (1...1).intersection(0..10).should eq(nil)

# Float ranges
(1.0..5.5).intersection(3.2..7.8).should eq(3.2..5.5) # Overlapping float ranges
(1.0..2.5).intersection(3.0..4.0).should eq(nil) # Non-overlapping float ranges

# String ranges
('a'..'e').intersection('c'..'g').should eq('c'..'e') # Overlapping string ranges
('a'..'c').intersection('d'..'f').should eq(nil) # Non-overlapping string ranges

# Time ranges
t1 = Time.local(2024, 10, 1)
t2 = Time.local(2024, 10, 5)
t3 = Time.local(2024, 10, 10)
t4 = Time.local(2024, 10, 15)
(t1..t3).intersection(t2..t4).should eq(t2..t3) # Overlapping time ranges
(t1..t2).intersection(t3..t4).should eq(nil) # Non-overlapping time ranges

# Exclusive ranges
(1...5).intersection(3...7).should eq(3...5) # Overlapping exclusive integer ranges
(1.0...5.5).intersection(3.2...7.8).should eq(3.2...5.5) # Overlapping exclusive float ranges
('a'...'e').intersection('c'...'g').should eq('c'...'e') # Overlapping exclusive string ranges
(t1...t3).intersection(t2...t4).should eq(t2...t3) # Overlapping exclusive time ranges
end

it "overlaps?" do
# Int ranges
(1..5).overlaps?(3..7).should eq(true) # Overlapping integer ranges
(1..5).overlaps?(6..10).should eq(false) # Non-overlapping integer ranges

# Float ranges
(1.0..5.5).overlaps?(3.2..7.8).should eq(true) # Overlapping float ranges
(1.0..2.5).overlaps?(3.0..4.0).should eq(false) # Non-overlapping float ranges

# String ranges
('a'..'e').overlaps?('c'..'g').should eq(true) # Overlapping string ranges
('a'..'c').overlaps?('d'..'f').should eq(false) # Non-overlapping string ranges

# Time ranges
t1 = Time.local(2024, 10, 1)
t2 = Time.local(2024, 10, 5)
t3 = Time.local(2024, 10, 10)
t4 = Time.local(2024, 10, 15)
(t1..t3).overlaps?(t2..t4).should eq(true) # Overlapping time ranges
(t1..t2).overlaps?(t3..t4).should eq(false) # Non-overlapping time ranges
end

it "does to_s" do
(1...5).to_s.should eq("1...5")
(1..5).to_s.should eq("1..5")
Expand Down
48 changes: 48 additions & 0 deletions src/range.cr
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,54 @@ struct Range(B, E)
{% end %}
end

# Returns the union of this range, and another.
# Returns `nil` if there is no overlap.
#
# ```
# (1..5).union(5..10) # => 1..10
# (1...10).union(1..10) # => 1..10
# ```
def union(other : Range)
e_s : E = self.end
e_o : E = other.end

return if self.end < other.begin && (!e_s.responds_to?(:succ) || e_s.succ != other.begin)
return if other.end < self.begin && (!e_o.responds_to?(:succ) || e_o.succ != self.begin)

Range.new(
Math.min(self.begin, other.begin),
Math.max(self.end, other.end),
exclusive: ({self, other}.max_by(&.end).excludes_end?),
CTC97 marked this conversation as resolved.
Show resolved Hide resolved
)
end

# Returns the intersection of this range, and another.
# Returns `nil` if there is no overlap.
#
# ```
# (2..10).intersection(0..8) # => 2..8
# (1...10).intersection(7..12) # => 7...10
# ```
def intersection(other : Range)
return if self.end < other.begin || other.end < self.begin

Range.new(
Math.max(self.begin, other.begin),
Math.min(self.end, other.end),
exclusive: ({self, other}.max_by(&.end).excludes_end?),
CTC97 marked this conversation as resolved.
Show resolved Hide resolved
)
end

# Returns `true` if this range overlaps with another range.
#
# ```
# (1..10).overlaps?(5..9) # => true
# (1...10).overlaps?(10..11) # => false
# ```
def overlaps?(other : Range) : Bool
!(self.end <= other.begin || other.end <= self.begin)
end

# Returns `true` if this range excludes the *end* element.
#
# ```
Expand Down