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

Handle Dates-array arithmetic with promote_op #12370

Merged
merged 1 commit into from
Jul 30, 2015
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
36 changes: 26 additions & 10 deletions base/dates/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ for op in (:.+, :.-)
($op){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = ($op)(x,y)
($op_){T<:TimeType,P<:GeneralPeriod}(x::StridedArray{P}, y::T) = ($op)(x,y)
($op_){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = ($op)(x,y)

# AbstractArray{TimeType}, StridedArray{GeneralPeriod}
($op_){T<:TimeType,P<:GeneralPeriod}(x::Range{T}, y::StridedArray{P}) = ($op_)(collect(x),y)
($op_){T<:TimeType,P<:GeneralPeriod}(x::AbstractArray{T}, y::StridedArray{P}) =
reshape(TimeType[($op_)(x[i],y[i]) for i in eachindex(x, y)], promote_shape(size(x),size(y)))
($op_){T<:TimeType,P<:GeneralPeriod}(y::StridedArray{P}, x::AbstractArray{T}) = ($op_)(x,y)
end
end

Expand All @@ -100,7 +94,29 @@ end
# AbstractArray{TimeType}, AbstractArray{TimeType}
(-){T<:TimeType}(x::OrdinalRange{T}, y::OrdinalRange{T}) = collect(x) - collect(y)
(-){T<:TimeType}(x::Range{T}, y::Range{T}) = collect(x) - collect(y)
(-){T<:TimeType}(x::AbstractArray{T}, y::Range{T}) = y - collect(x)
(-){T<:TimeType}(x::Range{T}, y::AbstractArray{T}) = collect(x) - y
(-){T<:TimeType}(x::AbstractArray{T}, y::AbstractArray{T}) =
reshape(Period[x[i] - y[i] for i in eachindex(x, y)], promote_shape(size(x),size(y)))

# promotion rules

for (op,F) in ((:+, Base.AddFun),
(:-, Base.SubFun),
(:.+, Base.DotAddFun),
(:.-, Base.DotSubFun))
@eval begin
Base.promote_op{P<:Period}(::$F, ::Type{P}, ::Type{P}) = P
Base.promote_op{P1<:Period,P2<:Period}(::$F, ::Type{P1}, ::Type{P2}) = CompoundPeriod
Base.promote_op{D<:Date}(::$F, ::Type{D}, ::Type{D}) = Day
Base.promote_op{D<:DateTime}(::$F, ::Type{D}, ::Type{D}) = Millisecond
end
end

for (op,F) in ((:/, Base.RDivFun),
(:%, Base.RemFun),
(:div, Base.IDivFun),
(:mod, Base.ModFun),
(:./, Base.DotRDivFun),
(:.%, Base.DotRemFun))
@eval begin
Base.promote_op{P<:Period}(::$F, ::Type{P}, ::Type{P}) = typeof($op(1,1))
Base.promote_op{P<:Period,R<:Real}(::$F, ::Type{P}, ::Type{R}) = P
end
end
3 changes: 3 additions & 0 deletions base/functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ call(::DotMulFun, x, y) = x .* y
immutable RDivFun <: Func{2} end
call(::RDivFun, x, y) = x / y

immutable DotRDivFun <: Func{2} end
call(::DotRDivFun, x, y) = x ./ y

immutable LDivFun <: Func{2} end
call(::LDivFun, x, y) = x \ y

Expand Down