Skip to content

Commit

Permalink
add intersect for CartesianIndices
Browse files Browse the repository at this point in the history
  • Loading branch information
stev47 committed Jul 14, 2020
1 parent 5f2bb1d commit 90ee4a6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ module IteratorsMD

Base.LinearIndices(inds::CartesianIndices{N,R}) where {N,R} = LinearIndices{N,R}(inds.indices)

# array operations
Base.intersect(a::CartesianIndices{N}, b::CartesianIndices{N}) where N =
CartesianIndices(intersect.(a.indices, b.indices))

# Views of reshaped CartesianIndices are used for partitions — ensure these are fast
const CartesianPartition{T<:CartesianIndex, P<:CartesianIndices, R<:ReshapedArray{T,1,P}} = SubArray{T,1,R,Tuple{UnitRange{Int}},false}
eltype(::Type{PartitionIterator{T}}) where {T<:ReshapedArrayLF} = SubArray{eltype(T), 1, T, Tuple{UnitRange{Int}}, true}
Expand Down
5 changes: 5 additions & 0 deletions test/cartesian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ end
@test iterate(I, CartesianIndex(4, typemax(Int))) === nothing
end

@testset "CartesianIndices operations" begin
I = CartesianIndices((1:3, 4:6))
J = CartesianIndices((2:4, 3:5))

@test @inferred(intersect(I, J)) == CartesianIndices((2:3, 4:5))
end

0 comments on commit 90ee4a6

Please sign in to comment.