Skip to content

Commit 3a312a4

Browse files
authored
Merge pull request #4 from JuliaArrays/mb/fasteriteration
Faster iteration over RepeatedRangeMatrix
2 parents 435ed6e + 28c0fa7 commit 3a312a4

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/repeatedrange.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ RepeatedRangeMatrix{T}(r::Range{T}, at::AbstractVector{T}) = RepeatedRangeMatrix
1414
Base.size(R::RepeatedRangeMatrix) = (length(R.r), length(R.at))
1515
@compat Base.IndexStyle(::Type{<:RepeatedRangeMatrix}) = IndexCartesian()
1616

17+
# This coupled iteration over the two fields is 10-20x faster than Cartesian iteration
18+
@inline function Base.start(R::RepeatedRangeMatrix)
19+
is = start(R.r)
20+
idone = done(R.r, is)
21+
js = start(R.at)
22+
jdone = done(R.at, js)
23+
return (idone | jdone) ? ((one(eltype(R.r)), is), (one(eltype(R.at)), js), true) :
24+
(next(R.r, is), next(R.at, js), false)
25+
end
26+
@inline function Base.next(R::RepeatedRangeMatrix, state)
27+
(i, is), (j, js), _ = state
28+
val = i + j
29+
if done(R.r, is)
30+
if done(R.at, js)
31+
return (val, ((i, is), (j, js), true))
32+
end
33+
is = start(R.r)
34+
j, js = next(R.at, js)
35+
end
36+
return (val, (next(R.r, is), (j, js), false))
37+
end
38+
@inline Base.done(R::RepeatedRangeMatrix, state) = state[end]
39+
1740
# Scalar indexing
1841
@inline function Base.getindex(R::RepeatedRangeMatrix, i::Int, j::Int)
1942
@boundscheck checkbounds(R, i, j)

0 commit comments

Comments
 (0)