APRONXX  0.9.12
/home/mine/apron/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 
120 inline linexpr0::~linexpr0()
121 {
123 }
124 
125 
126 /* assignment */
127 /* ========== */
128 
129 inline linexpr0& linexpr0::operator= (const linexpr0& x)
130 {
131  if (&x!=this) {
132  apxx_linexpr0_clear(&l);
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 
153 inline void linexpr0::permute_dimensions(const dimperm& d)
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 
178 inline coeff& linexpr0::get_cst()
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 
342 inline void linexpr0::const_iterator::skip_AP_DIM_MAX()
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 
354 inline linexpr0::const_iterator::const_iterator(const linexpr0& e)
355 {
356  l = const_cast<ap_linexpr0_t*>(e.get_ap_linexpr0_t());
357  pos = 0;
358  skip_AP_DIM_MAX();
359 }
360 
361 inline linexpr0::const_iterator::const_iterator(const const_iterator& i)
362  : l(i.l), pos(i.pos)
363 {}
364 
365 
366 inline linexpr0::iterator::iterator(ap_linexpr0_t* e)
367  : linexpr0::const_iterator(e)
368 {}
369 
370 inline linexpr0::iterator::iterator(linexpr0& e)
371  : linexpr0::const_iterator(e.get_ap_linexpr0_t())
372 {}
373 
374 inline linexpr0::iterator::iterator(const iterator& i)
375  : linexpr0::const_iterator(i.l)
376 {}
377 
378 inline linexpr0::const_iterator& linexpr0::const_iterator::operator=(const const_iterator& i)
379 {
380  l = i.l;
381  pos = i.pos;
382  return *this;
383 }
384 
385 inline linexpr0::iterator& linexpr0::iterator::operator=(const iterator& i)
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 
399 inline const coeff& linexpr0::const_iterator::get_coeff() const
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 
406 inline coeff& linexpr0::iterator::get_coeff() const
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 
413 inline void linexpr0::const_iterator::next()
414 {
415  pos++;
416  skip_AP_DIM_MAX();
417 }
418 
419 inline void linexpr0::const_iterator::operator++()
420 {
421  next();
422 }
423 
424 inline bool linexpr0::const_iterator::valid() const
425 {
426  return pos < l->size;
427 }
428 
429 inline linexpr0::iterator linexpr0::begin()
430 {
431  return iterator(*this);
432 }
433 
434 inline linexpr0::const_iterator linexpr0::begin() const
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 }
std::ostream & operator<<(std::ostream &os, const linexpr0 &s)
Definition: apxx_linexpr0_inline.hh:229
std::vector< std::string > * get_varname(std::basic_ostream< charT, Traits > &os)
Definition: apxx_dimension_inline.hh:43
static void apxx_linexpr0_clear(ap_linexpr0_t *d)
Definition: apxx_linexpr0_inline.hh:33
abstract0 & add_dimensions(manager &m, abstract0 &dst, const abstract0 &src, const dimchange &d, bool project=false)
Definition: apxx_abstract0_inline.hh:1037
bool operator==(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0.hh:410
static void apxx_linexpr0_init(ap_linexpr0_t *d, ap_linexpr_discr_t discr, size_t size)
Definition: apxx_linexpr0_inline.hh:23
bool operator>(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0.hh:434
static bool print_coeff_sign(std::ostream &os, const coeff &c, bool &first, bool cst)
Definition: apxx_linexpr0_inline.hh:206
bool equal(const linexpr0 &x, const linexpr0 &y)
Definition: apxx_linexpr0_inline.hh:293
abstract0 & permute_dimensions(manager &m, abstract0 &dst, const abstract0 &src, const dimperm &d)
Definition: apxx_abstract0_inline.hh:1060
bool operator>=(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0.hh:429
bool operator!=(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0.hh:417
static void apxx_linexpr0_copy(ap_linexpr0_t *d, const ap_linexpr0_t *s)
Definition: apxx_linexpr0_inline.hh:39
bool operator<=(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0.hh:422
int compare(const linexpr0 &x, const linexpr0 &y)
Definition: apxx_linexpr0_inline.hh:287
bool operator<(const abstract0 &x, const abstract0 &y)
Definition: apxx_abstract0.hh:439