1
1
# implementation of a sorted dict (not optimized for speed) for storing
2
2
# the factorization of an integer
3
3
4
- struct Factorization{T<: Integer } <: AbstractDict{T, Int}
4
+ struct Factorization{T} <: AbstractDict{T, Int}
5
5
pe:: Vector{Pair{T, Int}} # Prime-Exponent
6
6
7
- function Factorization {T} () where {T <: Integer }
7
+ function Factorization {T} () where T
8
8
# preallocates enough space that numbers smaller than 2310 won't need to resize
9
9
v = Vector {Pair{T, Int}} (undef, 4 )
10
10
empty! (v)
11
11
new {T} (v)
12
12
end
13
13
end
14
14
15
- function Factorization {T} (d:: AbstractDict ) where T<: Integer
15
+ function Factorization {T} (d:: AbstractDict ) where T
16
16
f = Factorization {T} ()
17
17
append! (f. pe, sort! (collect (d)))
18
18
f
19
19
end
20
20
21
- Factorization (d:: AbstractDict{T} ) where {T <: Integer } = Factorization {T} (d)
21
+ Factorization (d:: AbstractDict{T} ) where T = Factorization {T} (d)
22
22
Base. convert (:: Type{Factorization} , d:: AbstractDict ) = Factorization (d)
23
23
24
24
Base. iterate (f:: Factorization , state... ) = iterate (f. pe, state... )
25
25
26
26
function Base. get (f:: Factorization , p, default)
27
- found = searchsortedfirst (f. pe, p, by= first)
27
+ found = searchsortedfirst (f. pe, p=> 0 , by= first)
28
28
(found > length (f. pe) || first (f. pe[found])) != p ? default : last (f. pe[found])
29
29
end
30
30
31
- Base. getindex (f:: Factorization , p:: Integer ) = get (f, p, 0 )
31
+ Base. getindex (f:: Factorization , p) = get (f, p, 0 )
32
32
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)
35
35
if found > length (f. pe)
36
36
push! (f. pe, T (p)=> e)
37
37
elseif first (f. pe[found]) != p
45
45
"""
46
46
impliments f[p] += e faster
47
47
"""
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)
50
50
if found > length (f. pe)
51
51
push! (f. pe, T (p)=> e)
52
52
elseif first (f. pe[found]) != p
@@ -56,7 +56,7 @@ function increment!(f::Factorization{T}, e::Int, p::Integer) where T
56
56
end
57
57
f
58
58
end
59
- function increment! (f:: AbstractDict , e:: Int , p:: Integer )
59
+ function increment! (f:: AbstractDict , e:: Int , p)
60
60
f[p] = get (f, p, 0 ) + e
61
61
return f
62
62
end
@@ -66,4 +66,4 @@ Base.length(f::Factorization) = length(f.pe)
66
66
Base. show (io:: IO , :: MIME{Symbol("text/plain")} , f:: Factorization ) =
67
67
join (io, isempty (f) ? " 1" : [(e == 1 ? " $p " : " $p ^$e " ) for (p,e) in f. pe], " * " )
68
68
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