Skip to content

Commit b06de67

Browse files
tpapptimholy
authored andcommitted
Avoid a level of indirection when nesting OffsetArrays. (#67)
1 parent 39a16a3 commit b06de67

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/OffsetArrays.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ end
5858
OffsetArray(A::AbstractArray{T,N}, inds::Vararg{AbstractUnitRange,N}) where {T,N} =
5959
OffsetArray(A, inds)
6060

61+
# avoid a level of indirection when nesting OffsetArrays
62+
function OffsetArray(A::OffsetArray, inds::NTuple{N,AbstractUnitRange}) where {N}
63+
OffsetArray(parent(A), inds)
64+
end
65+
OffsetArray(A::OffsetArray{T,0}, inds::Tuple{}) where {T} = OffsetArray{T,0,typeof(A)}(parent(A), ())
66+
OffsetArray(A::OffsetArray{T,N}, inds::Tuple{}) where {T,N} = error("this should never be called")
67+
6168
Base.IndexStyle(::Type{OA}) where {OA<:OffsetArray} = IndexStyle(parenttype(OA))
6269
parenttype(::Type{OffsetArray{T,N,AA}}) where {T,N,AA} = AA
6370
parenttype(A::OffsetArray) = parenttype(typeof(A))

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,14 @@ end
401401
@test OffsetVector(v, -2:2) == OffsetArray(v, -2:2)
402402
@test typeof(OffsetVector{Float64}(undef, -2:2)) == typeof(OffsetArray{Float64}(undef, -2:2))
403403
end
404+
405+
@testset "no nesting" begin
406+
A = randn(2, 3)
407+
x = A[2, 2]
408+
O1 = OffsetArray(A, -1:0, -1:1)
409+
O2 = OffsetArray(O1, 0:1, 0:2)
410+
@test parent(O1) parent(O2)
411+
@test eltype(O1) eltype(O2)
412+
O2[1, 1] = x + 1 # just a sanity check
413+
@test A[2, 2] == x + 1
414+
end

0 commit comments

Comments
 (0)