Skip to content

Commit 2be1cff

Browse files
authored
Update in anticipation of deprecating similar(f, ...) (#43)
* Update in anticipation of deprecating similar(f, ...) Requires JuliaLang/julia#26733 * Add VERSIONing
1 parent 0e0e6bf commit 2be1cff

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/OffsetArrays.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ function Base.similar(A::AbstractArray, ::Type{T}, inds::Tuple{UnitRange,Vararg{
114114
OffsetArray(B, map(indexoffset, inds))
115115
end
116116

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

134+
if VERSION < v"0.7.0-DEV.4873"
135+
# Julia PR #26733 removed similar(f, ...) in favor of just using method extension directly
136+
# https://github.com/JuliaLang/julia/pull/26733
137+
Base.similar(f::Function, shape::Tuple{UnitRange,Vararg{UnitRange}}) =
138+
OffsetArray(f(map(length, shape)), map(indexoffset, shape))
139+
else
140+
Base.fill(v, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
141+
fill!(OffsetArray(Array{typeof(v), N}(undef, map(indexlength, inds)), map(indexoffset, inds)), v)
142+
Base.zeros(::Type{T}, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {T, N} =
143+
fill!(OffsetArray(Array{T, N}(undef, map(indexlength, inds)), map(indexoffset, inds)), zero(T))
144+
Base.ones(::Type{T}, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {T, N} =
145+
fill!(OffsetArray(Array{T, N}(undef, map(indexlength, inds)), map(indexoffset, inds)), one(T))
146+
Base.trues(inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
147+
fill!(OffsetArray(BitArray{N}(undef, map(indexlength, inds)), map(indexoffset, inds)), true)
148+
Base.falses(inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
149+
fill!(OffsetArray(BitArray{N}(undef, map(indexlength, inds)), map(indexoffset, inds)), false)
150+
end
151+
136152
# Don't allow bounds-checks to be removed during Julia 0.5
137153
@inline function Base.getindex(A::OffsetArray{T,N}, I::Vararg{Int,N}) where {T,N}
138154
checkbounds(A, I...)
@@ -185,6 +201,8 @@ offset(offsets::Tuple{Vararg{Int}}, inds::Tuple{}) = error("inds cannot be short
185201

186202
indexoffset(r::AbstractRange) = first(r) - 1
187203
indexoffset(i::Integer) = 0
204+
indexlength(r::AbstractRange) = length(r)
205+
indexlength(i::Integer) = i
188206

189207
macro unsafe(ex)
190208
esc(unsafe(ex))

0 commit comments

Comments
 (0)