COIN-OR::LEMON - Graph Library

Changeset 1041:9d503ce002db in lemon-0.x for src/lemon/maps.h


Ignore:
Timestamp:
01/03/05 17:19:46 (20 years ago)
Author:
Alpar Juttner
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1435
Message:
  • Several convenience maps added to maps.h
  • Improvements in doc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/lemon/maps.h

    r987 r1041  
    1818#define LEMON_MAPS_H
    1919
     20#include<math.h>
     21
    2022///\file
     23///\ingroup maps
    2124///\brief Miscellaneous property maps
    2225///
     
    2730
    2831namespace lemon {
     32
     33  /// \addtogroup maps
     34  /// @{
    2935
    3036  /// Base class of maps.
     
    169175    };
    170176  };
     177
     178
     179  ///Sum of two maps
     180
     181  ///This \ref concept::ReadMap "read only map" returns the sum of the two
     182  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
     183  ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
     184
     185  template<class M1,class M2>
     186  class AddMap
     187  {
     188    const M1 &m1;
     189    const M2 &m2;
     190  public:
     191    typedef typename M1::Key Key;
     192    typedef typename M1::Value Value;
     193
     194    ///Constructor
     195
     196    ///\e
     197    ///
     198    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
     199    Value operator[](Key k) {return m1[k]+m2[k];}
     200  };
     201 
     202  ///Returns an \ref AddMap class
     203
     204  ///This function just returns an \ref AddMap class.
     205  ///\todo How to call these type of functions?
     206  ///
     207  ///\relates AddMap
     208  ///\todo Wrong scope in Doxygen when \c \\relates is used
     209  template<class M1,class M2>
     210  inline AddMap<M1,M2> addMap(const M1 &m1,const M2 &m2)
     211  {
     212    return AddMap<M1,M2>(m1,m2);
     213  }
     214
     215  ///Difference of two maps
     216
     217  ///This \ref concept::ReadMap "read only map" returns the difference
     218  ///of the values returned by the two
     219  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
     220  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
     221
     222  template<class M1,class M2>
     223  class SubMap
     224  {
     225    const M1 &m1;
     226    const M2 &m2;
     227  public:
     228    typedef typename M1::Key Key;
     229    typedef typename M1::Value Value;
     230
     231    ///Constructor
     232
     233    ///\e
     234    ///
     235    SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
     236    Value operator[](Key k) {return m1[k]-m2[k];}
     237  };
     238 
     239  ///Returns a \ref SubMap class
     240
     241  ///This function just returns a \ref SubMap class.
     242  ///
     243  ///\relates SubMap
     244  template<class M1,class M2>
     245  inline SubMap<M1,M2> subMap(const M1 &m1,const M2 &m2)
     246  {
     247    return SubMap<M1,M2>(m1,m2);
     248  }
     249
     250  ///Product of two maps
     251
     252  ///This \ref concept::ReadMap "read only map" returns the product of the
     253  ///values returned by the two
     254  ///given
     255  ///maps. Its \c Key and \c Value will be inherited from \c M1.
     256  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
     257
     258  template<class M1,class M2>
     259  class MulMap
     260  {
     261    const M1 &m1;
     262    const M2 &m2;
     263  public:
     264    typedef typename M1::Key Key;
     265    typedef typename M1::Value Value;
     266
     267    ///Constructor
     268
     269    ///\e
     270    ///
     271    MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
     272    Value operator[](Key k) {return m1[k]*m2[k];}
     273  };
     274 
     275  ///Returns a \ref MulMap class
     276
     277  ///This function just returns a \ref MulMap class.
     278  ///\relates MulMap
     279  template<class M1,class M2>
     280  inline MulMap<M1,M2> mulMap(const M1 &m1,const M2 &m2)
     281  {
     282    return MulMap<M1,M2>(m1,m2);
     283  }
     284 
     285  ///Quotient of two maps
     286
     287  ///This \ref concept::ReadMap "read only map" returns the quotient of the
     288  ///values returned by the two
     289  ///given maps. Its \c Key and \c Value will be inherited from \c M1.
     290  ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
     291
     292  template<class M1,class M2>
     293  class DivMap
     294  {
     295    const M1 &m1;
     296    const M2 &m2;
     297  public:
     298    typedef typename M1::Key Key;
     299    typedef typename M1::Value Value;
     300
     301    ///Constructor
     302
     303    ///\e
     304    ///
     305    DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
     306    Value operator[](Key k) {return m1[k]/m2[k];}
     307  };
     308 
     309  ///Returns a \ref DivMap class
     310
     311  ///This function just returns a \ref DivMap class.
     312  ///\relates DivMap
     313  template<class M1,class M2>
     314  inline DivMap<M1,M2> divMap(const M1 &m1,const M2 &m2)
     315  {
     316    return DivMap<M1,M2>(m1,m2);
     317  }
     318 
     319  ///Composition of two maps
     320
     321  ///This \ref concept::ReadMap "read only map" returns the composition of
     322  ///two
     323  ///given maps. That is to say, if \c m1 is of type \c M1 and \c m2 is
     324  ///of \c M2,
     325  ///then for
     326  ///\code
     327  ///  ComposeMap<M1,M2> cm(m1,m2);
     328  ///\endcode
     329  ///<tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
     330  ///
     331  ///Its \c Key is inherited from \c M2 and its \c Value is from
     332  ///\c M1.
     333  ///The \c M2::Value must be convertible to \c M1::Key.
     334  ///\todo Check the requirements.
     335
     336  template<class M1,class M2>
     337  class ComposeMap
     338  {
     339    const M1 &m1;
     340    const M2 &m2;
     341  public:
     342    typedef typename M2::Key Key;
     343    typedef typename M1::Value Value;
     344
     345    ///Constructor
     346
     347    ///\e
     348    ///
     349    ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
     350    Value operator[](Key k) {return m1[m2[k]];}
     351  };
     352 
     353  ///Returns a \ref ComposeMap class
     354
     355  ///This function just returns a \ref ComposeMap class.
     356  ///\relates ComposeMap
     357  template<class M1,class M2>
     358  inline ComposeMap<M1,M2> composeMap(const M1 &m1,const M2 &m2)
     359  {
     360    return ComposeMap<M1,M2>(m1,m2);
     361  }
     362
     363  ///Negative value of a map
     364
     365  ///This \ref concept::ReadMap "read only map" returns the negative
     366  ///value of the
     367  ///value returned by the
     368  ///given map. Its \c Key and \c Value will be inherited from \c M.
     369  ///The unary \c - operator must be defined for \c Value, of course.
     370
     371  template<class M>
     372  class NegMap
     373  {
     374    const M &m;
     375  public:
     376    typedef typename M::Key Key;
     377    typedef typename M::Value Value;
     378
     379    ///Constructor
     380
     381    ///\e
     382    ///
     383    NegMap(const M &_m) : m(_m) {};
     384    Value operator[](Key k) {return -m[k];}
     385  };
     386 
     387  ///Returns a \ref NegMap class
     388
     389  ///This function just returns a \ref NegMap class.
     390  ///\relates NegMap
     391  template<class M>
     392  inline NegMap<M> negMap(const M &m)
     393  {
     394    return NegMap<M>(m);
     395  }
     396
     397
     398  //\todo We need a unified way to handle the situation below!
     399  struct _UnConvertible {};
     400  template<class A> inline A t_abs(A a) {return _UnConvertible();}
     401
     402  template<> inline int t_abs<>(int n) {return abs(n);}
     403  template<> inline long int t_abs<>(long int n) {return labs(n);}
     404  //\bug llabs() is overloaded
     405  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
     406
     407  template<> inline float t_abs<>(float n) {return fabsf(n);}
     408  template<> inline double t_abs<>(double n) {return fabs(n);}
     409  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
     410
     411  ///Absolute value of a map
     412
     413  ///This \ref concept::ReadMap "read only map" returns the absolute value
     414  ///of the
     415  ///value returned by the
     416  ///given map. Its \c Key and \c Value will be inherited from \c M.
     417  ///The function <tt>Value abs(Value)<tt> must be defined, of course.
     418
     419  template<class M>
     420  class AbsMap
     421  {
     422    const M &m;
     423  public:
     424    typedef typename M::Key Key;
     425    typedef typename M::Value Value;
     426
     427    ///Constructor
     428
     429    ///\e
     430    ///
     431    AbsMap(const M &_m) : m(_m) {};
     432    Value operator[](Key k) {return t_abs(m[k]);}
     433  };
     434 
     435  ///Returns a \ref AbsMap class
     436
     437  ///This function just returns a \ref AbsMap class.
     438  ///\relates AbsMap
     439  template<class M>
     440  inline AbsMap<M> absMap(const M &m)
     441  {
     442    return AbsMap<M>(m);
     443  }
     444
     445  /// @}
    171446 
    172447}
     448
     449
    173450#endif // LEMON_MAPS_H
Note: See TracChangeset for help on using the changeset viewer.