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 10 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
84 changes: 84 additions & 0 deletions spec/std/range_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,90 @@ 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

# TODO: This test will fail -
# In an integer setting this case is doable. We can simply check adjaceny with +-1.
# However, how do we define an adjacent range for other data types?
# Examples of adjacency ambiguity:
# - Floats: Is 4.1's upper adjacent value 4.2 or 4.11, etc.?
# - Times: Is a time's adjacent value +1 second or +1 hour, etc.?
#
# How do we define adjacency?
#
# (1..5).union(6..10).should eq(1..10) # Adjacent ranges
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the Range documentation you'd use #succ and #pred:

Ranges typically involve integers, but can be created using arbitrary objects as long as they define succ (or pred for #reverse_each), to get the next element in the range, and < and #==, to know when the range reached the end:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the Range documentation you'd use #succ and #pred:

Ranges typically involve integers, but can be created using arbitrary objects as long as they define succ (or pred for #reverse_each), to get the next element in the range, and < and #==, to know when the range reached the end:

Making this change this weekend. ✨


(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('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..t2).union(t2..t3).should eq(t1..t3) # Adjacent time ranges
(t1..t2).union(t3..t4).should eq(nil) # Disjoint 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
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
46 changes: 46 additions & 0 deletions src/range.cr
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,52 @@ struct Range(B, E)
{% end %}
end

# Returns the union of this range, and another.
# Returns 0..0 if there is no overlap.
#
# ```
# (1..5).union(5..10) # => 1..10
# (1...10).union(1..10) # => 1..10
# ```
def union(other : Range)
return nil if self.end < other.begin || other.end < self.begin
CTC97 marked this conversation as resolved.
Show resolved Hide resolved

larger = (self.end > other.end)
Range.new(
self.begin < other.begin ? self.begin : other.begin,
self.end > other.end ? self.end : other.end,
exclusive: (larger && self.excludes_end?) || (!larger && other.excludes_end?),
)
end

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

larger = (self.end > other.end)
Range.new(
self.begin > other.begin ? self.begin : other.begin,
self.end < other.end ? self.end : other.end,
exclusive:(!larger && self.excludes_end?) || (larger && other.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
Loading