diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 2ed0451df77bb..92e70fdfbac3e 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -781,22 +781,28 @@ end end end # When wrapping a BitArray, lean heavily upon its internals. -@inline function iterate(L::Base.LogicalIndex{Int,<:BitArray}) +@inline function iterate(L::LogicalIndex{Int,<:BitArray}) L.sum == 0 && return nothing Bc = L.mask.chunks - return iterate(L, (1, @inbounds Bc[1])) + return iterate(L, (1, 1, (), @inbounds Bc[1])) end -@inline function iterate(L::Base.LogicalIndex{Int,<:BitArray}, s) +@inline function iterate(L::LogicalIndex{<:CartesianIndex,<:BitArray}) + L.sum == 0 && return nothing + Bc = L.mask.chunks + irest = ntuple(one, ndims(L.mask)-1) + return iterate(L, (1, 1, irest, @inbounds Bc[1])) +end +@inline function iterate(L::LogicalIndex{<:Any,<:BitArray}, (i1, Bi, irest, c)) Bc = L.mask.chunks - i1, c = s - while c==0 - i1 % UInt >= length(Bc) % UInt && return nothing - i1 += 1 - @inbounds c = Bc[i1] + while c == 0 + Bi >= length(Bc) && return nothing + i1 += 64 + @inbounds c = Bc[Bi+=1] end - tz = trailing_zeros(c) + 1 + tz = trailing_zeros(c) c = _blsr(c) - return ((i1-1)<<6 + tz, (i1, c)) + i1, irest = _overflowind(i1 + tz, irest, size(L.mask)) + return eltype(L)(i1, irest...), (i1 - tz, Bi, irest, c) end @inline checkbounds(::Type{Bool}, A::AbstractArray, I::LogicalIndex{<:Any,<:AbstractArray{Bool,1}}) =