Skip to content

Commit

Permalink
add intersect for CartesianIndices (#36643)
Browse files Browse the repository at this point in the history
  • Loading branch information
stev47 authored Jul 15, 2020
1 parent aa40074 commit 2850dfb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Standard library changes
* `Libdl` has been moved to `Base.Libc.Libdl`, however it is still accessible as an stdlib ([#35628]).
* `first` and `last` functions now accept an integer as second argument to get that many
leading or trailing elements of any iterable ([#34868]).
* `intersect` on `CartesianIndices` now returns `CartesianIndices` instead of `Vector{<:CartesianIndex}` ([#36643]).

#### LinearAlgebra
* New method `LinearAlgebra.issuccess(::CholeskyPivoted)` for checking whether pivoted Cholesky factorization was successful ([#36002]).
Expand Down
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 2850dfb

Please sign in to comment.