Skip to content

Commit

Permalink
Merge pull request #6 from jishnub/intersect
Browse files Browse the repository at this point in the history
specialize intersect for SingleValuedRange
  • Loading branch information
jishnub authored May 10, 2021
2 parents 7c464b8 + 980a822 commit 461a848
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "SphericalHarmonicModes"
uuid = "0e9554e2-b38b-11e9-16d7-9d9abfec665a"
version = "0.4.9"
version = "0.4.10"

[compat]
Aqua = "0.5"
Expand Down
39 changes: 37 additions & 2 deletions src/SphericalHarmonicModes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ Base.last(x::SingleValuedRange) = x.n
Base.length(x::SingleValuedRange) = 1
Base.show(io::IO, x::SingleValuedRange) = print(io, repr(x.n), ":", repr(x.n))

function SingleValuedRange(r::AbstractUnitRange{<:Integer})
length(r) == 1 || throw(ArgumentError("range must contain only one value"))
SingleValuedRange(first(r))
end

"""
ZeroTo(l::Int)
Expand All @@ -326,6 +331,11 @@ Base.first(::ZeroTo) = 0
Base.last(r::ZeroTo) = r.l
Base.show(io::IO, r::ZeroTo) = print(io, "0:", repr(r.l))

function ZeroTo(r::AbstractUnitRange{<:Integer})
first(r) == 0 || throw(ArgumentError("range must start at zero"))
ZeroTo(last(r))
end

"""
ToZero(l::Int)
Expand All @@ -344,6 +354,11 @@ Base.first(r::ToZero) = -r.l
Base.last(::ToZero) = 0
Base.show(io::IO, r::ToZero) = print(io,"-",repr(r.l),":0")

function ToZero(r::AbstractUnitRange{<:Integer})
last(r) == 0 || throw(ArgumentError("range must end at zero"))
ToZero(-first(r))
end

