APRONXX  0.9.12
/builddir/build/BUILD/apron-0.9.13/apronxx/apxx_linexpr0_inline.hh
Go to the documentation of this file.
1 /* -*- C++ -*-
2  * apxx_linexpr0_inline.hh
3  *
4  * APRON Library / C++ inline functions
5  *
6  * DO NOT INCLUDE THIS FILE DIRECTLY
7  *
8  * Copyright (C) Antoine Mine' 2007
9  *
10  */
11 /* This file is part of the APRON Library, released under LGPL license
12  with an exception allowing the redistribution of statically linked
13  executables.
14 
15  Please read the COPYING file packaged in the distribution.
16 */
17 
18 
19 /* ================================= */
20 /* linexpr0 */
21 /* ================================= */
22 
23 static inline void apxx_linexpr0_init(ap_linexpr0_t* d, ap_linexpr_discr_t discr, size_t size)
24 {
25  d->discr = discr;
26  d->size = 0;
27  ap_coeff_init(&d->cst,AP_COEFF_SCALAR);
28  if (discr==AP_LINEXPR_DENSE) d->p.coeff = NULL;
29  else d->p.linterm = NULL;
30  if (size) ap_linexpr0_realloc(d,size);
31 }
32 
33 static inline void apxx_linexpr0_clear(ap_linexpr0_t* d)
34 {
35  ap_linexpr0_realloc(d, 0);
36  ap_coeff_clear(&d->cst);
37 }
38 
39 static inline void apxx_linexpr0_copy(ap_linexpr0_t* d, const ap_linexpr0_t* s)
40 {
41  if (d==s) return;
42  assert(d->discr==s->discr);
43  ap_linexpr0_realloc(d,s->size);
44  ap_coeff_set(&d->cst, const_cast<ap_coeff_t*>(&s->cst));
45  if (d->discr==AP_LINEXPR_DENSE) {
46  for (size_t i=0; i<d->size; i++)
47  ap_coeff_set(&d->p.coeff[i], &s->p.coeff[i]);
48  }
49  else {
50  for (size_t i=0; i<d->size; i++) {
51  ap_coeff_set(&d->p.linterm[i].coeff, &s->p.linterm[i].coeff);
52  d->p.linterm[i].dim = s->p.linterm[i].dim;
53  }
54  }
55 }
56 
57 /* constructors */
58 /* ============ */
59 
60 inline linexpr0::linexpr0(ap_linexpr0_t* p) : l(*p)
61 {
62  free(p);
63 }
64 
65 inline linexpr0::linexpr0(ap_linexpr_discr_t discr, size_t size)
66 {
67  apxx_linexpr0_init(&l, discr, size);
68 }
69 
70 inline linexpr0::linexpr0(const linexpr0& x)
71 {
72  apxx_linexpr0_init(&l, x.l.discr, x.l.size);
73  apxx_linexpr0_copy(&l, &x.l);
74 }
75 
76 inline linexpr0::linexpr0(const linexpr0& x, const dimchange& d)
77 {
78  ap_linexpr0_t* p;
79  p = ap_linexpr0_add_dimensions(const_cast<ap_linexpr0_t*>(&x.l),
80  const_cast<ap_dimchange_t*>(d.get_ap_dimchange_t()));
81  l = *p;
82  free(p);
83 }
84 
85 inline linexpr0::linexpr0(const linexpr0& x, const dimperm& d)
86 {
87  ap_linexpr0_t* p;
88  p = ap_linexpr0_permute_dimensions(const_cast<ap_linexpr0_t*>(&x.l),
89  const_cast<ap_dimperm_t*>(d.get_ap_dimperm_t()));
90  l = *p;
91  free(p);
92 }
93 
94 inline linexpr0::linexpr0(size_t size, const coeff coeffs[], const coeff& cst, ap_linexpr_discr_t discr)
95 {
96  apxx_linexpr0_init(&l, discr, size);
97  for (size_t i=0;i<size;i++) (*this)[i] = coeffs[i];
98  get_cst() = cst;
99 }
100 
101 inline linexpr0::linexpr0(const std::vector<coeff>& coeffs, const coeff& cst, ap_linexpr_discr_t discr)
102 {
103  size_t size = coeffs.size();
104  apxx_linexpr0_init(&l, discr, size);
105  for (size_t i=0;i<size;i++) (*this)[i] = coeffs[i];
106  get_cst() = cst;
107 }
108 
109 inline linexpr0::linexpr0(size_t size, const coeff coeffs[], const ap_dim_t dims[], const coeff& cst)
110 {
111  apxx_linexpr0_init(&l, AP_LINEXPR_SPARSE, size);
112  for (size_t i=0;i<size;i++) (*this)[dims[i]] = coeffs[i];
113  get_cst() = cst;
114 }
115 
116 
117 /* destructor */
118 /* ========== */
119 
121 {
123 }
124 
125 
126 /* assignment */
127 /* ========== */
128 
130 {
131  if (&x!=this) {
133  apxx_linexpr0_init(&l, x.l.discr, x.l.size);
134  apxx_linexpr0_copy(&l, &x.l);
135  }
136  return *this;
137 }
138 
139 
140 /* dimension operations */
141 /* ==================== */
142 
143 inline void linexpr0::resize(size_t size)
144 {
145  ap_linexpr0_realloc(&l, size);
146 }
147 
148 inline void linexpr0::add_dimensions(const dimchange& d)
149 {
150  ap_linexpr0_add_dimensions_with(&l, const_cast<ap_dimchange_t*>(d.get_ap_dimchange_t()));
151 }
152 
154 {
155  ap_linexpr0_permute_dimensions_with(&l, const_cast<ap_dimperm_t*>(d.get_ap_dimperm_t()));
156 }
157 
158 
159 /* access */
160 /* ====== */
161 
162 /* size */
163 
164 inline size_t linexpr0::size() const
165 {
166  return ap_linexpr0_size(const_cast<ap_linexpr0_t*>(&l));
167 }
168 
169 
170 /* get */
171 
172 inline ap_linexpr_discr_t linexpr0::get_discr() const
173 {
174  return l.discr;
175 }
176 
177 
179 {
180  return reinterpret_cast<coeff&>(*ap_linexpr0_cstref(const_cast<ap_linexpr0_t*>(&l)));
181 }
182 
183 inline const coeff& linexpr0::get_cst() const
184 {
185  return reinterpret_cast<coeff&>(*ap_linexpr0_cstref(const_cast<ap_linexpr0_t*>(&l)));
186 }
187 
188 inline coeff& linexpr0::operator[](ap_dim_t dim)
189 {
190  ap_coeff_t* x = ap_linexpr0_coeffref(&l, dim);
191  if (!x) throw std::out_of_range("apron::linexpr0::operator[](ap_dim_t)");
192  return reinterpret_cast<coeff&>(*x);
193 }
194 
195 inline const coeff& linexpr0::operator[](ap_dim_t dim) const
196 {
197  const ap_coeff_t* x = ap_linexpr0_coeffref(const_cast<ap_linexpr0_t*>(&l), dim);
198  if (!x) throw std::out_of_range("apron::linexpr0::operator[](ap_dim)t");
199  return reinterpret_cast<const coeff&>(*x);
200 }
201 
202 
203 /* print */
204 /* ===== */
205 
206 static inline bool print_coeff_sign(std::ostream& os, const coeff& c, bool& first, bool cst)
207 {
208  if (c.is_zero()) return false;
209  if (c.get_discr()==AP_COEFF_SCALAR) {
210  if (c.get_scalar()==1) {
211  if (!first) os << " + ";
212  if (cst) os << "1";
213  }
214  else if (c.get_scalar().sgn()<0) {
215  if (first) os << "- " << -c;
216  else os << " - " << -c;
217  }
218  else if (first) os << c;
219  else os << " + " << c;
220  }
221  else {
222  if (first) os << c;
223  else os << " + " << c;
224  }
225  first = false;
226  return true;
227 }
228 
229 inline std::ostream& operator<<(std::ostream& os, const linexpr0& s)
230 {
231  std::vector<std::string>* names = get_varname(os);
232  bool first = true;
233  if (names) {
234  size_t sz = (*names).size();
235  for (linexpr0::const_iterator i=s.begin();i.valid();++i) {
236  if (print_coeff_sign(os, i.get_coeff(), first, false)) {
237  if (i.get_dim()<sz) os << (*names)[i.get_dim()];
238  else os << "x" << i.get_dim();
239  }
240  }
241  }
242  else {
243  for (linexpr0::const_iterator i=s.begin();i.valid();++i) {
244  if (print_coeff_sign(os, i.get_coeff(), first, false))
245  os << "x" << i.get_dim();
246  }
247  }
248  print_coeff_sign(os, s.get_cst(), first, true);
249  if (first) os << "0";
250  return os;
251 }
252 
253 inline void linexpr0::print(char** name_of_dim, FILE* stream) const
254 {
255  ap_linexpr0_fprint(stream, const_cast<ap_linexpr0_t*>(&l), name_of_dim);
256 }
257 
258 
259 /* tests */
260 /* ===== */
261 
262 inline bool linexpr0::is_integer(size_t intdim) const
263 {
264  return ap_linexpr0_is_integer(const_cast<ap_linexpr0_t*>(&l), intdim);
265 }
266 
267 inline bool linexpr0::is_real(size_t intdim) const
268 {
269  return ap_linexpr0_is_real(const_cast<ap_linexpr0_t*>(&l), intdim);
270 }
271 
272 inline ap_linexpr_type_t linexpr0::get_type() const
273 {
274  return ap_linexpr0_type(const_cast<ap_linexpr0_t*>(&l));
275 }
276 
277 inline bool linexpr0::is_linear() const
278 {
279  return ap_linexpr0_is_linear(const_cast<ap_linexpr0_t*>(&l));
280 }
281 
282 inline bool linexpr0::is_quasilinear() const
283 {
284  return ap_linexpr0_is_quasilinear(const_cast<ap_linexpr0_t*>(&l));
285 }
286 
287 inline int compare(const linexpr0& x, const linexpr0& y)
288 {
289  return ap_linexpr0_compare(const_cast<ap_linexpr0_t*>(&x.l),
290  const_cast<ap_linexpr0_t*>(&y.l));
291 }
292 
293 inline bool equal (const linexpr0& x, const linexpr0& y)
294 {
295  return ap_linexpr0_equal(const_cast<ap_linexpr0_t*>(&x.l),
296  const_cast<ap_linexpr0_t*>(&y.l));
297 }
298 
299 #if 0 // overloaded to make constraints
300 
301 inline bool operator>= (const linexpr0& x, const linexpr0& y)
302 {
303  return ap_linexpr0_compare(const_cast<ap_linexpr0_t*>(&x.l),
304  const_cast<ap_linexpr0_t*>(&y.l)) >= 0; }
305 
306 inline bool operator<= (const linexpr0& x, const linexpr0& y)
307 {
308  return ap_linexpr0_compare(const_cast<ap_linexpr0_t*>(&x.l),
309  const_cast<ap_linexpr0_t*>(&y.l)) <= 0;
310 }
311 
312 inline bool operator> (const linexpr0& x, const linexpr0& y)
313 {
314  return ap_linexpr0_compare(const_cast<ap_linexpr0_t*>(&x.l),
315  const_cast<ap_linexpr0_t*>(&y.l)) > 0;
316 }
317 
318 inline bool operator< (const linexpr0& x, const linexpr0& y)
319 {
320  return ap_linexpr0_compare(const_cast<ap_linexpr0_t*>(&x.l),
321  const_cast<ap_linexpr0_t*>(&y.l)) < 0;
322 }
323 
324 inline bool operator== (const linexpr0& x, const linexpr0& y)
325 {
326  return ap_linexpr0_equal(const_cast<ap_linexpr0_t*>(&x.l),
327  const_cast<ap_linexpr0_t*>(&y.l));
328 }
329 
330 inline bool operator!= (const linexpr0& x, const linexpr0& y)
331 {
332  return !ap_linexpr0_equal(const_cast<ap_linexpr0_t*>(&x.l),
333  const_cast<ap_linexpr0_t*>(&y.l));
334 }
335 
336 #endif
337 
338 
339 /* iterators */
340 /* ========= */
341 
343 {
344  if (l->discr == AP_LINEXPR_DENSE) return;
345  while (pos < l->size && l->p.linterm[pos].dim == AP_DIM_MAX) pos++;
346 }
347 
348 inline linexpr0::const_iterator::const_iterator(ap_linexpr0_t* e)
349  : l(e), pos(0)
350 {
351  skip_AP_DIM_MAX();
352 }
353 
355 {
356  l = const_cast<ap_linexpr0_t*>(e.get_ap_linexpr0_t());
357  pos = 0;
358  skip_AP_DIM_MAX();
359 }
360 
362  : l(i.l), pos(i.pos)
363 {}
364 
365 
366 inline linexpr0::iterator::iterator(ap_linexpr0_t* e)
368 {}
369 
372 {}
373 
376 {}
377 
379 {
380  l = i.l;
381  pos = i.pos;
382  return *this;
383 }
384 
386 {
387  l = i.l;
388  pos = i.pos;
389  return *this;
390 }
391 
392 inline ap_dim_t linexpr0::const_iterator::get_dim() const
393 {
394  if (pos >= l->size) throw std::out_of_range("apron::linexpr0::const_iterator::get_dim()");
395  if (l->discr == AP_LINEXPR_DENSE) return pos;
396  else return l->p.linterm[pos].dim;
397 }
398 
400 {
401  if (pos >= l->size) throw std::out_of_range("apron::linexpr0::const_iterator::get_coeff()");
402  if (l->discr == AP_LINEXPR_DENSE) return reinterpret_cast<coeff&>(l->p.coeff[pos]);
403  else return reinterpret_cast<coeff&>(l->p.linterm[pos].coeff);
404 }
405 
407 {
408  if (pos >= l->size) throw std::out_of_range("apron::linexpr0::iterator::get_coeff()");
409  if (l->discr == AP_LINEXPR_DENSE) return reinterpret_cast<coeff&>(l->p.coeff[pos]);
410  else return reinterpret_cast<coeff&>(l->p.linterm[pos].coeff);
411 }
412 
414 {
415  pos++;
416  skip_AP_DIM_MAX();
417 }
418 
420 {
421  next();
422 }
423 
425 {
426  return pos < l->size;
427 }
428 
430 {
431  return iterator(*this);
432 }
433 
435 {
436  return const_iterator(*this);
437 }
438 
439 
440 
441 /* other operators */
442 /* =============== */
443 
444 inline void linexpr0::minimize()
445 {
446  ap_linexpr0_minimize(&l);
447 }
448 
449 inline long linexpr0::hash() const
450 {
451  return ap_linexpr0_hash(const_cast<ap_linexpr0_t*>(&l));
452 }
453 
454 
455 
456 /* C-level compatibility */
457 /* ===================== */
458 
459 inline const ap_linexpr0_t* linexpr0::get_ap_linexpr0_t() const
460 {
461  return &l;
462 }
463 
464 inline ap_linexpr0_t* linexpr0::get_ap_linexpr0_t()
465 {
466  return &l;
467 }
bool operator==(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0_inline.hh:409
bool operator<=(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0_inline.hh:421
bool operator>(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0_inline.hh:433
bool operator<(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0_inline.hh:438
bool operator>=(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0_inline.hh:428
bool operator!=(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0_inline.hh:416
std::vector< std::string > * get_varname(std::basic_ostream< charT, Traits > &os)
Definition: apxx_dimension_inline.hh:43
int compare(const linexpr0 &x, const linexpr0 &y)
Definition: apxx_linexpr0_inline.hh:287
bool equal(const linexpr0 &x, const linexpr0 &y)
Definition: apxx_linexpr0_inline.hh:293
static void apxx_linexpr0_init(ap_linexpr0_t *d, ap_linexpr_discr_t discr, size_t size)
Definition: apxx_linexpr0_inline.hh:23
static bool print_coeff_sign(std::ostream &os, const coeff &c, bool &first, bool cst)
Definition: apxx_linexpr0_inline.hh:206
static void apxx_linexpr0_clear(ap_linexpr0_t *d)
Definition: apxx_linexpr0_inline.hh:33
static void apxx_linexpr0_copy(ap_linexpr0_t *d, const ap_linexpr0_t *s)
Definition: apxx_linexpr0_inline.hh:39
std::ostream & operator<<(std::ostream &os, const linexpr0 &s)
Definition: apxx_linexpr0_inline.hh:229
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
const ap_dimchange_t * get_ap_dimchange_t() const
Returns a pointer to the internal APRON object stored in *this.
Definition: apxx_dimension_inline.hh:204
Dimension permutation object (ap_dimperm_t wrapper).
Definition: apxx_dimension.hh:292
const ap_dimperm_t * get_ap_dimperm_t() const
Returns a pointer to the internal APRON object stored in *this.
Definition: apxx_dimension_inline.hh:422
Iterator to traverse a constant linexpr0.
Definition: apxx_linexpr0.hh:286
void operator++()
Moves the iterator to the following position.
Definition: apxx_linexpr0_inline.hh:419
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_inline.hh:342
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_inline.hh:378
ap_dim_t get_dim() const
Returns the dimension of the coefficient at the current iterator position.
Definition: apxx_linexpr0_inline.hh:392
void next()
Moves the iterator to the following position.
Definition: apxx_linexpr0_inline.hh:413
bool valid() const
Whether we are at a valid position (true) or past the last iterator position (false).
Definition: apxx_linexpr0_inline.hh:424
const_iterator(ap_linexpr0_t *l)
Internal use only.
const coeff & get_coeff() const
Returns a reference to the coefficient at the current iterator position.
Definition: apxx_linexpr0_inline.hh:399
Iterator to traverse and mutate a linear expression.
Definition: apxx_linexpr0.hh:352
iterator & operator=(const iterator &i)
Assigns the iterator.
Definition: apxx_linexpr0_inline.hh:385
iterator(ap_linexpr0_t *l)
Internal use only.
coeff & get_coeff() const
Returns a (modifiable) reference to the coefficient at the current iterator position.
Definition: apxx_linexpr0_inline.hh:406
Level 0 linear expression (ap_linexpr0_t wrapper).
Definition: apxx_linexpr0.hh:44
void minimize()
Minimizes all coefficients.
Definition: apxx_linexpr0_inline.hh:444
~linexpr0()
Frees all space for the expression and coefficients.
Definition: apxx_linexpr0_inline.hh:120
linexpr0(ap_linexpr0_t *p)
Internal use only. Shallow copy of structure followed by a free to take ownership of expression.
Definition: apxx_linexpr0_inline.hh:60
iterator begin()
Returns a new iterator to traverse and mutate the linear expression.
Definition: apxx_linexpr0_inline.hh:429
bool is_integer(size_t intdim) const
Whether only dimensions greater than intdim have a non-zero coefficient.
Definition: apxx_linexpr0_inline.hh:262
coeff & operator[](ap_dim_t dim)
Returns a (modifiable) reference to the coefficient corresponding to the given dimension.
Definition: apxx_linexpr0_inline.hh:188
bool is_linear() const
Whether all coefficients are scalar.
Definition: apxx_linexpr0_inline.hh:277
ap_linexpr_type_t get_type() const
Gets the type of the linear expression.
Definition: apxx_linexpr0_inline.hh:272
bool is_real(size_t intdim) const
Whether only dimensions strictly smaller than intdim have a non-zero coefficient.
Definition: apxx_linexpr0_inline.hh:267
linexpr0 & operator=(const linexpr0 &x)
Makes a (deep) copy.
Definition: apxx_linexpr0_inline.hh:129
void resize(size_t size)
Changes the number of coefficients in the expression. (Useful only for dense expressions....
Definition: apxx_linexpr0_inline.hh:143
void print(char **name_of_dim=NULL, FILE *stream=stdout) const
Prints to a C stream.
Definition: apxx_linexpr0_inline.hh:253
bool is_quasilinear() const
Whether all coefficients are scalar, except maybe the constant one.
Definition: apxx_linexpr0_inline.hh:282
long hash() const
Returns a hash-code.
Definition: apxx_linexpr0_inline.hh:449
size_t size() const
Returns the number of coefficients in the expression.
Definition: apxx_linexpr0_inline.hh:164
const ap_linexpr0_t * get_ap_linexpr0_t() const
Returns a pointer to the internal APRON object stored in *this.
Definition: apxx_linexpr0_inline.hh:459
coeff & get_cst()
Returns a (modifiable) reference to the constant coefficient.
Definition: apxx_linexpr0_inline.hh:178
void permute_dimensions(const dimperm &d)
Applies a permutation on coefficients.
Definition: apxx_linexpr0_inline.hh:153
ap_linexpr_discr_t get_discr() const
Returns the expression type.
Definition: apxx_linexpr0_inline.hh:172
ap_linexpr0_t l
Structure managed by APRON.
Definition: apxx_linexpr0.hh:48
void add_dimensions(const dimchange &d)
Adds some dimensions, shifting coefficients if needed.
Definition: apxx_linexpr0_inline.hh:148