delpi  0.0.1
DElta-complete LP solver
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
13namespace delpi::qsopt_ex {
14
15mpq_class *StringToMpqPtr(const std::string &str) { return CStringToMpqPtr(str.c_str()); }
16mpq_class StringToMpq(const std::string &str) { return CStringToMpq(str.c_str()); }
17mpq_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}
25mpq_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
34void 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
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
60MpqArray::MpqArray(const size_t n_elements) : array_{nullptr} { AllocateMpqArray(n_elements); }
61
63
64void MpqArray::Resize(const size_t nElements) {
65 {
67 AllocateMpqArray(nElements);
68 }
69}
70
71std::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
81namespace {
82bool is_qsopt_initialized = false;
83}
84
85void QSXStart() {
86 if (!is_qsopt_initialized) QSexactStart();
87 is_qsopt_initialized = true;
88}
89
90void QSXFinish() {
91 if (is_qsopt_initialized) QSexactClear();
92 is_qsopt_initialized = false;
93}
94
95} // namespace delpi::qsopt_ex
A wrapper around an array of mpq_t elements.
Definition qsopt_ex.h:66
void Resize(size_t nElements)
Resize the array to have nElements elements.
Definition qsopt_ex.cpp:64
mpq_t * array_
array of mpq_t. It is allocated by AllocateMpqArray() and freed by FreeMpqArray().
Definition qsopt_ex.h:120
~MpqArray()
Destroy the MpqArray object, freeing the array.
Definition qsopt_ex.cpp:62
void FreeMpqArray()
Free the array of mpq_t.
Definition qsopt_ex.cpp:50
void AllocateMpqArray(size_t n_elements)
Allocate the array with n_elements elements.
Definition qsopt_ex.cpp:34
MpqArray(size_t n_elements=0)
Construct a new MpqArray object, allocating the array with n_elements elements.
Definition qsopt_ex.cpp:60
Namespace containing all the utility functions to interact with the QSopt_ex solver.
Definition qsopt_ex.cpp:13
mpq_class StringToMpq(const std::string &str)
Convert a string to a mpq_class.
Definition qsopt_ex.cpp:16
mpq_class * StringToMpqPtr(const std::string &str)
Convert a string to a mpq_class.
Definition qsopt_ex.cpp:15
mpq_class * CStringToMpqPtr(const char str[])
Convert a C-string to a mpq_class.
Definition qsopt_ex.cpp:17
mpq_class CStringToMpq(const char str[])
Convert a string to a mpq_class.
Definition qsopt_ex.cpp:25