Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Base implementation for compute_offset1 #135

Merged
merged 2 commits into from
Aug 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ offset_coerce(::Type{I}, r::AbstractUnitRange) where I<:AbstractUnitRange{T} whe
@inline Base.axes1(r::IdOffsetRange) = IdOffsetRange(Base.axes1(r.parent), r.offset)
@inline Base.unsafe_indices(r::IdOffsetRange) = (r,)
@inline Base.length(r::IdOffsetRange) = length(r.parent)
# issue 100: IdOffsetRange as another index-preserving case shouldn't comtribute offsets
@inline Base.compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{IdOffsetRange}, I::Tuple) =
Base.compute_linindex(parent, I) - stride1*first(axes(parent, dims[1]))
Base.reduced_index(i::IdOffsetRange) = typeof(i)(first(i):first(i))
# Workaround for #92 on Julia < 1.4
Base.reduced_index(i::IdentityUnitRange{<:IdOffsetRange}) = typeof(i)(first(i):first(i))
Expand Down Expand Up @@ -170,3 +167,9 @@ Base.show(io::IO, r::IdOffsetRange) = print(io, first(r), ':', last(r))

# Optimizations
@inline Base.checkindex(::Type{Bool}, inds::IdOffsetRange, i::Real) = Base.checkindex(Bool, inds.parent, i - inds.offset)

if VERSION < v"1.6.0-DEV.762"
# issue 100, 133: IdOffsetRange as another index-preserving case shouldn't comtribute offsets
@inline Base.compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{IdOffsetRange}, I::Tuple) =
Base.compute_linindex(parent, I) - stride1*first(inds[1])
end
29 changes: 29 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,35 @@ end
@test_throws BoundsError S[1]
@test axes(S) == (OffsetArrays.IdOffsetRange(3:4), )

# issue 133
r = OffsetArrays.IdOffsetRange(1:2, -1)
v1 = view(A, r, 3)
@test v1[0] == 1
@test v1[1] == 2
@test axes(v1, 1) == axes(r, 1)
v2 = view(A, UnitRange(r), 3)
for (indflat, indoffset) in enumerate(r)
@test v1[indoffset] == v2[indflat]
end

# issue 133
r = OffsetArrays.IdOffsetRange(1:2, 2)
v1 = view(A, 1, r)
@test v1[3] == 2
@test v1[4] == 4
@test axes(v1, 1) == axes(r, 1)
v2 = view(A, 1, UnitRange(r))
for (indflat, indoffset) in enumerate(r)
@test v1[indoffset] == v2[indflat]
end

# issue 133
a12 = zeros(3:8, 3:4)
r = OffsetArrays.IdOffsetRange(Base.OneTo(3), 5)
a12[r, 4] .= 3
@test all(a12[r, 4] .== 3)
@test all(a12[UnitRange(r), 4] .== 3)

A0 = collect(reshape(1:24, 2, 3, 4))
A = OffsetArray(A0, (-1,2,1))
S = view(A, axes(A, 1), 3:4, axes(A, 3))
Expand Down