Skip to content

Commit

Permalink
Zero-allocation isone (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat authored Feb 14, 2023
1 parent c1ea8bb commit 3beca78
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/comparison.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ Base.iszero(m::AbstractMonomial) = false
Base.iszero(t::AbstractTerm) = iszero(coefficient(t))
Base.iszero(t::AbstractPolynomial) = iszero(nterms(t))

Base.isone(v::AbstractVariable) = false
Base.isone(m::AbstractMonomial) = isconstant(m)
Base.isone(t::AbstractTerm) = isone(coefficient(t)) && isconstant(monomial(t))
function Base.isone(p::AbstractPolynomial)
return isone(nterms(p)) && isone(first(terms(p)))
end

# See https://github.com/blegat/MultivariatePolynomials.jl/issues/22
# avoids the call to be transfered to left_constant_eq
Base.:(==)(α::Nothing, x::APL) = false
Expand Down
7 changes: 6 additions & 1 deletion test/allocations.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module TestAllocations

include("utils.jl")

function runtests()
Expand Down Expand Up @@ -86,6 +85,12 @@ function test_isapproxzero()
alloc_test(0) do
isapproxzero(q; ztol = 1e-8)
end
alloc_test(0) do
iszero(q)
end
alloc_test(0) do
isone(q)
end
end

function _test_gcd(T)
Expand Down

0 comments on commit 3beca78

Please sign in to comment.