Skip to content

Commit 770a484

Browse files
use Base implementation for compute_offset1 (#135)
* Fix #133: compute_offset1 for an IdOffsetRange axis * [compat] use Base implementation for compute_offset1 for Julia >= 1.6 Julia < 1.6 still need this for correctness closes #100 closes #133 closes #134 Co-authored-by: jishnub <jishnuonline@gmail.com>
1 parent c6fae67 commit 770a484

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/axes.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ offset_coerce(::Type{I}, r::AbstractUnitRange) where I<:AbstractUnitRange{T} whe
129129
@inline Base.axes1(r::IdOffsetRange) = IdOffsetRange(Base.axes1(r.parent), r.offset)
130130
@inline Base.unsafe_indices(r::IdOffsetRange) = (r,)
131131
@inline Base.length(r::IdOffsetRange) = length(r.parent)
132-
# issue 100: IdOffsetRange as another index-preserving case shouldn't comtribute offsets
133-
@inline Base.compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{IdOffsetRange}, I::Tuple) =
134-
Base.compute_linindex(parent, I) - stride1*first(axes(parent, dims[1]))
135132
Base.reduced_index(i::IdOffsetRange) = typeof(i)(first(i):first(i))
136133
# Workaround for #92 on Julia < 1.4
137134
Base.reduced_index(i::IdentityUnitRange{<:IdOffsetRange}) = typeof(i)(first(i):first(i))
@@ -170,3 +167,9 @@ Base.show(io::IO, r::IdOffsetRange) = print(io, first(r), ':', last(r))
170167

171168
# Optimizations
172169
@inline Base.checkindex(::Type{Bool}, inds::IdOffsetRange, i::Real) = Base.checkindex(Bool, inds.parent, i - inds.offset)
170+
171+
if VERSION < v"1.6.0-DEV.762"
172+
# issue 100, 133: IdOffsetRange as another index-preserving case shouldn't comtribute offsets
173+
@inline Base.compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{IdOffsetRange}, I::Tuple) =
174+
Base.compute_linindex(parent, I) - stride1*first(inds[1])
175+
end

test/runtests.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,35 @@ end
335335
@test_throws BoundsError S[1]
336336
@test axes(S) == (OffsetArrays.IdOffsetRange(3:4), )
337337

338+
# issue 133
339+
r = OffsetArrays.IdOffsetRange(1:2, -1)
340+
v1 = view(A, r, 3)
341+
@test v1[0] == 1
342+
@test v1[1] == 2
343+
@test axes(v1, 1) == axes(r, 1)
344+
v2 = view(A, UnitRange(r), 3)
345+
for (indflat, indoffset) in enumerate(r)
346+
@test v1[indoffset] == v2[indflat]
347+
end
348+
349+
# issue 133
350+
r = OffsetArrays.IdOffsetRange(1:2, 2)
351+
v1 = view(A, 1, r)
352+
@test v1[3] == 2
353+
@test v1[4] == 4
354+
@test axes(v1, 1) == axes(r, 1)
355+
v2 = view(A, 1, UnitRange(r))
356+
for (indflat, indoffset) in enumerate(r)
357+
@test v1[indoffset] == v2[indflat]
358+
end
359+
360+
# issue 133
361+
a12 = zeros(3:8, 3:4)
362+
r = OffsetArrays.IdOffsetRange(Base.OneTo(3), 5)
363+
a12[r, 4] .= 3
364+
@test all(a12[r, 4] .== 3)
365+
@test all(a12[UnitRange(r), 4] .== 3)
366+
338367
A0 = collect(reshape(1:24, 2, 3, 4))
339368
A = OffsetArray(A0, (-1,2,1))
340369
S = view(A, axes(A, 1), 3:4, axes(A, 3))

0 commit comments

Comments
 (0)