Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short-circuit all for AbstractFill #329

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,19 @@ end
# In particular, these make iszero(Eye(n)) efficient.
# use any/all on scalar to get Boolean error message
any(f::Function, x::AbstractFill) = !isempty(x) && any(f(getindex_value(x)))
all(f::Function, x::AbstractFill) = isempty(x) || all(f(getindex_value(x)))
function all(f::Function, x::AbstractFill)
fval = f(getindex_value(x))
# checking the Bool path before isempty(x) allows short-curcuiting
# in case the value is known from the type, e.g. in Zeros
if fval isa Bool
return fval || isempty(x)
elseif isempty(x) # cases like all(Fill(2,0))
return true
elseif ismissing(fval)
return missing
end
return all(fval)
end
any(x::AbstractFill) = any(identity, x)
all(x::AbstractFill) = all(identity, x)

Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,9 @@ end
@test iszero(Fill(SMatrix{2,2}(0,0,0,0), 2))
@test iszero(Fill(SMatrix{2,2}(0,0,0,1), 0))

# compile-time evaluation
@test @inferred((Z -> Val(iszero(Z)))(Zeros(3,3))) == Val(true)

@testset "all/any" begin
@test any(Ones{Bool}(10)) === all(Ones{Bool}(10)) === any(Fill(true,10)) === all(Fill(true,10)) === true
@test any(Zeros{Bool}(10)) === all(Zeros{Bool}(10)) === any(Fill(false,10)) === all(Fill(false,10)) === false
Expand All @@ -1345,6 +1348,8 @@ end
@test all(Fill(2,0))
@test !any(Fill(2,0))
@test any(Trues(2,0)) == any(trues(2,0))
@test_throws TypeError all(Fill(2,2))
@test all(iszero, Fill(missing,2)) === all(iszero, fill(missing,2))
end

@testset "Error" begin
Expand Down