Skip to content

Commit

Permalink
Revert "Optimize findall(f, ::AbstractArray{Bool}) (#42202)" (#51039)
Browse files Browse the repository at this point in the history
This reverts commit 4c4c94f. (Except
keeps the tests and adds a new one)

fixes #46425

(cherry picked from commit defe187)
  • Loading branch information
JeffBezanson authored and KristofferC committed Sep 15, 2023
1 parent 78c8cf1 commit aa11508
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
39 changes: 8 additions & 31 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}()
Expand Down
9 changes: 9 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit aa11508

Please sign in to comment.