carl  24.04
Computer ARithmetic Library
LPContext.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <carl-common/config.h>
4 #ifdef USE_LIBPOLY
5 
6 #include "../../core/Variable.h"
7 
8 #include <map>
9 #include <memory>
10 #include <algorithm>
11 
12 #include <poly/poly.h>
13 #include <poly/polynomial_context.h>
14 
16 
17 #include "LPVariables.h"
18 
19 namespace carl {
20 
21 class LPContext {
22  struct Data {
23  std::vector<Variable> variable_order;
24 
25  lp_variable_order_t* lp_var_order;
26  lp_polynomial_context_t* lp_context;
27 
28  Data(const std::vector<Variable>& v);
29  ~Data();
30  };
31 
32  std::shared_ptr<Data> m_data;
33 
34 public:
35  /**
36  * Default constructor shall not exist.
37  */
38  LPContext() = delete;
39 
40  LPContext(const LPContext& rhs) {
41  m_data = rhs.m_data;
42  }
43 
44  LPContext& operator=(const LPContext& rhs) {
45  m_data = rhs.m_data;
46  return *this;
47  }
48 
49  std::optional<carl::Variable> carl_variable(lp_variable_t var) const {
50  return LPVariables::getInstance().carl_variable(var);
51  }
52 
53  lp_variable_t lp_variable(carl::Variable var) const {
54  return LPVariables::getInstance().lp_variable(var);
55  }
56 
57  std::optional<lp_variable_t> lp_variable_opt(carl::Variable var) const {
58  return LPVariables::getInstance().lp_variable_opt(var);
59  }
60 
61 
62  /**
63  * Construct a Context with a given order of carl::Variable in decreasing order of precedence
64  */
65  LPContext(const std::vector<Variable>& carl_var_order) : m_data(std::make_shared<Data>(carl_var_order)) {
66  CARL_LOG_FUNC("carl.poly", carl_var_order);
67  for (size_t i = 0; i < carl_var_order.size(); i++) {
68  lp_variable_t poly_var = LPVariables::getInstance().lp_variable(carl_var_order[i]);
69  lp_variable_order_push(m_data->lp_var_order, poly_var);
70  }
71  };
72 
73  lp_polynomial_context_t* lp_context() {
74  return m_data->lp_context;
75  };
76 
77  const lp_polynomial_context_t* lp_context() const {
78  return m_data->lp_context;
79  };
80 
81  const std::vector<Variable>& variable_ordering() const {
82  return m_data->variable_order;
83  }
84 
85  bool has(const Variable& var) const {
86  return std::find(variable_ordering().begin(), variable_ordering().end(), var) != variable_ordering().end();
87  };
88 
89  /**
90  * @brief Returns true if the underlying data is the same (by pointers).
91  */
92  inline bool operator==(const LPContext& rhs) const {
93  return m_data == rhs.m_data;
94  }
95 
96  bool is_extension_of(const LPContext& other) const {
97  auto it_a = variable_ordering().begin();
98  auto it_b = other.variable_ordering().begin();
99  while (it_a != variable_ordering().end() && it_b != other.variable_ordering().end() && *it_a == *it_b) {
100  it_a++;
101  it_b++;
102  }
103  return it_b == other.variable_ordering().end();
104  }
105 };
106 
107 inline std::ostream& operator<<(std::ostream& os, const LPContext& ctx) {
108  os << ctx.variable_ordering();
109  return os;
110 }
111 
112 } // namespace carl
113 
114 #endif
#define CARL_LOG_FUNC(channel, args)
Definition: carl-logging.h:46
carl is the main namespace for the library.
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
bool operator==(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85