User Manual, Developers Guide and API Documentation

Metric.hpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  * WiFiMac                                                                    *
00003  * This file is part of openWNS (open Wireless Network Simulator)
00004  * _____________________________________________________________________________
00005  *
00006  * Copyright (C) 2004-2007
00007  * Chair of Communication Networks (ComNets)
00008  * Kopernikusstr. 16, D-52074 Aachen, Germany
00009  * phone: ++49-241-80-27910,
00010  * fax: ++49-241-80-22242
00011  * email: info@openwns.org
00012  * www: http://www.openwns.org
00013  * _____________________________________________________________________________
00014  *
00015  * openWNS is free software; you can redistribute it and/or modify it under the
00016  * terms of the GNU Lesser General Public License version 2 as published by the
00017  * Free Software Foundation;
00018  *
00019  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00020  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00021  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00022  * details.
00023  *
00024  * You should have received a copy of the GNU Lesser General Public License
00025  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00026  *
00027  ******************************************************************************/
00028 
00029 #ifndef WIFIMAC_PATHSELECTION_METRIC_HPP
00030 #define WIFIMAC_PATHSELECTION_METRIC_HPP
00031 
00032 #include <iostream>
00033 #include <cassert>
00034 
00035 namespace wifimac { namespace pathselection {
00036 
00042     class Metric
00043     {
00044     public:
00045 
00046         inline Metric();
00047         inline Metric(const Metric& m);
00048         inline Metric(const double& d);
00049 
00050         inline Metric operator - (const Metric& m) const;
00051         inline void   operator -=(const Metric& m);
00052         inline Metric operator + (const Metric& m) const;
00053         inline void   operator +=(const Metric& m);
00054         inline bool   operator < (const Metric& m) const;
00055         inline bool   operator > (const Metric& m) const;
00056         inline bool   operator >=(const Metric& m) const;
00057         inline bool   operator <=(const Metric& m) const;
00058         inline bool   operator ==(const Metric& m) const;
00059         inline bool   operator !=(const Metric& m) const;
00060         inline void   operator = (const Metric& m);
00061         inline Metric operator * (const double& d) const;
00062 
00063         inline bool   isInf() const;
00064         inline bool   isNotInf() const;
00065 
00069         inline bool   isNear(const Metric &m, const double limit = 0.1) const;
00070 
00074         inline double toDouble() const;
00075 
00076         friend std::ostream& operator <<(std::ostream &str, const wifimac::pathselection::Metric& m)
00077         {
00078             if(m.inf)
00079             {
00080                 str << "Inf";
00081             }
00082             else
00083             {
00084                 str << m.value;
00085             }
00086             return str;
00087         }
00088     protected:
00089         double value;
00090         bool inf;
00091     };
00092 
00093 
00094     inline Metric::Metric() :
00095         value(0),
00096         inf(true)
00097     {}
00098 
00099     inline Metric::Metric(const Metric& m) :
00100         value(m.value),
00101         inf(m.inf)
00102     {
00103         assert(m.value >= 0 || inf);
00104     }
00105 
00106     inline Metric::Metric(const double& d) :
00107         value(d),
00108         inf(false)
00109     {
00110         assert(d>=0);
00111     }
00112 
00113     inline Metric Metric::operator - (const Metric &m) const
00114     {
00115         assert(m.value >= 0 || m.inf);
00116         assert(value >= 0 || inf);
00117         if (m.inf || inf)
00118         {
00119             return(Metric());
00120         }
00121         else
00122         {
00123             return(Metric(value - m.value));
00124         }
00125     }
00126 
00127     inline Metric Metric::operator + (const Metric &m) const
00128     {
00129         assert(m.value >= 0 || m.inf);
00130         assert(value >= 0 || inf);
00131         if (m.inf || inf)
00132         {
00133             return(Metric());
00134         }
00135         else
00136         {
00137             return(Metric(value + m.value));
00138         }
00139     }
00140 
00141     inline Metric Metric::operator * (const double& d) const
00142     {
00143         assert(!inf);
00144         assert((d >= 0.0) and (d <= 1.0));
00145         return(Metric(value*d));
00146     }
00147 
00148     inline bool   Metric::operator < (const Metric &m) const
00149     {
00150         // cannot compare two inf's with each other
00151         assert((!inf) || (!m.inf));
00152 
00153         if(inf)
00154         {
00155             return false;
00156         }
00157         if(m.inf)
00158         {
00159             return true;
00160         }
00161         return(value < m.value);
00162     }
00163 
00164     inline bool   Metric::operator > (const Metric &m) const
00165     {
00166         // cannot compare two inf's with each other
00167         assert((!inf) || (!m.inf));
00168 
00169         if(inf)
00170         {
00171             return true;
00172         }
00173         if(m.inf)
00174         {
00175             return false;
00176         }
00177         return(value > m.value);
00178     }
00179 
00180     inline bool   Metric::operator >=(const Metric &m) const
00181     {
00182         return((*this > m) || (*this == m));
00183     }
00184 
00185     inline bool   Metric::operator <=(const Metric &m) const
00186     {
00187         return((*this < m) || (*this == m));
00188     }
00189 
00190     inline bool   Metric::operator ==(const Metric &m) const
00191     {
00192         // cannot compare two inf's with each other
00193         assert((!inf) || (!m.inf));
00194         if (inf || m.inf)
00195         {
00196             return false;
00197         }
00198         else
00199         {
00200             return(value == m.value);
00201         }
00202     }
00203 
00204     inline bool   Metric::operator !=(const Metric &m) const
00205     {
00206         assert((!inf) || (!m.inf));
00207         if (inf || m.inf)
00208         {
00209             return false;
00210         }
00211         else
00212         {
00213             return(value != m.value);
00214         }
00215     }
00216 
00217     inline void   Metric::operator = (const Metric &m)
00218     {
00219         assert(m.value >= 0);
00220         value = m.value;
00221         inf = m.inf;
00222     }
00223 
00224     inline bool   Metric::isInf() const
00225     {
00226         return (inf);
00227     }
00228 
00229     inline bool   Metric::isNotInf() const
00230     {
00231         return (!inf);
00232     }
00233     
00234     inline bool   Metric::isNear(const Metric &m, const double /*limit*/) const
00235     {
00236         if((inf) || (m.inf))
00237             return(false);
00238         if(m.value == 0)
00239             return (value < 0.1 ? true : false);
00240         if (m.value < value)
00241             return((value - m.value) < 0.1*value ? true : false);
00242         else
00243             return((m.value - value) < 0.1*m.value ? true : false);
00244     }
00245     
00246     inline double Metric::toDouble() const
00247     {
00248         assert(!inf);
00249         return(value);
00250     }
00251             
00252 } // pathselection
00253 } // wifimac
00254 
00255 #endif // WIFIMAC_SCHEDULER_HPP

Generated on Fri May 25 03:32:14 2012 for openWNS by  doxygen 1.5.5