@@ -459,11 +459,6 @@ struct ExponentsIterator{M,D<:Union{Nothing,Int},O}
459
459
),
460
460
)
461
461
end
462
- if length (object) == 0 && isnothing (maxdegree)
463
- # Otherwise, it will incorrectly think that the iterator is infinite
464
- # while it actually has zero elements
465
- maxdegree = mindegree
466
- end
467
462
return new {M,typeof(maxdegree),typeof(object)} (
468
463
object,
469
464
mindegree,
@@ -477,21 +472,39 @@ Base.eltype(::Type{ExponentsIterator{M,D,O}}) where {M,D,O} = O
477
472
function Base. IteratorSize (:: Type{<:ExponentsIterator{M,Nothing}} ) where {M}
478
473
return Base. IsInfinite ()
479
474
end
475
+ function Base. IteratorSize (it:: ExponentsIterator{M,Nothing} ) where {M}
476
+ if isempty (it. object)
477
+ return Base. HasLength ()
478
+ end
479
+ return Base. IsInfinite ()
480
+ end
480
481
function Base. IteratorSize (:: Type{<:ExponentsIterator{M,Int}} ) where {M}
481
482
return Base. HasLength ()
482
483
end
483
484
484
- function Base . length (it:: ExponentsIterator{M,Int} ) where {M}
485
- if it . maxdegree < it. mindegree
485
+ function _length (it:: ExponentsIterator , maxdegree)
486
+ if maxdegree < it. mindegree
486
487
return 0
487
488
end
488
- len = binomial (nvariables (it) + it . maxdegree, nvariables (it))
489
+ len = binomial (nvariables (it) + maxdegree, nvariables (it))
489
490
if it. mindegree > 0
490
491
len -= binomial (nvariables (it) + it. mindegree - 1 , nvariables (it))
491
492
end
492
493
return len
493
494
end
494
495
496
+ function Base. length (it:: ExponentsIterator{M,Int} ) where {M}
497
+ return _length (it, it. maxdegree)
498
+ end
499
+
500
+ function Base. length (it:: ExponentsIterator{M,Nothing} ) where {M}
501
+ if isempty (it. object)
502
+ return _length (it, it. mindegree)
503
+ else
504
+ error (" The iterator is infinity because `maxdegree` is `nothing`." )
505
+ end
506
+ end
507
+
495
508
nvariables (it:: ExponentsIterator ) = length (it. object)
496
509
497
510
_last_lex_index (n, :: Type{LexOrder} ) = n
0 commit comments