Skip to content

Commit

Permalink
Merge 9481e80 into c5c3633
Browse files Browse the repository at this point in the history
  • Loading branch information
mforets committed Oct 1, 2018
2 parents c5c3633 + 9481e80 commit bbe709d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 50 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ MathProgBase 0.6
Polyhedra 0.3 0.4
@windows WinRPM
@osx Homebrew
Compat 1.0.0
3 changes: 2 additions & 1 deletion src/CDDLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ __precompile__()

module CDDLib

using Compat
using BinDeps
using Polyhedra

Expand Down Expand Up @@ -34,7 +35,7 @@ macro cdd_ccall(f, args...)
end

function __init__()
@dd_ccall set_global_constants Void ()
@dd_ccall set_global_constants Nothing ()
end

import Base.convert, Base.push!, Base.eltype, Base.copy
Expand Down
8 changes: 4 additions & 4 deletions src/lp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ mutable struct Cdd_LPSolutionData{T<:MyType}
end

function dd_freelpsolution(lp::Ptr{Cdd_LPSolutionData{Cdouble}})
@ddf_ccall FreeLPSolution Void (Ptr{Cdd_LPSolutionData{Cdouble}},) lp
@ddf_ccall FreeLPSolution Nothing (Ptr{Cdd_LPSolutionData{Cdouble}},) lp
end
function dd_freelpsolution(lp::Ptr{Cdd_LPSolutionData{GMPRational}})
@dd_ccall FreeLPSolution Void (Ptr{Cdd_LPSolutionData{GMPRational}},) lp
@dd_ccall FreeLPSolution Nothing (Ptr{Cdd_LPSolutionData{GMPRational}},) lp
end

mutable struct CDDLPSolution{T<:MyType}
Expand Down Expand Up @@ -181,10 +181,10 @@ mutable struct Cdd_LPData{T<:MyType}
end

function dd_freelpdata(lp::Ptr{Cdd_LPData{Cdouble}})
@ddf_ccall FreeLPData Void (Ptr{Cdd_LPData{Cdouble}},) lp
@ddf_ccall FreeLPData Nothing (Ptr{Cdd_LPData{Cdouble}},) lp
end
function dd_freelpdata(lp::Ptr{Cdd_LPData{GMPRational}})
@dd_ccall FreeLPData Void (Ptr{Cdd_LPData{GMPRational}},) lp
@dd_ccall FreeLPData Nothing (Ptr{Cdd_LPData{GMPRational}},) lp
end

mutable struct CDDLP{T<:MyType}
Expand Down
34 changes: 17 additions & 17 deletions src/matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,42 @@ function dd_creatematrix(::Type{GMPRational}, m::Cdd_rowrange, n::Cdd_colrange)
end

# function dd_copyAmatrixvectorizedbycolumn(mat::Cdd_Amatrix{Cdouble}, M::Matrix{Cdouble}, m::Cdd_rowrange, n::Cdd_colrange)
# @ddf_ccall CopyAmatrixVectorizedByColumn Void (Cdd_Amatrix{Cdouble}, Ptr{Cdouble}, Cdd_rowrange, Cdd_colrange) mat M m n
# @ddf_ccall CopyAmatrixVectorizedByColumn Nothing (Cdd_Amatrix{Cdouble}, Ptr{Cdouble}, Cdd_rowrange, Cdd_colrange) mat M m n
# end
# function dd_copyAmatrixvectorizedbycolumn(mat::Cdd_Amatrix{GMPRational}, M::Matrix{GMPRational}, m::Cdd_rowrange, n::Cdd_colrange)
# @dd_ccall CopyAmatrixVectorizedByColumn Void (Cdd_Amatrix{GMPRational}, Ptr{GMPRational}, Cdd_rowrange, Cdd_colrange) mat M m n
# @dd_ccall CopyAmatrixVectorizedByColumn Nothing (Cdd_Amatrix{GMPRational}, Ptr{GMPRational}, Cdd_rowrange, Cdd_colrange) mat M m n
# end

