Skip to content

Commit

Permalink
Merge pull request #10858 from JuliaLang/teh/eachindex
Browse files Browse the repository at this point in the history
Change `for i=1:length(A)` to `for i in eachindex(A)`
  • Loading branch information
timholy committed Apr 20, 2015
2 parents 5d2a0ec + 4216ae5 commit 3dbc828
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 35 deletions.
19 changes: 15 additions & 4 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ linearindexing{T<:AbstractArray}(::Type{T}) = LinearSlow()
linearindexing{T<:Array}(::Type{T}) = LinearFast()
linearindexing{T<:Range}(::Type{T}) = LinearFast()

*(::LinearFast, ::LinearFast) = LinearFast()
*(::LinearSlow, ::LinearFast) = LinearSlow()
*(::LinearFast, ::LinearSlow) = LinearSlow()
*(::LinearSlow, ::LinearSlow) = LinearSlow()

## Bounds checking ##
checkbounds(sz::Int, ::Colon) = nothing
checkbounds(sz::Int, i::Int) = 1 <= i <= sz || throw(BoundsError())
Expand Down Expand Up @@ -350,9 +355,15 @@ next(A::AbstractArray,i) = (@_inline_meta(); (idx, s) = next(i[1], i[2]); (A[idx
done(A::AbstractArray,i) = done(i[1], i[2])

# eachindex iterates over all indices. LinearSlow definitions are later.
eachindex(A::AbstractArray) = (@_inline_meta; eachindex(linearindexing(A), A))
eachindex(A::AbstractArray) = (@_inline_meta(); eachindex(linearindexing(A), A))
eachindex(::LinearFast, A::AbstractArray) = 1:length(A)

function eachindex(A::AbstractArray, B::AbstractArray)
@_inline_meta
eachindex(linearindexing(A)*linearindexing(B), A, B)
end
eachindex(::LinearFast, A::AbstractArray, B::AbstractArray) = 1:max(length(A),length(B))

isempty(a::AbstractArray) = (length(a) == 0)

## Conversions ##
Expand Down Expand Up @@ -431,7 +442,7 @@ getindex(t::AbstractArray, i::Real) = error("indexing not defined for ", typeof(
# linear indexing with a single multi-dimensional index
function getindex(A::AbstractArray, I::AbstractArray)
x = similar(A, size(I))
for i=1:length(I)
for i in eachindex(I)
x[i] = A[I[i]]
end
return x
Expand Down Expand Up @@ -856,7 +867,7 @@ function isequal(A::AbstractArray, B::AbstractArray)
if isa(A,Range) != isa(B,Range)
return false
end
for i = 1:length(A)
for i in eachindex(A)
if !isequal(A[i], B[i])
return false
end
Expand All @@ -880,7 +891,7 @@ function (==)(A::AbstractArray, B::AbstractArray)
if isa(A,Range) != isa(B,Range)
return false
end
for i = 1:length(A)
for i in eachindex(A)
if !(A[i]==B[i])
return false
end
Expand Down
28 changes: 14 additions & 14 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function fill!{T<:Union(Integer,FloatingPoint)}(a::Array{T}, x)
ccall(:memset, Ptr{Void}, (Ptr{Void}, Cint, Csize_t),
a, 0, length(a)*sizeof(T))
else
for i = 1:length(a)
for i in eachindex(a)
@inbounds a[i] = xT
end
end
Expand Down Expand Up @@ -703,7 +703,7 @@ end
## Unary operators ##

function conj!{T<:Number}(A::AbstractArray{T})
for i=1:length(A)
for i in eachindex(A)
A[i] = conj(A[i])
end
return A
Expand All @@ -713,15 +713,15 @@ for f in (:-, :~, :conj, :sign)
@eval begin
function ($f)(A::StridedArray)
F = similar(A)
for i=1:length(A)
for i in eachindex(A)
F[i] = ($f)(A[i])
end
return F
end
end
end

(-)(A::StridedArray{Bool}) = reshape([ -A[i] for i=1:length(A) ], size(A))
(-)(A::StridedArray{Bool}) = reshape([ -A[i] for i in eachindex(A) ], size(A))

real(A::StridedArray) = reshape([ real(x) for x in A ], size(A))
imag(A::StridedArray) = reshape([ imag(x) for x in A ], size(A))
Expand All @@ -730,7 +730,7 @@ imag{T<:Real}(x::StridedArray{T}) = zero(x)

function !(A::StridedArray{Bool})
F = similar(A)
for i=1:length(A)
for i in eachindex(A)
F[i] = !A[i]
end
return F
Expand Down Expand Up @@ -789,7 +789,7 @@ for f in (:+, :-, :div, :mod, :&, :|, :$)
end
function ($f){S,T}(A::AbstractArray{S}, B::AbstractArray{T})
F = similar(A, promote_type(S,T), promote_shape(size(A),size(B)))
for i=1:length(A)
for i in eachindex(A,B)
@inbounds F[i] = ($f)(A[i], B[i])
end
return F
Expand All @@ -800,14 +800,14 @@ for f in (:.+, :.-, :.*, :.%, :.<<, :.>>, :div, :mod, :rem, :&, :|, :$)
@eval begin
function ($f){T}(A::Number, B::AbstractArray{T})
F = similar(B, promote_array_type(typeof(A),T))
for i=1:length(B)
for i in eachindex(B)
@inbounds F[i] = ($f)(A, B[i])
end
return F
end
function ($f){T}(A::AbstractArray{T}, B::Number)
F = similar(A, promote_array_type(typeof(B),T))
for i=1:length(A)
for i in eachindex(A)
@inbounds F[i] = ($f)(A[i], B)
end
return F
Expand All @@ -830,14 +830,14 @@ for f in (:.+, :.-)
@eval begin
function ($f)(A::Bool, B::StridedArray{Bool})
F = similar(B, Int, size(B))
for i=1:length(B)
for i in eachindex(B)
@inbounds F[i] = ($f)(A, B[i])
end
return F
end
function ($f)(A::StridedArray{Bool}, B::Bool)
F = similar(A, Int, size(A))
for i=1:length(A)
for i in eachindex(A)
@inbounds F[i] = ($f)(A[i], B)
end
return F
Expand All @@ -848,7 +848,7 @@ for f in (:+, :-)
@eval begin
function ($f)(A::StridedArray{Bool}, B::StridedArray{Bool})
F = similar(A, Int, promote_shape(size(A), size(B)))
for i=1:length(A)
for i in eachindex(A,B)
@inbounds F[i] = ($f)(A[i], B[i])
end
return F
Expand All @@ -861,23 +861,23 @@ end
function complex{S<:Real,T<:Real}(A::Array{S}, B::Array{T})
if size(A) != size(B); throw(DimensionMismatch()); end
F = similar(A, typeof(complex(zero(S),zero(T))))
for i=1:length(A)
for i in eachindex(A)
@inbounds F[i] = complex(A[i], B[i])
end
return F
end

function complex{T<:Real}(A::Real, B::Array{T})
F = similar(B, typeof(complex(A,zero(T))))
for i=1:length(B)
for i in eachindex(B)
@inbounds F[i] = complex(A, B[i])
end
return F
end

function complex{T<:Real}(A::Array{T}, B::Real)
F = similar(A, typeof(complex(zero(T),B)))
for i=1:length(A)
for i in eachindex(A)
@inbounds F[i] = complex(A[i], B)
end
return F
Expand Down
6 changes: 3 additions & 3 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ for f in (:trunc,:floor,:ceil,:round)
[ ($f)(T, x[i,j])::T for i = 1:size(x,1), j = 1:size(x,2) ]
end
function ($f){T}(::Type{T}, x::AbstractArray)
reshape([ ($f)(T, x[i])::T for i = 1:length(x) ], size(x))
reshape([ ($f)(T, x[i])::T for i in eachindex(x) ], size(x))
end
end
end
Expand All @@ -85,7 +85,7 @@ function round{R}(x::AbstractArray{R,2}, r::RoundingMode)
[ round(x[i,j], r) for i = 1:size(x,1), j = 1:size(x,2) ]
end
function round(x::AbstractArray, r::RoundingMode)
reshape([ round(x[i], r) for i = 1:length(x) ], size(x))
reshape([ round(x[i], r) for i in eachindex(x) ], size(x))
end

function round{T,R}(::Type{T}, x::AbstractArray{R,1}, r::RoundingMode)
Expand All @@ -95,7 +95,7 @@ function round{T,R}(::Type{T}, x::AbstractArray{R,2}, r::RoundingMode)
[ round(T, x[i,j], r)::T for i = 1:size(x,1), j = 1:size(x,2) ]
end
function round{T}(::Type{T}, x::AbstractArray, r::RoundingMode)
reshape([ round(T, x[i], r)::T for i = 1:length(x) ], size(x))
reshape([ round(T, x[i], r)::T for i in eachindex(x) ], size(x))
end

# adapted from Matlab File Exchange roundsd: http://www.mathworks.com/matlabcentral/fileexchange/26212
Expand Down
4 changes: 2 additions & 2 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ write(s::IO, x::Float64) = write(s, reinterpret(Int64,x))

function write(s::IO, a::AbstractArray)
nb = 0
for i = 1:length(a)
for i in eachindex(a)
nb += write(s, a[i])
end
nb
Expand Down Expand Up @@ -125,7 +125,7 @@ read{T}(s::IO, t::Type{T}, d1::Integer, dims::Integer...) =
read{T}(s::IO, ::Type{T}, dims::Dims) = read!(s, Array(T, dims))

function read!{T}(s::IO, a::Array{T})
for i = 1:length(a)
for i in eachindex(a)
a[i] = read(s, T)
end
return a
Expand Down
2 changes: 1 addition & 1 deletion base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ end
function frexp{T<:FloatingPoint}(A::Array{T})
f = similar(A)
e = Array(Int, size(A))
for i = 1:length(A)
for i in eachindex(A)
f[i], e[i] = frexp(A[i])
end
return (f, e)
Expand Down
19 changes: 18 additions & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,27 @@ stagedfunction eachindex{T,N}(::LinearSlow, A::AbstractArray{T,N})
:($meta; CartesianRange(CartesianIndex{$N}($(startargs...)), CartesianIndex{$N}($(stopargs...))))
end

stagedfunction eachindex{S,T,M,N}(::LinearSlow, A::AbstractArray{S,M}, B::AbstractArray{T,N})
K = max(M,N)
startargs = fill(1, K)
stopargs = [:(max(size(A,$i),size(B,$i))) for i=1:K]
meta = Expr(:meta, :inline)
:($meta; CartesianRange(CartesianIndex{$K}($(startargs...)), CartesianIndex{$K}($(stopargs...))))
end

eltype{I}(::Type{CartesianRange{I}}) = I
eltype{I}(::CartesianRange{I}) = I

start(iter::CartesianRange) = iter.start
stagedfunction start{I<:CartesianIndex}(iter::CartesianRange{I})
N = length(I)
cmp = [:(iter.start[$d] > iter.stop[$d]) for d = 1:N]
extest = Expr(:||, cmp...)
inc = [d < N ? :(iter.start[$d]) : :(iter.stop[$N]+1) for d = 1:N]
exstop = :(CartesianIndex{$N}($(inc...)))
quote
$extest ? $exstop : iter.start
end
end
stagedfunction next{I<:CartesianIndex}(iter::CartesianRange{I}, state)
N = length(I)
meta = Expr(:meta, :inline)
Expand Down
8 changes: 4 additions & 4 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -358,21 +358,21 @@ macro vectorize_1arg(S,f)
($f){$T<:$S}(x::AbstractArray{$T,2}) =
[ ($f)(x[i,j]) for i=1:size(x,1), j=1:size(x,2) ]
($f){$T<:$S}(x::AbstractArray{$T}) =
reshape([ ($f)(x[i]) for i=1:length(x) ], size(x))
reshape([ ($f)(x[i]) for i in eachindex(x) ], size(x))
end
end

macro vectorize_2arg(S,f)
S = esc(S); f = esc(f); T1 = esc(:T1); T2 = esc(:T2)
quote
($f){$T1<:$S, $T2<:$S}(x::($T1), y::AbstractArray{$T2}) =
reshape([ ($f)(x, y[i]) for i=1:length(y) ], size(y))
reshape([ ($f)(x, y[i]) for i in eachindex(y) ], size(y))
($f){$T1<:$S, $T2<:$S}(x::AbstractArray{$T1}, y::($T2)) =
reshape([ ($f)(x[i], y) for i=1:length(x) ], size(x))
reshape([ ($f)(x[i], y) for i in eachindex(x) ], size(x))

function ($f){$T1<:$S, $T2<:$S}(x::AbstractArray{$T1}, y::AbstractArray{$T2})
shp = promote_shape(size(x),size(y))
reshape([ ($f)(x[i], y[i]) for i=1:length(x) ], shp)
reshape([ ($f)(x[i], y[i]) for i in eachindex(x,y) ], shp)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ rand(r::AbstractRNG, T::Type, d1::Integer, dims::Integer...) = rand(r, T, tuple(
# moreover, a call like rand(r, NotImplementedType()) would be an infinite loop

function rand!{T}(r::AbstractRNG, A::AbstractArray{T})
for i = 1:length(A)
for i in eachindex(A)
@inbounds A[i] = rand(r, T)
end
A
Expand Down Expand Up @@ -356,7 +356,7 @@ end
function rand!{T<:Union(Float16, Float32)}(r::MersenneTwister, A::Array{T}, ::Type{CloseOpen})
rand!(r, A, Close1Open2)
I32 = one(Float32)
for i in 1:length(A)
for i in eachindex(A)
@inbounds A[i] = T(Float32(A[i])-I32) # faster than "A[i] -= one(T)" for T==Float16
end
A
Expand Down Expand Up @@ -1104,7 +1104,7 @@ function randn_unlikely(rng, idx, rabs, x)
end

function randn!(rng::AbstractRNG, A::AbstractArray{Float64})
for i = 1:length(A)
for i in eachindex(A)
@inbounds A[i] = randn(rng)
end
A
Expand Down Expand Up @@ -1137,7 +1137,7 @@ function randexp_unlikely(rng, idx, x)
end

function randexp!(rng::AbstractRNG, A::Array{Float64})
for i = 1:length(A)
for i in eachindex(A)
@inbounds A[i] = randexp(rng)
end
A
Expand Down
3 changes: 1 addition & 2 deletions base/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ end
##### standard deviation #####

function sqrt!(A::AbstractArray)
for i = 1:length(A)
for i in eachindex(A)
@inbounds A[i] = sqrt(A[i])
end
A
Expand Down Expand Up @@ -657,4 +657,3 @@ hist2d(v::AbstractMatrix, n1::Integer, n2::Integer) =
hist2d(v, histrange(sub(v,:,1),n1), histrange(sub(v,:,2),n2))
hist2d(v::AbstractMatrix, n::Integer) = hist2d(v, n, n)
hist2d(v::AbstractMatrix) = hist2d(v, sturges(size(v,1)))

6 changes: 6 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,12 @@ val, state = next(itr, state)
@test r[val] == 8
@test done(itr, state)

R = CartesianRange((1,3))
@test done(R, start(R)) == false
R = CartesianRange((0,3))
@test done(R, start(R)) == true
R = CartesianRange((3,0))
@test done(R, start(R)) == true

#rotates

Expand Down

0 comments on commit 3dbc828

Please sign in to comment.