Skip to content

Commit 97cef1a

Browse files
authored
use the first linear index instead of 1 for zero-argument getindex (redo #195) (#196)
1 parent 1263513 commit 97cef1a

File tree

3 files changed

+13
-24
lines changed

3 files changed

+13
-24
lines changed

src/OffsetArrays.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,12 @@ if VERSION < v"1.1.0-DEV.783"
507507
Base.copyfirst!(dest::OffsetArray, src::OffsetArray) = (maximum!(parent(dest), parent(src)); return dest)
508508
end
509509

510+
if VERSION <= v"1.7.0-DEV.400"
511+
# https://github.com/JuliaLang/julia/pull/39393
512+
# index for zero-argument getindex should be first linear index instead of 1 (#194)
513+
Base._to_linear_index(A::OffsetArray) = first(LinearIndices(A))
514+
end
515+
510516
##
511517
# Adapt allows for automatic conversion of CPU OffsetArrays to GPU OffsetArrays
512518
##

src/axes.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,6 @@ Base.show(io::IO, r::IdOffsetRange) = print(io, "OffsetArrays.IdOffsetRange(",fi
179179
# Optimizations
180180
@inline Base.checkindex(::Type{Bool}, inds::IdOffsetRange, i::Real) = Base.checkindex(Bool, inds.parent, i - inds.offset)
181181

182-
# issue 194
183-
# The indexing operation A[] gets mapped to A[1].
184-
# The bounds-checking for this needs to be handled separately for AbstractVectors
185-
# See https://github.com/JuliaLang/julia/issues/39379 for this issue reported in Base
186-
# Once a PR fixing it is merged, we may limit our fix to earlier Julia versions
187-
@inline function Base.checkbounds_indices(::Type{Bool}, IA::Tuple{IdOffsetRange}, ::Tuple{})
188-
x = IA[1]
189-
length(x) == 1 && first(x) == one(eltype(x))
190-
end
191-
192182
if VERSION < v"1.5.2"
193183
# issue 100, 133: IdOffsetRange as another index-preserving case shouldn't comtribute offsets
194184
# fixed by https://github.com/JuliaLang/julia/pull/37204

test/runtests.jl

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -712,20 +712,13 @@ end
712712
@test A[4] == 2
713713
end
714714

715-
@testset "issue 194" begin
716-
A = OffsetArray([0], 1);
717-
@test Base.checkbounds_indices(Bool, axes(A), ()) == false
718-
@test_throws BoundsError A[]
719-
A = OffsetArray([6], 1:1)
720-
@test A[] == 6
721-
722-
A = OffsetArray(reshape(1:4, 2, 2), 2, 2);
723-
@test Base.checkbounds_indices(Bool, axes(A), ()) == false
724-
@test_throws BoundsError A[]
725-
A = OffsetArray(reshape([6], 1, 1), 1:1, 1:1);
726-
@test A[] == 6
727-
A = OffsetArray(A, 1:1, 2:2);
728-
@test A[] == 6
715+
@testset "Zero-index indexing (#194)" begin
716+
@test OffsetArray([6], 2:2)[] == 6
717+
@test OffsetArray(fill(6, 1, 1), 2:2, 3:3)[] == 6
718+
@test OffsetArray(fill(6))[] == 6
719+
@test_throws BoundsError OffsetArray([6,7], 2:3)[]
720+
@test_throws BoundsError OffsetArray([6 7], 2:2, 2:3)[]
721+
@test_throws BoundsError OffsetArray([], 2:1)[]
729722
end
730723
end
731724

0 commit comments

Comments
 (0)