Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a more efficient implementation of in(::CartesianIndex, ::CartesianRange) #18277

Merged
merged 1 commit into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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