Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Disentangle arrays in iteration #15434

Merged
merged 1 commit into from
Mar 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -964,21 +964,20 @@ function isequal(A::AbstractArray, B::AbstractArray)
if isa(A,Range) != isa(B,Range)
return false
end
for i in eachindex(A,B)
if !isequal(A[i], B[i])
for (a, b) in zip(A, B)
if !isequal(a, b)
return false
end
end
return true
end

function lexcmp(A::AbstractArray, B::AbstractArray)
nA, nB = length(A), length(B)
for i = 1:min(nA, nB)
res = lexcmp(A[i], B[i])
for (a, b) in zip(A, B)
res = lexcmp(a, b)
res == 0 || return res
end
return cmp(nA, nB)
return cmp(length(A), length(B))
end

function (==)(A::AbstractArray, B::AbstractArray)
Expand All @@ -988,8 +987,8 @@ function (==)(A::AbstractArray, B::AbstractArray)
if isa(A,Range) != isa(B,Range)
return false
end
for i in eachindex(A,B)
if !(A[i]==B[i])
for (a, b) in zip(A, B)
if !(a == b)
return false
end
end
Expand Down
38 changes: 16 additions & 22 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ for f in (:-, :~, :conj, :sign)
@eval begin
function ($f)(A::AbstractArray)
F = similar(A)
for i in eachindex(A)
F[i] = ($f)(A[i])
for (iF, iA) in zip(eachindex(F), eachindex(A))
F[iF] = ($f)(A[iA])
end
return F
end
Expand All @@ -28,8 +28,8 @@ imag(A::AbstractArray) = reshape([ imag(x) for x in A ], size(A))

