346
346
347
347
Base. isless (t1:: AbstractTermLike , t2:: AbstractTermLike ) = compare (t1, t2) < 0
348
348
349
- _last_lex_index (n, :: Type{LexOrder} ) = n
350
- _prev_lex_index (i, :: Type{LexOrder} ) = i - 1
351
- _not_first_indices (n, :: Type{LexOrder} ) = n: - 1 : 2
352
- _last_lex_index (_, :: Type{InverseLexOrder} ) = 1
353
- _prev_lex_index (i, :: Type{InverseLexOrder} ) = i + 1
354
- _not_first_indices (n, :: Type{InverseLexOrder} ) = 1 : (n- 1 )
355
- _last_lex_index (n, :: Type{Graded{M}} ) where {M} = _last_lex_index (n, M)
356
- _prev_lex_index (i, :: Type{Graded{M}} ) where {M} = _prev_lex_index (i, M)
357
- _not_first_indices (n, :: Type{Graded{M}} ) where {M} = _not_first_indices (n, M)
358
-
359
349
"""
360
350
struct ExponentsIterator{M}(
361
351
object;
@@ -418,25 +408,39 @@ struct ExponentsIterator{M,D<:Union{Nothing,Int},O}
418
408
mindegree:: Int
419
409
maxdegree:: D
420
410
inline:: Bool
421
- end
422
411
423
- function ExponentsIterator {M} (
424
- object;
425
- mindegree:: Int = 0 ,
426
- maxdegree:: Union{Nothing,Int} = nothing ,
427
- inline:: Bool = false ,
428
- ) where {M}
429
- if length (object) == 0 && isnothing (maxdegree)
430
- # Otherwise, it will incorrectly think that the iterator is infinite
431
- # while it actually has zero elements
432
- maxdegree = mindegree
412
+ function ExponentsIterator {M} (
413
+ object;
414
+ mindegree:: Int = 0 ,
415
+ maxdegree:: Union{Nothing,Int} = nothing ,
416
+ inline:: Bool = false ,
417
+ ) where {M}
418
+ if mindegree < 0
419
+ throw (
420
+ ArgumentError (
421
+ " The `mindegree` of `ExponentsIterator` cannot be negative." ,
422
+ ),
423
+ )
424
+ end
425
+ if M <: Reverse
426
+ throw (
427
+ ArgumentError (
428
+ " Ordering `$M ` is not a valid ordering, use `Graded{$M }` instead." ,
429
+ ),
430
+ )
431
+ end
432
+ if length (object) == 0 && isnothing (maxdegree)
433
+ # Otherwise, it will incorrectly think that the iterator is infinite
434
+ # while it actually has zero elements
435
+ maxdegree = mindegree
436
+ end
437
+ return new {M,typeof(maxdegree),typeof(object)} (
438
+ object,
439
+ mindegree,
440
+ maxdegree,
441
+ inline,
442
+ )
433
443
end
434
- return ExponentsIterator {M,typeof(maxdegree),typeof(object)} (
435
- object,
436
- mindegree,
437
- maxdegree,
438
- inline,
439
- )
440
444
end
441
445
442
446
Base. eltype (:: Type{ExponentsIterator{M,D,O}} ) where {M,D,O} = O
460
464
461
465
nvariables (it:: ExponentsIterator ) = length (it. object)
462
466
467
+ _last_lex_index (n, :: Type{LexOrder} ) = n
468
+ _prev_lex_index (i, :: Type{LexOrder} ) = i - 1
469
+ _not_first_indices (n, :: Type{LexOrder} ) = n: - 1 : 2
470
+ _last_lex_index (_, :: Type{InverseLexOrder} ) = 1
471
+ _prev_lex_index (i, :: Type{InverseLexOrder} ) = i + 1
472
+ _not_first_indices (n, :: Type{InverseLexOrder} ) = 1 : (n- 1 )
473
+ _last_lex_index (n, :: Type{Reverse{LexOrder}} ) = 1
474
+ _prev_lex_index (i, :: Type{Reverse{LexOrder}} ) = i + 1
475
+ _not_first_indices (n, :: Type{Reverse{LexOrder}} ) = (n- 1 ): - 1 : 1
476
+ _last_lex_index (n, :: Type{Reverse{InverseLexOrder}} ) = n
477
+ _prev_lex_index (i, :: Type{Reverse{InverseLexOrder}} ) = i - 1
478
+ _not_first_indices (n, :: Type{Reverse{InverseLexOrder}} ) = 2 : n
479
+ _last_lex_index (n, :: Type{Graded{M}} ) where {M} = _last_lex_index (n, M)
480
+ _prev_lex_index (i, :: Type{Graded{M}} ) where {M} = _prev_lex_index (i, M)
481
+ _not_first_indices (n, :: Type{Graded{M}} ) where {M} = _not_first_indices (n, M)
482
+
463
483
_increase_degree (it:: ExponentsIterator{<:Graded,Nothing} , _) = false
464
484
_increase_degree (it:: ExponentsIterator{<:Graded,Int} , _) = false
465
485
_increase_degree (it:: ExponentsIterator{M,Nothing} , _) where {M} = true
@@ -474,9 +494,10 @@ _adjust_degree(::ExponentsIterator{<:Graded}, _, Δ) = Δ
474
494
# so we need to add `it.mindegree - deg`
475
495
_adjust_degree (it:: ExponentsIterator , deg, _) = max (0 , it. mindegree - deg)
476
496
497
+ # Same as `BangBang.setindex!!`
477
498
_setindex! (x, v, i) = Base. setindex! (x, v, i)
478
499
_setindex! (x:: Tuple , v, i) = Base. setindex (x, v, i)
479
- _increment! (x, i) = _setindex! (x, x[i] + 1 , i)
500
+ _increment! (x, Δ, i) = _setindex! (x, x[i] + Δ , i)
480
501
481
502
_zero (x) = zero (x)
482
503
_zero (x:: Tuple ) = zero .(x)
@@ -487,9 +508,34 @@ _zero!(x::Tuple) = _zero(x)
487
508
_copy (x) = copy (x)
488
509
_copy (x:: Tuple ) = x
489
510
511
+ function __iterate! (
512
+ it:: ExponentsIterator{Graded{Reverse{M}}} ,
513
+ z,
514
+ i,
515
+ deg,
516
+ ) where {M}
517
+ z = _increment! (z, - 1 , i)
518
+ k = _last_lex_index (nvariables (it), M)
519
+ Δ = z[k] + 1
520
+ z = _setindex! (z, 0 , k)
521
+ z = _setindex! (z, Δ, _prev_lex_index (i, Reverse{M}))
522
+ return z, deg
523
+ end
524
+
525
+ function __iterate! (it:: ExponentsIterator{M} , z, i, deg) where {M}
526
+ Δ = z[i] - 1
527
+ z = _setindex! (z, 0 , i)
528
+ deg -= Δ
529
+ Δ = _adjust_degree (it, deg, Δ)
530
+ deg += Δ
531
+ z = _setindex! (z, Δ, _last_lex_index (nvariables (it), M))
532
+ z = _increment! (z, 1 , _prev_lex_index (i, M))
533
+ return z, deg
534
+ end
535
+
490
536
function _iterate! (it:: ExponentsIterator{M} , z, deg) where {M}
491
537
if _increase_degree (it, deg)
492
- z = _increment! (z, _last_lex_index (nvariables (it), M))
538
+ z = _increment! (z, 1 , _last_lex_index (nvariables (it), M))
493
539
return z, deg + 1
494
540
end
495
541
I = _not_first_indices (nvariables (it), M)
@@ -502,17 +548,7 @@ function _iterate!(it::ExponentsIterator{M}, z, deg) where {M}
502
548
z = _setindex! (z, deg + 1 , _last_lex_index (nvariables (it), M))
503
549
return z, deg + 1
504
550
end
505
- j = I[i]
506
- Δ = z[j] - 1
507
- z = _setindex! (z, 0 , j)
508
- j = I[i]
509
- deg -= Δ
510
- Δ = _adjust_degree (it, deg, Δ)
511
- deg += Δ
512
- z = _setindex! (z, Δ, _last_lex_index (nvariables (it), M))
513
- j = I[i]
514
- z = _increment! (z, _prev_lex_index (j, M))
515
- return z, deg
551
+ return __iterate! (it, z, I[i], deg)
516
552
end
517
553
518
554
function Base. iterate (it:: ExponentsIterator{M} ) where {M}
0 commit comments