Skip to content

Commit a83914e

Browse files
committed
Only allocate when asked
1 parent 6757cb3 commit a83914e

File tree

2 files changed

+65
-25
lines changed

2 files changed

+65
-25
lines changed

src/MPoly.jl

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,11 @@ Return an iterator for the coefficients of the given polynomial. To retrieve
501501
an array of the coefficients, use `collect(coefficients(a))`.
502502
"""
503503
function coefficients(a::MPolyRingElem{T}; inplace::Bool = false) where T <: RingElement
504-
t = zero(coefficient_ring(parent(a)))
505-
return Generic.MPolyCoeffs(a, inplace, t)
504+
if inplace
505+
t = zero(coefficient_ring(parent(a)))
506+
return Generic.MPolyCoeffs(a, inplace, t)
507+
end
508+
return Generic.MPolyCoeffs(a)
506509
end
507510

508511
@doc raw"""
@@ -517,9 +520,12 @@ function exponent_vectors(a::MPolyRingElem{T}; inplace::Bool = false) where T <:
517520
end
518521

519522
function exponent_vectors(::Type{Vector{S}}, a::MPolyRingElem{T}; inplace::Bool = false) where {T <: RingElement, S}
520-
# Don't use `zeros`: If S === ZZRingElem, then all the entries would be identical
521-
t = [zero(S) for _ in 1:nvars(parent(a))]
522-
return Generic.MPolyExponentVectors(a, inplace, t)
523+
if inplace
524+
# Don't use `zeros`: If S === ZZRingElem, then all the entries would be identical
525+
t = [zero(S) for _ in 1:nvars(parent(a))]
526+
return Generic.MPolyExponentVectors(a, inplace, t)
527+
end
528+
return Generic.MPolyExponentVectors(Vector{S}, a)
523529
end
524530

525531
@doc raw"""
@@ -529,8 +535,11 @@ Return an iterator for the monomials of the given polynomial. To retrieve
529535
an array of the monomials, use `collect(monomials(a))`.
530536
"""
531537
function monomials(a::MPolyRingElem{T}; inplace::Bool = false) where T <: RingElement
532-
t = zero(parent(a))
533-
return Generic.MPolyMonomials(a, inplace, t)
538+
if inplace
539+
t = zero(parent(a))
540+
return Generic.MPolyMonomials(a, inplace, t)
541+
end
542+
return Generic.MPolyMonomials(a)
534543
end
535544

536545
@doc raw"""
@@ -540,8 +549,11 @@ Return an iterator for the terms of the given polynomial. To retrieve
540549
an array of the terms, use `collect(terms(a))`.
541550
"""
542551
function terms(a::MPolyRingElem{T}; inplace::Bool = false) where T <: RingElement
543-
t = zero(parent(a))
544-
return Generic.MPolyTerms(a, inplace, t)
552+
if inplace
553+
t = zero(parent(a))
554+
return Generic.MPolyTerms(a, inplace, t)
555+
end
556+
return Generic.MPolyTerms(a)
545557
end
546558

547559
###############################################################################

src/generic/GenericTypes.jl

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -385,46 +385,74 @@ end
385385

386386
# Iterators
387387

388-
struct MPolyCoeffs{T <: AbstractAlgebra.NCRingElem, S <: AbstractAlgebra.RingElement}
388+
mutable struct MPolyCoeffs{T <: AbstractAlgebra.NCRingElem, S <: AbstractAlgebra.RingElement}
389389
poly::T
390390
inplace::Bool
391391
temp::S # only used if inplace == true
392-
end
393392

394-
function MPolyCoeffs(f::AbstractAlgebra.NCRingElem)
395-
return MPolyCoeffs(f, false, zero(coefficient_ring(parent(f))))
393+
function MPolyCoeffs(f::AbstractAlgebra.NCRingElem)
394+
I = new{typeof(f), elem_type(coefficient_ring_type(f))}()
395+
I.poly = f
396+
I.inplace = false
397+
return I
398+
end
399+
400+
function MPolyCoeffs(f::AbstractAlgebra.NCRingElem, inplace::Bool, temp::AbstractAlgebra.RingElement)
401+
return new{typeof(f), typeof(temp)}(f, inplace, temp)
402+
end
396403
end
397404

398405
# S may be the type of anything that can store an exponent vector, for example
399406
# Vector{Int}, ZZMatrix, ...
400-
struct MPolyExponentVectors{T <: AbstractAlgebra.RingElem, S}
407+
mutable struct MPolyExponentVectors{T <: AbstractAlgebra.RingElem, S}
401408
poly::T
402409
inplace::Bool
403410
temp::S # only used if inplace == true
404-
end
405411

406-
function MPolyExponentVectors(f::AbstractAlgebra.RingElem)
407-
return MPolyExponentVectors(f, false, Vector{Int}())
412+
function MPolyExponentVectors(::Type{S}, f::AbstractAlgebra.NCRingElem) where S
413+
I = new{typeof(f), S}()
414+
I.poly = f
415+
I.inplace = false
416+
return I
417+
end
418+
419+
function MPolyExponentVectors(f::AbstractAlgebra.NCRingElem, inplace::Bool, temp::S) where S
420+
return new{typeof(f), S}(f, inplace, temp)
421+
end
408422
end
409423

410-
struct MPolyTerms{T <: AbstractAlgebra.NCRingElem}
424+
mutable struct MPolyTerms{T <: AbstractAlgebra.NCRingElem}
411425
poly::T
412426
inplace::Bool
413427
temp::T # only used if inplace == true
414-
end
415428

416-
function MPolyTerms(f::AbstractAlgebra.NCRingElem)
417-
return MPolyTerms(f, false, zero(parent(f)))
429+
function MPolyTerms(f::AbstractAlgebra.NCRingElem)
430+
I = new{typeof(f)}()
431+
I.poly = f
432+
I.inplace = false
433+
return I
434+
end
435+
436+
function MPolyTerms(f::T, inplace::Bool, temp::T) where {T <: AbstractAlgebra.NCRingElem}
437+
return new{T}(f, inplace, temp)
438+
end
418439
end
419440

420-
struct MPolyMonomials{T <: AbstractAlgebra.NCRingElem}
441+
mutable struct MPolyMonomials{T <: AbstractAlgebra.NCRingElem}
421442
poly::T
422443
inplace::Bool
423444
temp::T # only used if inplace == true
424-
end
425445

426-
function MPolyMonomials(f::NCRingElem)
427-
return MPolyMonomials(f, false, zero(parent(f)))
446+
function MPolyMonomials(f::AbstractAlgebra.NCRingElem)
447+
I = new{typeof(f)}()
448+
I.poly = f
449+
I.inplace = false
450+
return I
451+
end
452+
453+
function MPolyMonomials(f::T, inplace::Bool, temp::T) where {T <: AbstractAlgebra.NCRingElem}
454+
return new{T}(f, inplace, temp)
455+
end
428456
end
429457

430458
mutable struct MPolyBuildCtx{T, S}

0 commit comments

Comments
 (0)