11# implementation of a sorted dict (not optimized for speed) for storing
22# the factorization of an integer
33
4- struct Factorization{T<: Integer } <: AbstractDict{T, Int}
4+ struct Factorization{T} <: AbstractDict{T, Int}
55 pe:: Vector{Pair{T, Int}} # Prime-Exponent
66
7- function Factorization {T} () where {T <: Integer }
7+ function Factorization {T} () where T
88 # preallocates enough space that numbers smaller than 2310 won't need to resize
99 v = Vector {Pair{T, Int}} (undef, 4 )
1010 empty! (v)
1111 new {T} (v)
1212 end
1313end
1414
15- function Factorization {T} (d:: AbstractDict ) where T<: Integer
15+ function Factorization {T} (d:: AbstractDict ) where T
1616 f = Factorization {T} ()
1717 append! (f. pe, sort! (collect (d)))
1818 f
1919end
2020
21- Factorization (d:: AbstractDict{T} ) where {T <: Integer } = Factorization {T} (d)
21+ Factorization (d:: AbstractDict{T} ) where T = Factorization {T} (d)
2222Base. convert (:: Type{Factorization} , d:: AbstractDict ) = Factorization (d)
2323
2424Base. iterate (f:: Factorization , state... ) = iterate (f. pe, state... )
2525
2626function Base. get (f:: Factorization , p, default)
27- found = searchsortedfirst (f. pe, p, by= first)
27+ found = searchsortedfirst (f. pe, p=> 0 , by= first)
2828 (found > length (f. pe) || first (f. pe[found])) != p ? default : last (f. pe[found])
2929end
3030
31- Base. getindex (f:: Factorization , p:: Integer ) = get (f, p, 0 )
31+ Base. getindex (f:: Factorization , p) = get (f, p, 0 )
3232
33- function Base. setindex! (f:: Factorization{T} , e:: Int , p:: Integer ) where T
34- found = searchsortedfirst (f. pe, p, by= first)
33+ function Base. setindex! (f:: Factorization{T} , e:: Int , p) where T
34+ found = searchsortedfirst (f. pe, p=> 0 , by= first)
3535 if found > length (f. pe)
3636 push! (f. pe, T (p)=> e)
3737 elseif first (f. pe[found]) != p
4545"""
4646 impliments f[p] += e faster
4747"""
48- function increment! (f:: Factorization{T} , e:: Int , p:: Integer ) where T
49- found = searchsortedfirst (f. pe, p, by= first)
48+ function increment! (f:: Factorization{T} , e:: Int , p) where T
49+ found = searchsortedfirst (f. pe, p=> 0 , by= first)
5050 if found > length (f. pe)
5151 push! (f. pe, T (p)=> e)
5252 elseif first (f. pe[found]) != p
@@ -56,7 +56,7 @@ function increment!(f::Factorization{T}, e::Int, p::Integer) where T
5656 end
5757 f
5858end
59- function increment! (f:: AbstractDict , e:: Int , p:: Integer )
59+ function increment! (f:: AbstractDict , e:: Int , p)
6060 f[p] = get (f, p, 0 ) + e
6161 return f
6262end
@@ -66,4 +66,4 @@ Base.length(f::Factorization) = length(f.pe)
6666Base. show (io:: IO , :: MIME{Symbol("text/plain")} , f:: Factorization ) =
6767 join (io, isempty (f) ? " 1" : [(e == 1 ? " $p " : " $p ^$e " ) for (p,e) in f. pe], " * " )
6868
69- Base. sign (f:: Factorization ) = isempty (f. pe) ? one (keytype (f)) : sign (first (f. pe[1 ]))
69+ Base. sign (f:: Factorization ) = isempty (f. pe) ? one (keytype (f)) : sign (first (f. pe[1 ]))
0 commit comments