Skip to content

Commit 565a8f2

Browse files
authored
Merge pull request #99 from goretkin/construction-closure
Fix unnecessary nested `OffsetArray`
2 parents e7bc016 + 33c1943 commit 565a8f2

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/OffsetArrays.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ OffsetArray(A::AbstractArray{T,N}, inds::Vararg{AbstractUnitRange,N}) where {T,N
7373
OffsetArray(A, inds)
7474

7575
# avoid a level of indirection when nesting OffsetArrays
76-
function OffsetArray(A::OffsetArray, inds::NTuple{N,AbstractUnitRange}) where {N}
77-
OffsetArray(parent(A), inds)
76+
function OffsetArray(A::OffsetArray, offsets::NTuple{N,Int}) where {N}
77+
OffsetArray(parent(A), offsets .+ A.offsets)
7878
end
7979
OffsetArray(A::OffsetArray{T,0}, inds::Tuple{}) where {T} = OffsetArray(parent(A), ())
8080
# OffsetArray(A::OffsetArray{T,N}, inds::Tuple{}) where {T,N} = error("this should never be called")

test/runtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,23 @@ end
124124
@test axes(y) == (r,)
125125
end
126126

127+
@testset "OffsetArray of OffsetArray construction" begin
128+
# guarantee no unnecessary nesting of `OffsetArray`s
129+
r = -2:5
130+
d = collect(r)
131+
y = OffsetArray(d, r)
132+
133+
# range constructor
134+
y0 = OffsetArray(y, 0:7)
135+
@test y0[0] == r[1]
136+
@test typeof(parent(y0)) <: Array
137+
138+
# offset constructor
139+
y1 = OffsetArray(y, +2)
140+
@test y1[0] == r[1]
141+
@test typeof(parent(y1)) <: Array
142+
end
143+
127144
@testset "Axes supplied to constructor correspond to final result" begin
128145
# Ref https://github.com/JuliaArrays/OffsetArrays.jl/pull/65#issuecomment-457181268
129146
B = BidirectionalVector([1, 2, 3], -2)

0 commit comments

Comments
 (0)