dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
libqsopt_ex.cpp
1
7#include "libqsopt_ex.h"
8
10
11mpq_class *StringToMpqPtr(const std::string &str) { return CStringToMpqPtr(str.c_str()); }
12mpq_class StringToMpq(const std::string &str) { return CStringToMpq(str.c_str()); }
13mpq_class *CStringToMpqPtr(const char str[]) {
14 mpq_t val;
15 mpq_init(val);
16 mpq_EGlpNumReadStr(val, str);
17 auto result = new mpq_class(val);
18 mpq_clear(val);
19 return result;
20}
21mpq_class CStringToMpq(const char str[]) {
22 mpq_t val;
23 mpq_init(val);
24 mpq_EGlpNumReadStr(val, str);
25 mpq_class result(val);
26 mpq_clear(val);
27 return result;
28}
29
30void MpqArray::AllocateMpqArray(size_t n_elements) {
31 auto const memSize = static_cast<size_t>(sizeof(mpq_t) * n_elements + sizeof(size_t));
32 void *newArray = nullptr;
33 if (memSize) {
34 newArray = calloc(1, memSize);
35 if (!newArray) {
36 fprintf(stderr, "EXIT: Not enough memory while allocating %zd bytes", memSize);
37 exit(1);
38 }
39 }
40 size_t *sizeArray = n_elements ? static_cast<size_t *>(newArray) : nullptr;
41 if (n_elements) sizeArray[0] = n_elements;
42
43 array_ = reinterpret_cast<mpq_t *>(n_elements ? (sizeArray + 1) : nullptr);
44 for (size_t i = 0; i < n_elements; ++i) mpq_init(array_[i]);
45}
46
48 auto *sizeArray = reinterpret_cast<size_t *>(array_);
49 if (sizeArray) sizeArray--;
50 size_t nElements = sizeArray ? sizeArray[0] : 0;
51
52 for (size_t i = 0; i < nElements; ++i) mpq_clear(array_[i]);
53 free(sizeArray);
54 array_ = nullptr;
55}
56
57MpqArray::MpqArray(size_t n_elements) : array_{nullptr} { AllocateMpqArray(n_elements); }
58
60
61void MpqArray::Resize(size_t nElements) {
62 {
64 AllocateMpqArray(nElements);
65 }
66}
67
68void QSXStart() { QSexactStart(); }
69
70void QSXFinish() { QSexactClear(); }
71
72} // namespace dlinear::qsopt_ex
void Resize(size_t nElements)
Resize the array to have nElements elements.
MpqArray(size_t n_elements)
Construct a new MpqArray object, allocating the array with n_elements elements.
mpq_t * array_
array of mpq_t. It is allocated by AllocateMpqArray() and freed by FreeMpqArray().
void AllocateMpqArray(size_t n_elements)
Allocate the array with n_elements elements.
void FreeMpqArray()
Free the array of mpq_t.
~MpqArray()
Destroy the MpqArray object, freeing the array.
mpq_class * CStringToMpqPtr(const char str[])
Convert a C-string to a mpq_class.
mpq_class CStringToMpq(const char str[])
Convert a string to a mpq_class.
mpq_class StringToMpq(const std::string &str)
Convert a string to a mpq_class.
mpq_class * StringToMpqPtr(const std::string &str)
Convert a string to a mpq_class.