function !(A::AbstractArray{Bool})
F = similar(A)
for i in eachindex(A)
F[i] = !A[i]
for (iF, iA) in zip(eachindex(F), eachindex(A))
F[iF] = !A[iA]
end
return F
end
Expand Down Expand Up @@ -66,35 +66,29 @@ for (f,F) in ((:+, AddFun()),
@eval begin
function ($f){S,T}(A::Range{S}, B::Range{T})
F = similar(A, promote_op($F,S,T), promote_shape(size(A),size(B)))
i = 1
for (a,b) in zip(A,B)
@inbounds F[i] = ($f)(a, b)
i += 1
for (iF, iA, iB) in zip(eachindex(F), eachindex(A), eachindex(B))
@inbounds F[iF] = ($f)(A[iA], B[iB])
end
return F
end
function ($f){S,T}(A::AbstractArray{S}, B::Range{T})
F = similar(A, promote_op($F,S,T), promote_shape(size(A),size(B)))
i = 1
for b in B
@inbounds F[i] = ($f)(A[i], b)
i += 1
for (iF, iA, iB) in zip(eachindex(F), eachindex(A), eachindex(B))
@inbounds F[iF] = ($f)(A[iA], B[iB])
end
return F
end
function ($f){S,T}(A::Range{S}, B::AbstractArray{T})
F = similar(B, promote_op($F,S,T), promote_shape(size(A),size(B)))
i = 1
for a in A
@inbounds F[i] = ($f)(a, B[i])
i += 1
for (iF, iA, iB) in zip(eachindex(F), eachindex(A), eachindex(B))
@inbounds F[iF] = ($f)(A[iA], B[iB])
end
return F
end
function ($f){S,T}(A::AbstractArray{S}, B::AbstractArray{T})
F = similar(A, promote_op($F,S,T), promote_shape(size(A),size(B)))
for i in eachindex(A,B)
@inbounds F[i] = ($f)(A[i], B[i])
for (iF, iA, iB) in zip(eachindex(F), eachindex(A), eachindex(B))
@inbounds F[iF] = ($f)(A[iA], B[iB])
end
return F
end
Expand All @@ -116,15 +110,15 @@ for (f,F) in ((:.+, DotAddFun()),
@eval begin
function ($f){T}(A::Number, B::AbstractArray{T})
F = similar(B, promote_array_type($F,typeof(A),T))
for i in eachindex(B)
@inbounds F[i] = ($f)(A, B[i])
for (iF, iB) in zip(eachindex(F), eachindex(B))
@inbounds F[iF] = ($f)(A, B[iB])
end
return F
end
function ($f){T}(A::AbstractArray{T}, B::Number)
F = similar(A, promote_array_type($F,typeof(B),T))
for i in eachindex(A)
@inbounds F[i] = ($f)(A[i], B)
for (iF, iA) in zip(eachindex(F), eachindex(A))
@inbounds F[iF] = ($f)(A[iA], B)
end
return F
end
Expand Down
3 changes: 1 addition & 2 deletions base/combinatorics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ Return the inverse permutation of `v`
function invperm(a::AbstractVector)
b = zero(a) # similar vector of zeros
n = length(a)
@inbounds for i in eachindex(a)
j = a[i]
@inbounds for (i, j) in enumerate(a)
((1 <= j <= n) && b[j] == 0) ||
throw(ArgumentError("argument is not a permutation"))
b[j] = i
Expand Down
12 changes: 6 additions & 6 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -795,24 +795,24 @@ promote_array_type{S<:Union{Complex, Real}, AT<:AbstractFloat}(F, ::Type{S}, ::T
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 in eachindex(A)
@inbounds F[i] = complex(A[i], B[i])
for (iF, iA, iB) in zip(eachindex(F), eachindex(A), eachindex(B))
@inbounds F[iF] = complex(A[iA], B[iB])
end
return F
end

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

function complex{T<:Real}(A::Array{T}, B::Real)
F = similar(A, typeof(complex(zero(T),B)))
for i in eachindex(A)
@inbounds F[i] = complex(A[i], B)
for (iF, iA) in zip(eachindex(F), eachindex(A))
@inbounds F[iF] = complex(A[iA], B)
end
return F
end
2 changes: 1 addition & 1 deletion base/dates/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ for op in (:.+, :.-)
($op_){P<:GeneralPeriod}(x::GeneralPeriod,Y::StridedArray{P}) = ($op)(Y,x) |> ($op_)
($op_){P<:GeneralPeriod}(Y::StridedArray{P},x::GeneralPeriod) = ($op)(Y,x)
($op_){P<:GeneralPeriod, Q<:GeneralPeriod}(X::StridedArray{P}, Y::StridedArray{Q}) =
reshape(CompoundPeriod[($op_)(X[i],Y[i]) for i in eachindex(X, Y)], promote_shape(size(X),size(Y)))
reshape(CompoundPeriod[($op_)(x,y) for (x,y) in zip(X, Y)], promote_shape(size(X),size(Y)))
end
end

Expand Down
16 changes: 8 additions & 8 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,44 @@ round{T<:Integer}(::Type{T}, x::AbstractFloat, r::RoundingMode) = trunc(T,round(
for f in (:trunc,:floor,:ceil,:round)
@eval begin
function ($f){T,R}(::Type{T}, x::AbstractArray{R,1})
[ ($f)(T, x[i])::T for i = 1:length(x) ]
[ ($f)(T, y)::T for y in x ]
end
function ($f){T,R}(::Type{T}, x::AbstractArray{R,2})
[ ($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 in eachindex(x) ], size(x))
reshape([ ($f)(T, y)::T for y in x ], size(x))
end
function ($f){R}(x::AbstractArray{R,1}, digits::Integer, base::Integer=10)
[ ($f)(x[i], digits, base) for i = 1:length(x) ]
[ ($f)(y, digits, base) for y in x ]
end
function ($f){R}(x::AbstractArray{R,2}, digits::Integer, base::Integer=10)
[ ($f)(x[i,j], digits, base) for i = 1:size(x,1), j = 1:size(x,2) ]
end
function ($f)(x::AbstractArray, digits::Integer, base::Integer=10)
reshape([ ($f)(x[i], digits, base) for i in eachindex(x) ], size(x))
reshape([ ($f)(y, digits, base) for y in x ], size(x))
end
end
end

function round{R}(x::AbstractArray{R,1}, r::RoundingMode)
[ round(x[i], r) for i = 1:length(x) ]
[ round(y, r) for y in x ]
end
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 in eachindex(x) ], size(x))
reshape([ round(y, r) for y in x ], size(x))
end

function round{T,R}(::Type{T}, x::AbstractArray{R,1}, r::RoundingMode)
[ round(T, x[i], r)::T for i = 1:length(x) ]
[ round(T, y, r)::T for y in x ]
end
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 in eachindex(x) ], size(x))
reshape([ round(T, y, r)::T for y in x ], size(x))
end

# adapted from Matlab File Exchange roundsd: http://www.mathworks.com/matlabcentral/fileexchange/26212
Expand Down
10 changes: 5 additions & 5 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ end
write(s::IO, x::Bool) = write(s, UInt8(x))
write(to::IO, p::Ptr) = write(to, convert(UInt, p))

function write(s::IO, a::AbstractArray)
function write(s::IO, A::AbstractArray)
nb = 0
for i in eachindex(a)
nb += write(s, a[i])
for a in A
nb += write(s, a)
end
return nb
end
Expand All @@ -159,8 +159,8 @@ end
return unsafe_write(s, pointer(a), sizeof(a))
else
nb = 0
for i in eachindex(a)
nb += write(s, a[i])
for b in a
nb += write(s, b)
end
return nb
end
Expand Down
30 changes: 6 additions & 24 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ function generic_scale!(C::AbstractArray, X::AbstractArray, s::Number)
if length(C) != length(X)
throw(DimensionMismatch("first array has length $(length(C)) which does not match the length of the second, $(length(X))."))
end
if size(C) == size(X)
for I in eachindex(C, X)
@inbounds C[I] = X[I]*s
end
else
for (IC, IX) in zip(eachindex(C), eachindex(X))
@inbounds C[IC] = X[IX]*s
end
for (IC, IX) in zip(eachindex(C), eachindex(X))
@inbounds C[IC] = X[IX]*s
end
C
end
Expand All @@ -39,14 +33,8 @@ function generic_scale!(C::AbstractArray, s::Number, X::AbstractArray)
throw(DimensionMismatch("first array has length $(length(C)) which does not
match the length of the second, $(length(X))."))
end
if size(C) == size(X)
for I in eachindex(C, X)
@inbounds C[I] = s*X[I]
end
else
for (IC, IX) in zip(eachindex(C), eachindex(X))
@inbounds C[IC] = s*X[IX]
end
for (IC, IX) in zip(eachindex(C), eachindex(X))
@inbounds C[IC] = s*X[IX]
end
C
end
Expand Down Expand Up @@ -273,14 +261,8 @@ function vecdot(x::AbstractArray, y::AbstractArray)
return dot(zero(eltype(x)), zero(eltype(y)))
end
s = zero(dot(x[1], y[1]))
if size(x) == size(y)
for I in eachindex(x, y)
@inbounds s += dot(x[I], y[I])
end
else
for (Ix, Iy) in zip(eachindex(x), eachindex(y))
@inbounds s += dot(x[Ix], y[Iy])
end
for (Ix, Iy) in zip(eachindex(x), eachindex(y))
@inbounds s += dot(x[Ix], y[Iy])
end
s
end
Expand Down
6 changes: 3 additions & 3 deletions base/linalg/lapack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ subsetrows(X::AbstractVector, Y::AbstractArray, k) = Y[1:k]
subsetrows(X::AbstractMatrix, Y::AbstractArray, k) = Y[1:k, :]

function chkfinite(A::StridedMatrix)
for i = eachindex(A)
if !isfinite(A[i])
throw(ArgumentError("matrix contains NaNs"))
for a in A
if !isfinite(a)
throw(ArgumentError("matrix contains Infs or NaNs"))
end
end
return true
Expand Down
10 changes: 5 additions & 5 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ function frexp{T<:AbstractFloat}(x::T)
end

function frexp{T<:AbstractFloat}(A::Array{T})
f = similar(A)
e = Array(Int, size(A))
for i in eachindex(A)
f[i], e[i] = frexp(A[i])
F = similar(A)
E = Array(Int, size(A))
for (iF, iE, iA) in zip(eachindex(F), eachindex(E), eachindex(A))
F[iF], E[iE] = frexp(A[iA])
end
return (f, e)
return (F, E)
end

modf(x) = rem(x,one(x)), trunc(x)
Expand Down
40 changes: 10 additions & 30 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,9 @@ function _unsafe_getindex(::LinearIndexing, src::AbstractArray, I::AbstractArray
size(dest) == (N,) || throw(DimensionMismatch())
D = eachindex(dest)
Ds = start(D)
s = 0
for b in eachindex(I)
s+=1
@inbounds Ib = I[b]
if Ib
for (i, s) in zip(eachindex(I), eachindex(src))
@inbounds Ii = I[i]
if Ii
d, Ds = next(D, Ds)
@inbounds dest[d] = src[s]
end
Expand Down Expand Up @@ -304,15 +302,13 @@ end
function _unsafe_setindex!(::LinearIndexing, A::AbstractArray, x, I::AbstractArray{Bool})
X = _iterable(x)
Xs = start(X)
i = 0
c = 0
for b in eachindex(I)
i+=1
@inbounds Ib = I[b]
if Ib
for (iA, i) in zip(eachindex(A), eachindex(I))
@inbounds Ii = I[i]
if Ii
done(X, Xs) && throw_setindex_mismatch(x, c+1)
(v, Xs) = next(X, Xs)
@inbounds A[i] = v
@inbounds A[iA] = v
c += 1
end
end
Expand Down Expand Up @@ -526,25 +522,9 @@ function fill!{T}(A::AbstractArray{T}, x)
end

function copy!{T,N}(dest::AbstractArray{T,N}, src::AbstractArray{T,N})
samesize = true
for d = 1:N
if size(dest,d) != size(src,d)
samesize = false
break
end
end
if samesize && linearindexing(dest) == linearindexing(src)
for I in eachindex(dest)
@inbounds dest[I] = src[I]
end
else
length(dest) >= length(src) || throw(BoundsError())
iterdest = eachindex(dest)
sdest = start(iterdest)
for Isrc in eachindex(src)
Idest, sdest = next(iterdest, sdest)
@inbounds dest[Idest] = src[Isrc]
end
length(dest) >= length(src) || throw(BoundsError())
for (Isrc, Idest) in zip(eachindex(src), eachindex(dest))
@inbounds dest[Idest] = src[Isrc]
end
dest
end
Expand Down
Loading