Skip to content

Commit

Permalink
Update in anticipation of deprecating similar(f, ...) (#43)
Browse files Browse the repository at this point in the history
* Update in anticipation of deprecating similar(f, ...)

Requires JuliaLang/julia#26733

* Add VERSIONing
  • Loading branch information
mbauman committed Jun 6, 2018
1 parent 0e0e6bf commit 2be1cff
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ function Base.similar(A::AbstractArray, ::Type{T}, inds::Tuple{UnitRange,Vararg{
OffsetArray(B, map(indexoffset, inds))
end

Base.similar(f::Function, shape::Tuple{UnitRange,Vararg{UnitRange}}) =
OffsetArray(f(map(length, shape)), map(indexoffset, shape))
Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:OffsetArray} =
OffsetArray(T(map(length, shape)), map(indexoffset, shape))
Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:Array} =
Expand All @@ -133,6 +131,24 @@ function Base.reshape(A::OffsetArray, inds::Tuple{UnitRange,Vararg{Union{UnitRan
throw(ArgumentError("reshape must supply UnitRange axes, got $(typeof(inds)).\n Note that reshape(A, Val{N}) is not supported for OffsetArrays."))
end

if VERSION < v"0.7.0-DEV.4873"
# Julia PR #26733 removed similar(f, ...) in favor of just using method extension directly
# https://github.com/JuliaLang/julia/pull/26733
Base.similar(f::Function, shape::Tuple{UnitRange,Vararg{UnitRange}}) =
OffsetArray(f(map(length, shape)), map(indexoffset, shape))
else
Base.fill(v, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
fill!(OffsetArray(Array{typeof(v), N}(undef, map(indexlength, inds)), map(indexoffset, inds)), v)
Base.zeros(::Type{T}, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {T, N} =
fill!(OffsetArray(Array{T, N}(undef, map(indexlength, inds)), map(indexoffset, inds)), zero(T))
Base.ones(::Type{T}, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {T, N} =
fill!(OffsetArray(Array{T, N}(undef, map(indexlength, inds)), map(indexoffset, inds)), one(T))
Base.trues(inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
fill!(OffsetArray(BitArray{N}(undef, map(indexlength, inds)), map(indexoffset, inds)), true)
Base.falses(inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
fill!(OffsetArray(BitArray{N}(undef, map(indexlength, inds)), map(indexoffset, inds)), false)
end

# Don't allow bounds-checks to be removed during Julia 0.5
@inline function Base.getindex(A::OffsetArray{T,N}, I::Vararg{Int,N}) where {T,N}
checkbounds(A, I...)
Expand Down Expand Up @@ -185,6 +201,8 @@ offset(offsets::Tuple{Vararg{Int}}, inds::Tuple{}) = error("inds cannot be short

indexoffset(r::AbstractRange) = first(r) - 1
indexoffset(i::Integer) = 0
indexlength(r::AbstractRange) = length(r)
indexlength(i::Integer) = i

macro unsafe(ex)
esc(unsafe(ex))
Expand Down

0 comments on commit 2be1cff

Please sign in to comment.