Skip to content

Commit

Permalink
Fix accumulate/cumsum performance regression by adding getindex(::Lin…
Browse files Browse the repository at this point in the history
…earIndices, ::AbstractRange)

Plus two small fixes.
  • Loading branch information
nalimilan committed May 7, 2018
1 parent c6b6100 commit 3a3683c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions base/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,14 @@ LinearIndices(A::Union{AbstractArray,SimpleVector}) = LinearIndices(axes(A))
IndexStyle(::Type{<:LinearIndices}) = IndexLinear()
axes(iter::LinearIndices) = iter.indices
size(iter::LinearIndices) = map(unsafe_length, iter.indices)
length(iter::LinearIndices) = prod(size(iter))
function getindex(iter::LinearIndices, i::Int)
@propagate_inbounds function getindex(iter::LinearIndices, i::Int)
@boundscheck checkbounds(iter, i)
i
end
@propagate_inbounds function getindex(iter::LinearIndices, i::AbstractRange{<:Integer})
@boundscheck checkbounds(iter, i)
@inbounds (first(iter):last(iter))[i]
end
# More efficient iteration — predominantly for non-vector LinearIndices
# but one-dimensional LinearIndices must be special-cased to support OffsetArrays
start(iter::LinearIndices{1}) = start(iter.indices[1])
Expand Down
8 changes: 8 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ end
for i in ((), fill(0))
@test LinearIndices(i)[1] == 1
@test_throws BoundsError LinearIndices(i)[2]
@test_throws BoundsError LinearIndices(i)[1:2]
@test LinearIndices(i)[1,1] == 1
@test LinearIndices(i)[] == 1
@test size(LinearIndices(i)) == ()
@test CartesianIndices(i)[1] == CartesianIndex()
@test_throws BoundsError CartesianIndices(i)[2]
@test_throws BoundsError CartesianIndices(i)[1:2]
end
end

Expand All @@ -135,6 +137,9 @@ end
@test LinearIndices((3,))[2,1] == 2
@test LinearIndices((3,))[[1]] == [1]
@test size(LinearIndices((3,))) == (3,)
@test LinearIndices((3,))[1:2] === 1:2
@test LinearIndices((3,))[1:2:3] === 1:2:3
@test_throws BoundsError LinearIndices((3,))[2:4]
@test_throws BoundsError CartesianIndices((3,))[2,2]
# ambiguity btw cartesian indexing and linear indexing in 1d when
# indices may be nontraditional
Expand Down Expand Up @@ -162,6 +167,9 @@ end
@test cartesian[vec(linear)] == vec(cartesian)
@test cartesian[cartesian] == cartesian
@test cartesian[vec(cartesian)] == vec(cartesian)
@test linear[2:3] === 2:3
@test linear[3:-1:1] === 3:-1:1
@test_throws BoundsError linear[4:13]
end

@testset "3-dimensional" begin
Expand Down

0 comments on commit 3a3683c

Please sign in to comment.