@@ -664,47 +664,34 @@ Iteration =#
664
664
# `pairs(p)`: `i => pᵢ` possibly skipping over values of `i` with `pᵢ == 0` (SparsePolynomial)
665
665
# and possibly non ordered (SparsePolynomial)
666
666
# `monomials(p)`: iterates over pᵢ ⋅ basis(p, i) i ∈ keys(p)
667
- function Base. iterate (p:: AbstractPolynomial , state= nothing )
668
- i = firstindex (p)
669
- if state == nothing
670
- return (p[i], i)
671
- else
672
- j = lastindex (p)
673
- if i <= state < j
674
- return (p[state+ 1 ], state+ 1 )
675
- end
676
- return nothing
677
- end
667
+ function _iterate (p, state)
668
+ firstindex (p) <= state <= lastindex (p) || return nothing
669
+ return p[state], state+ 1
678
670
end
671
+ Base. iterate (p:: AbstractPolynomial , state = firstindex (p)) = _iterate (p, state)
679
672
680
673
# pairs map i -> aᵢ *possibly* skipping over ai == 0
681
674
# cf. abstractdict.jl
682
- struct PolynomialKeys{P}
675
+ struct PolynomialKeys{P} <: AbstractSet{Int}
683
676
p:: P
684
677
end
685
- struct PolynomialValues{P}
678
+ struct PolynomialValues{P, T} <: AbstractSet{T }
686
679
p:: P
680
+
681
+ PolynomialValues {P} (p:: P ) where {P} = new {P, eltype(p)} (p)
682
+ PolynomialValues (p:: P ) where {P} = new {P, eltype(p)} (p)
687
683
end
688
684
Base. keys (p:: AbstractPolynomial ) = PolynomialKeys (p)
689
685
Base. values (p:: AbstractPolynomial ) = PolynomialValues (p)
690
686
Base. length (p:: PolynomialValues ) = length (p. p. coeffs)
691
687
Base. length (p:: PolynomialKeys ) = length (p. p. coeffs)
692
688
Base. size (p:: Union{PolynomialValues, PolynomialKeys} ) = (length (p),)
693
- function Base. iterate (v:: PolynomialKeys , state= nothing )
694
- i = firstindex (v. p)
695
- state== nothing && return (i, i)
696
- j = lastindex (v. p)
697
- i <= state < j && return (state+ 1 , state+ 1 )
698
- return nothing
689
+ function Base. iterate (v:: PolynomialKeys , state = firstindex (v. p))
690
+ firstindex (v. p) <= state <= lastindex (v. p) || return nothing
691
+ return state, state+ 1
699
692
end
700
693
701
- function Base. iterate (v:: PolynomialValues , state= nothing )
702
- i = firstindex (v. p)
703
- state== nothing && return (v. p[i], i)
704
- j = lastindex (v. p)
705
- i <= state < j && return (v. p[state+ 1 ], state+ 1 )
706
- return nothing
707
- end
694
+ Base. iterate (v:: PolynomialValues , state = firstindex (v. p)) = _iterate (v. p, state)
708
695
709
696
710
697
# iterate over monomials of the polynomial
0 commit comments