Skip to content

Commit 3beca78

Browse files
authored
Zero-allocation isone (#239)
1 parent c1ea8bb commit 3beca78

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/comparison.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ Base.iszero(m::AbstractMonomial) = false
55
Base.iszero(t::AbstractTerm) = iszero(coefficient(t))
66
Base.iszero(t::AbstractPolynomial) = iszero(nterms(t))
77

8+
Base.isone(v::AbstractVariable) = false
9+
Base.isone(m::AbstractMonomial) = isconstant(m)
10+
Base.isone(t::AbstractTerm) = isone(coefficient(t)) && isconstant(monomial(t))
11+
function Base.isone(p::AbstractPolynomial)
12+
return isone(nterms(p)) && isone(first(terms(p)))
13+
end
14+
815
# See https://github.com/blegat/MultivariatePolynomials.jl/issues/22
916
# avoids the call to be transfered to left_constant_eq
1017
Base.:(==)(α::Nothing, x::APL) = false

test/allocations.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module TestAllocations
2-
32
include("utils.jl")
43

54
function runtests()
@@ -86,6 +85,12 @@ function test_isapproxzero()
8685
alloc_test(0) do
8786
isapproxzero(q; ztol = 1e-8)
8887
end
88+
alloc_test(0) do
89+
iszero(q)
90+
end
91+
alloc_test(0) do
92+
isone(q)
93+
end
8994
end
9095

9196
function _test_gcd(T)

0 commit comments

Comments
 (0)