function dd_copyArow(acopy::Cdd_Arow{Cdouble}, a::Vector{Cdouble})
@ddf_ccall CopyArow Void (Cdd_Arow{Cdouble}, Cdd_Arow{Cdouble}, Cdd_colrange) acopy a length(a)
@ddf_ccall CopyArow Nothing (Cdd_Arow{Cdouble}, Cdd_Arow{Cdouble}, Cdd_colrange) acopy a length(a)
end
function dd_copyArow(acopy::Cdd_Arow{GMPRational}, a::Vector{Rational{BigInt}})
b = Vector{GMPRational}(a)
@dd_ccall CopyArow Void (Cdd_Arow{GMPRational}, Cdd_Arow{GMPRational}, Cdd_colrange) acopy b length(b)
@dd_ccall CopyArow Nothing (Cdd_Arow{GMPRational}, Cdd_Arow{GMPRational}, Cdd_colrange) acopy b length(b)
myfree(b)
end

dd_copyArow(acopy::Cdd_Arow, a::AbstractVector) = dd_copyArow(acopy, collect(a)) # e.g. for sparse a

function dd_setmatrixobjective(matrix::Ptr{Cdd_MatrixData{Cdouble}}, objective::Cdd_LPObjectiveType)
@ddf_ccall SetMatrixObjective Void (Ptr{Cdd_MatrixData{Cdouble}}, Cdd_LPObjectiveType) matrix objective
@ddf_ccall SetMatrixObjective Nothing (Ptr{Cdd_MatrixData{Cdouble}}, Cdd_LPObjectiveType) matrix objective
end
function dd_setmatrixobjective(matrix::Ptr{Cdd_MatrixData{GMPRational}}, objective::Cdd_LPObjectiveType)
@dd_ccall SetMatrixObjective Void (Ptr{Cdd_MatrixData{GMPRational}}, Cdd_LPObjectiveType) matrix objective
@dd_ccall SetMatrixObjective Nothing (Ptr{Cdd_MatrixData{GMPRational}}, Cdd_LPObjectiveType) matrix objective
end

function dd_setmatrixnumbertype(matrix::Ptr{Cdd_MatrixData{Cdouble}})
@ddf_ccall SetMatrixNumberType Void (Ptr{Cdd_MatrixData{Cdouble}}, Cdd_NumberType) matrix dd_Real
@ddf_ccall SetMatrixNumberType Nothing (Ptr{Cdd_MatrixData{Cdouble}}, Cdd_NumberType) matrix dd_Real
end
function dd_setmatrixnumbertype(matrix::Ptr{Cdd_MatrixData{GMPRational}})
@dd_ccall SetMatrixNumberType Void (Ptr{Cdd_MatrixData{GMPRational}}, Cdd_NumberType) matrix dd_Rational
@dd_ccall SetMatrixNumberType Nothing (Ptr{Cdd_MatrixData{GMPRational}}, Cdd_NumberType) matrix dd_Rational
end

function dd_setmatrixrepresentationtype(matrix::Ptr{Cdd_MatrixData{Cdouble}}, inequality::Bool)
@ddf_ccall SetMatrixRepresentationType Void (Ptr{Cdd_MatrixData{Cdouble}}, Cdd_RepresentationType) matrix (inequality ? dd_Inequality : dd_Generator)
@ddf_ccall SetMatrixRepresentationType Nothing (Ptr{Cdd_MatrixData{Cdouble}}, Cdd_RepresentationType) matrix (inequality ? dd_Inequality : dd_Generator)
end
function dd_setmatrixrepresentationtype(matrix::Ptr{Cdd_MatrixData{GMPRational}}, inequality::Bool)
@dd_ccall SetMatrixRepresentationType Void (Ptr{Cdd_MatrixData{GMPRational}}, Cdd_RepresentationType) matrix (inequality ? dd_Inequality : dd_Generator)
@dd_ccall SetMatrixRepresentationType Nothing (Ptr{Cdd_MatrixData{GMPRational}}, Cdd_RepresentationType) matrix (inequality ? dd_Inequality : dd_Generator)
end

function dd_matrixcopy(matrix::Ptr{Cdd_MatrixData{Cdouble}})
Expand All @@ -62,7 +62,7 @@ function dd_matrixcopy(matrix::Ptr{Cdd_MatrixData{GMPRational}})
@dd_ccall MatrixCopy Ptr{Cdd_MatrixData{GMPRational}} (Ptr{Cdd_MatrixData{GMPRational}},) matrix
end

