Skip to content

Commit

Permalink
add special case for findlast on tuples of length >= 32, fixes #45117 (
Browse files Browse the repository at this point in the history
  • Loading branch information
pcjentsch authored May 10, 2022
1 parent 0ba4e71 commit c69a202
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,17 @@ function _findfirst_loop(f::Function, t)
end
findfirst(f::Function, t::Tuple) = length(t) < 32 ? _findfirst_rec(f, 1, t) : _findfirst_loop(f, t)

function findlast(f::Function, x::Tuple)
findlast(f::Function, t::Tuple) = length(t) < 32 ? _findlast_rec(f, t) : _findlast_loop(f, t)
function _findlast_rec(f::Function, x::Tuple)
r = findfirst(f, reverse(x))
return isnothing(r) ? r : length(x) - r + 1
end
function _findlast_loop(f::Function, t)
for i in reverse(1:length(t))
f(t[i]) && return i
end
return nothing
end

## filter ##

Expand Down
6 changes: 6 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,12 @@ end
@test Base.return_types() do
findlast(==(0), (1.0,2,3f0))
end == Any[Nothing]

@testset "long tuples" begin
longtuple = ntuple(i -> i in (15,17) ? 1 : 0, 40)
@test findfirst(isequal(1), longtuple) == 15
@test findlast(isequal(1), longtuple) == 17
end
end

@testset "properties" begin
Expand Down

0 comments on commit c69a202

Please sign in to comment.