Skip to content

Commit

Permalink
Use state::Int for iterating over LinearFast AbstractArrays
Browse files Browse the repository at this point in the history
This makes it easier to use integer offsets when iterating over a
subset of an AbstractVector.
  • Loading branch information
timholy committed Mar 21, 2016
1 parent a74d45e commit d19bc22
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 10 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,16 @@ zero{T}(x::AbstractArray{T}) = fill!(similar(x), zero(T))
# While the definitions for LinearFast are all simple enough to inline on their
# own, LinearSlow's CartesianRange is more complicated and requires explicit
# inlining.
start(A::AbstractArray) = (@_inline_meta(); itr = eachindex(A); (itr, start(itr)))
next(A::AbstractArray,i) = (@_inline_meta(); (idx, s) = next(i[1], i[2]); (A[idx], (i[1], s)))
done(A::AbstractArray,i) = done(i[1], i[2])
start(A::AbstractArray) = (@_inline_meta(); _start(linearindexing(A), A))
_start(::LinearFast, A::AbstractArray) = 1
_start(::LinearSlow, A::AbstractArray) = (@_inline_meta(); itr = eachindex(A); (itr, start(itr)))

next(A::AbstractArray,i) = (@_inline_meta(); _next(A,i))
_next(A::AbstractArray,i) = (@_inline_meta(); (idx, s) = next(i[1], i[2]); (A[idx], (i[1], s)))
_next(A::AbstractArray,i::Int) = (A[i],i+1)
done(A::AbstractArray,i) = (@_inline_meta(); _done(A, i))
_done(A::AbstractArray,i) = done(i[1], i[2])
_done(A::AbstractArray,i::Int) = i == length(A)+1

# eachindex iterates over all indices. LinearSlow definitions are later.
eachindex(A::AbstractArray) = (@_inline_meta(); eachindex(linearindexing(A), A))
Expand Down
5 changes: 0 additions & 5 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,6 @@ Return an array of all items in a collection. For associative collections, retur
"""
collect(itr) = collect(eltype(itr), itr)

## Iteration ##
start(A::Array) = 1
next(a::Array,i) = (a[i],i+1)
done(a::Array,i) = i == length(a)+1

## Indexing: getindex ##

# This is more complicated than it needs to be in order to get Win64 through bootstrap
Expand Down

0 comments on commit d19bc22

Please sign in to comment.