delpi
0.0.1
DElta-complete LP solver
Toggle main menu visibility
Loading...
Searching...
No Matches
qsopt_ex.cpp
1
6
7
#include "delpi/libs/qsopt_ex.h"
8
9
#include <cstdlib>
10
#include <ostream>
11
#include <string>
12
13
namespace
delpi::qsopt_ex
{
14
15
mpq_class *
StringToMpqPtr
(
const
std::string &str) {
return
CStringToMpqPtr
(str.c_str()); }
16
mpq_class
StringToMpq
(
const
std::string &str) {
return
CStringToMpq
(str.c_str()); }
17
mpq_class *
CStringToMpqPtr
(
const
char
str[]) {
18
mpq_t val;
19
mpq_init(val);
20
mpq_EGlpNumReadStr(val, str);
21
mpq_class *
const
result =
new
mpq_class(val);
22
mpq_clear(val);
23
return
result;
24
}
25
mpq_class
CStringToMpq
(
const
char
str[]) {
26
mpq_t val;
27
mpq_init(val);
28
mpq_EGlpNumReadStr(val, str);
29
mpq_class result(val);
30
mpq_clear(val);
31
return
result;
32
}
33
34
void
MpqArray::AllocateMpqArray
(
size_t
n_elements) {
35
if
(n_elements == 0)
return
;
36
auto
const
memSize =
static_cast<
size_t
>
(
sizeof
(mpq_t) * n_elements +
sizeof
(size_t));
37
38
std::size_t *newArray =
nullptr
;
39
newArray =
static_cast<
std::size_t *
>
(calloc(1, memSize));
40
if
(!newArray) {
41
fprintf(stderr,
"EXIT: Not enough memory while allocating %zd bytes"
, memSize);
42
exit(1);
43
}
44
45
newArray[0] = n_elements;
46
array_
=
reinterpret_cast<
mpq_t *
>
(newArray + 1);
47
for
(std::size_t i = 0; i < n_elements; ++i) mpq_init(
array_
[i]);
48
}
49
50
void
MpqArray::FreeMpqArray
() {
51
auto
*sizeArray =
reinterpret_cast<
size_t
*
>
(
array_
);
52
if
(sizeArray) sizeArray--;
53
const
std::size_t nElements = sizeArray ? sizeArray[0] : 0;
54
55
for
(std::size_t i = 0; i < nElements; ++i) mpq_clear(
array_
[i]);
56
free(sizeArray);
57
array_
=
nullptr
;
58
}
59
60
MpqArray::MpqArray
(
const
size_t
n_elements) :
array_
{nullptr} {
AllocateMpqArray
(n_elements); }
61
62
MpqArray::~MpqArray
() {
FreeMpqArray
(); }
63
64
void
MpqArray::Resize
(
const
size_t
nElements) {
65
{
66
FreeMpqArray
();
67
AllocateMpqArray
(nElements);
68
}
69
}
70
71
std::ostream &operator<<(std::ostream &os,
const
MpqArray
&array) {
72
os <<
"["
;
73
for
(
int
i = 0; i < static_cast<int>(array.size()); ++i) {
74
os << array[i];
75
if
(i + 1u < array.size()) os <<
", "
;
76
}
77
os <<
"]"
;
78
return
os;
79
}
80
81
namespace
{
82
bool
is_qsopt_initialized =
false
;
83
}
84
85
void
QSXStart() {
86
if
(!is_qsopt_initialized) QSexactStart();
87
is_qsopt_initialized =
true
;
88
}
89
90
void
QSXFinish() {
91
if
(is_qsopt_initialized) QSexactClear();
92
is_qsopt_initialized =
false
;
93
}
94
95
}
// namespace delpi::qsopt_ex
delpi::qsopt_ex::MpqArray
A wrapper around an array of mpq_t elements.
Definition
qsopt_ex.h:66
delpi::qsopt_ex::MpqArray::Resize
void Resize(size_t nElements)
Resize the array to have nElements elements.
Definition
qsopt_ex.cpp:64
delpi::qsopt_ex::MpqArray::array_
mpq_t * array_
array of mpq_t. It is allocated by AllocateMpqArray() and freed by FreeMpqArray().
Definition
qsopt_ex.h:120
delpi::qsopt_ex::MpqArray::~MpqArray
~MpqArray()
Destroy the MpqArray object, freeing the array.
Definition
qsopt_ex.cpp:62
delpi::qsopt_ex::MpqArray::FreeMpqArray
void FreeMpqArray()
Free the array of mpq_t.
Definition
qsopt_ex.cpp:50
delpi::qsopt_ex::MpqArray::AllocateMpqArray
void AllocateMpqArray(size_t n_elements)
Allocate the array with n_elements elements.
Definition
qsopt_ex.cpp:34
delpi::qsopt_ex::MpqArray::MpqArray
MpqArray(size_t n_elements=0)
Construct a new MpqArray object, allocating the array with n_elements elements.
Definition
qsopt_ex.cpp:60
delpi::qsopt_ex
Namespace containing all the utility functions to interact with the QSopt_ex solver.
Definition
qsopt_ex.cpp:13
delpi::qsopt_ex::StringToMpq
mpq_class StringToMpq(const std::string &str)
Convert a string to a mpq_class.
Definition
qsopt_ex.cpp:16
delpi::qsopt_ex::StringToMpqPtr
mpq_class * StringToMpqPtr(const std::string &str)
Convert a string to a mpq_class.
Definition
qsopt_ex.cpp:15
delpi::qsopt_ex::CStringToMpqPtr
mpq_class * CStringToMpqPtr(const char str[])
Convert a C-string to a mpq_class.
Definition
qsopt_ex.cpp:17
delpi::qsopt_ex::CStringToMpq
mpq_class CStringToMpq(const char str[])
Convert a string to a mpq_class.
Definition
qsopt_ex.cpp:25
delpi
libs
qsopt_ex.cpp
Generated by
1.17.0