PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/apronxx/apxx_lincons1.hh

https://bitbucket.org/arieg/apron
C++ Header | 528 lines | 82 code | 154 blank | 292 comment | 0 complexity | 7fe0490560553692afa460be3a5e0587 MD5 | raw file
  1. /* -*- C++ -*-
  2. * apxx_lincons1.hh
  3. *
  4. * APRON Library / C++ class wrappers
  5. *
  6. * Copyright (C) Antoine Mine' 2007
  7. *
  8. */
  9. /* This file is part of the APRON Library, released under LGPL license.
  10. Please read the COPYING file packaged in the distribution.
  11. */
  12. #ifndef __APXX_LINCONS1_HH
  13. #define __APXX_LINCONS1_HH
  14. #include "ap_lincons1.h"
  15. #include "apxx_environment.hh"
  16. #include "apxx_lincons0.hh"
  17. #include "apxx_linexpr1.hh"
  18. namespace apron {
  19. /* ================================= */
  20. /* lincons1 */
  21. /* ================================= */
  22. /*! \brief Level 1 linear constraint (ap_lincons1_t wrapper).
  23. *
  24. * Level 1 version of linear constraints with scalar or interval coefficients (coeff).
  25. * Terms are indexed using variable names (var) instead of dimensions (ap_dim_t).
  26. * Internally, a lincons1 wraps together a lincons0 (memory managed) and an environment (holding a
  27. * reference count).
  28. */
  29. class lincons1 : public use_malloc {
  30. protected:
  31. ap_lincons1_t l; //!< Structure managed by APRON.
  32. //! Internal use only. Shallow copy (no copy of lincons0 or environment).
  33. lincons1(ap_lincons1_t p);
  34. friend class lincons1_array;
  35. public:
  36. /* constructors */
  37. /* ============ */
  38. /** @name Constructors */
  39. //@{
  40. /*! \brief Creates a new constraint from a lincons0 (copied) and an environment (reference count
  41. * incremented) associating names to dimensions in lincons0.
  42. */
  43. lincons1(const environment& e, const lincons0& x);
  44. /*! \brief Creates a new (non-modulo) constraint from an empty linear expression.
  45. *
  46. * The auxiliary scalar is not created (has_modulo returns false).
  47. * The linear expression is created sparse and empty (has_linexpr returns true).
  48. * \arg \c e associates a variable name to each dimension (reference counter incremented).
  49. * \arg \c constyp can be \c AP_CONS_EQ, \c AP_CONS_SUPEQ, \c AP_CONS_SUP, or \c AP_CONS_DISEQ (but not \c AP_CONS_EQMOD).
  50. */
  51. lincons1(const environment& e, ap_constyp_t constyp=AP_CONS_SUPEQ);
  52. /*! \brief Creates a new (non-modulo) constraint from a linear expression (copied).
  53. *
  54. * The auxiliary scalar is not created (has_modulo returns false).
  55. * \arg \c constyp can be \c AP_CONS_EQ, \c AP_CONS_SUPEQ, \c AP_CONS_SUP, or \c AP_CONS_DISEQ (but not \c AP_CONS_EQMOD).
  56. */
  57. lincons1(ap_constyp_t constyp, const linexpr1& lin);
  58. /*! \brief Creates a new constraint from a linear expression and a modulo scalar (both copied).
  59. *
  60. * \arg \c constyp can be \c AP_CONS_EQ, \c AP_CONS_SUPEQ, \c AP_CONS_SUP, \c AP_CONS_EQMOD, or \c AP_CONS_DISEQ.
  61. */
  62. lincons1(ap_constyp_t constyp, const linexpr1& lin, const scalar& modulo);
  63. //! Makes an unsatisfiable constraint (-1>=0).
  64. lincons1(const environment& e, unsat x);
  65. //! (Deep) copy of a constraint.
  66. lincons1(const lincons1& x);
  67. /*! \brief Makes a (deep) copy of x and extends its environment.
  68. *
  69. * \throw std::invalid_argument if e is not a super-environment of that of x.
  70. */
  71. lincons1(const lincons1& x, const environment& e);
  72. //@}
  73. /* destructor */
  74. /* ========== */
  75. /** @name Destructor */
  76. //@{
  77. /*! \brief Frees all space for the expression and coefficients, and decrements the reference count
  78. * of the environment.
  79. */
  80. ~lincons1();
  81. //@}
  82. /* assignment */
  83. /* ========== */
  84. /** @name Assignment */
  85. //@{
  86. //! Makes a (deep) copy.
  87. lincons1& operator= (const lincons1& x);
  88. //! Assigns an unsatisfiable constraint to *this (-1>=0).
  89. lincons1& operator= (unsat x);
  90. /*! \brief Sets the auxiliary scalar modulo to c (copied).
  91. *
  92. * Does not fail as get_modulo can: if the constraint was created without an auxiliary scalar,
  93. * it is created.
  94. */
  95. void set_modulo(const scalar& c);
  96. /*! \brief Sets the underlying linear expression to c (copied).
  97. *
  98. * Does not fail as get_linexpr can: if the constraint was created without an underlying expression,
  99. * it is created.
  100. *
  101. * \warning assumes that c and *this have equal environments (unchecked).
  102. */
  103. void set_linexpr(const linexpr1& c);
  104. //@}
  105. /* dimension operations */
  106. /* ==================== */
  107. /** @name Dimension operations */
  108. //@{
  109. /*! \brief Extends the environment of the expression.
  110. *
  111. * \throw std::invalid_argument if e is not a super-environment of that of *this.
  112. */
  113. void extend_environment(const environment& e);
  114. //@}
  115. /* access */
  116. /* ====== */
  117. /** @name Accesses, size */
  118. //@{
  119. //! Returns the environment of the constraint (with incremented reference count).
  120. environment get_environment() const;
  121. //! Returns a reference to the underlying lincons0.
  122. const lincons0& get_lincons0() const;
  123. //! Returns a (modifiable) reference to the underlying lincons0.
  124. lincons0& get_lincons0();
  125. /*! \brief Returns the size of the underlying linear expression.
  126. *
  127. * \throw std::invalid_argument if no valid linear expression has been defined.
  128. */
  129. size_t size() const;
  130. /*! \brief Returns a (modifiable) reference to the constraint type.
  131. *
  132. * \return either \c AP_CONS_EQ, \c AP_CONS_SUPEQ, \c AP_CONS_SUP, \c AP_CONS_EQMOD, or \c AP_CONS_DISEQ.
  133. */
  134. ap_constyp_t& get_constyp();
  135. /*! \brief Returns a reference to the constraint type.
  136. *
  137. * \return either \c AP_CONS_EQ, \c AP_CONS_SUPEQ, \c AP_CONS_SUP, \c AP_CONS_EQMOD, or \c AP_CONS_DISEQ.
  138. */
  139. const ap_constyp_t& get_constyp() const;
  140. //! Whether the constraint has a valid auxiliary scalar (used in modulo constraints).
  141. bool has_modulo() const;
  142. /*! \brief Whether the constraint has a valid linear expression.
  143. *
  144. * \note The only way the linear expression may be invalid is when accessing fields of uninitialised
  145. * (or enlarged) lincons1_array.
  146. */
  147. bool has_linexpr() const;
  148. /*! \brief Returns a (modifiable) reference to the auxiliary scalar.
  149. *
  150. * \throw std::invalid_argument if no valid auxiliary scalar has been defined.
  151. */
  152. scalar& get_modulo();
  153. /*! \brief Returns a reference to the auxiliary scalar.
  154. *
  155. * \throw std::invalid_argument if no valid auxiliary scalar has been defined.
  156. */
  157. const scalar& get_modulo() const;
  158. /*! \brief Returns a copy of the underlying linear expression.
  159. *
  160. * \throw std::invalid_argument if no valid linear expression has been defined.
  161. */
  162. linexpr1 get_linexpr() const;
  163. /*! \brief Returns a (modifiable) reference to the constant coefficient.
  164. *
  165. * \throw std::invalid_argument if no valid linear expression has been defined.
  166. */
  167. coeff& get_cst();
  168. /*! \brief Returns a reference to the constant coefficient.
  169. *
  170. * \throw std::invalid_argument if no valid linear expression has been defined.
  171. */
  172. const coeff& get_cst() const;
  173. /*! \brief Returns a (modifiable) reference to the coefficient corresponding to the given variable name.
  174. *
  175. * \throw std::invalid_argument if the variable name is not present in the environment.
  176. * \throw std::invalid_argument if no valid linear expression has been defined.
  177. */
  178. coeff& operator[](const var& v);
  179. /*! \brief Returns a reference to the coefficient corresponding to the given variable name.
  180. *
  181. * \throw std::invalid_argument if the variable name is not present in the environment.
  182. * \throw std::invalid_argument if no valid linear expression has been defined.
  183. */
  184. const coeff& operator[](const var& v) const;
  185. //@}
  186. /* print */
  187. /* ===== */
  188. /** @name Printing */
  189. //@{
  190. //! Printing.
  191. friend std::ostream& operator<< (std::ostream& os, const lincons1& s);
  192. //! Prints to a C stream.
  193. void print(FILE* stream=stdout) const;
  194. //@}
  195. /* tests */
  196. /* ===== */
  197. /** @name Tests */
  198. //@{
  199. /*! \brief Whether the constraint is unsatisfiable.
  200. *
  201. * \throw std::invalid_argument if no valid linear expression has been defined.
  202. */
  203. bool is_unsat() const;
  204. /*! \brief Whether the underlying linear expression has only scalar coefficients.
  205. *
  206. * \throw std::invalid_argument if no valid linear expression has been defined.
  207. */
  208. bool is_linear() const;
  209. /*! \brief Whether the underlying linear expression has only scalar coefficients,
  210. * except maybe for the constant one.
  211. *
  212. * \throw std::invalid_argument if no valid linear expression has been defined.
  213. */
  214. bool is_quasilinear() const;
  215. // TODO: equal, compare (currently not in ap_lincons1.h) ???
  216. //@}
  217. /* TODO: evaluation, linearization, intelligent constructors */
  218. /* C-level compatibility */
  219. /* ===================== */
  220. /** @name C API compatibility */
  221. //@{
  222. //! Returns a pointer to the internal APRON object stored in *this.
  223. const ap_lincons1_t* get_ap_lincons1_t() const;
  224. //! Returns a pointer to the internal APRON object stored in *this.
  225. ap_lincons1_t* get_ap_lincons1_t();
  226. //@}
  227. };
  228. /* ================================= */
  229. /* lincons1_array */
  230. /* ================================= */
  231. /*! \brief Array of linear constraints (ap_lincons1_array_t wrapper).
  232. *
  233. * Level 1 version of linear constraint arrays.
  234. * Internally, a lincons1_array wraps together a lincons0_array (memory managed) and an environment
  235. * (holding a reference count).
  236. * Please note that all constraints share the same environment!
  237. */
  238. class lincons1_array : public use_malloc {
  239. protected:
  240. ap_lincons1_array_t a; //!< Structure managed by APRON.
  241. //! Internal use only. Shallow copy (no copy of lincons0_array or environment).
  242. lincons1_array(ap_lincons1_array_t& a);
  243. friend class abstract1;
  244. public:
  245. /* constructors */
  246. /* ============ */
  247. /** @name Constructors */
  248. //@{
  249. /*! \brief Creates a new constraint array from a lincons0_array (copied) and an environment (reference count
  250. * incremented) associating names to dimensions.
  251. */
  252. lincons1_array(const environment& e, const lincons0_array& x);
  253. /*! \brief Creates a new array of the given size containing uninitialized constraints.
  254. *
  255. * has_modulo and has_linexpr will return false on all elements of the array.
  256. * \arg \c e associates a variable name to each dimension (reference count incremented).
  257. */
  258. lincons1_array(const environment& e, size_t size);
  259. //! (Deep) copy.
  260. lincons1_array(const lincons1_array& x);
  261. /*! \brief Makes a (deep) copy of x and extends its environment.
  262. *
  263. * \throw std::invalid_argument if e is not a super-environment of that of x.
  264. */
  265. lincons1_array(const lincons1_array& x, const environment& e);
  266. /*! \brief Creates a lincons1_array from an array (of size >0) of constraints of the given size (copied).
  267. *
  268. * \warning assumes that all constraints have the same environment (unchecked).
  269. * \throw std::invalid_argument if size<1.
  270. */
  271. lincons1_array(size_t size, const lincons1 x[]);
  272. /*! \brief Creates a lincons1_array from an vector (of size >0) of constraints of the given size (copied).
  273. *
  274. * \warning assumes that all constraints have the same environment (unchecked).
  275. * \throw std::invalid_argument if vector size<1.
  276. */
  277. lincons1_array(const std::vector<lincons1>& x);
  278. //@}
  279. /* destructor */
  280. /* ========== */
  281. /** @name Destructor */
  282. //@{
  283. /*! \brief Frees the space used by the array and all its constraints, and decrements the
  284. * reference count of the environment.
  285. */
  286. ~lincons1_array();
  287. //@}
  288. /* assignment */
  289. /* ========== */
  290. /** @name Assignments */
  291. //@{
  292. //! (Deep) copy.
  293. lincons1_array& operator= (const lincons1_array& x);
  294. /*! \brief Copies the constraints from the array into *this.
  295. *
  296. * \warning assumes that all constraints have the same environment (unchecked).
  297. * \arg x should contain (at least) size elements.
  298. */
  299. lincons1_array& operator= (const lincons1 x[]);
  300. /*! Copies the constraints from the vector into the array, changing its size if needed.
  301. *
  302. * \warning assumes that all constraints have the same environment (unchecked).
  303. */
  304. lincons1_array& operator= (const std::vector<lincons1>& x);
  305. //@}
  306. /* dimension operations */
  307. /* ==================== */
  308. /** @name Dimension operations */
  309. //@{
  310. //! Resizes the array.
  311. void resize(size_t size);
  312. /*! \brief Extends the environment of all expressions in array.
  313. *
  314. * \throw std::invalid_argument if e is not a super-environment of that of *this.
  315. */
  316. void extend_environment(const environment& e);
  317. //@}
  318. /* access */
  319. /* ====== */
  320. /** @name Accesses */
  321. //@{
  322. //! Returns the size of the array.
  323. size_t size() const;
  324. //! Returns the environment shared by all constraints (with incremented reference count).
  325. environment get_environment() const;
  326. //! Returns a reference to the underlying lincons0_array.
  327. const lincons0_array& get_lincons0_array() const;
  328. //! Returns a (modifiable) reference to the underlying lincons0_array.
  329. lincons0_array& get_lincons0_array();
  330. /*! \brief Returns a copy of the constraint at index i.
  331. *
  332. * \throw std::out_of_range if i exceeds the dimension of the array.
  333. */
  334. lincons1 get(size_t i) const;
  335. /*! \brief Changes the constraint at index i.
  336. *
  337. * \warning assumes that x and *this have equal environments.
  338. * \throw std::out_of_range if i exceeds the dimension of the array.
  339. */
  340. void set(size_t i, const lincons1& x);
  341. //@}
  342. /* conversion */
  343. /* ========== */
  344. /** @name Conversion */
  345. //@{
  346. //! Returns a copy of the linear constraints in the form of a vector.
  347. operator std::vector<lincons1>() const;
  348. //@}
  349. /* print */
  350. /* ===== */
  351. /** @name Printing */
  352. //@{
  353. /*! \brief Printing.
  354. *
  355. * \throw std::invalid_argument an underlying expression is missing, or an
  356. * auxiliary scalar is missing (for modulo constraint).
  357. */
  358. friend std::ostream& operator<< (std::ostream& os, const lincons1_array& s);
  359. //! Prints to a C stream.
  360. void print(FILE* stream=stdout) const;
  361. //@}
  362. /* C-level compatibility */
  363. /* ===================== */
  364. /** @name C API compatibility */
  365. //@{
  366. //! Returns a pointer to the internal APRON object stored in *this.
  367. const ap_lincons1_array_t* get_ap_lincons1_array_t() const;
  368. //! Returns a pointer to the internal APRON object stored in *this.
  369. ap_lincons1_array_t* get_ap_lincons1_array_t();
  370. //@}
  371. };
  372. #include "apxx_lincons1_inline.hh"
  373. }
  374. #endif /* __APXX_LINCONS1_HH */