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

Interval with independent endpoint types #122

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "IntervalSets"
uuid = "8197267c-284f-5f27-9208-e0e47529a953"
version = "0.7.3"
version = "0.8.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ You can construct `ClosedInterval`s in a variety of ways:
julia> using IntervalSets

julia> ClosedInterval{Float64}(1,3)
1.0..3.0
1..3

julia> 0.5..2.5
0.5..2.5
Expand All @@ -44,7 +44,7 @@ julia> 1.5±1
Similarly, you can construct `OpenInterval`s and `Interval{:open,:closed}`s, and `Interval{:closed,:open}`:
```julia
julia> OpenInterval{Float64}(1,3)
1.0..3.0 (open)
1..3 (open)

julia> OpenInterval(0.5..2.5)
0.5..2.5 (open)
Expand Down
23 changes: 15 additions & 8 deletions src/IntervalSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@ export AbstractInterval, Interval, OpenInterval, ClosedInterval,
searchsorted_interval

"""
A subtype of `Domain{T}` represents a subset of type `T`, that provides `in`.
A subtype of `Domain{T}` represents a set that provides `in`. `T` is a type suitable for representing elements in the domain.
"""
abstract type Domain{T} end

"""
eltype(::Domain{T})
eltype(::Type{<:Domain{T}})

Return `T`. The `eltype`, `T`, of a `Domain` is the type that best represents elements of the domain according to the criteria chosen by the programmer who created the domain.

Note: Objects of other types may be in the domain (as determined by the `in` function) and there may not be a unique object of type `T` for each mathematical element in the domain (e.g. a real interval may be represented by a `Domain{Float64}`, but there there are not unique `Float64`s for each real number in the interval).
"""
Base.eltype(::Type{<:Domain{T}}) where T = T

Base.IteratorSize(::Type{<:Domain}) = Base.SizeUnknown()
Base.isdisjoint(a::Domain, b::Domain) = isempty(a ∩ b)

Expand Down Expand Up @@ -64,20 +74,17 @@ isclosedset(d::AbstractInterval) = isleftclosed(d) && isrightclosed(d)
"Is the interval open?"
isopenset(d::AbstractInterval) = isleftopen(d) && isrightopen(d)

eltype(::Type{AbstractInterval{T}}) where {T} = T
@pure eltype(::Type{I}) where {I<:AbstractInterval} = eltype(supertype(I))

convert(::Type{AbstractInterval}, i::AbstractInterval) = i
convert(::Type{AbstractInterval{T}}, i::AbstractInterval{T}) where T = i


ordered(a::T, b::T) where {T} = ifelse(a < b, (a, b), (b, a))
ordered(a, b) = ordered(promote(a, b)...)

checked_conversion(::Type{T}, a, b) where {T} = _checked_conversion(T, convert(T, a), convert(T, b))
_checked_conversion(::Type{T}, a::T, b::T) where {T} = a, b
_checked_conversion(::Type{Any}, a, b) = throw(ArgumentError("$a and $b promoted to type Any"))
_checked_conversion(::Type{T}, a, b) where {T} = throw(ArgumentError("$a and $b are not both of type $T"))
default_interval_eltype(left, right) = default_interval_eltype(typeof(left), typeof(right))
default_interval_eltype(TL::Type, TR::Type) = default_interval_eltype(promote_type(TL, TR))
default_interval_eltype(T::Type) = T
default_interval_eltype(T::Type{<:Number}) = float(T)

function infimum(d::AbstractInterval{T}) where T
a = leftendpoint(d)
Expand Down
43 changes: 26 additions & 17 deletions src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@ is an interval set containg `x` such that
3. `left ≤ x < right` if `L == :closed` and `R == :open`, or
4. `left < x < right` if `L == R == :open`
"""
struct Interval{L,R,T} <: TypedEndpointsInterval{L,R,T}
left::T
right::T

Interval{L,R,T}(l, r) where {L,R,T} = ((a, b) = checked_conversion(T, l, r); new{L,R,T}(a, b))
struct Interval{L,R,T,TL,TR} <: TypedEndpointsInterval{L,R,T}
left::TL
right::TR
end


"""
A `ClosedInterval(left, right)` is an interval set that includes both its upper and lower bounds. In
mathematical notation, the constructed range is `[left, right]`.
"""
const ClosedInterval{T} = Interval{:closed,:closed,T}
const ClosedInterval{T,TL,TR} = Interval{:closed,:closed,T,TL,TR}

"""
An `TypedEndpointsInterval{:open,:open}(left, right)` is an interval set that includes both its upper and lower bounds. In
mathematical notation, the constructed range is `(left, right)`.
"""
const OpenInterval{T} = Interval{:open,:open,T}
const OpenInterval{T,TL,TR} = Interval{:open,:open,T,TL,TR}

Interval{L,R,T}(i::AbstractInterval) where {L,R,T} = Interval{L,R,T}(endpoints(i)...)
Interval{L,R}(left, right) where {L,R} = Interval{L,R}(promote(left,right)...)
Interval{L,R}(left::T, right::T) where {L,R,T} = Interval{L,R,T}(left, right)
Interval{L,R,T,TL,TR}(i::AbstractInterval) where {L,R,T,TL,TR} = Interval{L,R,T,TL,TR}(endpoints(i)...)
Interval{L,R,T}(l, r) where {L,R,T} = Interval{L,R,T,typeof(l),typeof(r)}(l, r)
function Interval{L,R}(left, right) where {L,R}
T = default_interval_eltype(left, right)
if T == Any
error("Endpoints ($left, $right) of Interval were incompatible (inferred eltype was Any).")
end
Interval{L,R,T}(left, right)
end
Interval(left, right) = ClosedInterval(left, right)


Expand Down Expand Up @@ -94,10 +98,15 @@ Construct a ClosedInterval `iv` spanning the region from
±(x, y) = ClosedInterval(x - y, x + y)
±(x::CartesianIndex, y::CartesianIndex) = ClosedInterval(x-y, x+y)

show(io::IO, I::ClosedInterval) = print(io, leftendpoint(I), "..", rightendpoint(I))
show(io::IO, I::OpenInterval) = print(io, leftendpoint(I), "..", rightendpoint(I), " (open)")
show(io::IO, I::Interval{:open,:closed}) = print(io, leftendpoint(I), "..", rightendpoint(I), " (open–closed)")
show(io::IO, I::Interval{:closed,:open}) = print(io, leftendpoint(I), "..", rightendpoint(I), " (closed–open)")
function show(io::IO, I::ClosedInterval)
print(io, leftendpoint(I), "..", rightendpoint(I))
if eltype(I) != default_interval_eltype(leftendpoint(I), rightendpoint(I))
print(io, " (", eltype(I), ")")
end
end
show(io::IO, I::OpenInterval) = print(io, leftendpoint(I), "..", rightendpoint(I), " (", eltype(I), ", open)")
show(io::IO, I::Interval{:open,:closed}) = print(io, leftendpoint(I), "..", rightendpoint(I), " (", eltype(I), ", open–closed)")
show(io::IO, I::Interval{:closed,:open}) = print(io, leftendpoint(I), "..", rightendpoint(I), " (", eltype(I), ", closed–open)")

# The following are not typestable for mixed endpoint types
_left_intersect_type(::Type{Val{:open}}, ::Type{Val{L2}}, a1, a2) where L2 = a1 < a2 ? (a2,L2) : (a1,:open)
Expand Down Expand Up @@ -159,18 +168,18 @@ function _union(A::TypedEndpointsInterval{L1,R1}, B::TypedEndpointsInterval{L2,R
end

# random sampling from interval
Random.gentype(::Type{Interval{L,R,T}}) where {L,R,T} = float(T)
Random.gentype(::Type{Interval{L,R,T}}) where {L,R,T} = T
function Random.rand(rng::AbstractRNG, i::Random.SamplerTrivial{<:TypedEndpointsInterval{:closed, :closed, T}}) where T<:Real
_i = i[]
isempty(_i) && throw(ArgumentError("The interval should be non-empty."))
a,b = endpoints(_i)
t = rand(rng, float(T)) # technically this samples from [0, 1), but we still allow it with TypedEndpointsInterval{:closed, :closed} for convenience
return clamp(t*a+(1-t)*b, _i)
return convert(T, clamp(t*a+(1-t)*b, _i))
end

ClosedInterval{T}(i::AbstractUnitRange{I}) where {T,I<:Integer} = ClosedInterval{T}(minimum(i), maximum(i))
ClosedInterval(i::AbstractUnitRange{I}) where {I<:Integer} = ClosedInterval{I}(minimum(i), maximum(i))

Base.promote_rule(::Type{Interval{L,R,T1}}, ::Type{Interval{L,R,T2}}) where {L,R,T1,T2} = Interval{L,R,promote_type(T1, T2)}
Base.promote_rule(::Type{Interval{L,R,T1,TL1,TR1}}, ::Type{Interval{L,R,T2,TL2,TR2}}) where {L,R,T1,T2,TL1,TR1,TL2,TR2} = Interval{L,R,promote_type(T1, T2),promote_type(TL1,TL2),promote_type(TR1,TR2)}

float(i::Interval{L, R, T}) where {L,R,T} = Interval{L, R, float(T)}(endpoints(i)...)
Loading