-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
i=1:length(A)
to i in eachindex(A)
This fixes performance problems for many SubArray operations
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mbauman
Member
|
||
*(::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()) | ||
|
@@ -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 ## | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
2 comments
on commit 4297135
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just as in the discussion about Int^N, and @prcastro 's suggestion, could you have both? A nice short form, and a spelled out function, to make it easy to find documentation, etc.?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only an internal interface (not exported), so I don't personally care whether it's short or long. Jeff, if you like my suggestion of promote_linear
, I'm fine with that. Or should this just be promote_type
?
This is kind of weird. Could we use a different function for this?