src/lemon/maps.h
changeset 1422 469b3f628dd1
parent 1402 655d8e78454d
equal deleted inserted replaced
17:c92a112fc50c 18:2dd317f48b01
    15  */
    15  */
    16 
    16 
    17 #ifndef LEMON_MAPS_H
    17 #ifndef LEMON_MAPS_H
    18 #define LEMON_MAPS_H
    18 #define LEMON_MAPS_H
    19 
    19 
       
    20 #include <lemon/graph_utils.h>
       
    21 #include <lemon/utility.h>
       
    22 
    20 
    23 
    21 ///\file
    24 ///\file
    22 ///\ingroup maps
    25 ///\ingroup maps
    23 ///\brief Miscellaneous property maps
    26 ///\brief Miscellaneous property maps
    24 ///
    27 ///
    53   /// data written to it will sent to <tt>/dev/null</tt>...
    56   /// data written to it will sent to <tt>/dev/null</tt>...
    54   template<typename K, typename T>
    57   template<typename K, typename T>
    55   class NullMap : public MapBase<K,T>
    58   class NullMap : public MapBase<K,T>
    56   {
    59   {
    57   public:
    60   public:
       
    61     
       
    62     typedef True NeedCopy;
    58 
    63 
    59     /// Gives back a default constructed element.
    64     /// Gives back a default constructed element.
    60     T operator[](const K&) const { return T(); }
    65     T operator[](const K&) const { return T(); }
    61     /// Absorbs the value.
    66     /// Absorbs the value.
    62     void set(const K&, const T&) {}
    67     void set(const K&, const T&) {}
    63   };
    68   };
       
    69 
       
    70   template <typename K, typename V> 
       
    71   NullMap<K, V> nullMap() {
       
    72     return NullMap<K, V>();
       
    73   }
    64 
    74 
    65 
    75 
    66   /// Constant map.
    76   /// Constant map.
    67 
    77 
    68   /// This is a readable map which assigns a specified value to each key.
    78   /// This is a readable map which assigns a specified value to each key.
    71   template<typename K, typename T>
    81   template<typename K, typename T>
    72   class ConstMap : public MapBase<K,T>
    82   class ConstMap : public MapBase<K,T>
    73   {
    83   {
    74     T v;
    84     T v;
    75   public:
    85   public:
       
    86 
       
    87     typedef True NeedCopy;
    76 
    88 
    77     /// Default constructor
    89     /// Default constructor
    78 
    90 
    79     /// The value of the map will be uninitialized. 
    91     /// The value of the map will be uninitialized. 
    80     /// (More exactly it will be default constructed.)
    92     /// (More exactly it will be default constructed.)
   206   ///  ConstMap<X::Key, X::Value> c_tmp(v);
   218   ///  ConstMap<X::Key, X::Value> c_tmp(v);
   207   ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
   219   ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
   208   ///\endcode
   220   ///\endcode
   209   ///\bug wrong documentation
   221   ///\bug wrong documentation
   210   template<class M, class T> 
   222   template<class M, class T> 
   211   class ConvertMap
   223   class ConvertMap {
   212   {
   224     typename SmartConstReference<M>::Type m;
   213     const M &m;
   225   public:
   214   public:
   226 
       
   227     typedef True NeedCopy;
       
   228 
   215     typedef typename M::Key Key;
   229     typedef typename M::Key Key;
   216     typedef T Value;
   230     typedef T Value;
   217 
   231 
   218     ///Constructor
   232     ///Constructor
   219 
   233 
   248   ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
   262   ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
   249 
   263 
   250   template<class M1,class M2> 
   264   template<class M1,class M2> 
   251   class AddMap
   265   class AddMap
   252   {
   266   {
   253     const M1 &m1;
   267     typename SmartConstReference<M1>::Type m1;
   254     const M2 &m2;
   268     typename SmartConstReference<M2>::Type m2;
   255   public:
   269 
       
   270   public:
       
   271 
       
   272     typedef True NeedCopy;
       
   273 
   256     typedef typename M1::Key Key;
   274     typedef typename M1::Key Key;
   257     typedef typename M1::Value Value;
   275     typedef typename M1::Value Value;
   258 
   276 
   259     ///Constructor
   277     ///Constructor
   260 
   278 
   293   ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
   311   ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
   294   ///\endcode
   312   ///\endcode
   295   template<class M> 
   313   template<class M> 
   296   class ShiftMap
   314   class ShiftMap
   297   {
   315   {
   298     const M &m;
   316     typename SmartConstReference<M>::Type m;
   299     typename M::Value v;
   317     typename M::Value v;
   300   public:
   318   public:
       
   319 
       
   320     typedef True NeedCopy;
   301     typedef typename M::Key Key;
   321     typedef typename M::Key Key;
   302     typedef typename M::Value Value;
   322     typedef typename M::Value Value;
   303 
   323 
   304     ///Constructor
   324     ///Constructor
   305 
   325 
   329   ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
   349   ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
   330 
   350 
   331   template<class M1,class M2> 
   351   template<class M1,class M2> 
   332   class SubMap
   352   class SubMap
   333   {
   353   {
   334     const M1 &m1;
   354     typename SmartConstReference<M1>::Type m1;
   335     const M2 &m2;
   355     typename SmartConstReference<M2>::Type m2;
   336   public:
   356   public:
       
   357 
       
   358     typedef True NeedCopy;
   337     typedef typename M1::Key Key;
   359     typedef typename M1::Key Key;
   338     typedef typename M1::Value Value;
   360     typedef typename M1::Value Value;
   339 
   361 
   340     ///Constructor
   362     ///Constructor
   341 
   363 
   365   ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
   387   ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
   366 
   388 
   367   template<class M1,class M2> 
   389   template<class M1,class M2> 
   368   class MulMap
   390   class MulMap
   369   {
   391   {
   370     const M1 &m1;
   392     typename SmartConstReference<M1>::Type m1;
   371     const M2 &m2;
   393     typename SmartConstReference<M2>::Type m2;
   372   public:
   394   public:
       
   395 
       
   396     typedef True NeedCopy;
   373     typedef typename M1::Key Key;
   397     typedef typename M1::Key Key;
   374     typedef typename M1::Value Value;
   398     typedef typename M1::Value Value;
   375 
   399 
   376     ///Constructor
   400     ///Constructor
   377 
   401 
   407   ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
   431   ///  MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
   408   ///\endcode
   432   ///\endcode
   409   template<class M> 
   433   template<class M> 
   410   class ScaleMap
   434   class ScaleMap
   411   {
   435   {
   412     const M &m;
   436     typename SmartConstReference<M>::Type m;
   413     typename M::Value v;
   437     typename M::Value v;
   414   public:
   438   public:
       
   439 
       
   440     typedef True NeedCopy;
   415     typedef typename M::Key Key;
   441     typedef typename M::Key Key;
   416     typedef typename M::Value Value;
   442     typedef typename M::Value Value;
   417 
   443 
   418     ///Constructor
   444     ///Constructor
   419 
   445 
   443   ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
   469   ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
   444 
   470 
   445   template<class M1,class M2> 
   471   template<class M1,class M2> 
   446   class DivMap
   472   class DivMap
   447   {
   473   {
   448     const M1 &m1;
   474     typename SmartConstReference<M1>::Type m1;
   449     const M2 &m2;
   475     typename SmartConstReference<M2>::Type m2;
   450   public:
   476   public:
       
   477 
       
   478     typedef True NeedCopy;
   451     typedef typename M1::Key Key;
   479     typedef typename M1::Key Key;
   452     typedef typename M1::Value Value;
   480     typedef typename M1::Value Value;
   453 
   481 
   454     ///Constructor
   482     ///Constructor
   455 
   483 
   487   ///\todo Check the requirements.
   515   ///\todo Check the requirements.
   488 
   516 
   489   template<class M1,class M2> 
   517   template<class M1,class M2> 
   490   class ComposeMap
   518   class ComposeMap
   491   {
   519   {
   492     const M1 &m1;
   520     typename SmartConstReference<M1>::Type m1;
   493     const M2 &m2;
   521     typename SmartConstReference<M2>::Type m2;
   494   public:
   522   public:
       
   523 
       
   524     typedef True NeedCopy;
   495     typedef typename M2::Key Key;
   525     typedef typename M2::Key Key;
   496     typedef typename M1::Value Value;
   526     typedef typename M1::Value Value;
       
   527 
       
   528     typedef True NeedCopy;
   497 
   529 
   498     ///Constructor
   530     ///Constructor
   499 
   531 
   500     ///\e
   532     ///\e
   501     ///
   533     ///
   534   ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
   566   ///The \c M2::Value and \c M1::Value must be convertible to the corresponding
   535   ///input parameter of \c F and the return type of \c F must be convertible
   567   ///input parameter of \c F and the return type of \c F must be convertible
   536   ///to \c V.
   568   ///to \c V.
   537   ///\todo Check the requirements.
   569   ///\todo Check the requirements.
   538 
   570 
   539   template<class M1,class M2,class F,class V> 
   571   template<class M1,class M2,class F,class V = typename F::result_type> 
   540   class CombineMap
   572   class CombineMap
   541   {
   573   {
   542     const M1 &m1;
   574     typename SmartConstReference<M1>::Type m1;
   543     const M2 &m2;
   575     typename SmartConstReference<M2>::Type m2;
   544     const F &f;
   576     F f;
   545   public:
   577   public:
       
   578 
       
   579     typedef True NeedCopy;
   546     typedef typename M1::Key Key;
   580     typedef typename M1::Key Key;
   547     typedef V Value;
   581     typedef V Value;
   548 
   582 
   549     ///Constructor
   583     ///Constructor
   550 
   584 
   569   ///\code
   603   ///\code
   570   ///addMap(m1,m2)
   604   ///addMap(m1,m2)
   571   ///\endcode
   605   ///\endcode
   572   ///
   606   ///
   573   ///\relates CombineMap
   607   ///\relates CombineMap
   574   template<class V,class M1,class M2,class F> 
   608   template<class M1,class M2,class F> 
   575   inline CombineMap<M1,M2,F,V> combineMap(const M1 &m1,const M2 &m2,const F &f) 
   609   inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f) 
   576   {
   610   {
   577     return CombineMap<M1,M2,F,V>(m1,m2,f);
   611     return CombineMap<M1,M2,F>(m1,m2,f);
   578   }
   612   }
   579 
   613 
   580   ///Negative value of a map
   614   ///Negative value of a map
   581 
   615 
   582   ///This \ref concept::ReadMap "read only map" returns the negative
   616   ///This \ref concept::ReadMap "read only map" returns the negative
   586   ///The unary \c - operator must be defined for \c Value, of course.
   620   ///The unary \c - operator must be defined for \c Value, of course.
   587 
   621 
   588   template<class M> 
   622   template<class M> 
   589   class NegMap
   623   class NegMap
   590   {
   624   {
   591     const M &m;
   625     typename SmartConstReference<M>::Type m;
   592   public:
   626   public:
       
   627 
       
   628     typedef True NeedCopy;
   593     typedef typename M::Key Key;
   629     typedef typename M::Key Key;
   594     typedef typename M::Value Value;
   630     typedef typename M::Value Value;
   595 
   631 
   596     ///Constructor
   632     ///Constructor
   597 
   633 
   636   
   672   
   637 
   673 
   638   template<class M> 
   674   template<class M> 
   639   class AbsMap
   675   class AbsMap
   640   {
   676   {
   641     const M &m;
   677     typename SmartConstReference<M>::Type m;
   642   public:
   678   public:
       
   679 
       
   680     typedef True NeedCopy;
   643     typedef typename M::Key Key;
   681     typedef typename M::Key Key;
   644     typedef typename M::Value Value;
   682     typedef typename M::Value Value;
   645 
   683 
   646     ///Constructor
   684     ///Constructor
   647 
   685 
   677   template<class K,class V,class F> 
   715   template<class K,class V,class F> 
   678   class FunctorMap
   716   class FunctorMap
   679   {
   717   {
   680     const F &f;
   718     const F &f;
   681   public:
   719   public:
       
   720 
       
   721     typedef True NeedCopy;
   682     typedef K Key;
   722     typedef K Key;
   683     typedef V Value;
   723     typedef V Value;
   684 
   724 
   685     ///Constructor
   725     ///Constructor
   686 
   726 
   712   ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
   752   ///<tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
   713 
   753 
   714   template<class M> 
   754   template<class M> 
   715   class MapFunctor
   755   class MapFunctor
   716   {
   756   {
   717     const M &m;
   757     typename SmartConstReference<M>::Type m;
   718   public:
   758   public:
       
   759 
       
   760     typedef True NeedCopy;
   719     typedef typename M::Key argument_type;
   761     typedef typename M::Key argument_type;
   720     typedef typename M::Value result_type;
   762     typedef typename M::Value result_type;
   721     typedef typename M::Key Key;
   763     typedef typename M::Key Key;
   722     typedef typename M::Value Value;
   764     typedef typename M::Value Value;
   723 
   765 
   759   ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
   801   ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
   760 
   802 
   761   template<class M1,class M2> 
   803   template<class M1,class M2> 
   762   class ForkMap
   804   class ForkMap
   763   {
   805   {
   764     const M1 &m1;
   806     typename SmartConstReference<M1>::Type m1;
   765     const M2 &m2;
   807     typename SmartConstReference<M2>::Type m2;
   766   public:
   808   public:
       
   809 
       
   810     typedef True NeedCopy;
   767     typedef typename M1::Key Key;
   811     typedef typename M1::Key Key;
   768     typedef typename M1::Value Value;
   812     typedef typename M1::Value Value;
   769 
   813 
   770     ///Constructor
   814     ///Constructor
   771 
   815