APRONXX 0.9.15
/home/mine/apron/apronxx/apxx_linexpr0.hh
Go to the documentation of this file.
1/* -*- C++ -*-
2 * apxx_linexpr0.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 with an exception allowing the redistribution of statically linked
11 executables.
12
13 Please read the COPYING file packaged in the distribution.
14*/
15
16#ifndef __APXX_LINEXPR0_HH
17#define __APXX_LINEXPR0_HH
18
19#include "ap_linexpr0.h"
20#include "ap_linearize.h"
21#include "apxx_coeff.hh"
22#include "apxx_dimension.hh"
23
25namespace apron {
26
27
28
29/* ================================= */
30/* linexpr0 */
31/* ================================= */
32
33
34/*! \brief Level 0 linear expression (ap_linexpr0_t wrapper).
35 *
36 * A linexpr0 object represents a linear expression with scalar or interval coefficients (coeff).
37 * They have a constant coefficient, plus a coefficient for each represented dimension.
38 * An expression can be either sparse (\c AP_LINEXPR_SPARSE) or dense (\c AP_LINEXPR_DENSE).
39 * Dense expressions use an array of coefficients to represent dimensions from 0 up to the
40 * expression size.
41 * Sparse expressions can have 'holes' between coefficients and grow automatically when
42 * a coefficient for a non-existing dimension is accessed.
43 */
44class linexpr0 : public use_malloc {
45
46protected:
47
48 ap_linexpr0_t l;
49
51 linexpr0(ap_linexpr0_t* p);
52
53 friend class texpr0_tmp;
54 friend class texpr0;
55
56public:
57
58
59 /* constructors */
60 /* ============ */
64
65
66 *
67 * \arg \c discr whether the expression is sparse (\c AP_LINEXPR_SPARSE) or dense (\c AP_LINEXPR_DENSE).
68 * \arg \c size is the initial number of dimensions.
69 */
70 linexpr0(ap_linexpr_discr_t discr=AP_LINEXPR_SPARSE, size_t size=0);
73 linexpr0(const linexpr0& x);
74
76 linexpr0(const linexpr0& x, const dimchange& d);
79 linexpr0(const linexpr0& x, const dimperm& d);
80
85
86 */
87 linexpr0(size_t size, const coeff coeffs[], const coeff& cst, ap_linexpr_discr_t discr=AP_LINEXPR_SPARSE);
88
94
95 linexpr0(const std::vector<coeff>& coeffs, const coeff& cst, ap_linexpr_discr_t discr=AP_LINEXPR_SPARSE);
96
101
102 * \arg \c cst corresponds to the constant coefficient.
103 */
104 linexpr0(size_t size, const coeff coeffs[], const ap_dim_t dims[], const coeff& cst);
105
107
108
109 /* destructor */
110 /* ========== */
111
114
116 ~linexpr0();
117
119
120
121 /* assignment */
122 /* ========== */
123
125
126
128 linexpr0& operator= (const linexpr0& x);
129
130 //@}
131
132
133 /* dimension operations */
134 /* ==================== */
135
138
139
140 void resize(size_t size);
141
143 void add_dimensions(const dimchange& d);
146 void permute_dimensions(const dimperm& d);
147
148
150
151 /* access */
152 /* ====== */
153
154 /** @name Accesses, size */
156
157 /* size */
158
160 size_t size() const;
161
162
163 /* get */
164
165 /*! \brief Returns the expression type.
166 *
167 * \return Either \c AP_LINEXPR_SPARSE or \c AP_LINEXPR_DENSE.
168 */
169 ap_linexpr_discr_t get_discr() const;
170
172 coeff& get_cst();
175 const coeff& get_cst() const;
176
177
179 * Always succeeds if the expression is sparse: if the dimension has no coefficient yet, it is
180 * created and returned (linear time in the worst case).
181 * \throw std::out_of_range if the expression is dense and the dimension exceeds the size
182 * of the expression.
183 * \warning as a side-effect, the method renders previous references to coefficients within the
184 * same expression invalid. For instance, \code l.get_coeff(0) = l.get_coeff(1) = 0; \endcode is invalid.
185 */
186 coeff& operator[](ap_dim_t dim);
187
188
189 const coeff& operator[](ap_dim_t dim) const;
190
192
193
194 /* print */
195 /* ===== */
199
202
204 friend std::ostream& operator<< (std::ostream& os, const linexpr0& s);
205
207 void print(char** name_of_dim = NULL, FILE* stream=stdout) const;
208
210
211
212 /* tests */
213 /* ===== */
214
217
219 bool is_integer(size_t intdim) const;
220
222 bool is_real(size_t intdim) const;
223
229
230 ap_linexpr_type_t get_type() const;
231
233 bool is_linear() const;
234
236 bool is_quasilinear() const;
237
239 friend int compare(const linexpr0& x, const linexpr0& y);
240
242 friend bool equal(const linexpr0& x, const linexpr0& y);
243
244#if 0 // overloaded to make constraints
246 friend bool operator>= (const linexpr0& x, const linexpr0& y);
247
249 friend bool operator<= (const linexpr0& x, const linexpr0& y);
250
252 friend bool operator> (const linexpr0& x, const linexpr0& y);
253
254 //! Whether compare(x,y)<0.
255 friend bool operator< (const linexpr0& x, const linexpr0& y);
256
258 friend bool operator== (const linexpr0& x, const linexpr0& y);
259
261 friend bool operator!= (const linexpr0& x, const linexpr0& y);
262#endif
265
266 /* iterators */
267 /* ========= */
269
272
277
278 * (holes are skipped.)
279 * In all cases, the expression is traversed in increasing dimension order.
280 *
281 * To mutate a linear expression, use the iterator class instead.
282 *
283 * Sample code:
284 * \code for (linexpr0::const_iterator i = m.begin();i.valid();++i) cout << i.get_coeff() << " "; \endcode
285 */
287
288 /* TODO:
289 - reverse traversal (operator--).
290 */
291
292 protected:
293
294 ap_linexpr0_t* l;
295 ap_dim_t pos;
296
298 void skip_AP_DIM_MAX();
299
301 const_iterator(ap_linexpr0_t* l);
302
303 friend class linexpr0;
304
305 public:
306
308 const_iterator(const linexpr0& e);
309
312
315
320 ap_dim_t get_dim() const;
321
326 const coeff& get_coeff() const;
327
329 void next();
330
335 void operator++();
336
338 bool valid() const;
339
340 };
341
342
343 /*! \brief Iterator to traverse and mutate a linear expression.
344 *
345 * As const_iterator, but for non-constant linexpr0.
346 *
347 * Caution: do not modify the expression during traversal, except through the iterator.
348 *
349 * Sample code:
350 * \code for (linexpr0::iterator i = m.begin();i.valid();++i) i.get_coeff().neg(); \endcode
351 */
352 class iterator : public const_iterator {
353
354 /* TODO:
355 - removing/inserting coefficients at iterator position for sparse expressions.
356 */
357
358 protected:
359
361 iterator(ap_linexpr0_t* l);
363 friend class linexpr0;
364
365
366 public:
370
371 //! Duplicates the iterator.
372 iterator(const iterator& i);
373
374
376
378
379 * \throw std::out_of_range if valid() returns false (we are past the last position).
380 */
381 coeff& get_coeff() const;
382
383 };
384
385
387
389 const_iterator begin() const;
390
392
394 /* other operators */
395 /* =============== */
396
399
400 /*! \brief Minimizes all coefficients.
401 *
402 * In sparse expressions, also remove zero and unused (hole) coefficients to save space.
403 */
404 void minimize();
405
406
407 long hash() const;
408
410
411
412 /* TODO: evaluation, linearization, intelligent constructors */
413
415 /* C-level compatibility */
416 /* ===================== */
417
419
422 const ap_linexpr0_t* get_ap_linexpr0_t() const;
423
424
425 ap_linexpr0_t* get_ap_linexpr0_t();
426
428
429};
432
433}
434
435#endif /* __APXX_LINEXPR0_HH */
Coefficient (ap_coeff_t wrapper).
Definition apxx_coeff.hh:36
Represents a dimension (i.e., variable by index) in an expression tree.
Definition apxx_texpr0.hh:33
Dimension change object (ap_dimchange_t wrapper).
Definition apxx_dimension.hh:102
Dimension permutation object (ap_dimperm_t wrapper).
Definition apxx_dimension.hh:292
void operator++()
Moves the iterator to the following position.
Definition apxx_linexpr0.hh:420
ap_dim_t pos
Internal use only. Current index.
Definition apxx_linexpr0.hh:295
void skip_AP_DIM_MAX()
Internal use only. Skips free coefficients in sparse expressions.
Definition apxx_linexpr0.hh:343
ap_linexpr0_t * l
Internal use only. Pointer to the underlying APRON structure.
Definition apxx_linexpr0.hh:294
const_iterator & operator=(const const_iterator &i)
Assigns the iterator.
Definition apxx_linexpr0.hh:379
ap_dim_t get_dim() const
Returns the dimension of the coefficient at the current iterator position.
Definition apxx_linexpr0.hh:393
void next()
Moves the iterator to the following position.
Definition apxx_linexpr0.hh:414
bool valid() const
Whether we are at a valid position (true) or past the last iterator position (false).
Definition apxx_linexpr0.hh:425
const_iterator(ap_linexpr0_t *l)
Internal use only.
Definition apxx_linexpr0.hh:349
const coeff & get_coeff() const
Returns a reference to the coefficient at the current iterator position.
Definition apxx_linexpr0.hh:400
friend class linexpr0
Definition apxx_linexpr0.hh:303
iterator & operator=(const iterator &i)
Assigns the iterator.
Definition apxx_linexpr0.hh:386
iterator(ap_linexpr0_t *l)
Internal use only.
Definition apxx_linexpr0.hh:367
coeff & get_coeff() const
Returns a (modifiable) reference to the coefficient at the current iterator position.
Definition apxx_linexpr0.hh:407
friend class linexpr0
Definition apxx_linexpr0.hh:363
void minimize()
Minimizes all coefficients.
Definition apxx_linexpr0.hh:445
~linexpr0()
Frees all space for the expression and coefficients.
Definition apxx_linexpr0.hh:121
linexpr0(ap_linexpr0_t *p)
Internal use only. Shallow copy of structure followed by a free to take ownership of expression.
Definition apxx_linexpr0.hh:61
iterator begin()
Returns a new iterator to traverse and mutate the linear expression.
Definition apxx_linexpr0.hh:430
bool is_integer(size_t intdim) const
Whether only dimensions greater than intdim have a non-zero coefficient.
Definition apxx_linexpr0.hh:263
coeff & operator[](ap_dim_t dim)
Returns a (modifiable) reference to the coefficient corresponding to the given dimension.
Definition apxx_linexpr0.hh:189
friend int compare(const linexpr0 &x, const linexpr0 &y)
Lexicography ordering, terminating with constant coefficients.
Definition apxx_linexpr0.hh:288
bool is_linear() const
Whether all coefficients are scalar.
Definition apxx_linexpr0.hh:278
ap_linexpr_type_t get_type() const
Gets the type of the linear expression.
Definition apxx_linexpr0.hh:273
friend class texpr0_tmp
Definition apxx_linexpr0.hh:53
bool is_real(size_t intdim) const
Whether only dimensions strictly smaller than intdim have a non-zero coefficient.
Definition apxx_linexpr0.hh:268
linexpr0 & operator=(const linexpr0 &x)
Makes a (deep) copy.
Definition apxx_linexpr0.hh:130
friend bool equal(const linexpr0 &x, const linexpr0 &y)
Structural, syntactical equality.
Definition apxx_linexpr0.hh:294
void resize(size_t size)
Changes the number of coefficients in the expression. (Useful only for dense expressions....
Definition apxx_linexpr0.hh:144
void print(char **name_of_dim=NULL, FILE *stream=stdout) const
Prints to a C stream.
Definition apxx_linexpr0.hh:254
friend std::ostream & operator<<(std::ostream &os, const linexpr0 &s)
Printing.
Definition apxx_linexpr0.hh:230
friend class texpr0
Definition apxx_linexpr0.hh:54
linexpr0(const linexpr0 &x)
Makes a (deep) copy.
Definition apxx_linexpr0.hh:71
bool is_quasilinear() const
Whether all coefficients are scalar, except maybe the constant one.
Definition apxx_linexpr0.hh:283
long hash() const
Returns a hash-code.
Definition apxx_linexpr0.hh:450
size_t size() const
Returns the number of coefficients in the expression.
Definition apxx_linexpr0.hh:165
const ap_linexpr0_t * get_ap_linexpr0_t() const
Returns a pointer to the internal APRON object stored in *this.
Definition apxx_linexpr0.hh:460
coeff & get_cst()
Returns a (modifiable) reference to the constant coefficient.
Definition apxx_linexpr0.hh:179
void permute_dimensions(const dimperm &d)
Applies a permutation on coefficients.
Definition apxx_linexpr0.hh:154
ap_linexpr_discr_t get_discr() const
Returns the expression type.
Definition apxx_linexpr0.hh:173
ap_linexpr0_t l
Structure managed by APRON.
Definition apxx_linexpr0.hh:48
linexpr0(ap_linexpr_discr_t discr=AP_LINEXPR_SPARSE, size_t size=0)
Creates a new expression.
Definition apxx_linexpr0.hh:66
void add_dimensions(const dimchange &d)
Adds some dimensions, shifting coefficients if needed.
Definition apxx_linexpr0.hh:149
Definition apxx_abstract0.hh:27
bool operator<(const abstract0 &x, const abstract0 &y)
Definition apxx_abstract0.hh:439
bool operator>(const abstract0 &x, const abstract0 &y)
Definition apxx_abstract0.hh:434
bool operator==(const abstract0 &x, const abstract0 &y)
Definition apxx_abstract0.hh:410
bool operator!=(const abstract0 &x, const abstract0 &y)
Definition apxx_abstract0.hh:417
bool operator<=(const abstract0 &x, const abstract0 &y)
Definition apxx_abstract0.hh:422
Inherited by most wrappers to map new and delete to malloc and free.
Definition apxx_scalar.hh:69