Skip to content

Commit

Permalink
use the first linear index instead of 1 for zero-argument getindex
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Bauman <mbauman@juliacomputing.com>
  • Loading branch information
johnnychen94 and mbauman committed Jan 26, 2021
1 parent 1263513 commit 9bd6bec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,13 @@ if VERSION < v"1.1.0-DEV.783"
Base.copyfirst!(dest::OffsetArray, src::OffsetArray) = (maximum!(parent(dest), parent(src)); return dest)
end

# TODO: modify this when https://github.com/JuliaLang/julia/pull/39393 get merged
if VERSION <= v"1.7.0-DEV.375"
# issue 194
# index for zero-argument getindex should be first linear index instead of 1
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

0 comments on commit 9bd6bec

Please sign in to comment.