Skip to content

Add Range overlap and set operations#17081

Open
mamantoha wants to merge 13 commits into
crystal-lang:masterfrom
mamantoha:range-set-operations
Open

Add Range overlap and set operations#17081
mamantoha wants to merge 13 commits into
crystal-lang:masterfrom
mamantoha:range-set-operations

Conversation

@mamantoha

Copy link
Copy Markdown
Contributor

This PR continues the work from #15106.
Compared to that earlier PR, this version covers the edge cases raised in review comments: open-ended ranges, generic endpoint types, endpoint exclusivity, and adjacent discrete ranges using succ/pred.

It also keeps the nil return behavior for cases where union cannot produce a single continuous range or intersection has no overlap.

Fixes #14487
Fixes #14488
Fixes #10148

Comment thread spec/std/range_spec.cr
Comment thread spec/std/range_spec.cr Outdated
Comment thread spec/std/range_spec.cr Outdated
Comment thread spec/std/range_spec.cr Outdated
Comment thread spec/std/range_spec.cr Outdated
Comment thread spec/std/range_spec.cr
mamantoha and others added 6 commits June 26, 2026 14:59
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
Co-authored-by: Johannes Müller <straightshoota@gmail.com>

@jgaskins jgaskins left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I really like the simplicity here. A couple minor notes and a fix for the failing spec, but this is really good. I ran a simple benchmark and both union and overlaps? execute in 1ns on my machine.

Comment thread spec/std/range_spec.cr Outdated
Comment thread spec/std/range_spec.cr Outdated
Comment thread src/range.cr
end
end

private def adjacent_to?(other : Range) : Bool

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How bad an idea would it be to make this method public?

Comment thread src/range.cr Outdated
Comment thread src/range.cr Outdated
mamantoha and others added 5 commits June 27, 2026 17:21
Co-authored-by: Jamie Gaskins <jgaskins@hey.com>
Co-authored-by: Jamie Gaskins <jgaskins@hey.com>
Co-authored-by: Jamie Gaskins <jgaskins@hey.com>
Co-authored-by: Jamie Gaskins <jgaskins@hey.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Union and intersection of Ranges Add a method to Range to detect overlap/intersection with another Range Add Range#clamp

4 participants