From d19bc22df3f7c559d59433d4cac92206e763f086 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sun, 20 Mar 2016 23:06:25 -0500 Subject: [PATCH] Use state::Int for iterating over LinearFast AbstractArrays This makes it easier to use integer offsets when iterating over a subset of an AbstractVector. --- base/abstractarray.jl | 13 ++++++++++--- base/array.jl | 5 ----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 81b9e9daa4932..2a0b8ccc8e26f 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -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)) diff --git a/base/array.jl b/base/array.jl index 9691b325c3541..0113298104268 100644 --- a/base/array.jl +++ b/base/array.jl @@ -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