Skip to content

Commit

Permalink
Use IndexLinear indexing for LinearIndices
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Apr 3, 2018
1 parent 93b7089 commit e87817b
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,16 +410,13 @@ module IteratorsMD
LinearIndices(A::AbstractArray) = LinearIndices(CartesianIndices(A))

# AbstractArray implementation
Base.IndexStyle(::Type{LinearIndices{N,R}}) where {N,R} = IndexCartesian()
Base.IndexStyle(::Type{LinearIndices{N,R}}) where {N,R} = IndexLinear()
Base.axes(iter::LinearIndices{N,R}) where {N,R} = iter.indices
Base.size(iter::LinearIndices{N,R}) where {N,R} = length.(iter.indices)
@inline function Base.getindex(iter::LinearIndices{N,R}, I::Vararg{Int, N}) where {N,R}
dims = length.(iter.indices)
#without the inbounds, this is slower than Base._sub2ind(iter.indices, I...)
@inbounds result = reshape(1:prod(dims), dims)[(I .- first.(iter.indices) .+ 1)...]
return result
function Base.getindex(iter::LinearIndices{N,R}, i::Int) where {N,R}
@boundscheck checkbounds(iter, i)
i
end
@inline Base.getindex(iter::LinearIndices{1,R}, i::Int) where {R} = i
end # IteratorsMD


Expand Down

0 comments on commit e87817b

Please sign in to comment.