Skip to content

Commit

Permalink
change to accumulate through return for inference
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Mar 28, 2019
1 parent e31a748 commit 185e6f4
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,31 +341,30 @@ module IteratorsMD
iterfirst, iterfirst
end
@inline function iterate(iter::CartesianIndices, state)
valid, I = inc((), state.I, first(iter).I, last(iter).I)
valid, I = __inc(state.I, first(iter).I, last(iter).I)
valid || return nothing
return I, I
return CartesianIndex(I...), CartesianIndex(I...)
end

# increment & carry
@inline function inc(state, start, stop)
_, I = inc((), state, start, stop)
return I
_, I = __inc(state, start, stop)
return CartesianIndex(I...)
end

# increment post check to avoid integer overflow
@inline inc(out, ::Tuple{}, ::Tuple{}, ::Tuple{}) = false, CartesianIndex(out...)
@inline function inc(out, state::Tuple{Int}, start::Tuple{Int}, stop::Tuple{Int})
@inline __inc(::Tuple{}, ::Tuple{}, ::Tuple{}) = false, ()
@inline function __inc(state::Tuple{Int}, start::Tuple{Int}, stop::Tuple{Int})
valid = state[1] < stop[1]
nextstate = CartesianIndex(out..., state[1]+1)
return valid, nextstate
return valid, (state[1]+1,)
end

@inline function inc(out, state, start, stop)
@inline function __inc(state, start, stop)
if state[1] < stop[1]
nextstate = CartesianIndex(out..., state[1]+1, tail(state)...)
return true, nextstate
return true, (state[1]+1, tail(state)...)
end
return inc((out..., start[1]), tail(state), tail(start), tail(stop))
valid, I = __inc(tail(state), tail(start), tail(stop))
return valid, (start[1], I...)
end

# 0-d cartesian ranges are special-cased to iterate once and only once
Expand Down Expand Up @@ -432,31 +431,30 @@ module IteratorsMD
iterfirst, iterfirst
end
@inline function iterate(r::Reverse{<:CartesianIndices}, state)
valid, I = dec((), state.I, last(r.itr).I, first(r.itr).I)
valid, I = __dec(state.I, last(r.itr).I, first(r.itr).I)
valid || return nothing
return I, I
return CartesianIndex(I...), CartesianIndex(I...)
end

# decrement & carry
@inline function dec(state, start, stop)
_, I = dec((), state, start, stop)
return I
_, I = __dec(state, start, stop)
return CartesianIndex(I...)
end

# decrement post check to avoid integer overflow
@inline dec(out, ::Tuple{}, ::Tuple{}, ::Tuple{}) = false, CartesianIndices(out...)
@inline function dec(out, state::Tuple{Int}, start::Tuple{Int}, stop::Tuple{Int})
@inline __dec(::Tuple{}, ::Tuple{}, ::Tuple{}) = false, ()
@inline function __dec(state::Tuple{Int}, start::Tuple{Int}, stop::Tuple{Int})
valid = state[1] > stop[1]
nextstate = CartesianIndex(out..., state[1]-1)
return valid, nextstate
return valid, (state[1]-1,)
end

@inline function dec(out, state, start, stop)
@inline function __dec(state, start, stop)
if state[1] > stop[1]
nextstate = CartesianIndex(out..., state[1]-1, tail(state)...)
return true, nextstate
return true, (state[1]-1, tail(state)...)
end
return dec((out..., start[1]), tail(state), tail(start), tail(stop))
valid, I = __dec(tail(state), tail(start), tail(stop))
return valid, (start[1], I...)
end

# 0-d cartesian ranges are special-cased to iterate once and only once
Expand Down

0 comments on commit 185e6f4

Please sign in to comment.