Skip to content

Commit a26041e

Browse files
authored
Merge pull request #127 from jmichel7/master
suppressed Integer annotation on Factorization; fixed bug in calls of…
2 parents 8c20fb3 + 42b4d92 commit a26041e

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

src/Primes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ end
10121012
Return a vector with the positive divisors of the number whose factorization is `f`.
10131013
Divisors are sorted lexicographically, rather than numerically.
10141014
"""
1015-
function divisors(f::Factorization{T}) where {T<:Integer}
1015+
function divisors(f::Factorization{T}) where T
10161016
sgn = sign(f)
10171017
if iszero(sgn) # n == 0
10181018
return T[]

src/factorization.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
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
1313
end
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
1919
end
2020

21-
Factorization(d::AbstractDict{T}) where {T<:Integer} = Factorization{T}(d)
21+
Factorization(d::AbstractDict{T}) where T = Factorization{T}(d)
2222
Base.convert(::Type{Factorization}, d::AbstractDict) = Factorization(d)
2323

2424
Base.iterate(f::Factorization, state...) = iterate(f.pe, state...)
2525

2626
function 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])
2929
end
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
@@ -45,8 +45,8 @@ end
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
5858
end
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
6262
end
@@ -66,4 +66,4 @@ Base.length(f::Factorization) = length(f.pe)
6666
Base.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]))

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,17 @@ for T = (Int, UInt, BigInt)
289289
@test issorted(f.pe)
290290
end
291291

292+
d = Dict(:a=>1,:b=>2)
293+
f = Factorization(d)
294+
@test f == d == Dict(f) == convert(Factorization, d)
295+
@test collect(f) == sort!(collect(d)) # test start/next/done
296+
@test length(f) == length(d)
297+
@test get(f, :c, nothing) === nothing
298+
@test f[:c] == 0
299+
@test f[:a] == 1
300+
f[:c] = 1
301+
@test get(f, :c, 0) == 1
302+
292303
# dumb implementation of Euler totient for correctness tests
293304
ϕ(n) = count(m -> gcd(n, m) == 1, 1:n)
294305

0 commit comments

Comments
 (0)