"""
FullRange(l::Int)
Expand All @@ -362,13 +377,31 @@ Base.first(r::FullRange) = -r.l
Base.last(r::FullRange) = r.l
Base.show(io::IO, r::FullRange) = print(io, repr(-r.l),":",repr(r.l))

function FullRange(r::AbstractUnitRange{<:Integer})
first(r) == -last(r) || throw(ArgumentError("starting value is not the negative of the ending value"))
FullRange(last(r))
end

Base.intersect(a::FullRange, b::FullRange) = FullRange(min(maximum(a), maximum(b)))
Base.intersect(a::T, b::FullRange) where {T<:ZeroClampedRange} = T(min(a.l, b.l))
Base.intersect(a::FullRange, b::T) where {T<:ZeroClampedRange} = T(min(a.l, b.l))

Base.intersect(a::T, b::T) where {T<:ZeroClampedRange} = T(min(a.l, b.l))
Base.intersect(a::ZeroClampedRange, b::ZeroClampedRange) = ZeroTo(0)

function Base.intersect(a::SingleValuedRange, b::AbstractUnitRange{<:Integer})
first(a) in b || return nothing
return a
end
Base.intersect(b::AbstractUnitRange{<:Integer}, a::SingleValuedRange) = intersect(a, b)
function Base.intersect(a::SingleValuedRange, b::SingleValuedRange)
first(a) == first(b) || return nothing
return a
end

Base.intersect(a::SingleValuedRange, b::PartiallySpecifiedRange) = intersect(a, UnitRange(b))
Base.intersect(a::PartiallySpecifiedRange, b::SingleValuedRange) = intersect(UnitRange(a), b)

for DT in [:LM, :ML]
@eval function $DT(l_range::LT, ::Type{MT}) where {MT<:PartiallySpecifiedRange, LT<:AbstractUnitRange{<:Integer}}
ensure_nonempty(l_range)
Expand Down Expand Up @@ -424,6 +457,8 @@ true
flip(m::LM) = ML(m)
flip(m::ML) = LM(m)

Base.convert(::Type{T}, m::SHModeRange) where {T<:SHModeRange} = m isa T ? m : T(m)

function L2L1Triangle(l_min::Integer, l_max::Integer, mr::SHModeRange, args...)
Δ = last(l_range(mr))
L2L1Triangle(l_min, l_max, Δ, args...)
Expand Down Expand Up @@ -956,9 +991,9 @@ end

for DT in [:LM, :ML]
@eval function Base.intersect(mr1::$DT, mr2::$DT)
lr = intersect(l_range(mr1), l_range(mr2))
mr = intersect(m_range(mr1), m_range(mr2))
(isempty(lr) || isempty(mr)) && return nothing
lr = intersect(l_range(mr1), l_range(mr2))
(mr === nothing || lr === nothing || isempty(lr) || isempty(mr)) && return nothing
$DT(lr, mr)
end
end
Expand Down
45 changes: 37 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,33 @@ end
@test first(r) == 0
@test last(r) == 3
@test !isempty(r)

r = ZeroTo(0:2)
@test first(r) == 0
@test last(r) == 2
@test_throws ArgumentError ZeroTo(1:2)
end
@testset "FullRange" begin
r = FullRange(3)
@test first(r) == -3
@test last(r) == 3
@test !isempty(r)
end
@testset "ZeroTo" begin
r = ZeroTo(3)
@test first(r) == 0
@test last(r) == 3
@test !isempty(r)

r = FullRange(-1:1)
@test first(r) == -1
@test last(r) == 1
@test_throws ArgumentError FullRange(0:1)
end
@testset "ToZero" begin
r = ToZero(3)
@test first(r) == -3
@test last(r) == 0
@test !isempty(r)

r = ToZero(-2:0)
@test first(r) == -2
@test last(r) == 0
@test_throws ArgumentError ToZero(-2:-1)
end
@testset "SingleValuedRange" begin
n = 3
Expand All @@ -47,6 +56,11 @@ end
@test last(r) == n
@test length(r) == 1
@test !isempty(r)

r = SingleValuedRange(n:n)
@test first(r) == n
@test last(r) == n
@test_throws ArgumentError SingleValuedRange(1:2)
end
end

Expand Down Expand Up @@ -656,20 +670,24 @@ end
@testset "LM" begin
m = LM(0:1)
@test ML(m) == ML(0:1)
@test convert(ML, m) == ML(0:1)
@test flip(m) == ML(0:1)
@test LM(m) === m
@test convert(LM, m) === m
end
@testset "ML" begin
m = ML(0:1)
@test LM(m) == LM(0:1)
@test convert(LM, m) == LM(0:1)
@test flip(m) == LM(0:1)
@test ML(m) === m
@test convert(ML, m) === m
end
end

@testset "intersect" begin
@testset "same l, different m" begin
for T in [LM,ML]
for T in [LM, ML]
@eval begin
l1 = $T(3:3,0:2)
l2 = $T(3:3,1:3)
Expand Down Expand Up @@ -697,7 +715,7 @@ end
end
end
@testset "different l, same m" begin
for T in [LM,ML]
for T in [LM, ML]
@eval begin
l1 = $T(1:3,1:1)
l2 = $T(1:1,1:1)
Expand Down Expand Up @@ -746,6 +764,17 @@ end
end

@test intersect(FullRange(2), FullRange(1)) === FullRange(1)

@test intersect(SingleValuedRange(1), -1:1) === SingleValuedRange(1)
@test intersect(SingleValuedRange(1), FullRange(1)) === SingleValuedRange(1)
@test intersect(SingleValuedRange(1), ZeroTo(1)) === SingleValuedRange(1)
@test intersect(SingleValuedRange(1), 2:3) === nothing
@test intersect(-1:1, SingleValuedRange(1)) === SingleValuedRange(1)
@test intersect(FullRange(1), SingleValuedRange(1)) === SingleValuedRange(1)
@test intersect(ZeroTo(1), SingleValuedRange(1)) === SingleValuedRange(1)
@test intersect(2:3, SingleValuedRange(1)) === nothing
@test intersect(SingleValuedRange(1), SingleValuedRange(1)) === SingleValuedRange(1)
@test intersect(SingleValuedRange(1), SingleValuedRange(2)) === nothing
end

@testset "show" begin
Expand Down

2 comments on commit 461a848

@jishnub
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/36459

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.10 -m "<description of version>" 461a848d25468381bcca3a20551b0824a6d4479a
git push origin v0.4.10

Please sign in to comment.