Skip to content

Commit 2e884f7

Browse files
authored
Return nothing for findfirst with empty arrays. (#610)
1 parent 7f11083 commit 2e884f7

File tree

3 files changed

+6
-0
lines changed

3 files changed

+6
-0
lines changed

src/host/indexing.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Base.getindex(ei::EachIndex, i::Int) = ei.indices[i]
191191
Base.IndexStyle(::Type{<:EachIndex}) = Base.IndexLinear()
192192

193193
function Base.findfirst(f::Function, A::AnyGPUArray)
194+
isempty(A) && return nothing
194195
indices = EachIndex(A)
195196
dummy_index = first(indices)
196197

test/testsuite.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ using Test
1616

1717
using Adapt
1818

19+
test_result(a, b; kwargs...) = a == b
1920
test_result(a::Number, b::Number; kwargs...) = (a, b; kwargs...)
2021
test_result(a::Missing, b::Missing; kwargs...) = true
2122
test_result(a::Number, b::Missing; kwargs...) = false

test/testsuite/indexing.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ end
170170
let x = rand(Float32, 10, 10)
171171
@test findfirst(>(0.5f0), x) == findfirst(>(0.5f0), AT(x))
172172
end
173+
174+
# emtpy
175+
@test compare(findfirst, AT, Bool[])
176+
@test compare(x->findfirst(>(0.5f0), x), AT, Float32[])
173177
end
174178

175179
@testset "findmax & findmin" begin

0 commit comments

Comments
 (0)