Skip to content
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
13 changes: 10 additions & 3 deletions lib/elixir/lib/range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ defmodule Range do
"""
@doc since: "1.8.0"
@spec disjoint?(t, t) :: boolean
def disjoint?(first1..last1//step1 = range1, first2..last2//step2 = range2) do
if size(range1) == 0 or size(range2) == 0 do
def disjoint?(first1..last1//step1, first2..last2//step2) do
if empty?(first1, last1, step1) or empty?(first2, last2, step2) do
true
else
{first1, last1, step1} = normalize(first1, last1, step1)
Expand Down Expand Up @@ -513,7 +513,14 @@ defmodule Range do
end
end

@compile inline: [normalize: 3]
@compile inline: [empty?: 3, normalize: 3]
defp empty?(first, last, step) do
case step > 0 do
true -> first > last
false -> first < last
end
end

defp normalize(first, last, step) when first > last,
do: {first - abs(div(first - last, step) * step), first, -step}

Expand Down
8 changes: 8 additions & 0 deletions lib/elixir/test/elixir/range_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ defmodule RangeTest do
end

describe "disjoint?" do
test "empty ranges are disjoint" do
for empty <- [10..0//1, 10..0//2, 0..10//-1, 0..10//-2, 0..-1//1] do
assert Range.disjoint?(empty, 0..10)
assert Range.disjoint?(0..10, empty)
assert Range.disjoint?(empty, empty)
end
end

test "returns true for disjoint ranges" do
assert_disjoint(1..5, 6..9)
assert_disjoint(-3..1, 2..3)
Expand Down
Loading