Skip to content

Commit 97e945f

Browse files
authored
Restore similar for Julia v0.6 (#49)
1 parent 64cebbf commit 97e945f

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/OffsetArrays.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ function Base.similar(A::AbstractArray, ::Type{T}, inds::Tuple{OffsetAxis,Vararg
119119
OffsetArray(B, map(indexoffset, inds))
120120
end
121121

122-
Base.similar(::Type{T}, shape::Tuple{OffsetAxis,Vararg{OffsetAxis}}) where {T<:AbstractArray} =
123-
OffsetArray(T(undef, map(indexlength, shape)), map(indexoffset, shape))
124-
125122
if VERSION < v"0.7.0-DEV.5242"
126123
# Reshape's methods in Base changed, so using the new definitions leads to ambiguities
127124
Base.reshape(A::AbstractArray, inds::Tuple{UnitRange,Vararg{UnitRange}}) =
@@ -147,9 +144,14 @@ end
147144
if VERSION < v"0.7.0-DEV.4873"
148145
# Julia PR #26733 removed similar(f, ...) in favor of just using method extension directly
149146
# https://github.com/JuliaLang/julia/pull/26733
147+
Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:AbstractArray} =
148+
OffsetArray(T(undef, map(indexlength, shape)), map(indexoffset, shape))
150149
Base.similar(f::Function, shape::Tuple{UnitRange,Vararg{UnitRange}}) =
151-
OffsetArray(f(map(length, shape)), map(indexoffset, shape))
150+
OffsetArray(f(map(indexlength, shape)), map(indexoffset, shape))
152151
else
152+
Base.similar(::Type{T}, shape::Tuple{OffsetAxis,Vararg{OffsetAxis}}) where {T<:AbstractArray} =
153+
OffsetArray(T(undef, map(indexlength, shape)), map(indexoffset, shape))
154+
153155
Base.fill(v, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
154156
fill!(OffsetArray(Array{typeof(v), N}(undef, map(indexlength, inds)), map(indexoffset, inds)), v)
155157
Base.zeros(::Type{T}, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {T, N} =

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ B = similar(A, (-3:3,1:4))
173173
B = similar(parent(A), (-3:3,1:4))
174174
@test isa(B, OffsetArray{Int,2})
175175
@test axes(B) === AxisType.((-3:3, 1:4))
176+
@test isa([x for x in [1,2,3]], Vector{Int})
177+
@test similar(Array{Int}, (0:0, 0:0)) isa OffsetArray{Int, 2}
178+
@test similar(Array{Int}, (1, 1)) isa Matrix{Int}
179+
@test similar(Array{Int}, (Base.OneTo(1), Base.OneTo(1))) isa Matrix{Int}
180+
if VERSION < v"0.7.0-DEV.4873"
181+
@test similar(x->zeros(1, 1), (0:0, 0:0)) isa OffsetArray{Float64, 2}
182+
end
176183

177184
# Reshape
178185
B = reshape(A0, -10:-9, 9:10)

0 commit comments

Comments
 (0)