00001 00006 #ifndef MAXPLUS_HXX 00007 #define MAXPLUS_HXX 00008 00009 #include<my_lib/MaxPlus.hh> 00010 #include<limits> 00011 #include<sstream> 00012 00013 namespace my_lib { 00014 00015 template<typename T> 00016 MaxPlus<T>::MaxPlus(const T& value) : value(value) {} 00017 00018 template<typename T> 00019 MaxPlus<T>& MaxPlus<T>::operator +=(const MaxPlus& x) { 00020 if(x.value> value) value=x.value; 00021 return *this; 00022 } 00023 00024 template<typename T> 00025 MaxPlus<T>& MaxPlus<T>::operator *=(const MaxPlus& x) { 00026 if(x!=get_zero() && *this!=get_zero()) 00027 value+=x.value; 00028 else 00029 value=get_zero(); 00030 return *this; 00031 } 00032 00033 template<typename T> 00034 MaxPlus<T> MaxPlus<T>::operator +(const MaxPlus& x) const { 00035 MaxPlus y(*this); 00036 return y+=x; 00037 } 00038 00039 template<typename T> 00040 MaxPlus<T> MaxPlus<T>::operator *(const MaxPlus& x) const { 00041 MaxPlus y(*this); 00042 return y*=x; 00043 } 00044 00045 template<typename T> 00046 MaxPlus<T>::operator T() const { 00047 return value; 00048 } 00049 00050 template<typename T> 00051 const MaxPlus<T>& MaxPlus<T>::get_zero() { 00052 static MaxPlus zero(std::numeric_limits<T>::min()); 00053 return zero; 00054 } 00055 00056 template<typename T> 00057 std::ostream& operator<< (std::ostream& o, const MaxPlus<T>& x) { 00058 if(x.value==MaxPlus<T>::get_zero().value) 00059 o << "-oo"; 00060 else 00061 o << x.value; 00062 return o; 00063 } 00064 00065 template<typename T> 00066 std::istream& operator>> (std::istream& i, MaxPlus<T>& x) { 00067 std::string s; 00068 i >> s; 00069 if(s=="-oo") 00070 x=MaxPlus<T>::get_zero(); 00071 else { 00072 std::istringstream iss(s); 00073 iss >> x.value; 00074 } 00075 } 00076 } //my_lib 00077 00078 #endif //MAXPLUS_HXX