Skip to content

Commit d85108a

Browse files
N5N3KristofferC
andauthored
Add missing optimization for iterate(::LogicalIndex{<:CartesianIndex,<:BitArray}) (#43448)
* make `collect(::LogicalIndex)` faster by call `findall` * borrow code from findall borrow code from findall * Update base/multidimensional.jl Co-authored-by: Kristoffer Carlsson <kcarlsson89@gmail.com> Co-authored-by: Kristoffer Carlsson <kcarlsson89@gmail.com>
1 parent 1a3da30 commit d85108a

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

base/multidimensional.jl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -781,22 +781,28 @@ end
781781
end
782782
end
783783
# When wrapping a BitArray, lean heavily upon its internals.
784-
@inline function iterate(L::Base.LogicalIndex{Int,<:BitArray})
784+
@inline function iterate(L::LogicalIndex{Int,<:BitArray})
785785
L.sum == 0 && return nothing
786786
Bc = L.mask.chunks
787-
return iterate(L, (1, @inbounds Bc[1]))
787+
return iterate(L, (1, 1, (), @inbounds Bc[1]))
788788
end
789-
@inline function iterate(L::Base.LogicalIndex{Int,<:BitArray}, s)
789+
@inline function iterate(L::LogicalIndex{<:CartesianIndex,<:BitArray})
790+
L.sum == 0 && return nothing
791+
Bc = L.mask.chunks
792+
irest = ntuple(one, ndims(L.mask)-1)
793+
return iterate(L, (1, 1, irest, @inbounds Bc[1]))
794+
end
795+
@inline function iterate(L::LogicalIndex{<:Any,<:BitArray}, (i1, Bi, irest, c))
790796
Bc = L.mask.chunks
791-
i1, c = s
792-
while c==0
793-
i1 % UInt >= length(Bc) % UInt && return nothing
794-
i1 += 1
795-
@inbounds c = Bc[i1]
797+
while c == 0
798+
Bi >= length(Bc) && return nothing
799+
i1 += 64
800+
@inbounds c = Bc[Bi+=1]
796801
end
797-
tz = trailing_zeros(c) + 1
802+
tz = trailing_zeros(c)
798803
c = _blsr(c)
799-
return ((i1-1)<<6 + tz, (i1, c))
804+
i1, irest = _overflowind(i1 + tz, irest, size(L.mask))
805+
return eltype(L)(i1, irest...), (i1 - tz, Bi, irest, c)
800806
end
801807

802808
@inline checkbounds(::Type{Bool}, A::AbstractArray, I::LogicalIndex{<:Any,<:AbstractArray{Bool,1}}) =

0 commit comments

Comments
 (0)