APRONXX  0.9.12
/home/mine/apron/apronxx/apxx_environment_inline.hh
Go to the documentation of this file.
1 /* -*- C++ -*-
2  * apxx_environment_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 // shallow copy (does not call ap_var_operations->copy)
20 static inline ap_var_t* apxx_environment_get_names(const std::vector<var>& n)
21 {
22  ap_var_t* r = (ap_var_t*)malloc(sizeof(ap_var_t)*n.size());
23  assert(r);
24  for (size_t i=0; i<n.size(); i++) r[i] = n[i].get_ap_var_t();
25  return r;
26 }
27 
28 // shallow copy (does not call ap_var_operations->copy)
29 static inline void apxx_environment_get_names_pair(const std::vector<std::pair<var,var> >& n, ap_var_t** x, ap_var_t** y)
30 {
31  *x = (ap_var_t*)malloc(sizeof(ap_var_t)*n.size());
32  *y = (ap_var_t*)malloc(sizeof(ap_var_t)*n.size());
33  assert(x && y);
34  for (size_t i=0; i<n.size(); i++) {
35  (*x)[i] = n[i].first.get_ap_var_t();
36  (*y)[i] = n[i].second.get_ap_var_t();
37  }
38 }
39 
40 
41 /* constructors */
42 /* ============ */
43 
44 inline environment::environment()
45  : e(ap_environment_alloc_empty())
46 {
47 }
48 
49 inline environment::environment(const std::vector<var>& intdim, const std::vector<var>& realdim)
50 {
51  ap_var_t *i = apxx_environment_get_names(intdim);
52  ap_var_t *r = apxx_environment_get_names(realdim);
53  e = ap_environment_alloc(i, intdim.size(), r, realdim.size());
54  free(i); free(r);
55  if (!e) throw std::invalid_argument("apron::environment::environment(const vector<var>&, const vector<var>&) incompatible variable types");
56 }
57 
58 inline environment::environment(const var* intdim, size_t intdim_size, const var* realdim, size_t realdim_size)
59 {
60  e = ap_environment_alloc(const_cast<ap_var_t*>(reinterpret_cast<const ap_var_t*>(intdim)), intdim_size,
61  const_cast<ap_var_t*>(reinterpret_cast<const ap_var_t*>(realdim)), realdim_size);
62  if (!e) throw std::invalid_argument("apron::environment::environment(const var*, size_t, const var*, size_t) incompatible variable types");
63 }
64 
65 inline environment::environment(const environment& x)
66  : e(ap_environment_copy(x.e))
67 {
68 }
69 
70 inline environment::~environment()
71 {
72  ap_environment_free(e);
73 }
74 
75 
76 /* assignment */
77 /* ========== */
78 
79 inline const environment& environment::operator=(const environment& x)
80 {
81  ap_environment_t* ee = ap_environment_copy(x.e);
82  ap_environment_free(e);
83  e = ee;
84  return *this;
85 }
86 
87 
88 /* operations */
89 /* ========== */
90 
91 
92 inline environment environment::add(const std::vector<var>& intdim, const std::vector<var>& realdim) const
93 {
94  ap_var_t *i = apxx_environment_get_names(intdim);
95  ap_var_t *r = apxx_environment_get_names(realdim);
96  ap_environment_t* ee = ap_environment_add(e, i, intdim.size(), r, realdim.size());
97  free(i); free(r);
98  if (!ee) throw std::invalid_argument("apron::environment::add(const vector<var>&, const vector<var>&) incompatible variable types");
99  return ee;
100 }
101 
102 inline environment environment::add(const var* intdim, size_t intdim_size, const var* realdim, size_t realdim_size) const
103 {
104  ap_environment_t* ee =
105  ap_environment_add(e,
106  const_cast<ap_var_t*>(reinterpret_cast<const ap_var_t*>(intdim)), intdim_size,
107  const_cast<ap_var_t*>(reinterpret_cast<const ap_var_t*>(realdim)), realdim_size);
108  if (!ee) throw std::invalid_argument("apron::environment::add(const var*, size_t, const var*, size_t) incompatible variable types");
109  return ee;
110 }
111 
112 inline environment environment::add(const std::vector<var>& intdim, const std::vector<var>& realdim, dimperm& perm) const
113 {
114  ap_var_t *i = apxx_environment_get_names(intdim);
115  ap_var_t *r = apxx_environment_get_names(realdim);
116  ap_dimperm_t p;
117  ap_environment_t* ee = ap_environment_add_perm(e, i, intdim.size(), r, realdim.size(), &p);
118  free(i); free(r);
119  if (!ee) throw std::invalid_argument("apron::environment::add(const vector<var>&, const vector<var>&, dimperm& perm) incompatible variable types");
120  ap_dimperm_clear(perm.get_ap_dimperm_t());
121  *perm.get_ap_dimperm_t() = p;
122  return ee;
123 }
124 
125 inline environment environment::add(const var* intdim, size_t intdim_size, const var* realdim, size_t realdim_size, dimperm& perm) const
126 {
127  ap_dimperm_t p;
128  ap_environment_t* ee =
129  ap_environment_add_perm(e,
130  const_cast<ap_var_t*>(reinterpret_cast<const ap_var_t*>(intdim)), intdim_size,
131  const_cast<ap_var_t*>(reinterpret_cast<const ap_var_t*>(realdim)), realdim_size,
132  &p);
133  if (!ee) throw std::invalid_argument("apron::environment::add(const var*, size_t, const var*, size_t, dimperm&) incompatible variable types");
134  ap_dimperm_clear(perm.get_ap_dimperm_t());
135  *perm.get_ap_dimperm_t() = p;
136  return ee;
137 }
138 
139 inline environment environment::remove(const std::vector<var>& dims) const
140 {
141  ap_var_t *d = apxx_environment_get_names(dims);
142  ap_environment_t* ee = ap_environment_remove(e, d, dims.size());
143  free(d);
144  if (!ee) throw std::invalid_argument("apron::environment::remove(const vector<var>&) variable not in environment");
145  return ee;
146 }
147 
148 inline environment environment::remove(const var* dims, size_t dims_size) const
149 {
150  ap_environment_t* ee =
151  ap_environment_remove(e,
152  const_cast<ap_var_t*>(reinterpret_cast<const ap_var_t*>(dims)),
153  dims_size);
154  if (!ee) throw std::invalid_argument("apron::environment::remove(const var*, size_t) variable not in environment");
155  return ee;
156 }
157 
158 inline environment environment::rename(const std::vector<std::pair<var,var> >& dims) const
159 {
160  ap_var_t *a, *b;
161  ap_dimperm_t p;
162  apxx_environment_get_names_pair(dims, &a, &b);
163  ap_environment_t* ee = ap_environment_rename(e, a, b, dims.size(), &p);
164  free(a); free(b);
165  if (!ee) throw std::invalid_argument("apron::environment::rename(const vector<std::pair<var,var>>&) variable mismatch");
166  ap_dimperm_clear(&p);
167  return ee;
168 }
169 
170 inline environment environment::rename(const var* before, const var* after, size_t dims_size) const
171 {
172  ap_dimperm_t p;
173  ap_environment_t* ee =
174  ap_environment_rename(e,
175  const_cast<ap_var_t*>(reinterpret_cast<const ap_var_t*>(before)),
176  const_cast<ap_var_t*>(reinterpret_cast<const ap_var_t*>(after)),
177  dims_size, &p);
178  if (!ee) throw std::invalid_argument("apron::environment::rename(const var*, const var*, size_t) variable mismatch");
179  ap_dimperm_clear(&p);
180  return ee;
181 }
182 
183 inline environment environment::rename(const std::vector<std::pair<var,var> >& dims, dimperm& perm) const
184 {
185  ap_var_t *a, *b;
186  ap_dimperm_t p;
187  apxx_environment_get_names_pair(dims, &a, &b);
188  ap_dimperm_clear(perm.get_ap_dimperm_t());
189  ap_environment_t* ee = ap_environment_rename(e, a, b, dims.size(), &p);
190  free(a); free(b);
191  if (!ee) throw std::invalid_argument("apron::environment::rename(const vector<std::pair<var,var>>&, dimperm&) variable mismatch");
192  ap_dimperm_clear(perm.get_ap_dimperm_t());
193  *perm.get_ap_dimperm_t() = p;
194  return ee;
195 }
196 
197 inline environment environment::rename(const var* before, const var* after, size_t dims_size, dimperm& perm) const
198 {
199  ap_dimperm_t p;
200  ap_environment_t* ee =
201  ap_environment_rename(e,
202  const_cast<ap_var_t*>(reinterpret_cast<const ap_var_t*>(before)),
203  const_cast<ap_var_t*>(reinterpret_cast<const ap_var_t*>(after)),
204  dims_size, &p);
205  if (!ee) throw std::invalid_argument("apron::environment::rename(const var*, const var*, size_t, dimperm&) variable mismatch");
206  ap_dimperm_clear(perm.get_ap_dimperm_t());
207  *perm.get_ap_dimperm_t() = p;
208  return ee;
209 }
210 
211 
212 inline environment lce(const environment& x, const environment& y)
213 {
214  ap_dimchange_t *a = NULL, *b = NULL;
215  ap_environment_t* ee = ap_environment_lce(x.e, y.e, &a, &b);
216  if (a) ap_dimchange_free(a);
217  if (b) ap_dimchange_free(b);
218  if (!ee) throw std::invalid_argument("apron::environment::lce(const environment&, const environment&) incompatible variable types");
219  return ee;
220 }
221 
222 inline environment lce(const environment& x, const environment& y, dimchange& chgx, dimchange& chgy)
223 {
224  ap_dimchange_t *a = NULL, *b = NULL;
225  ap_environment_t* ee = ap_environment_lce(x.e, y.e, &a, &b);
226  ap_dimchange_clear(chgx.get_ap_dimchange_t());
227  ap_dimchange_clear(chgy.get_ap_dimchange_t());
228  if (a) { *chgx.get_ap_dimchange_t() = *a; free(a); }
229  else { ap_dimchange_init(chgx.get_ap_dimchange_t(), 0, 0); }
230  if (b) { *chgy.get_ap_dimchange_t() = *b; free(b); }
231  else { ap_dimchange_init(chgy.get_ap_dimchange_t(), 0, 0); }
232  if (!ee) throw std::invalid_argument("apron::environment::lce(const environment&, const environment&, dimchange&, dimchange&) incompatible variable types");
233  return ee;
234 }
235 
236 inline environment lce(const std::vector<environment>& x)
237 {
238  ap_environment_t** ar = (ap_environment_t**)malloc(sizeof(ap_environment_t*)*x.size());
239  ap_dimchange_t** r = NULL;
240  assert(ar);
241  for (size_t i=0; i<x.size(); i++) ar[i] = x[i].e;
242  ap_environment_t* ee = ap_environment_lce_array(ar, x.size(), &r);
243  free(ar);
244  if (r) {
245  for (size_t i=0; i<x.size(); i++)
246  if (r[i]) ap_dimchange_free(r[i]);
247  free(r);
248  }
249  if (!ee) throw std::invalid_argument("apron::environment::lce(const vector<environment>&) incompatible variable types");
250  return ee;
251 }
252 
253 inline environment lce(const environment* env, size_t env_size)
254 {
255  ap_dimchange_t** r = NULL;
256  ap_environment_t* ee =
257  ap_environment_lce_array(reinterpret_cast<ap_environment_t**>(const_cast<environment*>(env)),
258  env_size, &r);
259  if (r) {
260  for (size_t i=0; i<env_size; i++)
261  if (r[i]) ap_dimchange_free(r[i]);
262  free(r);
263  }
264  if (!ee) throw std::invalid_argument("apron::environment::lce(const environment*, size_t) incompatible variable types");
265  return ee;
266 }
267 
268 inline environment lce(const std::vector<environment>& x, std::vector<dimchange>& chg)
269 {
270  if (x.size()>chg.size()) throw std::invalid_argument("apron::environment::lce(const vector<environment>&, vector<dimchange>&) dimchange vector too short");
271  ap_environment_t** ar = (ap_environment_t**)malloc(sizeof(ap_environment_t*)*x.size());
272  ap_dimchange_t** r = NULL;
273  assert(ar);
274  for (size_t i=0; i<x.size(); i++) ar[i] = x[i].e;
275  ap_environment_t* ee = ap_environment_lce_array(ar, x.size(), &r);
276  free(ar);
277  for (size_t i=0; i<x.size(); i++) {
278  ap_dimchange_clear(chg[i].get_ap_dimchange_t());
279  if (r && r[i]) {
280  *chg[i].get_ap_dimchange_t() = *(r[i]);
281  free(r[i]);
282  }
283  else {
284  ap_dimchange_init(chg[i].get_ap_dimchange_t(), 0, 0);
285  }
286  }
287  if (r) free(r);
288  if (!ee) throw std::invalid_argument("apron::environment::lce(const vector<environment>&, vector<dimchange>&) incompatible variable types");
289  return ee;
290 }
291 
292 inline environment lce(const environment* env, dimchange* chg, size_t env_size)
293 {
294  ap_dimchange_t** r = NULL;
295  ap_environment_t* ee =
296  ap_environment_lce_array(reinterpret_cast<ap_environment_t**>(const_cast<environment*>(env)),
297  env_size, &r);
298  for (size_t i=0; i<env_size; i++) {
299  ap_dimchange_clear(chg[i].get_ap_dimchange_t());
300  if (r && r[i]) {
301  *chg[i].get_ap_dimchange_t() = *(r[i]);
302  free(r[i]);
303  }
304  else {
305  ap_dimchange_init(chg[i].get_ap_dimchange_t(), 0, 0);
306  }
307  }
308  if (r) free(r);
309  if (!ee) throw std::invalid_argument("apron::environment::lce(const environment*, dimchange*, size_t) incompatible variable types");
310  return ee;
311 }
312 
313 inline dimchange get_dimchange(const environment& x, const environment& y)
314 {
315  ap_dimchange_t* d = ap_environment_dimchange(x.e,y.e);
316  if (!d) throw std::invalid_argument("apron::environment::get_dimchange(const environment&, const environment &) not a super-type");
317  dimchange c;
318  *c.get_ap_dimchange_t() = *d;
319  free(d);
320  return c;
321 }
322 
323 
324 /* access */
325 /* ====== */
326 
327 inline size_t environment::intdim() const
328 {
329  return e->intdim;
330 }
331 
332 inline size_t environment::realdim() const
333 {
334  return e->realdim;
335 }
336 
337 inline bool environment::contains(const var& x) const
338 {
339  return ap_environment_mem_var(e, x.get_ap_var_t());
340 }
341 
342 inline ap_dim_t environment::operator[] (const var& x) const
343 {
344  ap_dim_t d = ap_environment_dim_of_var(e, x.get_ap_var_t());
345  if (d==AP_DIM_MAX) throw std::invalid_argument("apron::environment::operator[](const var&) variable not in environment");
346  return d;
347 }
348 
349 inline const var& environment::operator[] (ap_dim_t d) const
350 {
351  return *reinterpret_cast<var*>(&e->var_of_dim[d]);
352 }
353 
354 inline ap_dim_t environment::get_dim(const var& x) const
355 {
356  ap_dim_t d = ap_environment_dim_of_var(e, x.get_ap_var_t());
357  if (d==AP_DIM_MAX) throw std::invalid_argument("apron::environment::get_dim(const var&) variable not in environment");
358  return d;
359 }
360 
361 inline const var& environment::get_var(ap_dim_t d) const
362 {
363  if (d >= e->intdim+e->realdim) throw std::out_of_range("environment::get_var");
364  return *reinterpret_cast<var*>(&e->var_of_dim[d]);
365 }
366 
367 inline std::vector<var> environment::get_vars() const
368 {
369  std::vector<var> v = std::vector<var>(e->intdim+e->realdim, "");
370  for (size_t i=0; i<e->intdim+e->realdim; i++) v[i] = e->var_of_dim[i];
371  return v;
372 }
373 
374 
375 /* Tests */
376 /* ===== */
377 
378 inline bool operator==(const environment& x, const environment& y)
379 {
380  return ap_environment_is_eq(x.e, y.e);
381 }
382 
383 inline bool operator!=(const environment& x, const environment& y)
384 {
385  return !ap_environment_is_eq(x.e, y.e);
386 }
387 
388 inline bool operator<=(const environment& x, const environment& y)
389 {
390  return ap_environment_is_leq(x.e, y.e);
391 }
392 
393 inline bool operator>=(const environment& x, const environment& y)
394 {
395  return ap_environment_is_leq(y.e, x.e);
396 }
397 
398 inline int cmp(const environment& x, const environment& y)
399 {
400  return ap_environment_compare(x.e, y.e);
401 }
402 
403 
404 /* print */
405 
406 inline std::ostream& operator<< (std::ostream& os, const environment& s)
407 {
408  for (size_t i=0; i<s.e->intdim+s.e->realdim; i++) {
409  char* c = ap_var_operations->to_string(s.e->var_of_dim[i]);
410  os << i << ": " << c << (i<s.e->intdim ? " (int)" : " (real)") << std::endl;
411  free(c);
412  }
413  return os;
414 }
415 
416 inline void environment::print(FILE* stream) const
417 {
418  ap_environment_fdump(stream, e);
419 }
420 
421 
422 /* C API compatibility */
423 /* =================== */
424 
425 inline const ap_environment_t* environment::get_ap_environment_t() const
426 {
427  return e;
428 }
429 
430 inline ap_environment_t*environment:: get_ap_environment_t()
431 {
432  return e;
433 }
434 
bool operator>=(const environment &x, const environment &y)
Definition: apxx_environment_inline.hh:393
bool operator!=(const environment &x, const environment &y)
Definition: apxx_environment_inline.hh:383
environment lce(const environment &x, const environment &y)
Definition: apxx_environment_inline.hh:212
texpr0::builder add(const texpr0::builder &a, const texpr0::builder &b, ap_texpr_rtype_t rtype=AP_RTYPE_REAL, ap_texpr_rdir_t rdir=AP_RDIR_NEAREST)
Definition: apxx_texpr0.hh:772
int cmp(const environment &x, const environment &y)
Definition: apxx_environment_inline.hh:398
bool operator<=(const environment &x, const environment &y)
Definition: apxx_environment_inline.hh:388
abstract1 & rename(manager &m, abstract1 &dst, const abstract1 &src, size_t size, const var oldv[], const var newv[])
Definition: apxx_abstract1.hh:1396
std::ostream & operator<<(std::ostream &os, const environment &s)
Definition: apxx_environment_inline.hh:406
static ap_var_t * apxx_environment_get_names(const std::vector< var > &n)
Definition: apxx_environment_inline.hh:20
static void apxx_environment_get_names_pair(const std::vector< std::pair< var, var > > &n, ap_var_t **x, ap_var_t **y)
Definition: apxx_environment_inline.hh:29
bool operator==(const environment &x, const environment &y)
Definition: apxx_environment_inline.hh:378
dimchange get_dimchange(const environment &x, const environment &y)
Definition: apxx_environment_inline.hh:313