From 43d296111b085abde183e337d1dd29e09a39447b Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Fri, 25 Aug 2023 06:18:22 -0400 Subject: [PATCH] Revert "Optimize findall(f, ::AbstractArray{Bool}) (#42202)" (#51039) This reverts commit 4c4c94f4781da4f4109086368205db8a2f7ec7c4. (Except keeps the tests and adds a new one) fixes #46425 (cherry picked from commit defe187ed5b4f274778c964f979138334a2d1dfb) --- base/array.jl | 39 ++++++++------------------------------- test/arrayops.jl | 9 +++++++++ 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/base/array.jl b/base/array.jl index 825b5f9f7e206..7fb3d0501bb7b 100644 --- a/base/array.jl +++ b/base/array.jl @@ -2487,42 +2487,19 @@ function findall(A) end # Allocating result upfront is faster (possible only when collection can be iterated twice) -function _findall(f::Function, A::AbstractArray{Bool}) - n = count(f, A) +function findall(A::AbstractArray{Bool}) + n = count(A) I = Vector{eltype(keys(A))}(undef, n) - isempty(I) && return I - _findall(f, I, A) -end - -function _findall(f::Function, I::Vector, A::AbstractArray{Bool}) - cnt = 1 - len = length(I) - for (k, v) in pairs(A) - @inbounds I[cnt] = k - cnt += f(v) - cnt > len && return I - end - # In case of impure f, this line could potentially be hit. In that case, - # we can't assume I is the correct length. - resize!(I, cnt - 1) -end - -function _findall(f::Function, I::Vector, A::AbstractVector{Bool}) - i = firstindex(A) cnt = 1 - len = length(I) - while cnt ≤ len - @inbounds I[cnt] = i - cnt += f(@inbounds A[i]) - i = nextind(A, i) + for (i,a) in pairs(A) + if a + I[cnt] = i + cnt += 1 + end end - cnt - 1 == len ? I : resize!(I, cnt - 1) + I end -findall(f::Function, A::AbstractArray{Bool}) = _findall(f, A) -findall(f::Fix2{typeof(in)}, A::AbstractArray{Bool}) = _findall(f, A) -findall(A::AbstractArray{Bool}) = _findall(identity, A) - findall(x::Bool) = x ? [1] : Vector{Int}() findall(testf::Function, x::Number) = testf(x) ? [1] : Vector{Int}() findall(p::Fix2{typeof(in)}, x::Number) = x in p.x ? [1] : Vector{Int}() diff --git a/test/arrayops.jl b/test/arrayops.jl index 770cec3705038..ba02847094b0e 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -604,6 +604,15 @@ end @testset "issue 43078" begin @test_throws TypeError findall([1]) end + + @testset "issue #46425" begin + counter = 0 + function pred46425(x) + counter += 1 + counter < 4 && x + end + @test findall(pred46425, [false, false, true, true]) == [3] + end end @testset "find with Matrix" begin A = [1 2 0; 3 4 0]