LocARNA-1.9.2
src/LocARNA/infty_int.hh
00001 #ifndef LOCARNA_INFTY_INT_HH
00002 #define LOCARNA_INFTY_INT_HH
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #include <config.h>
00006 #endif
00007 
00008 #include <algorithm>
00009 #include <iosfwd>
00010 #include <assert.h>
00011 
00012 namespace LocARNA {
00013 
00014     class FiniteInt;
00015     class InftyInt;
00016 
00056     class TaintedInftyInt {
00057     public:
00059         typedef long int base_type;
00060 
00061     protected:
00062         base_type val; 
00063 
00065         static const base_type min_finity;
00066 
00068         static const base_type max_finity;
00069 
00071         static const base_type min_normal_neg_infty;
00072 
00074         static const base_type max_normal_pos_infty;
00075 
00076     public:
00080         TaintedInftyInt() : val(0) {}
00081 
00086         explicit
00087         TaintedInftyInt(const base_type &x) : val(x) {}
00088 
00094         static base_type
00095         min_finite() {
00096             return min_finity;
00097         }
00098 
00104         static base_type
00105         max_finite() {
00106             return max_finity;
00107         }
00108 
00114         bool
00115         is_neg_infty() const {
00116             return val < min_finity;
00117         }
00118 
00124         bool
00125         is_pos_infty() const {
00126             return val > max_finity;
00127         }
00128 
00134         bool
00135         is_finite() const {
00136             return min_finity <= val && val <= max_finity;
00137         }
00138 
00144         bool
00145         is_normal() const {
00146             return min_normal_neg_infty <= val && val <= max_normal_pos_infty;
00147         }
00148 
00155         base_type
00156         finite_value() const {
00157             assert(is_finite());
00158             return val;
00159         }
00160 
00168         TaintedInftyInt &
00169         operator=(const FiniteInt &x);
00170 
00179         friend bool
00180         operator==(const TaintedInftyInt &x, const TaintedInftyInt &y);
00181 
00190         friend TaintedInftyInt
00191         operator+(const TaintedInftyInt &x, const FiniteInt &y);
00192 
00201         friend TaintedInftyInt
00202         operator-(const TaintedInftyInt &x, const FiniteInt &y);
00203 
00212         friend TaintedInftyInt
00213         operator+(const InftyInt &x, const InftyInt &y);
00214 
00223         friend TaintedInftyInt
00224         operator-(const InftyInt &x, const InftyInt &y);
00225 
00234         friend TaintedInftyInt
00235         min(const TaintedInftyInt &x, const TaintedInftyInt &y);
00236 
00245         friend TaintedInftyInt
00246         max(const TaintedInftyInt &x, const TaintedInftyInt &y);
00247 
00259         friend bool
00260         operator>(const TaintedInftyInt &x, const TaintedInftyInt &y);
00261 
00273         friend bool
00274         operator<(const TaintedInftyInt &x, const TaintedInftyInt &y);
00275 
00287         friend bool
00288         operator>=(const TaintedInftyInt &x, const TaintedInftyInt &y);
00289 
00301         friend bool
00302         operator<=(const TaintedInftyInt &x, const TaintedInftyInt &y);
00303 
00304         friend class InftyInt;
00305 
00314         friend std::ostream &
00315         operator<<(std::ostream &out, const TaintedInftyInt &x);
00316     };
00317 
00325     class InftyInt : public TaintedInftyInt {
00326     public:
00328         static const InftyInt neg_infty;
00330         static const InftyInt pos_infty;
00331 
00332     private:
00333         void
00334         normalize() {
00335             // std::cout << "NORMALIZE" <<std::endl;
00336             if (is_neg_infty()) {
00337                 val = neg_infty.val;
00338             } else if (is_pos_infty()) {
00339                 val = pos_infty.val;
00340             }
00341         }
00342 
00343     public:
00348         InftyInt() : TaintedInftyInt() {}
00349 
00355         explicit
00356         InftyInt(const base_type &x) : TaintedInftyInt(x) {
00357             assert(is_normal());
00358         }
00359 
00365         explicit
00366         InftyInt(const FiniteInt &x);
00367 
00374         InftyInt(const TaintedInftyInt &x) : TaintedInftyInt(x) { normalize(); }
00375 
00383         InftyInt &
00384         operator=(TaintedInftyInt &x) {
00385             val = x.val;
00386             normalize();
00387             return *this;
00388         }
00389 
00397         InftyInt &
00398         operator+=(const FiniteInt &x);
00399 
00407         InftyInt &
00408         operator-=(const FiniteInt &x);
00409 
00418         friend InftyInt
00419         operator+(const InftyInt &x, const FiniteInt &y);
00420 
00429         friend InftyInt
00430         operator-(const InftyInt &x, const FiniteInt &y);
00431     };
00432 
00437     class FiniteInt : public InftyInt {
00438     public:
00442         FiniteInt() : InftyInt() {}
00443 
00449         FiniteInt(base_type x) : InftyInt(x) { assert(is_finite()); }
00450 
00456         const base_type &
00457         finite_value() const {
00458             return val;
00459         }
00460 
00468         friend FiniteInt
00469         operator+(const FiniteInt &x, const FiniteInt &y);
00470 
00478         friend FiniteInt
00479         operator-(const FiniteInt &x, const FiniteInt &y);
00480     };
00481 
00482     inline TaintedInftyInt
00483     operator+(const TaintedInftyInt &x, const FiniteInt &y) {
00484         TaintedInftyInt res(x);
00485         res.val += y.val;
00486         return res;
00487     }
00488 
00489     inline TaintedInftyInt
00490     operator-(const TaintedInftyInt &x, const FiniteInt &y) {
00491         TaintedInftyInt res(x);
00492         res.val -= y.val;
00493         return res;
00494     }
00495 
00496     inline TaintedInftyInt
00497     operator+(const InftyInt &x, const InftyInt &y) {
00498         TaintedInftyInt res(x);
00499         res.val += y.val;
00500         return res;
00501     }
00502 
00503     inline TaintedInftyInt
00504     operator-(const InftyInt &x, const InftyInt &y) {
00505         TaintedInftyInt res(x);
00506         res.val -= y.val;
00507         return res;
00508     }
00509 
00510     inline InftyInt &
00511     InftyInt::operator+=(const FiniteInt &x) {
00512         val += x.val;
00513         return *this;
00514     }
00515 
00516     inline InftyInt &
00517     InftyInt::operator-=(const FiniteInt &x) {
00518         val -= x.val;
00519         return *this;
00520     }
00521 
00522     inline InftyInt
00523     operator+(const InftyInt &x, const FiniteInt &y) {
00524         InftyInt res(x);
00525         res.val += y.val;
00526         return res;
00527     }
00528 
00529     inline InftyInt
00530     operator-(const InftyInt &x, const FiniteInt &y) {
00531         InftyInt res(x);
00532         res.val -= y.val;
00533         return res;
00534     }
00535 
00536     inline FiniteInt
00537     operator+(const FiniteInt &x, const FiniteInt &y) {
00538         FiniteInt res(x);
00539         res.val += y.val;
00540         return res;
00541     }
00542 
00543     inline FiniteInt
00544     operator-(const FiniteInt &x, const FiniteInt &y) {
00545         FiniteInt res(x);
00546         res.val -= y.val;
00547         return res;
00548     }
00549 
00550     inline bool
00551     operator==(const TaintedInftyInt &x, const TaintedInftyInt &y) {
00552         return x.val == y.val;
00553     }
00554 
00555     inline TaintedInftyInt &
00556     TaintedInftyInt::operator=(const FiniteInt &x) {
00557         val = x.val;
00558         return *this;
00559     }
00560 
00561     inline TaintedInftyInt
00562     min(const TaintedInftyInt &x, const TaintedInftyInt &y) {
00563         return TaintedInftyInt(std::min(x.val, y.val));
00564     }
00565 
00566     inline TaintedInftyInt
00567     max(const TaintedInftyInt &x, const TaintedInftyInt &y) {
00568         return TaintedInftyInt(std::max(x.val, y.val));
00569     }
00570 
00571     inline InftyInt::InftyInt(const FiniteInt &x) : TaintedInftyInt(x) {}
00572 
00573     inline bool
00574     operator>(const TaintedInftyInt &x, const TaintedInftyInt &y) {
00575         return x.val > y.val;
00576     }
00577 
00578     inline bool
00579     operator<(const TaintedInftyInt &x, const TaintedInftyInt &y) {
00580         return x.val < y.val;
00581     }
00582 
00583     inline bool
00584     operator>=(const TaintedInftyInt &x, const TaintedInftyInt &y) {
00585         return x.val >= y.val;
00586     }
00587 
00588     inline bool
00589     operator<=(const TaintedInftyInt &x, const TaintedInftyInt &y) {
00590         return x.val <= y.val;
00591     }
00592 
00593 } // end namespace LocARNA
00594 
00595 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends