Skip to content

Commit

Permalink
Restrict the some method arguments to reduce invalidations
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloferz committed Oct 1, 2020
1 parent c37cbb7 commit fe0a755
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/Intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ using Dates: AbstractDateTime, value, coarserperiod

import Base: , , , , union, union!, merge

const CharInts = Union{Char, Integer}
const DatesTypes = Union{Period, TimeType}
const OrderedBaseTypes = Union{Char, Number, DatesTypes}

abstract type Bound end
abstract type Bounded <: Bound end
struct Closed <: Bounded end
Expand Down
18 changes: 14 additions & 4 deletions src/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ span(interval::AnchoredInterval{P}) where P = abs(P)

# Allows an interval to be converted to a scalar when the set contained by the interval only
# contains a single element.
function Base.convert(::Type{T}, interval::AnchoredInterval{P,T}) where {P,T}
function Base.convert(::Type{T}, interval::AnchoredInterval{P, T}) where {P, T <: OrderedBaseTypes}
if isclosed(interval) && (sign(P) == 0 || first(interval) == last(interval))
return first(interval)
else
Expand Down Expand Up @@ -258,10 +258,20 @@ end

##### ARITHMETIC #####

Base.:+(a::T, b) where {T <: AnchoredInterval} = T(anchor(a) + b)
Base.:+(a::A, b::T) where {P, T, A <: AnchoredInterval{P,T}} = A(anchor(a) + b)
Base.:+(a::A, b::Number) where {P, T <: Number, A <: AnchoredInterval{P,T}} = A(anchor(a) + b)
Base.:+(a::A, b::CharInts) where {P, T <: CharInts, A <: AnchoredInterval{P,T}} = A(anchor(a) + b)
Base.:+(a::A, b::DatesTypes) where {P, T <: DatesTypes, A <: AnchoredInterval{P,T}} = A(anchor(a) + b)

Base.:+(a, b::AnchoredInterval) = b + a
Base.:-(a::AnchoredInterval, b) = a + -b
Base.:+(a::T, b::AnchoredInterval{P, T}) where {P, T} = b + a
Base.:+(a::Number, b::AnchoredInterval{P, T}) where {P, T <: Number} = b + a
Base.:+(a::CharInts, b::AnchoredInterval{P, T}) where {P, T <: CharInts} = b + a
Base.:+(a::DatesTypes, b::AnchoredInterval{P, T}) where {P, T <: DatesTypes} = b + a

Base.:-(a::AnchoredInterval{P, T}, b::T) where {P, T} = a + -b
Base.:-(a::AnchoredInterval{P, T}, b::Number) where {P, T <: Number} = a + -b
Base.:-(a::AnchoredInterval{P, T}, b::CharInts) where {P, T <: CharInts} = a + -b
Base.:-(a::AnchoredInterval{P, T}, b::DatesTypes) where {P, T <: DatesTypes} = a + -b

# Required for StepRange{<:AnchoredInterval}
Base.:-(a::AnchoredInterval, b::AnchoredInterval) = anchor(a) - anchor(b)
Expand Down
8 changes: 6 additions & 2 deletions src/endpoint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,12 @@ function Base.isless(a::RightEndpoint, b::LeftEndpoint)
end

# Comparisons between Scalars and Endpoints
Base.:(==)(a, b::Endpoint) = a == b.endpoint && isclosed(b)
Base.:(==)(a::Endpoint, b) = b == a
Base.:(==)(a::T, b::Endpoint{T}) where {T} = a == b.endpoint && isclosed(b)
Base.:(==)(a::Endpoint{T}, b::T) where {T} = b == a
Base.:(==)(a::Number, b::Endpoint{<:Number}) = a == b.endpoint && isclosed(b)
Base.:(==)(a::Endpoint{<:Number}, b::Number) = b == a
Base.:(==)(a::DatesTypes, b::Endpoint{<:DatesTypes}) = a == b.endpoint && isclosed(b)
Base.:(==)(a::Endpoint{<:DatesTypes}, b::DatesTypes) = b == a

function Base.isless(a, b::LeftEndpoint)
return (
Expand Down
24 changes: 19 additions & 5 deletions src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ end

# Allows an interval to be converted to a scalar when the set contained by the interval only
# contains a single element.
function Base.convert(::Type{T}, interval::Interval{T}) where T
function Base.convert(::Type{T}, interval::Interval{T}) where {T <: OrderedBaseTypes}
if first(interval) == last(interval) && isclosed(interval)
return first(interval)
else
Expand Down Expand Up @@ -291,11 +291,25 @@ end

##### ARITHMETIC #####

Base.:+(a::T, b) where {T <: Interval} = T(first(a) + b, last(a) + b)
Base.:+(a::A, b::T) where {T, A <: Interval{T}} = A(first(a) + b, last(a) + b)
Base.:+(a::A, b::Number) where {T <: Number, A <: Interval{T}} = A(first(a) + b, last(a) + b)
Base.:+(a::A, b::CharInts) where {T <: CharInts, A <: Interval{T}} = A(first(a) + b, last(a) + b)
Base.:+(a::A, b::DatesTypes) where {T <: DatesTypes, A <: Interval{T}} = A(first(a) + b, last(a) + b)

Base.:+(a::T, b::Interval{T}) where {T} = b + a
Base.:+(a::Number, b::Interval{T}) where {T <: Number} = b + a
Base.:+(a::CharInts, b::Interval{T}) where {T <: CharInts} = b + a
Base.:+(a::DatesTypes, b::Interval{T}) where {T <: DatesTypes} = b + a

Base.:-(a::Interval{T}, b::T) where {T} = a + -b
Base.:-(a::T, b::Interval{T}) where {T} = a + -b
Base.:-(a::Interval{T}, b::Number) where {T <: Number} = a + -b
Base.:-(a::Number, b::Interval{T}) where {T <: Number} = a + -b
Base.:-(a::Interval{T}, b::CharInts) where {T <: CharInts} = a + -b
Base.:-(a::CharInts, b::Interval{T}) where {T <: CharInts} = a + -b
Base.:-(a::Interval{T}, b::DatesTypes) where {T <: DatesTypes} = a + -b
Base.:-(a::DatesTypes, b::Interval{T}) where {T <: DatesTypes} = a + -b

Base.:+(a, b::Interval) = b + a
Base.:-(a::Interval, b) = a + -b
Base.:-(a, b::Interval) = a + -b
Base.:-(a::Interval{T,L,R}) where {T,L,R} = Interval{T,R,L}(-last(a), -first(a))

##### EQUALITY #####
Expand Down

0 comments on commit fe0a755

Please sign in to comment.