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 the first linear index instead of 1 for zero-argument getindex (redo #195) #196

Merged
merged 4 commits into from
Jan 29, 2021
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
6 changes: 6 additions & 0 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@ if VERSION < v"1.1.0-DEV.783"
Base.copyfirst!(dest::OffsetArray, src::OffsetArray) = (maximum!(parent(dest), parent(src)); return dest)
end

if VERSION <= v"1.7.0-DEV.400"
# https://github.com/JuliaLang/julia/pull/39393
# index for zero-argument getindex should be first linear index instead of 1 (#194)
Base._to_linear_index(A::OffsetArray) = first(LinearIndices(A))
end

##
# Adapt allows for automatic conversion of CPU OffsetArrays to GPU OffsetArrays
##
Expand Down
10 changes: 0 additions & 10 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,6 @@ Base.show(io::IO, r::IdOffsetRange) = print(io, "OffsetArrays.IdOffsetRange(",fi
# Optimizations
@inline Base.checkindex(::Type{Bool}, inds::IdOffsetRange, i::Real) = Base.checkindex(Bool, inds.parent, i - inds.offset)

# issue 194
# The indexing operation A[] gets mapped to A[1].
# The bounds-checking for this needs to be handled separately for AbstractVectors
# See https://github.com/JuliaLang/julia/issues/39379 for this issue reported in Base
# Once a PR fixing it is merged, we may limit our fix to earlier Julia versions
@inline function Base.checkbounds_indices(::Type{Bool}, IA::Tuple{IdOffsetRange}, ::Tuple{})
x = IA[1]
length(x) == 1 && first(x) == one(eltype(x))
end

if VERSION < v"1.5.2"
# issue 100, 133: IdOffsetRange as another index-preserving case shouldn't comtribute offsets
# fixed by https://github.com/JuliaLang/julia/pull/37204
Expand Down
21 changes: 7 additions & 14 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -712,20 +712,13 @@ end
@test A[4] == 2
end

@testset "issue 194" begin
A = OffsetArray([0], 1);
@test Base.checkbounds_indices(Bool, axes(A), ()) == false
@test_throws BoundsError A[]
A = OffsetArray([6], 1:1)
@test A[] == 6

A = OffsetArray(reshape(1:4, 2, 2), 2, 2);
@test Base.checkbounds_indices(Bool, axes(A), ()) == false
@test_throws BoundsError A[]
A = OffsetArray(reshape([6], 1, 1), 1:1, 1:1);
@test A[] == 6
A = OffsetArray(A, 1:1, 2:2);
@test A[] == 6
@testset "Zero-index indexing (#194)" begin
@test OffsetArray([6], 2:2)[] == 6
@test OffsetArray(fill(6, 1, 1), 2:2, 3:3)[] == 6
@test OffsetArray(fill(6))[] == 6
@test_throws BoundsError OffsetArray([6,7], 2:3)[]
@test_throws BoundsError OffsetArray([6 7], 2:2, 2:3)[]
@test_throws BoundsError OffsetArray([], 2:1)[]
end
end

Expand Down