APRONXX 0.9.15
/home/mine/apron/apronxx/apxx_scalar_inline.hh
Go to the documentation of this file.
1/* -*- C++ -*-
2 * apxx_scalar_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/* use_malloc */
20/* ================================= */
21
22inline void* use_malloc::operator new(size_t sz)
23{
24 void* x = malloc(sz);
25 if (!x) throw std::bad_alloc();
26 return x;
27}
28
29inline void* use_malloc::operator new[](size_t sz)
30{
31 void* x = malloc(sz);
32 if (!x) throw std::bad_alloc();
33 return x;
34}
35
36inline void use_malloc::operator delete(void* p)
37{
38 free(p);
39}
40
41inline void use_malloc::operator delete[](void* p)
42{
43 free(p);
44}
45
46
47
48/* ================================= */
49/* scalar */
50/* ================================= */
51
52
53/* constructors */
54/* ============ */
55
56inline scalar::scalar()
57{
58 ap_scalar_init(&c, AP_SCALAR_DOUBLE);
59}
60
61inline scalar::scalar(int x)
62{
63 ap_scalar_init(&c, AP_SCALAR_MPQ);
64 ap_scalar_set_int(&c, x);
65}
66
67inline scalar::scalar(long x)
68{
69 ap_scalar_init(&c, AP_SCALAR_MPQ);
70 ap_scalar_set_int(&c, x);
71}
72
73inline scalar::scalar(double x)
74{
75 ap_scalar_init(&c, AP_SCALAR_DOUBLE);
76 ap_scalar_set_double(&c, x);
77}
78
79inline scalar::scalar(const frac& x)
80{
81 ap_scalar_init(&c, AP_SCALAR_MPQ);
82 ap_scalar_set_frac(&c, x.num, x.den);
83}
84
85inline scalar::scalar(infty x)
86{
87 ap_scalar_init(&c, AP_SCALAR_DOUBLE);
88 ap_scalar_set_infty(&c, x.sgn);
89}
90
91inline scalar::scalar(const mpq_class& x)
92{
93 ap_scalar_init(&c, AP_SCALAR_MPQ);
94 ap_scalar_set_mpq(&c, const_cast<mpq_class&>(x).get_mpq_t());
95}
96
97inline scalar::scalar(mpfr_t x)
98{
99 ap_scalar_init(&c, AP_SCALAR_MPFR);
100 ap_scalar_set_mpfr(&c,x);
101}
102
103inline scalar::scalar(const scalar& x)
104{
105 ap_scalar_init(&c, x.c.discr);
106 ap_scalar_set(&c, const_cast<ap_scalar_t*>(&x.c));
107}
108
109
110/* destructor */
111/* ========== */
112
113inline scalar::~scalar()
114{
115 ap_scalar_clear(&c);
116}
117
118
119/* assignments */
120/* =========== */
121
122/* = */
123
124inline scalar& scalar::operator= (int x)
125{
126 ap_scalar_set_int(&c, x);
127 return *this;
128}
129
130inline scalar& scalar::operator= (long x)
131{
132 ap_scalar_set_int(&c, x);
133 return *this;
134}
135
136inline scalar& scalar::operator= (double x)
137{
138 ap_scalar_set_double(&c, x);
139 return *this;
140}
141
142inline scalar& scalar::operator= (const frac& x)
143{
144 ap_scalar_set_frac(&c, x.num, x.den);
145 return *this;
146}
147
148inline scalar& scalar::operator= (infty x)
149{
150 ap_scalar_set_infty(&c, x.sgn);
151 return *this;
152}
153
154inline scalar& scalar::operator= (const mpq_class& x)
155{
156 ap_scalar_set_mpq(&c, const_cast<mpq_class&>(x).get_mpq_t());
157 return *this;
158}
159
160inline scalar& scalar::operator= (mpfr_t x)
161{
162 ap_scalar_set_mpfr(&c, x);
163 return *this;
164}
165
166inline scalar& scalar::operator= (const scalar& x)
167{
168 ap_scalar_set(&c, const_cast<ap_scalar_t*>(&x.c));
169 return *this;
170}
171
172
173/* swap */
174
175inline void swap(scalar& a, scalar &b)
176{
177 ap_scalar_swap(&a.c, &b.c);
178}
179
180
181/* access */
182/* ====== */
183
184inline ap_scalar_discr_t scalar::get_discr() const
185{
186 return c.discr;
187}
188
189inline double& scalar::get_double()
190{
191 if (c.discr!=AP_SCALAR_DOUBLE) throw(bad_discriminant("apron::scalar::get_double()"));
192 return c.val.dbl;
193}
194
195inline const double& scalar::get_double() const
196{
197 if (c.discr!=AP_SCALAR_DOUBLE) throw(bad_discriminant("apron::scalar::get_double()"));
198 return c.val.dbl;
199}
200
201inline mpq_class& scalar::get_mpq()
202{
203 if (c.discr!=AP_SCALAR_MPQ) throw(bad_discriminant("apron::scalar::get_mpq()"));
204 return reinterpret_cast<mpq_class&>(*c.val.mpq);
205}
206
207inline const mpq_class& scalar::get_mpq() const
208{
209 if (c.discr!=AP_SCALAR_MPQ) throw(bad_discriminant("apron::scalar::get_mpq()"));
210 return reinterpret_cast<mpq_class&>(*c.val.mpq);
211}
212
213
214/* conversion */
215/* ========== */
216
217inline mpq_class scalar::to_mpq(mp_rnd_t round, order& conv) const
218{
219 mpq_class r;
220 int o = ap_mpq_set_scalar(r.get_mpq_t(), const_cast<ap_scalar_t*>(&c), round);
221 conv = (o>0) ? GREATER : (o<0) ? LESS : EQUAL;
222 return r;
223}
224
225inline double scalar::to_double(mp_rnd_t round, order& conv) const
226{
227 double r;
228 int o = ap_double_set_scalar(&r, const_cast<ap_scalar_t*>(&c), round);
229 conv = (o>0) ? GREATER : (o<0) ? LESS : EQUAL;
230 return r;
231}
232
233inline void scalar::to_mpfr(mpfr_t r, mp_rnd_t round, order& conv) const
234{
235 int o = ap_mpfr_set_scalar(r, const_cast<ap_scalar_t*>(&c), round);
236 conv = (o>0) ? GREATER : (o<0) ? LESS : EQUAL;
237}
238
239inline scalar::operator mpq_class() const
240{
241 order c;
242 return to_mpq(GMP_RNDN, c);
243}
244
245inline scalar::operator double() const
246{
247 order c;
248 return to_double(GMP_RNDN, c);
249}
250
251
252/* print */
253/* ===== */
254
255inline std::ostream& operator<< (std::ostream& os, const scalar& s)
256{
257 int i = ap_scalar_infty(const_cast<ap_scalar_t*>(&s.c));
258 if (i>0) return os << "+oo";
259 if (i<0) return os << "-oo";
260 switch (s.c.discr) {
261 case AP_SCALAR_DOUBLE: return os << s.c.val.dbl;
262 case AP_SCALAR_MPQ: return os << s.c.val.mpq;
263 case AP_SCALAR_MPFR:
264 {
265 double d = mpfr_get_d(s.c.val.mpfr,GMP_RNDU);
266 if (!mpfr_cmp_d(s.c.val.mpfr,d)) return os << d;
267 mp_exp_t e;
268 char* tmp = mpfr_get_str(NULL,&e,10,os.precision(),s.c.val.mpfr,GMP_RNDU);
269 if (tmp[0]=='-' || tmp[0]=='+') {
270 os << tmp[0] << "." << tmp+1;
271 if (e>0) os << "e+" << e;
272 if (e<0) os << "e" << e;
273 }
274 else {
275 os << "." << tmp;
276 if (e>0) os << "e+" << e;
277 if (e<0) os << "e" << e;
278 }
279 mpfr_free_str(tmp);
280 return os;
281 }
282 default: return os;
283 }
284}
285
286inline void scalar::print(FILE* stream) const
287{
288 ap_scalar_fprint(stream, const_cast<ap_scalar_t*>(&c));
289}
290
291
292/* tests */
293/* ===== */
294
295inline scalar::order scalar::is_infty() const
296{
297 return (order) ap_scalar_infty(const_cast<ap_scalar_t*>(&c));
298}
299
300inline scalar::order scalar::sgn () const
301{
302 return (order) ap_scalar_sgn(const_cast<ap_scalar_t*>(&c));
303}
304
305
306inline scalar::order cmp(const scalar& a, const scalar& b)
307{
308 return (scalar::order) ap_scalar_cmp(const_cast<ap_scalar_t*>(&a.c), const_cast<ap_scalar_t*>(&b.c));
309}
310
311inline bool operator>=(const scalar& a, const scalar& b)
312{
313 return ap_scalar_cmp(const_cast<ap_scalar_t*>(&a.c), const_cast<ap_scalar_t*>(&b.c))>=0;
314}
315
316inline bool operator<=(const scalar& a, const scalar& b)
317{
318 return ap_scalar_cmp(const_cast<ap_scalar_t*>(&a.c), const_cast<ap_scalar_t*>(&b.c))<=0;
319}
320
321inline bool operator>(const scalar& a, const scalar& b)
322{
323 return ap_scalar_cmp(const_cast<ap_scalar_t*>(&a.c), const_cast<ap_scalar_t*>(&b.c))>0;
324}
325
326inline bool operator<(const scalar& a, const scalar& b)
327{
328 return ap_scalar_cmp(const_cast<ap_scalar_t*>(&a.c), const_cast<ap_scalar_t*>(&b.c))<0;
329}
330
331inline bool operator==(const scalar& a, const scalar& b)
332{
333 return ap_scalar_equal(const_cast<ap_scalar_t*>(&a.c), const_cast<ap_scalar_t*>(&b.c));
334}
335
336inline bool operator!=(const scalar& a, const scalar& b)
337{
338 return !ap_scalar_equal(const_cast<ap_scalar_t*>(&a.c), const_cast<ap_scalar_t*>(&b.c));
339}
340
341
342inline scalar::order cmp(const scalar& a, int b)
343{
344 return (scalar::order) ap_scalar_cmp_int(const_cast<ap_scalar_t*>(&a.c), b);
345}
346
347inline bool operator>=(const scalar& a, int b)
348{
349 return ap_scalar_cmp_int(const_cast<ap_scalar_t*>(&a.c), b)>=0;
350}
351
352inline bool operator<=(const scalar& a, int b)
353{
354 return ap_scalar_cmp_int(const_cast<ap_scalar_t*>(&a.c), b)<=0;
355}
356
357inline bool operator>(const scalar& a, int b)
358{
359 return ap_scalar_cmp_int(const_cast<ap_scalar_t*>(&a.c), b)>0;
360}
361
362inline bool operator<(const scalar& a, int b)
363{
364 return ap_scalar_cmp_int(const_cast<ap_scalar_t*>(&a.c), b)<0;
365}
366
367inline bool operator==(const scalar& a, int b)
368{
369 return ap_scalar_equal_int(const_cast<ap_scalar_t*>(&a.c), b);
370}
371
372inline bool operator!=(const scalar& a, int b)
373{
374 return !ap_scalar_equal_int(const_cast<ap_scalar_t*>(&a.c), b);
375}
376
377inline scalar::order cmp(int a, const scalar& b)
378{
379 return (scalar::order) ap_scalar_cmp_int(const_cast<ap_scalar_t*>(&b.c), a);
380}
381
382inline bool operator>=(int a, const scalar& b)
383{
384 return ap_scalar_cmp_int(const_cast<ap_scalar_t*>(&b.c), a)<=0;
385}
386
387inline bool operator<=(int a, const scalar& b)
388{
389 return ap_scalar_cmp_int(const_cast<ap_scalar_t*>(&b.c), a)>=0;
390}
391
392inline bool operator>(int a, const scalar& b)
393{
394 return ap_scalar_cmp_int(const_cast<ap_scalar_t*>(&b.c), a)<0;
395}
396
397inline bool operator<(int a, const scalar& b)
398{
399 return ap_scalar_cmp_int(const_cast<ap_scalar_t*>(&b.c), a)>0;
400}
401
402inline bool operator==(int a, const scalar& b)
403{
404 return ap_scalar_equal_int(const_cast<ap_scalar_t*>(&b.c), a);
405}
406
407inline bool operator!=(int a, const scalar& b)
408{
409 return !ap_scalar_equal_int(const_cast<ap_scalar_t*>(&b.c), a);
410}
411
412
413/* other operators */
414/* =============== */
415
416inline void scalar::neg()
417{
418 ap_scalar_neg(&c, &c);
419}
420
421inline scalar scalar::operator- () const
422{
423 scalar r = *this;
424 r.neg();
425 return r;
426}
427
428inline void scalar::inv()
429{
430 ap_scalar_inv(&c, &c);
431}
432
433inline scalar scalar::operator~ () const
434{
435 scalar r = *this;
436 r.inv();
437 return r;
438}
439
440inline long scalar::hash() const
441{
442 return ap_scalar_hash(const_cast<ap_scalar_t*>(&c));
443}
444
445
446/* C API compatibility */
447/* =================== */
448
449inline const ap_scalar_t* scalar::get_ap_scalar_t() const
450{
451 return &c;
452}
453
454inline ap_scalar_t* scalar::get_ap_scalar_t()
455{
456 return &c;
457}
std::ostream & operator<<(std::ostream &os, const scalar &s)
Definition apxx_scalar_inline.hh:255
bool operator<=(const scalar &a, const scalar &b)
Definition apxx_scalar_inline.hh:316
bool operator==(const scalar &a, const scalar &b)
Definition apxx_scalar_inline.hh:331
scalar::order cmp(const scalar &a, const scalar &b)
Definition apxx_scalar_inline.hh:306
bool operator>=(const scalar &a, const scalar &b)
Definition apxx_scalar_inline.hh:311
bool operator>(const scalar &a, const scalar &b)
Definition apxx_scalar_inline.hh:321
void swap(scalar &a, scalar &b)
Definition apxx_scalar_inline.hh:175
bool operator<(const scalar &a, const scalar &b)
Definition apxx_scalar_inline.hh:326
bool operator!=(const scalar &a, const scalar &b)
Definition apxx_scalar_inline.hh:336