Skip to content

Commit

Permalink
Merge pull request #4 from JuliaArrays/mb/fasteriteration
Browse files Browse the repository at this point in the history
Faster iteration over RepeatedRangeMatrix
  • Loading branch information
mbauman authored Apr 28, 2017
2 parents 435ed6e + 28c0fa7 commit 3a312a4
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/repeatedrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ RepeatedRangeMatrix{T}(r::Range{T}, at::AbstractVector{T}) = RepeatedRangeMatrix
Base.size(R::RepeatedRangeMatrix) = (length(R.r), length(R.at))
@compat Base.IndexStyle(::Type{<:RepeatedRangeMatrix}) = IndexCartesian()

# This coupled iteration over the two fields is 10-20x faster than Cartesian iteration
@inline function Base.start(R::RepeatedRangeMatrix)
is = start(R.r)
idone = done(R.r, is)
js = start(R.at)
jdone = done(R.at, js)
return (idone | jdone) ? ((one(eltype(R.r)), is), (one(eltype(R.at)), js), true) :
(next(R.r, is), next(R.at, js), false)
end
@inline function Base.next(R::RepeatedRangeMatrix, state)
(i, is), (j, js), _ = state
val = i + j
if done(R.r, is)
if done(R.at, js)
return (val, ((i, is), (j, js), true))
end
is = start(R.r)
j, js = next(R.at, js)
end
return (val, (next(R.r, is), (j, js), false))
end
@inline Base.done(R::RepeatedRangeMatrix, state) = state[end]

# Scalar indexing
@inline function Base.getindex(R::RepeatedRangeMatrix, i::Int, j::Int)
@boundscheck checkbounds(R, i, j)
Expand Down

0 comments on commit 3a312a4

Please sign in to comment.