lemon/maps.h
changeset 2057 6f84cdb6f4e8
parent 1993 2115143eceea
child 2080 630a5e16dc12
equal deleted inserted replaced
20:059b58989a79 21:261948a3650f
   349     ///\param _m is the undelying map
   349     ///\param _m is the undelying map
   350     ///\param _v is the shift value
   350     ///\param _v is the shift value
   351     ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
   351     ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
   352     Value operator[](Key k) const {return m[k] + v;}
   352     Value operator[](Key k) const {return m[k] + v;}
   353   };
   353   };
       
   354 
       
   355   ///Shift a map with a constant.
       
   356 
       
   357   ///This \ref concept::ReadWriteMap "read-write map" returns the sum of the
       
   358   ///given map and a constant value. It makes also possible to write the map.
       
   359   ///Its \c Key and \c Value is inherited from \c M.
       
   360   ///
       
   361   ///Actually,
       
   362   ///\code
       
   363   ///  ShiftMap<X> sh(x,v);
       
   364   ///\endcode
       
   365   ///is equivalent with
       
   366   ///\code
       
   367   ///  ConstMap<X::Key, X::Value> c_tmp(v);
       
   368   ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
       
   369   ///\endcode
       
   370   template<typename M, typename C = typename M::Value> 
       
   371   class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
       
   372     M& m;
       
   373     C v;
       
   374   public:
       
   375     typedef MapBase<typename M::Key, typename M::Value> Parent;
       
   376     typedef typename Parent::Key Key;
       
   377     typedef typename Parent::Value Value;
       
   378 
       
   379     ///Constructor
       
   380 
       
   381     ///Constructor
       
   382     ///\param _m is the undelying map
       
   383     ///\param _v is the shift value
       
   384     ShiftWriteMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
       
   385     Value operator[](Key k) const {return m[k] + v;}
       
   386     void set(Key k, const Value& c) { m.set(k, c - v); }
       
   387   };
   354   
   388   
   355   ///Returns an \ref ShiftMap class
   389   ///Returns an \ref ShiftMap class
   356 
   390 
   357   ///This function just returns an \ref ShiftMap class.
   391   ///This function just returns an \ref ShiftMap class.
   358   ///\relates ShiftMap
   392   ///\relates ShiftMap
   359   ///\todo A better name is required.
   393   ///\todo A better name is required.
   360   template<typename M, typename C> 
   394   template<typename M, typename C> 
   361   inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
   395   inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
   362     return ShiftMap<M, C>(m,v);
   396     return ShiftMap<M, C>(m,v);
       
   397   }
       
   398 
       
   399   template<typename M, typename C> 
       
   400   inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
       
   401     return ShiftWriteMap<M, C>(m,v);
   363   }
   402   }
   364 
   403 
   365   ///Difference of two maps
   404   ///Difference of two maps
   366 
   405 
   367   ///This \ref concept::ReadMap "read only map" returns the difference
   406   ///This \ref concept::ReadMap "read only map" returns the difference
   454     ///\param _m is the undelying map
   493     ///\param _m is the undelying map
   455     ///\param _v is the scaling value
   494     ///\param _v is the scaling value
   456     ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
   495     ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
   457     Value operator[](Key k) const {return v * m[k];}
   496     Value operator[](Key k) const {return v * m[k];}
   458   };
   497   };
       
   498 
       
   499   ///Scales a maps with a constant.
       
   500 
       
   501   ///This \ref concept::ReadWriteMap "read-write map" returns the value of the
       
   502   ///given map multiplied from the left side with a constant value. It can
       
   503   ///be used as write map also if the given multiplier is not zero.
       
   504   ///Its \c Key and \c Value is inherited from \c M.
       
   505   template<typename M, typename C = typename M::Value> 
       
   506   class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
       
   507     M& m;
       
   508     C v;
       
   509   public:
       
   510     typedef MapBase<typename M::Key, typename M::Value> Parent;
       
   511     typedef typename Parent::Key Key;
       
   512     typedef typename Parent::Value Value;
       
   513 
       
   514     ///Constructor
       
   515 
       
   516     ///Constructor
       
   517     ///\param _m is the undelying map
       
   518     ///\param _v is the scaling value
       
   519     ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
       
   520     Value operator[](Key k) const {return v * m[k];}
       
   521     void set(Key k, const Value& c) { m.set(k, c / v);}
       
   522   };
   459   
   523   
   460   ///Returns an \ref ScaleMap class
   524   ///Returns an \ref ScaleMap class
   461 
   525 
   462   ///This function just returns an \ref ScaleMap class.
   526   ///This function just returns an \ref ScaleMap class.
   463   ///\relates ScaleMap
   527   ///\relates ScaleMap
   464   ///\todo A better name is required.
   528   ///\todo A better name is required.
   465   template<typename M, typename C> 
   529   template<typename M, typename C> 
   466   inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
   530   inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
   467     return ScaleMap<M, C>(m,v);
   531     return ScaleMap<M, C>(m,v);
       
   532   }
       
   533 
       
   534   template<typename M, typename C> 
       
   535   inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
       
   536     return ScaleWriteMap<M, C>(m,v);
   468   }
   537   }
   469 
   538 
   470   ///Quotient of two maps
   539   ///Quotient of two maps
   471 
   540 
   472   ///This \ref concept::ReadMap "read only map" returns the quotient of the
   541   ///This \ref concept::ReadMap "read only map" returns the quotient of the
   633     ///Constructor
   702     ///Constructor
   634     NegMap(const M &_m) : m(_m) {};
   703     NegMap(const M &_m) : m(_m) {};
   635     Value operator[](Key k) const {return -m[k];}
   704     Value operator[](Key k) const {return -m[k];}
   636   };
   705   };
   637   
   706   
       
   707   ///Negative value of a map
       
   708 
       
   709   ///This \ref concept::ReadWriteMap "read-write map" returns the negative
       
   710   ///value of the value returned by the
       
   711   ///given map. Its \c Key and \c Value will be inherited from \c M.
       
   712   ///The unary \c - operator must be defined for \c Value, of course.
       
   713 
       
   714   template<typename M> 
       
   715   class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
       
   716     M& m;
       
   717   public:
       
   718     typedef MapBase<typename M::Key, typename M::Value> Parent;
       
   719     typedef typename Parent::Key Key;
       
   720     typedef typename Parent::Value Value;
       
   721 
       
   722     ///Constructor
       
   723     NegWriteMap(M &_m) : m(_m) {};
       
   724     Value operator[](Key k) const {return -m[k];}
       
   725     void set(Key k, const Value& v) { m.set(k, -v); }
       
   726   };
       
   727 
   638   ///Returns a \ref NegMap class
   728   ///Returns a \ref NegMap class
   639 
   729 
   640   ///This function just returns a \ref NegMap class.
   730   ///This function just returns a \ref NegMap class.
   641   ///\relates NegMap
   731   ///\relates NegMap
   642   template <typename M> 
   732   template <typename M> 
   643   inline NegMap<M> negMap(const M &m) {
   733   inline NegMap<M> negMap(const M &m) {
   644     return NegMap<M>(m);
   734     return NegMap<M>(m);
   645   }
   735   }
   646 
   736 
       
   737   template <typename M> 
       
   738   inline NegWriteMap<M> negMap(M &m) {
       
   739     return NegWriteMap<M>(m);
       
   740   }
   647 
   741 
   648   ///Absolute value of a map
   742   ///Absolute value of a map
   649 
   743 
   650   ///This \ref concept::ReadMap "read only map" returns the absolute value
   744   ///This \ref concept::ReadMap "read only map" returns the absolute value
   651   ///of the
   745   ///of the
   785   template<typename M> 
   879   template<typename M> 
   786   inline MapFunctor<M> mapFunctor(const M &m) {
   880   inline MapFunctor<M> mapFunctor(const M &m) {
   787     return MapFunctor<M>(m);
   881     return MapFunctor<M>(m);
   788   }
   882   }
   789 
   883 
       
   884   ///Applies all map setting operations to two maps
       
   885 
       
   886   ///This map has two \ref concept::ReadMap "readable map"
       
   887   ///parameters and each read request will be passed just to the
       
   888   ///first map. This class is the just readable map type of the ForkWriteMap.
       
   889   ///
       
   890   ///The \c Key and \c Value will be inherited from \c M1.
       
   891   ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
       
   892 
       
   893   template<typename  M1, typename M2> 
       
   894   class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
       
   895     const M1& m1;
       
   896     const M2& m2;
       
   897   public:
       
   898     typedef MapBase<typename M1::Key, typename M1::Value> Parent;
       
   899     typedef typename Parent::Key Key;
       
   900     typedef typename Parent::Value Value;
       
   901 
       
   902     ///Constructor
       
   903     ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
       
   904     Value operator[](Key k) const {return m1[k];}
       
   905   };
       
   906 
   790 
   907 
   791   ///Applies all map setting operations to two maps
   908   ///Applies all map setting operations to two maps
   792 
   909 
   793   ///This map has two \ref concept::WriteMap "writable map"
   910   ///This map has two \ref concept::WriteMap "writable map"
   794   ///parameters and each write request will be passed to both of them.
   911   ///parameters and each write request will be passed to both of them.
   798   ///
   915   ///
   799   ///The \c Key and \c Value will be inherited from \c M1.
   916   ///The \c Key and \c Value will be inherited from \c M1.
   800   ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
   917   ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
   801 
   918 
   802   template<typename  M1, typename M2> 
   919   template<typename  M1, typename M2> 
   803   class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
   920   class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
   804     const M1& m1;
   921     M1& m1;
   805     const M2& m2;
   922     M2& m2;
   806   public:
   923   public:
   807     typedef MapBase<typename M1::Key, typename M1::Value> Parent;
   924     typedef MapBase<typename M1::Key, typename M1::Value> Parent;
   808     typedef typename Parent::Key Key;
   925     typedef typename Parent::Key Key;
   809     typedef typename Parent::Value Value;
   926     typedef typename Parent::Value Value;
   810 
   927 
   811     ///Constructor
   928     ///Constructor
   812     ForkMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
   929     ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
   813     Value operator[](Key k) const {return m1[k];}
   930     Value operator[](Key k) const {return m1[k];}
   814     //    void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
   931     void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
   815   };
   932   };
   816   
   933   
   817   ///Returns an \ref ForkMap class
   934   ///Returns an \ref ForkMap class
   818 
   935 
   819   ///This function just returns an \ref ForkMap class.
   936   ///This function just returns an \ref ForkMap class.
   820   ///\todo How to call these type of functions?
   937   ///\todo How to call these type of functions?
   821   ///
   938   ///
   822   ///\relates ForkMap
   939   ///\relates ForkMap
   823   ///\todo Wrong scope in Doxygen when \c \\relates is used
   940   ///\todo Wrong scope in Doxygen when \c \\relates is used
   824   template <typename M1, typename M2> 
   941   template <typename M1, typename M2> 
   825   inline ForkMap<M1, M2> forkMap(const M1 &m1,const M2 &m2) {
   942   inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
   826     return ForkMap<M1, M2>(m1,m2);
   943     return ForkMap<M1, M2>(m1,m2);
       
   944   }
       
   945 
       
   946   template <typename M1, typename M2> 
       
   947   inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
       
   948     return ForkWriteMap<M1, M2>(m1,m2);
   827   }
   949   }
   828 
   950 
   829 
   951 
   830   
   952   
   831   /* ************* BOOL MAPS ******************* */
   953   /* ************* BOOL MAPS ******************* */
   848 
   970 
   849     /// Constructor
   971     /// Constructor
   850     NotMap(const M &_m) : m(_m) {};
   972     NotMap(const M &_m) : m(_m) {};
   851     Value operator[](Key k) const {return !m[k];}
   973     Value operator[](Key k) const {return !m[k];}
   852   };
   974   };
       
   975 
       
   976   ///Logical 'not' of a map with writing possibility
       
   977   
       
   978   ///This bool \ref concept::ReadWriteMap "read-write map" returns the 
       
   979   ///logical negation of value returned by the given map. It is setted
       
   980   ///then the negation of the value be setted to the original map.
       
   981   ///Its \c Key and will be inherited from \c M,
       
   982   ///its Value is <tt>bool</tt>.
       
   983   template <typename M> 
       
   984   class NotWriteMap : public MapBase<typename M::Key, bool> {
       
   985     M& m;
       
   986   public:
       
   987     typedef MapBase<typename M::Key, bool> Parent;
       
   988     typedef typename Parent::Key Key;
       
   989     typedef typename Parent::Value Value;
       
   990 
       
   991     /// Constructor
       
   992     NotWriteMap(M &_m) : m(_m) {};
       
   993     Value operator[](Key k) const {return !m[k];}
       
   994     void set(Key k, bool v) { m.set(k, !v); }
       
   995   };
   853   
   996   
   854   ///Returns a \ref NotMap class
   997   ///Returns a \ref NotMap class
   855   
   998   
   856   ///This function just returns a \ref NotMap class.
   999   ///This function just returns a \ref NotMap class.
   857   ///\relates NotMap
  1000   ///\relates NotMap
   858   template <typename M> 
  1001   template <typename M> 
   859   inline NotMap<M> notMap(const M &m) {
  1002   inline NotMap<M> notMap(const M &m) {
   860     return NotMap<M>(m);
  1003     return NotMap<M>(m);
   861   }
  1004   }
   862 
  1005 
       
  1006   template <typename M> 
       
  1007   inline NotWriteMap<M> notMap(M &m) {
       
  1008     return NotWriteMap<M>(m);
       
  1009   }
       
  1010 
   863   /// \brief Writable bool map for store each true assigned elements.
  1011   /// \brief Writable bool map for store each true assigned elements.
   864   ///
  1012   ///
   865   /// Writable bool map for store each true assigned elements. It will
  1013   /// Writable bool map for store each true assigned elements. It will
   866   /// copies all the true setted keys to the given iterator.
  1014   /// copies all the true setted keys to the given iterator.
   867   ///
  1015   ///