function fillmatrix(inequality::Bool, matrix::Ptr{Ptr{T}}, itr, linset::IntSet, offset) where T
function fillmatrix(inequality::Bool, matrix::Ptr{Ptr{T}}, itr, linset::BitSet, offset) where T
for (i, item) in enumerate(itr)
row = unsafe_load(matrix, offset+i)
if islin(item)
Expand All @@ -83,7 +83,7 @@ function initmatrix(::Polyhedra.FullDim{N}, ::Type{T}, inequality::Bool, itr::Po
offset = [0; cs[1:end-1]]
matrix = dd_creatematrix(mytype(T), Cdd_rowrange(m), Cdd_colrange(n))
mat = unsafe_load(matrix)
linset = IntSet()
linset = BitSet()
fillmatrix.(inequality, mat.matrix, itr, linset, offset)
dd_settype(mat.linset, linset)
dd_setmatrixnumbertype(matrix)
Expand Down Expand Up @@ -133,7 +133,7 @@ Polyhedra.similar_type(::Type{<:CDDGeneratorMatrix}, ::FullDim{N}, ::Type{T}) wh

function linset(matrix::CDDMatrix)
mat = unsafe_load(matrix.matrix)
Base.convert(IntSet, CDDSet(mat.linset, mat.rowsize))
Base.convert(BitSet, CDDSet(mat.linset, mat.rowsize))
end

CDDMatrix(hrep::HRepresentation) = CDDInequalityMatrix(hrep)
Expand All @@ -154,10 +154,10 @@ Base.length(matrix::CDDInequalityMatrix{N}) where N = _length(matrix.matrix, N)
Base.length(matrix::CDDGeneratorMatrix{N}) where N = _length(matrix.matrix, N) + matrix.cone

function dd_freematrix(matrix::Ptr{Cdd_MatrixData{Cdouble}})
@ddf_ccall FreeMatrix Void (Ptr{Cdd_MatrixData{Cdouble}},) matrix
@ddf_ccall FreeMatrix Nothing (Ptr{Cdd_MatrixData{Cdouble}},) matrix
end
function dd_freematrix(matrix::Ptr{Cdd_MatrixData{GMPRational}})
@dd_ccall FreeMatrix Void (Ptr{Cdd_MatrixData{GMPRational}},) matrix
@dd_ccall FreeMatrix Nothing (Ptr{Cdd_MatrixData{GMPRational}},) matrix
end
function myfree(matrix::CDDMatrix)
dd_freematrix(matrix.matrix)
Expand Down Expand Up @@ -203,7 +203,7 @@ function extractrow(mat::Cdd_MatrixData{GMPRational}, i)
row = unsafe_load(mat.matrix, i)
for j = 1:n
b[j] = GMPRationalMut()
ccall((:__gmpq_set, :libgmp), Void, (Ptr{GMPRationalMut}, Ptr{GMPRational}), pointer_from_objref(b[j]), row + ((j-1)*sizeof(GMPRational)))
ccall((:__gmpq_set, :libgmp), Nothing, (Ptr{GMPRationalMut}, Ptr{GMPRational}), pointer_from_objref(b[j]), row + ((j-1)*sizeof(GMPRational)))
end
Array{Rational{BigInt}}(Array{GMPRational}(b))
end
Expand Down Expand Up @@ -308,7 +308,7 @@ function extractA(mat::Cdd_MatrixData{GMPRational})
row = unsafe_load(mat.matrix, i)
for j = 1:n
A[i, j] = GMPRationalMut()
ccall((:__gmpq_set, :libgmp), Void, (Ptr{GMPRationalMut}, Ptr{GMPRational}), pointer_from_objref(A[i,j]), row + ((j-1)*sizeof(GMPRational)))
ccall((:__gmpq_set, :libgmp), Nothing, (Ptr{GMPRationalMut}, Ptr{GMPRational}), pointer_from_objref(A[i,j]), row + ((j-1)*sizeof(GMPRational)))
end
end
Array{GMPRational}(A)
Expand Down
38 changes: 19 additions & 19 deletions src/mytype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mutable struct GMPRationalMut

function GMPRationalMut()
m = new()
ccall((:__gmpq_init, :libgmp), Void, (Ptr{GMPRationalMut},), &m)
ccall((:__gmpq_init, :libgmp), Nothing, (Ptr{GMPRationalMut},), Ref(m))
# No need to clear anything since the num and den are used by
# the GMPRational that is created
#finalizer(m, _mpq_clear_fn)
Expand All @@ -24,10 +24,10 @@ end

function Base.convert(::Type{GMPRationalMut}, a::Rational{BigInt})
m = GMPRationalMut()
ccall((:__gmpq_set_num, :libgmp), Void,
(Ptr{GMPRationalMut}, Ptr{BigInt}), &m, &a.num)
ccall((:__gmpq_set_den, :libgmp), Void,
(Ptr{GMPRationalMut}, Ptr{BigInt}), &m, &a.den)
ccall((:__gmpq_set_num, :libgmp), Nothing,
(Ptr{GMPRationalMut}, Ptr{BigInt}), Ref(m), Ref(a.num))
ccall((:__gmpq_set_den, :libgmp), Nothing,
(Ptr{GMPRationalMut}, Ptr{BigInt}), Ref(m), Ref(a.den))
m
end
Base.convert(::Type{GMPRationalMut}, a::Rational) = GMPRationalMut(Rational{BigInt}(a))
Expand All @@ -40,8 +40,8 @@ function GMPRationalMut(a::Int, b::Int)
b = -b
end
m = GMPRationalMut()
ccall((:__gmpq_set_si, :libgmp), Void,
(Ptr{GMPRationalMut}, Clong, Culong), &m, a, b)
ccall((:__gmpq_set_si, :libgmp), Nothing,
(Ptr{GMPRationalMut}, Clong, Culong), Ref(m), a, b)
m
end
Base.convert(::Type{GMPRationalMut}, a::Int) = GMPRationalMut(a, 1)
Expand All @@ -65,7 +65,7 @@ function myfree(a::Array{Cdouble})
end
function myfree(a::Array{GMPRational})
for el in a
ccall((:__gmpq_clear, :libgmp), Void, (Ptr{GMPRational},), &el)
ccall((:__gmpq_clear, :libgmp), Nothing, (Ptr{GMPRational},), Ref(el))
end
end

Expand All @@ -92,20 +92,20 @@ end

function -(a::GMPRational)
m = GMPRationalMut()
ccall((:__gmpq_neg, :libgmp), Void,
(Ptr{GMPRationalMut}, Ptr{GMPRational}), &m, &a)
ccall((:__gmpq_neg, :libgmp), Nothing,
(Ptr{GMPRationalMut}, Ptr{GMPRational}), Ref(m), Ref(a))
GMPRational(m)
end
function *(a::GMPRational, b::GMPRational)
m = GMPRationalMut()
ccall((:__gmpq_mul, :libgmp), Void,
(Ptr{GMPRationalMut}, Ptr{GMPRational}, Ptr{GMPRational}), &m, &a, &b)
ccall((:__gmpq_mul, :libgmp), Nothing,
(Ptr{GMPRationalMut}, Ptr{GMPRational}, Ptr{GMPRational}), Ref(m), Ref(a), Ref(b))
GMPRational(m)
end
function +(a::GMPRational, b::GMPRational)
m = GMPRationalMut()
ccall((:__gmpq_add, :libgmp), Void,
(Ptr{GMPRationalMut}, Ptr{GMPRational}, Ptr{GMPRational}), &m, &a, &b)
ccall((:__gmpq_add, :libgmp), Nothing,
(Ptr{GMPRationalMut}, Ptr{GMPRational}, Ptr{GMPRational}), Ref(m), Ref(a), Ref(b))
GMPRational(m)
end

Expand All @@ -120,11 +120,11 @@ end

function Base.convert(::Type{Rational{BigInt}}, r::GMPRational)
a = BigInt()
ccall((:__gmpq_get_num, :libgmp), Void,
(Ptr{BigInt}, Ptr{GMPRational}), &a, &r)
ccall((:__gmpq_get_num, :libgmp), Nothing,
(Ptr{BigInt}, Ptr{GMPRational}), Ref(a), Ref(r))
b = BigInt()
ccall((:__gmpq_get_den, :libgmp), Void,
(Ptr{BigInt}, Ptr{GMPRational}), &b, &r)
ccall((:__gmpq_get_den, :libgmp), Nothing,
(Ptr{BigInt}, Ptr{GMPRational}), Ref(b), Ref(r))
a // b
end
Base.convert(::Type{Rational}, r::GMPRational) = Base.convert(Rational{BigInt}, r)
Expand Down Expand Up @@ -160,7 +160,7 @@ function myconvert(::Type{Array}, x::Ptr{GMPRational}, n)
y = Array{GMPRationalMut, 1}(n)
for i = 1:n
y[i] = GMPRationalMut()
ccall((:__gmpq_set, :libgmp), Void, (Ptr{GMPRationalMut}, Ptr{GMPRational}), pointer_from_objref(y[i]), x+((i-1)*sizeof(GMPRational)))
ccall((:__gmpq_set, :libgmp), Nothing, (Ptr{GMPRationalMut}, Ptr{GMPRational}), pointer_from_objref(y[i]), x+((i-1)*sizeof(GMPRational)))
end
Array{GMPRational}(y)
end
Expand Down
6 changes: 3 additions & 3 deletions src/polyhedra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mutable struct Cdd_PolyhedraData{T<:MyType}
A::Cdd_Amatrix{T}
# Inequality System: m times d matrix
numbtype::Cdd_NumberType
child::Ptr{Void} # dd_ConePtr
child::Ptr{Nothing} # dd_ConePtr
# pointing to the homogenized cone data
m_alloc::Cdd_rowrange
# allocated row size of matrix A
Expand Down Expand Up @@ -74,10 +74,10 @@ mutable struct CDDPolyhedra{N, T<:PolyType, S}
end

function myfree(poly::CDDPolyhedra{N, Cdouble}) where N
@ddf_ccall FreePolyhedra Void (Ptr{Cdd_PolyhedraData{Cdouble}},) poly.poly
@ddf_ccall FreePolyhedra Nothing (Ptr{Cdd_PolyhedraData{Cdouble}},) poly.poly
end
function myfree(poly::CDDPolyhedra{N, Rational{BigInt}}) where N
@dd_ccall FreePolyhedra Void (Ptr{Cdd_PolyhedraData{GMPRational}},) poly.poly
@dd_ccall FreePolyhedra Nothing (Ptr{Cdd_PolyhedraData{GMPRational}},) poly.poly
end

CDDPolyhedra(matrix::CDDMatrix{N, T, S}) where {N, T, S} = CDDPolyhedra{N, T, S}(matrix)
Expand Down
12 changes: 6 additions & 6 deletions src/settype.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function dd_set_initialize(maxel::Clong)
x = Ref{Cset_type}(0)
@cdd_ccall set_initialize Void (Ref{Ptr{Culong}}, Clong) x maxel
@cdd_ccall set_initialize Nothing (Ref{Ptr{Culong}}, Clong) x maxel
x[]
end

function dd_set_addelem(st::Cset_type, el::Clong)
@cdd_ccall set_addelem Void (Cset_type, Clong) st convert(Clong, el)
@cdd_ccall set_addelem Nothing (Cset_type, Clong) st convert(Clong, el)
end

function dd_set_member(st::Cset_type, el::Clong)
Expand Down Expand Up @@ -37,8 +37,8 @@ function CDDSet(s, maxel::Clong, offset::Integer)
end
CDDSet(s, maxel, offset::Integer) = CDDSet(s, Clong(maxel), offset)

function Base.convert(::Type{IntSet}, st::CDDSet)
s = IntSet()
function Base.convert(::Type{BitSet}, st::CDDSet)
s = BitSet()
for i = 1:st.maxel
if dd_set_member(st.s, convert(Clong, i))
push!(s, i)
Expand All @@ -48,9 +48,9 @@ function Base.convert(::Type{IntSet}, st::CDDSet)
end

# I don't want it to overwrite Base.convert behaviour
function myconvert(::Type{IntSet}, a::Matrix)
function myconvert(::Type{BitSet}, a::Matrix)
b = Array{Bool}(a)
s = IntSet()
s = BitSet()
for i = 1:length(a)
if b[i]
push!(s, i)
Expand Down

0 comments on commit bbe709d

Please sign in to comment.