src/lemon/maps.h
changeset 1064 5f0a20861a77
parent 1041 9d503ce002db
child 1070 6aa1520a0f2f
equal deleted inserted replaced
4:ef2cd9b00f51 5:ba16ff845c08
   194     ///Constructor
   194     ///Constructor
   195 
   195 
   196     ///\e
   196     ///\e
   197     ///
   197     ///
   198     AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   198     AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   199     Value operator[](Key k) {return m1[k]+m2[k];}
   199     Value operator[](Key k) const {return m1[k]+m2[k];}
   200   };
   200   };
   201   
   201   
   202   ///Returns an \ref AddMap class
   202   ///Returns an \ref AddMap class
   203 
   203 
   204   ///This function just returns an \ref AddMap class.
   204   ///This function just returns an \ref AddMap class.
   231     ///Constructor
   231     ///Constructor
   232 
   232 
   233     ///\e
   233     ///\e
   234     ///
   234     ///
   235     SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   235     SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   236     Value operator[](Key k) {return m1[k]-m2[k];}
   236     Value operator[](Key k) const {return m1[k]-m2[k];}
   237   };
   237   };
   238   
   238   
   239   ///Returns a \ref SubMap class
   239   ///Returns a \ref SubMap class
   240 
   240 
   241   ///This function just returns a \ref SubMap class.
   241   ///This function just returns a \ref SubMap class.
   267     ///Constructor
   267     ///Constructor
   268 
   268 
   269     ///\e
   269     ///\e
   270     ///
   270     ///
   271     MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   271     MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   272     Value operator[](Key k) {return m1[k]*m2[k];}
   272     Value operator[](Key k) const {return m1[k]*m2[k];}
   273   };
   273   };
   274   
   274   
   275   ///Returns a \ref MulMap class
   275   ///Returns a \ref MulMap class
   276 
   276 
   277   ///This function just returns a \ref MulMap class.
   277   ///This function just returns a \ref MulMap class.
   301     ///Constructor
   301     ///Constructor
   302 
   302 
   303     ///\e
   303     ///\e
   304     ///
   304     ///
   305     DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   305     DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   306     Value operator[](Key k) {return m1[k]/m2[k];}
   306     Value operator[](Key k) const {return m1[k]/m2[k];}
   307   };
   307   };
   308   
   308   
   309   ///Returns a \ref DivMap class
   309   ///Returns a \ref DivMap class
   310 
   310 
   311   ///This function just returns a \ref DivMap class.
   311   ///This function just returns a \ref DivMap class.
   324   ///of \c M2,
   324   ///of \c M2,
   325   ///then for
   325   ///then for
   326   ///\code
   326   ///\code
   327   ///  ComposeMap<M1,M2> cm(m1,m2);
   327   ///  ComposeMap<M1,M2> cm(m1,m2);
   328   ///\endcode
   328   ///\endcode
   329   ///<tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
   329   /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>
   330   ///
   330   ///
   331   ///Its \c Key is inherited from \c M2 and its \c Value is from
   331   ///Its \c Key is inherited from \c M2 and its \c Value is from
   332   ///\c M1.
   332   ///\c M1.
   333   ///The \c M2::Value must be convertible to \c M1::Key.
   333   ///The \c M2::Value must be convertible to \c M1::Key.
   334   ///\todo Check the requirements.
   334   ///\todo Check the requirements.
   345     ///Constructor
   345     ///Constructor
   346 
   346 
   347     ///\e
   347     ///\e
   348     ///
   348     ///
   349     ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   349     ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   350     Value operator[](Key k) {return m1[m2[k]];}
   350     Value operator[](Key k) const {return m1[m2[k]];}
   351   };
   351   };
   352   
   352   
   353   ///Returns a \ref ComposeMap class
   353   ///Returns a \ref ComposeMap class
   354 
   354 
   355   ///This function just returns a \ref ComposeMap class.
   355   ///This function just returns a \ref ComposeMap class.
   379     ///Constructor
   379     ///Constructor
   380 
   380 
   381     ///\e
   381     ///\e
   382     ///
   382     ///
   383     NegMap(const M &_m) : m(_m) {};
   383     NegMap(const M &_m) : m(_m) {};
   384     Value operator[](Key k) {return -m[k];}
   384     Value operator[](Key k) const {return -m[k];}
   385   };
   385   };
   386   
   386   
   387   ///Returns a \ref NegMap class
   387   ///Returns a \ref NegMap class
   388 
   388 
   389   ///This function just returns a \ref NegMap class.
   389   ///This function just returns a \ref NegMap class.
   393   {
   393   {
   394     return NegMap<M>(m);
   394     return NegMap<M>(m);
   395   }
   395   }
   396 
   396 
   397 
   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
   398   ///Absolute value of a map
   412 
   399 
   413   ///This \ref concept::ReadMap "read only map" returns the absolute value
   400   ///This \ref concept::ReadMap "read only map" returns the absolute value
   414   ///of the
   401   ///of the
   415   ///value returned by the
   402   ///value returned by the
   416   ///given map. Its \c Key and \c Value will be inherited from \c M.
   403   ///given map. Its \c Key and \c Value will be inherited
   417   ///The function <tt>Value abs(Value)<tt> must be defined, of course.
   404   ///from <tt>M</tt>. <tt>Value</tt>
       
   405   ///must be comparable to <tt>0</tt> and the unary <tt>-</tt>
       
   406   ///operator must be defined for it, of course.
       
   407   ///
       
   408   ///\bug We need a unified way to handle the situation below:
       
   409   ///\code
       
   410   ///  struct _UnConvertible {};
       
   411   ///  template<class A> inline A t_abs(A a) {return _UnConvertible();}
       
   412   ///  template<> inline int t_abs<>(int n) {return abs(n);}
       
   413   ///  template<> inline long int t_abs<>(long int n) {return labs(n);}
       
   414   ///  template<> inline long long int t_abs<>(long long int n) {return ::llabs(n);}
       
   415   ///  template<> inline float t_abs<>(float n) {return fabsf(n);}
       
   416   ///  template<> inline double t_abs<>(double n) {return fabs(n);}
       
   417   ///  template<> inline long double t_abs<>(long double n) {return fabsl(n);}
       
   418   ///\endcode
       
   419   
   418 
   420 
   419   template<class M> 
   421   template<class M> 
   420   class AbsMap
   422   class AbsMap
   421   {
   423   {
   422     const M &m;
   424     const M &m;
   427     ///Constructor
   429     ///Constructor
   428 
   430 
   429     ///\e
   431     ///\e
   430     ///
   432     ///
   431     AbsMap(const M &_m) : m(_m) {};
   433     AbsMap(const M &_m) : m(_m) {};
   432     Value operator[](Key k) {return t_abs(m[k]);}
   434     Value operator[](Key k) const {Value tmp=m[k]; return tmp>=0?tmp:-tmp;}
   433   };
   435   };
   434   
   436   
   435   ///Returns a \ref AbsMap class
   437   ///Returns a \ref AbsMap class
   436 
   438 
   437   ///This function just returns a \ref AbsMap class.
   439   ///This function just returns a \ref AbsMap class.