Skip to content

Commit

Permalink
Add a more efficient implementation of in(::CartesianIndex, ::Cartesi…
Browse files Browse the repository at this point in the history
…anRange) (JuliaLang#18277)
  • Loading branch information
timholy authored and stevengj committed Aug 29, 2016
1 parent 10f3c35 commit 10c2be1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### Multidimensional iterators
module IteratorsMD

import Base: eltype, length, size, start, done, next, last, getindex, setindex!, linearindexing, min, max, zero, one, isless, eachindex, ndims, iteratorsize
import Base: eltype, length, size, start, done, next, last, in, getindex, setindex!, linearindexing, min, max, zero, one, isless, eachindex, ndims, iteratorsize
importall ..Base.Operators
import Base: simd_outer_range, simd_inner_length, simd_index
using Base: LinearFast, LinearSlow, AbstractCartesianIndex, fill_to_length, tail
Expand Down Expand Up @@ -130,6 +130,12 @@ length(iter::CartesianRange) = prod(size(iter))

last(iter::CartesianRange) = iter.stop

@inline function in{I<:CartesianIndex}(i::I, r::CartesianRange{I})
_in(true, i.I, r.start.I, r.stop.I)
end
_in(b, ::Tuple{}, ::Tuple{}, ::Tuple{}) = b
@inline _in(b, i, start, stop) = _in(b & (start[1] <= i[1] <= stop[1]), tail(i), tail(start), tail(stop))

simd_outer_range(iter::CartesianRange{CartesianIndex{0}}) = iter
function simd_outer_range{I}(iter::CartesianRange{I})
start = CartesianIndex(tail(iter.start.I))
Expand Down
8 changes: 8 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,14 @@ indexes = collect(R)
@test length(indexes) == 12
@test length(R) == 12
@test ndims(R) == 2
@test in(CartesianIndex((2,3)), R)
@test in(CartesianIndex((3,3)), R)
@test in(CartesianIndex((3,5)), R)
@test in(CartesianIndex((5,5)), R)
@test !in(CartesianIndex((1,3)), R)
@test !in(CartesianIndex((3,2)), R)
@test !in(CartesianIndex((3,6)), R)
@test !in(CartesianIndex((6,5)), R)

@test CartesianRange((3:5,-7:7)) == CartesianRange(CartesianIndex{2}(3,-7),CartesianIndex{2}(5,7))
@test CartesianRange((3,-7:7)) == CartesianRange(CartesianIndex{2}(3,-7),CartesianIndex{2}(3,7))
Expand Down

0 comments on commit 10c2be1

Please sign in to comment.