Skip to content

Commit

Permalink
Require 0.6, fix 0.7 depwarns (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan authored Mar 19, 2018
1 parent acc31a4 commit 6748dc6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
language: julia
os:
- linux
- osx
julia:
- 0.5
- 0.6
- nightly
notifications:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Primes.jl

[![Primes](http://pkg.julialang.org/badges/Primes_0.5.svg)](http://pkg.julialang.org/?pkg=Primes)
[![Primes](http://pkg.julialang.org/badges/Primes_0.6.svg)](http://pkg.julialang.org/?pkg=Primes)
[![Primes](http://pkg.julialang.org/badges/Primes_0.7.svg)](http://pkg.julialang.org/?pkg=Primes)
[![Build Status](https://travis-ci.org/JuliaMath/Primes.jl.svg?branch=master)](https://travis-ci.org/JuliaMath/Primes.jl)
[![Windows Build](https://ci.appveyor.com/api/projects/status/ao64pk44lwo0092r/branch/master?svg=true)](https://ci.appveyor.com/project/ararslan/primes-jl/branch/master)
[![Coverage Status](https://coveralls.io/repos/github/JuliaMath/Primes.jl/badge.svg?branch=master)](https://coveralls.io/github/JuliaMath/Primes.jl?branch=master)
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.5
julia 0.6
Compat 0.30.0
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

Expand Down
38 changes: 19 additions & 19 deletions src/Primes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ else
end

using Base: BitSigned
using Base.Checked.checked_neg
using Base.Checked: checked_neg

export ismersenneprime, isrieselprime, nextprime, prevprime, prime, prodfactors, radical, totient

Expand Down Expand Up @@ -101,7 +101,7 @@ function primesmask(lo::Int, hi::Int)
end
return sieve
end
primesmask{T<:Integer}(lo::T, hi::T) = lo hi typemax(Int) ? primesmask(Int(lo), Int(hi)) :
primesmask(lo::Integer, hi::Integer) = lo hi typemax(Int) ? primesmask(Int(lo), Int(hi)) :
throw(ArgumentError("Both endpoints of the interval to sieve must be ≤ $(typemax(Int)), got $lo and $hi."))

primesmask(limit::Int) = primesmask(1, limit)
Expand Down Expand Up @@ -240,7 +240,7 @@ isprime(n::Int128) = n < 2 ? false :
# https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm
# http://maths-people.anu.edu.au/~brent/pub/pub051.html
#
function factor!{T<:Integer,K<:Integer}(n::T, h::Associative{K,Int})
function factor!(n::T, h::AbstractDict{K,Int}) where {T<:Integer,K<:Integer}
# check for special cases
if n < 0
h[-1] = 1
Expand Down Expand Up @@ -303,14 +303,14 @@ julia> collect(factor(0))
0=>1
```
"""
factor{T<:Integer}(n::T) = factor!(n, Factorization{T}())
factor(n::T) where {T<:Integer} = factor!(n, Factorization{T}())


"""
factor(ContainerType, n::Integer) -> ContainerType
Return the factorization of `n` stored in a `ContainerType`, which must be a
subtype of `Associative` or `AbstractArray`, a `Set`, or an `IntSet`.
subtype of `AbstractDict` or `AbstractArray`, a `Set`, or an `BitSet`.
```julia
julia> factor(DataStructures.SortedDict, 100)
Expand Down Expand Up @@ -342,18 +342,18 @@ julia> factor(Set, 100)
Set([2,5])
```
"""
factor{T<:Integer, D<:Associative}(::Type{D}, n::T) = factor!(n, D(Dict{T,Int}()))
factor{T<:Integer, A<:AbstractArray}(::Type{A}, n::T) = A(factor(Vector{T}, n))
factor{T<:Integer}(::Type{Vector{T}}, n::T) =
factor(::Type{D}, n::T) where {T<:Integer, D<:AbstractDict} = factor!(n, D(Dict{T,Int}()))
factor(::Type{A}, n::T) where {T<:Integer, A<:AbstractArray} = A(factor(Vector{T}, n))
factor(::Type{Vector{T}}, n::T) where {T<:Integer} =
mapreduce(collect, vcat, Vector{T}(), [repeated(k, v) for (k, v) in factor(n)])
factor{T<:Integer, S<:Union{Set,IntSet}}(::Type{S}, n::T) = S(keys(factor(n)))
factor{T<:Any}(::Type{T}, n) = throw(MethodError(factor, (T, n)))
factor(::Type{S}, n::T) where {T<:Integer, S<:Union{Set,BitSet}} = S(keys(factor(n)))
factor(::Type{T}, n) where {T<:Any} = throw(MethodError(factor, (T, n)))

"""
prodfactors(factors)
Compute `n` (or the radical of `n` when `factors` is of type `Set` or
`IntSet`) where `factors` is interpreted as the result of
`BitSet`) where `factors` is interpreted as the result of
`factor(typeof(factors), n)`. Note that if `factors` is of type
`AbstractArray` or `Primes.Factorization`, then `prodfactors` is equivalent
to `Base.prod`.
Expand All @@ -365,8 +365,8 @@ julia> prodfactors(factor(100))
"""
function prodfactors end

prodfactors{K}(factors::Associative{K}) = isempty(factors) ? one(K) : prod(p^i for (p, i) in factors)
prodfactors(factors::Union{AbstractArray, Set, IntSet}) = prod(factors)
prodfactors(factors::AbstractDict{K}) where {K} = isempty(factors) ? one(K) : prod(p^i for (p, i) in factors)
prodfactors(factors::Union{AbstractArray, Set, BitSet}) = prod(factors)

"""
Base.prod(factors::Primes.Factorization{T}) -> T
Expand All @@ -388,7 +388,7 @@ julia> radical(2*2*3)
"""
radical(n) = prod(factor(Set, n))

function pollardfactors!{T<:Integer,K<:Integer}(n::T, h::Associative{K,Int})
function pollardfactors!(n::T, h::AbstractDict{K,Int}) where {T<:Integer,K<:Integer}
while true
c::T = rand(1:(n - 1))
G::T = 1
Expand Down Expand Up @@ -510,7 +510,7 @@ Compute the Euler totient function of the number whose prime factorization is
given by `f`. This method may be preferable to [`totient(::Integer)`](@ref)
when the factorization can be reused for other purposes.
"""
function totient{T <: Integer}(f::Factorization{T})
function totient(f::Factorization{T}) where T <: Integer
if !isempty(f) && first(first(f)) == 0
throw(ArgumentError("ϕ(0) is not defined"))
end
Expand Down Expand Up @@ -541,9 +541,9 @@ function add_!(n::BigInt, x::Int)
# TODO: Change `Any` to `Ref{BigInt}` when 0.6 support is dropped.
# The two have the same effect but `Ref{BigInt}` won't be optimized on 0.6.
if x < 0
ccall((:__gmpz_sub_ui, :libgmp), Void, (Any, Any, Culong), n, n, -x)
ccall((:__gmpz_sub_ui, :libgmp), Cvoid, (Any, Any, Culong), n, n, -x)
else
ccall((:__gmpz_add_ui, :libgmp), Void, (Any, Any, Culong), n, n, x)
ccall((:__gmpz_add_ui, :libgmp), Cvoid, (Any, Any, Culong), n, n, x)
end
n
end
Expand Down Expand Up @@ -638,7 +638,7 @@ function prevprime(n::Integer, i::Integer=1)
end

"""
prime{T}(::Type{T}=Int, i::Integer)
prime(::Type{<:Integer}=Int, i::Integer)
The `i`-th prime number.
Expand All @@ -651,7 +651,7 @@ julia> prime(3)
```
"""
prime{T<:Integer}(::Type{T}, i::Integer) = i < 0 ? throw(DomainError(i)) : nextprime(T(2), i)
prime(::Type{T}, i::Integer) where {T<:Integer} = i < 0 ? throw(DomainError(i)) : nextprime(T(2), i)
prime(i::Integer) = prime(Int, i)

end # module
10 changes: 5 additions & 5 deletions src/factorization.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# implementation of a sorted dict (not optimized for speed) for storing
# the factorization of an integer

immutable Factorization{T<:Integer} <: Associative{T, Int}
struct Factorization{T<:Integer} <: AbstractDict{T, Int}
pe::Vector{Pair{T, Int}} # Prime-Exponent

# Factorization{T}() where {T} = new(Vector{Pair{T, Int}}())
(::Type{Factorization{T}}){T<:Integer}() = new{T}(Vector{Pair{T, Int}}())
Factorization{T}() where {T<:Integer} = new{T}(Vector{Pair{T, Int}}())
end

function (::Type{Factorization{T}}){T<:Integer}(d::Associative)
function Factorization{T}(d::AbstractDict) where T<:Integer
f = Factorization{T}()
append!(f.pe, sort!(collect(d)))
f
end

Base.convert{T}(::Type{Factorization}, d::Associative{T}) = Factorization{T}(d)
Base.convert(::Type{Factorization}, d::AbstractDict{T}) where {T} = Factorization{T}(d)

Base.start(f::Factorization) = start(f.pe)
Base.next(f::Factorization, i) = next(f.pe, i)
Expand All @@ -30,7 +30,7 @@ end

Base.getindex(f::Factorization, p::Integer) = get(f, p, 0)

function Base.setindex!{T}(f::Factorization{T}, e::Int, p::Integer)
function Base.setindex!(f::Factorization{T}, e::Int, p::Integer) where T
found = searchsorted(f.pe, p, by=first)
if isempty(found)
insert!(f.pe, first(found), T(p)=>e)
Expand Down
9 changes: 5 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Primes
using Base.Test
using Compat
using Compat.Test
using DataStructures: SortedDict

import Primes: isprime, primes, primesmask, factor, ismersenneprime, isrieselprime, Factorization
Expand Down Expand Up @@ -218,7 +219,7 @@ end

# factor sets
@test factor(Set, 100) == Set([2, 5])
@test factor(IntSet, 100) == IntSet([2, 5])
@test factor(BitSet, 100) == BitSet([2, 5])

# factor other things and fail
@test_throws MethodError factor(Int, 10)
Expand Down Expand Up @@ -254,7 +255,7 @@ ismersenneprime(9, check=false)
@test isrieselprime(3, BigInt(2)^607 - 1) # Case 2
@test_throws ErrorException isrieselprime(20, 31) # Case `else`

# @testset "Factorization{$T} as an Associative" for T = (Int, UInt, BigInt)
# @testset "Factorization{$T} as an AbstractDict" for T = (Int, UInt, BigInt)
for T = (Int, UInt, BigInt)
d = Dict(map(Pair, rand(T(1):T(100), 30), 1:30))
f = Factorization{T}(d)
Expand Down Expand Up @@ -378,7 +379,7 @@ for T in (Int, UInt, BigInt)
@test prodfactors(factor(C, n)) == n
end
if Primes.radical(n) == n
for C = (Set, IntSet)
for C = (Set, BitSet)
@test prodfactors(factor(C, n)) == n
end
end
Expand Down

0 comments on commit 6748dc6

Please sign in to comment.