Skip to content

Commit 4fa9e32

Browse files
authored
Support multidim booleans in checkbounds_indices (#38193)
Fixes #38192
1 parent d6b3ba1 commit 4fa9e32

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

base/multidimensional.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,16 @@ end
660660
checkindex(Bool, IA1, I[1]) & checkbounds_indices(Bool, IArest, tail(I))
661661
end
662662

663+
664+
@inline function checkbounds_indices(::Type{Bool}, IA::Tuple{},
665+
I::Tuple{AbstractArray{Bool,N},Vararg{Any}}) where N
666+
return checkbounds_indices(Bool, IA, (LogicalIndex(I[1]), tail(I)...))
667+
end
668+
@inline function checkbounds_indices(::Type{Bool}, IA::Tuple,
669+
I::Tuple{AbstractArray{Bool,N},Vararg{Any}}) where N
670+
return checkbounds_indices(Bool, IA, (LogicalIndex(I[1]), tail(I)...))
671+
end
672+
663673
function checkindex(::Type{Bool}, inds::Tuple, I::AbstractArray{<:CartesianIndex})
664674
b = true
665675
for i in I
@@ -773,7 +783,7 @@ end
773783
eachindex(IndexLinear(), A) == eachindex(IndexLinear(), I.mask)
774784
@inline checkbounds(::Type{Bool}, A::AbstractArray, I::LogicalIndex) = axes(A) == axes(I.mask)
775785
@inline checkindex(::Type{Bool}, indx::AbstractUnitRange, I::LogicalIndex) = (indx,) == axes(I.mask)
776-
checkindex(::Type{Bool}, inds::Tuple, I::LogicalIndex) = false
786+
checkindex(::Type{Bool}, inds::Tuple, I::LogicalIndex) = checkbounds_indices(Bool, inds, axes(I.mask))
777787

778788
ensure_indexable(I::Tuple{}) = ()
779789
@inline ensure_indexable(I::Tuple{Any, Vararg{Any}}) = (I[1], ensure_indexable(tail(I))...)

test/abstractarray.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ end
9393
@test checkbounds(Bool, A, trues(1, 5), trues(1, 4, 1), trues(1, 1, 2)) == false
9494
@test checkbounds(Bool, A, trues(1, 5), trues(1, 5, 1), trues(1, 1, 3)) == false
9595
@test checkbounds(Bool, A, trues(1, 5), :, 2) == false
96+
@test checkbounds(Bool, A, trues(5, 4), trues(3)) == true
97+
@test checkbounds(Bool, A, trues(4, 4), trues(3)) == true
98+
@test checkbounds(Bool, A, trues(5, 4), trues(2)) == false
99+
@test checkbounds(Bool, A, trues(6, 4), trues(3)) == false
100+
@test checkbounds(Bool, A, trues(5, 4), trues(4)) == false
96101
end
97102

98103
@testset "array of CartesianIndex" begin
@@ -453,6 +458,13 @@ function test_vector_indexing(::Type{T}, shape, ::Type{TestAbstractArray}) where
453458
@test B[mask1, mask2, trailing2] == A[mask1, mask2, trailing2] ==
454459
B[LinearIndices(mask1)[findall(mask1)], LinearIndices(mask2)[findall(mask2)], trailing2]
455460
@test B[mask1, 1, trailing2] == A[mask1, 1, trailing2] == LinearIndices(mask)[findall(mask1)]
461+
462+
if ndims(B) > 1
463+
maskfront = bitrand(shape[1:end-1])
464+
Bslice = B[ntuple(i->(:), ndims(B)-1)..., 1]
465+
@test B[maskfront,1] == Bslice[maskfront]
466+
@test size(B[maskfront, 1:1]) == (sum(maskfront), 1)
467+
end
456468
end
457469
end
458470
end
@@ -838,6 +850,13 @@ end
838850
@test ndims((1:3)[:,:,1:1,:,:,[1]]) == 6
839851
end
840852

853+
@testset "issue #38192" begin
854+
img = cat([1 2; 3 4], [1 5; 6 7]; dims=3)
855+
mask = img[:,:,1] .== img[:,:,2]
856+
img[mask,2] .= 0
857+
@test img == cat([1 2; 3 4], [0 5; 6 7]; dims=3)
858+
end
859+
841860
@testset "dispatch loop introduced in #19305" begin
842861
Z22, O33 = fill(0, 2, 2), fill(1, 3, 3)
843862
@test [(1:2) Z22; O33] == [[1,2] Z22; O33] == [[1 2]' Z22; O33]

0 commit comments

Comments
 (0)