lemon/maps.h
changeset 48 93ae269876de
parent 46 b6dd98b57d71
child 51 90201bb15a8d
equal deleted inserted replaced
11:741aa1b3732b 12:fe80377d8de6
    79   }
    79   }
    80 
    80 
    81 
    81 
    82   /// Constant map.
    82   /// Constant map.
    83 
    83 
    84   /// This is a readable map which assigns a specified value to each key.
    84   /// This is a \ref concepts::ReadMap "readable" map which assigns a 
    85   /// In other aspects it is equivalent to the \c NullMap.
    85   /// specified value to each key.
       
    86   /// In other aspects it is equivalent to \c NullMap.
    86   template<typename K, typename T>
    87   template<typename K, typename T>
    87   class ConstMap : public MapBase<K, T> {
    88   class ConstMap : public MapBase<K, T> {
    88   private:
    89   private:
    89     T v;
    90     T v;
    90   public:
    91   public:
   131   template<typename T, T v>
   132   template<typename T, T v>
   132   struct Const { };
   133   struct Const { };
   133 
   134 
   134   /// Constant map with inlined constant value.
   135   /// Constant map with inlined constant value.
   135 
   136 
   136   /// This is a readable map which assigns a specified value to each key.
   137   /// This is a \ref concepts::ReadMap "readable" map which assigns a 
   137   /// In other aspects it is equivalent to the \c NullMap.
   138   /// specified value to each key.
       
   139   /// In other aspects it is equivalent to \c NullMap.
   138   template<typename K, typename V, V v>
   140   template<typename K, typename V, V v>
   139   class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
   141   class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
   140   public:
   142   public:
   141     typedef MapBase<K, V> Parent;
   143     typedef MapBase<K, V> Parent;
   142     typedef typename Parent::Key Key;
   144     typedef typename Parent::Key Key;
   147     V operator[](const K&) const { return v; }
   149     V operator[](const K&) const { return v; }
   148     ///\e
   150     ///\e
   149     void set(const K&, const V&) { }
   151     void set(const K&, const V&) { }
   150   };
   152   };
   151 
   153 
   152   ///Returns a \c ConstMap class
   154   ///Returns a \c ConstMap class with inlined value
   153 
   155 
   154   ///This function just returns a \c ConstMap class with inlined value.
   156   ///This function just returns a \c ConstMap class with inlined value.
   155   ///\relates ConstMap
   157   ///\relates ConstMap
   156   template<typename K, typename V, V v> 
   158   template<typename K, typename V, V v> 
   157   inline ConstMap<K, Const<V, v> > constMap() {
   159   inline ConstMap<K, Const<V, v> > constMap() {
   160 
   162 
   161   ///Map based on \c std::map
   163   ///Map based on \c std::map
   162 
   164 
   163   ///This is essentially a wrapper for \c std::map with addition that
   165   ///This is essentially a wrapper for \c std::map with addition that
   164   ///you can specify a default value different from \c Value().
   166   ///you can specify a default value different from \c Value().
       
   167   ///It meets the \ref concepts::ReferenceMap "ReferenceMap" concept.
   165   template <typename K, typename T, typename Compare = std::less<K> >
   168   template <typename K, typename T, typename Compare = std::less<K> >
   166   class StdMap : public MapBase<K, T> {
   169   class StdMap : public MapBase<K, T> {
   167     template <typename K1, typename T1, typename C1>
   170     template <typename K1, typename T1, typename C1>
   168     friend class StdMap;
   171     friend class StdMap;
   169   public:
   172   public:
   188 
   191 
   189   public:
   192   public:
   190 
   193 
   191     /// Constructor with specified default value
   194     /// Constructor with specified default value
   192     StdMap(const T& value = T()) : _value(value) {}
   195     StdMap(const T& value = T()) : _value(value) {}
   193     /// \brief Constructs the map from an appropriate std::map, and explicitly
   196     /// \brief Constructs the map from an appropriate \c std::map, and 
   194     /// specifies a default value.
   197     /// explicitly specifies a default value.
   195     template <typename T1, typename Comp1>
   198     template <typename T1, typename Comp1>
   196     StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) 
   199     StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) 
   197       : _map(map.begin(), map.end()), _value(value) {}
   200       : _map(map.begin(), map.end()), _value(value) {}
   198     
   201     
   199     /// \brief Constructs a map from an other StdMap.
   202     /// \brief Constructs a map from an other \ref StdMap.
   200     template<typename T1, typename Comp1>
   203     template<typename T1, typename Comp1>
   201     StdMap(const StdMap<Key, T1, Comp1> &c) 
   204     StdMap(const StdMap<Key, T1, Comp1> &c) 
   202       : _map(c._map.begin(), c._map.end()), _value(c._value) {}
   205       : _map(c._map.begin(), c._map.end()), _value(c._value) {}
   203 
   206 
   204   private:
   207   private:
   263     return StdMap<K, V, Compare>(map, value);
   266     return StdMap<K, V, Compare>(map, value);
   264   }
   267   }
   265 
   268 
   266   /// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt>
   269   /// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt>
   267   ///
   270   ///
   268   /// The current map has the <tt>[0..size-1]</tt> keyset and the values
   271   /// This map has the <tt>[0..size-1]</tt> keyset and the values
   269   /// are stored in a \c std::vector<T>  container. It can be used with
   272   /// are stored in a \c std::vector<T>  container. It can be used with
   270   /// some data structures, for example \c UnionFind, \c BinHeap, when 
   273   /// some data structures, for example \c UnionFind, \c BinHeap, when 
   271   /// the used items are small integer numbers. 
   274   /// the used items are small integer numbers.
       
   275   /// This map meets the \ref concepts::ReferenceMap "ReferenceMap" concept.
   272   ///
   276   ///
   273   /// \todo Revise its name
   277   /// \todo Revise its name
   274   template <typename T>
   278   template <typename T>
   275   class IntegerMap : public MapBase<int, T> {
   279   class IntegerMap : public MapBase<int, T> {
   276 
   280 
   299   public:
   303   public:
   300 
   304 
   301     /// Constructor with specified default value
   305     /// Constructor with specified default value
   302     IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
   306     IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
   303 
   307 
   304     /// \brief Constructs the map from an appropriate std::vector.
   308     /// \brief Constructs the map from an appropriate \c std::vector.
   305     template <typename T1>
   309     template <typename T1>
   306     IntegerMap(const std::vector<T1>& vector) 
   310     IntegerMap(const std::vector<T1>& vector) 
   307       : _vector(vector.begin(), vector.end()) {}
   311       : _vector(vector.begin(), vector.end()) {}
   308     
   312     
   309     /// \brief Constructs a map from an other IntegerMap.
   313     /// \brief Constructs a map from an other \ref IntegerMap.
   310     template <typename T1>
   314     template <typename T1>
   311     IntegerMap(const IntegerMap<T1> &c) 
   315     IntegerMap(const IntegerMap<T1> &c) 
   312       : _vector(c._vector.begin(), c._vector.end()) {}
   316       : _vector(c._vector.begin(), c._vector.end()) {}
   313 
   317 
   314     /// \brief Resize the container
   318     /// \brief Resize the container
   398 
   402 
   399     ///Constructor.
   403     ///Constructor.
   400     ///\param _m is the underlying map.
   404     ///\param _m is the underlying map.
   401     ConvertMap(const M &_m) : m(_m) {};
   405     ConvertMap(const M &_m) : m(_m) {};
   402 
   406 
   403     /// \brief The subscript operator.
   407     ///\e
   404     ///
       
   405     /// The subscript operator.
       
   406     Value operator[](const Key& k) const {return m[k];}
   408     Value operator[](const Key& k) const {return m[k];}
   407   };
   409   };
   408   
   410   
   409   ///Returns a \c ConvertMap class
   411   ///Returns a \c ConvertMap class
   410 
   412 
   488   ///Sum of two maps
   490   ///Sum of two maps
   489 
   491 
   490   ///This \ref concepts::ReadMap "read only map" returns the sum of the two
   492   ///This \ref concepts::ReadMap "read only map" returns the sum of the two
   491   ///given maps.
   493   ///given maps.
   492   ///Its \c Key and \c Value are inherited from \c M1.
   494   ///Its \c Key and \c Value are inherited from \c M1.
   493   ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
   495   ///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
   494   template<typename M1, typename M2> 
   496   template<typename M1, typename M2> 
   495   class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
   497   class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
   496     const M1& m1;
   498     const M1& m1;
   497     const M2& m2;
   499     const M2& m2;
   498 
   500 
   508   };
   510   };
   509   
   511   
   510   ///Returns an \c AddMap class
   512   ///Returns an \c AddMap class
   511 
   513 
   512   ///This function just returns an \c AddMap class.
   514   ///This function just returns an \c AddMap class.
   513   ///\todo How to call these type of functions?
   515   ///\todo Extend the documentation: how to call these type of functions?
   514   ///
   516   ///
   515   ///\relates AddMap
   517   ///\relates AddMap
   516   template<typename M1, typename M2> 
   518   template<typename M1, typename M2> 
   517   inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
   519   inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
   518     return AddMap<M1, M2>(m1,m2);
   520     return AddMap<M1, M2>(m1,m2);
  1005   ///of a given functor.
  1007   ///of a given functor.
  1006   ///
  1008   ///
  1007   ///Template parameters \c K and \c V will become its
  1009   ///Template parameters \c K and \c V will become its
  1008   ///\c Key and \c Value. 
  1010   ///\c Key and \c Value. 
  1009   ///In most cases they have to be given explicitly because a 
  1011   ///In most cases they have to be given explicitly because a 
  1010   ///functor typically does not provide such typedefs.
  1012   ///functor typically does not provide \c argument_type and 
       
  1013   ///\c result_type typedefs.
  1011   ///
  1014   ///
  1012   ///Parameter \c F is the type of the used functor.
  1015   ///Parameter \c F is the type of the used functor.
  1013   ///
  1016   ///
  1014   ///\sa MapFunctor
  1017   ///\sa MapFunctor
  1015   template<typename F, 
  1018   template<typename F, 
  1030   
  1033   
  1031   ///Returns a \c FunctorMap class
  1034   ///Returns a \c FunctorMap class
  1032 
  1035 
  1033   ///This function just returns a \c FunctorMap class.
  1036   ///This function just returns a \c FunctorMap class.
  1034   ///
  1037   ///
  1035   ///It is specialized for adaptable function classes and
  1038   ///This function is specialized for adaptable binary function
  1036   ///C++ functions.
  1039   ///classes and C++ functions.
       
  1040   ///
  1037   ///\relates FunctorMap
  1041   ///\relates FunctorMap
  1038   template<typename K, typename V, typename F> inline 
  1042   template<typename K, typename V, typename F> inline 
  1039   FunctorMap<F, K, V> functorMap(const F &f) {
  1043   FunctorMap<F, K, V> functorMap(const F &f) {
  1040     return FunctorMap<F, K, V>(f);
  1044     return FunctorMap<F, K, V>(f);
  1041   }
  1045   }
  1054 
  1058 
  1055 
  1059 
  1056   ///Converts a map to an STL style (unary) functor
  1060   ///Converts a map to an STL style (unary) functor
  1057 
  1061 
  1058   ///This class Converts a map to an STL style (unary) functor.
  1062   ///This class Converts a map to an STL style (unary) functor.
  1059   ///that is it provides an <tt>operator()</tt> to read its values.
  1063   ///That is it provides an <tt>operator()</tt> to read its values.
  1060   ///
  1064   ///
  1061   ///For the sake of convenience it also works as
  1065   ///For the sake of convenience it also works as
  1062   ///a ususal \ref concepts::ReadMap "readable map",
  1066   ///a ususal \ref concepts::ReadMap "readable map",
  1063   ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
  1067   ///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
  1064   ///
  1068   ///
  1089   template<typename M> 
  1093   template<typename M> 
  1090   inline MapFunctor<M> mapFunctor(const M &m) {
  1094   inline MapFunctor<M> mapFunctor(const M &m) {
  1091     return MapFunctor<M>(m);
  1095     return MapFunctor<M>(m);
  1092   }
  1096   }
  1093 
  1097 
  1094   ///Applies all map setting operations to two maps
  1098   ///Just readable version of \ref ForkWriteMap
  1095 
  1099 
  1096   ///This map has two \ref concepts::ReadMap "readable map"
  1100   ///This map has two \ref concepts::ReadMap "readable map"
  1097   ///parameters and each read request will be passed just to the
  1101   ///parameters and each read request will be passed just to the
  1098   ///first map. This class is the just readable map type of the \c ForkWriteMap.
  1102   ///first map. This class is the just readable map type of \c ForkWriteMap.
  1099   ///
  1103   ///
  1100   ///The \c Key and \c Value are inherited from \c M1.
  1104   ///The \c Key and \c Value are inherited from \c M1.
  1101   ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
  1105   ///The \c Key and \c Value of \c M2 must be convertible from those of \c M1.
  1102   ///
  1106   ///
  1103   ///\sa ForkWriteMap
  1107   ///\sa ForkWriteMap
  1104   ///
  1108   ///
  1105   /// \todo Why is it needed?
  1109   /// \todo Why is it needed?
  1106   template<typename  M1, typename M2> 
  1110   template<typename  M1, typename M2> 
  1126   ///If \c M1 is also \ref concepts::ReadMap "readable",
  1130   ///If \c M1 is also \ref concepts::ReadMap "readable",
  1127   ///then the read operations will return the
  1131   ///then the read operations will return the
  1128   ///corresponding values of \c M1.
  1132   ///corresponding values of \c M1.
  1129   ///
  1133   ///
  1130   ///The \c Key and \c Value are inherited from \c M1.
  1134   ///The \c Key and \c Value are inherited from \c M1.
  1131   ///The \c Key and \c Value of M2 must be convertible from those of \c M1.
  1135   ///The \c Key and \c Value of \c M2 must be convertible from those of \c M1.
  1132   ///
  1136   ///
  1133   ///\sa ForkMap
  1137   ///\sa ForkMap
  1134   template<typename  M1, typename M2> 
  1138   template<typename  M1, typename M2> 
  1135   class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
  1139   class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
  1136     M1& m1;
  1140     M1& m1;
  1172   
  1176   
  1173   ///Logical 'not' of a map
  1177   ///Logical 'not' of a map
  1174   
  1178   
  1175   ///This bool \ref concepts::ReadMap "read only map" returns the 
  1179   ///This bool \ref concepts::ReadMap "read only map" returns the 
  1176   ///logical negation of the value returned by the given map.
  1180   ///logical negation of the value returned by the given map.
  1177   ///Its \c Key is inherited from \c M, its Value is \c bool.
  1181   ///Its \c Key is inherited from \c M, its \c Value is \c bool.
  1178   ///
  1182   ///
  1179   ///\sa NotWriteMap
  1183   ///\sa NotWriteMap
  1180   template <typename M> 
  1184   template <typename M> 
  1181   class NotMap : public MapBase<typename M::Key, bool> {
  1185   class NotMap : public MapBase<typename M::Key, bool> {
  1182     const M& m;
  1186     const M& m;
  1194   ///Logical 'not' of a map (ReadWrie version)
  1198   ///Logical 'not' of a map (ReadWrie version)
  1195   
  1199   
  1196   ///This bool \ref concepts::ReadWriteMap "read-write map" returns the 
  1200   ///This bool \ref concepts::ReadWriteMap "read-write map" returns the 
  1197   ///logical negation of the value returned by the given map. When it is set,
  1201   ///logical negation of the value returned by the given map. When it is set,
  1198   ///the opposite value is set to the original map.
  1202   ///the opposite value is set to the original map.
  1199   ///Its \c Key is inherited from \c M, its Value is \c bool.
  1203   ///Its \c Key is inherited from \c M, its \c Value is \c bool.
  1200   ///
  1204   ///
  1201   ///\sa NotMap
  1205   ///\sa NotMap
  1202   template <typename M> 
  1206   template <typename M> 
  1203   class NotWriteMap : public MapBase<typename M::Key, bool> {
  1207   class NotWriteMap : public MapBase<typename M::Key, bool> {
  1204     M& m;
  1208     M& m;
  1260   
  1264   
  1261 
  1265 
  1262   /// \brief Writable bool map for logging each \c true assigned element
  1266   /// \brief Writable bool map for logging each \c true assigned element
  1263   ///
  1267   ///
  1264   /// A \ref concepts::ReadWriteMap "read-write" bool map for logging 
  1268   /// A \ref concepts::ReadWriteMap "read-write" bool map for logging 
  1265   /// each \c true assigned element, i.e it/ copies all the keys set 
  1269   /// each \c true assigned element, i.e it copies all the keys set 
  1266   /// to \c true to the given iterator.
  1270   /// to \c true to the given iterator.
  1267   ///
  1271   ///
  1268   /// \note The container of the iterator should contain space 
  1272   /// \note The container of the iterator should contain space 
  1269   /// for each element.
  1273   /// for each element.
  1270   ///
  1274   ///
  1271   /// The following example shows how you can write the edges found by the Prim
  1275   /// The following example shows how you can write the edges found by 
  1272   /// algorithm directly
  1276   /// the \ref Prim algorithm directly to the standard output.
  1273   /// to the standard output.
       
  1274   ///\code
  1277   ///\code
  1275   /// typedef IdMap<Graph, Edge> EdgeIdMap;
  1278   /// typedef IdMap<Graph, Edge> EdgeIdMap;
  1276   /// EdgeIdMap edgeId(graph);
  1279   /// EdgeIdMap edgeId(graph);
  1277   ///
  1280   ///
  1278   /// typedef MapFunctor<EdgeIdMap> EdgeIdFunctor;
  1281   /// typedef MapFunctor<EdgeIdMap> EdgeIdFunctor;