diff --git a/lemon/maps.h b/lemon/maps.h --- a/lemon/maps.h +++ b/lemon/maps.h @@ -86,8 +86,8 @@ /// Constant map. - /// This is a \ref concepts::ReadMap "readable" map which assigns a - /// specified value to each key. + /// This \ref concepts::ReadMap "readable map" assigns a specified + /// value to each key. /// /// In other aspects it is equivalent to \ref NullMap. /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" @@ -149,8 +149,8 @@ /// Constant map with inlined constant value. - /// This is a \ref concepts::ReadMap "readable" map which assigns a - /// specified value to each key. + /// This \ref concepts::ReadMap "readable map" assigns a specified + /// value to each key. /// /// In other aspects it is equivalent to \ref NullMap. /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" @@ -189,10 +189,10 @@ } - /// \brief Identity map. - /// - /// This map gives back the given key as value without any - /// modification. + /// Identity map. + + /// This \ref concepts::ReadMap "read-only map" gives back the given + /// key as value without any modification. /// /// \sa ConstMap template @@ -203,8 +203,8 @@ typedef typename Parent::Value Value; /// Gives back the given value without any modification. - const T& operator[](const T& t) const { - return t; + Value operator[](const Key &k) const { + return k; } }; @@ -464,7 +464,7 @@ /// Composition of two maps - /// This \ref concepts::ReadMap "read only map" returns the + /// This \ref concepts::ReadMap "read-only map" returns the /// composition of two given maps. That is to say, if \c m1 is of /// type \c M1 and \c m2 is of \c M2, then for /// \code @@ -516,7 +516,7 @@ /// Combination of two maps using an STL (binary) functor. - /// This \ref concepts::ReadMap "read only map" takes two maps and a + /// This \ref concepts::ReadMap "read-only map" takes two maps and a /// binary functor and returns the combination of the two given maps /// using the functor. /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2 @@ -595,7 +595,7 @@ /// Converts an STL style (unary) functor to a map - /// This \ref concepts::ReadMap "read only map" returns the value + /// This \ref concepts::ReadMap "read-only map" returns the value /// of a given functor. Actually, it just wraps the functor and /// provides the \c Key and \c Value typedefs. /// @@ -775,7 +775,7 @@ /// Sum of two maps - /// This \ref concepts::ReadMap "read only map" returns the sum + /// This \ref concepts::ReadMap "read-only map" returns the sum /// of the values of the two given maps. /// Its \c Key and \c Value types are inherited from \c M1. /// The \c Key and \c Value of \c M2 must be convertible to those of @@ -824,7 +824,7 @@ /// Difference of two maps - /// This \ref concepts::ReadMap "read only map" returns the difference + /// This \ref concepts::ReadMap "read-only map" returns the difference /// of the values of the two given maps. /// Its \c Key and \c Value types are inherited from \c M1. /// The \c Key and \c Value of \c M2 must be convertible to those of @@ -872,7 +872,7 @@ /// Product of two maps - /// This \ref concepts::ReadMap "read only map" returns the product + /// This \ref concepts::ReadMap "read-only map" returns the product /// of the values of the two given maps. /// Its \c Key and \c Value types are inherited from \c M1. /// The \c Key and \c Value of \c M2 must be convertible to those of @@ -921,7 +921,7 @@ /// Quotient of two maps - /// This \ref concepts::ReadMap "read only map" returns the quotient + /// This \ref concepts::ReadMap "read-only map" returns the quotient /// of the values of the two given maps. /// Its \c Key and \c Value types are inherited from \c M1. /// The \c Key and \c Value of \c M2 must be convertible to those of @@ -969,7 +969,7 @@ /// Shifts a map with a constant. - /// This \ref concepts::ReadMap "read only map" returns the sum of + /// This \ref concepts::ReadMap "read-only map" returns the sum of /// the given map and a constant value (i.e. it shifts the map with /// the constant). Its \c Key and \c Value are inherited from \c M. /// @@ -1070,7 +1070,7 @@ /// Scales a map with a constant. - /// This \ref concepts::ReadMap "read only map" returns the value of + /// This \ref concepts::ReadMap "read-only map" returns the value of /// the given map multiplied from the left side with a constant value. /// Its \c Key and \c Value are inherited from \c M. /// @@ -1172,7 +1172,7 @@ /// Negative of a map - /// This \ref concepts::ReadMap "read only map" returns the negative + /// This \ref concepts::ReadMap "read-only map" returns the negative /// of the values of the given map (using the unary \c - operator). /// Its \c Key and \c Value are inherited from \c M. /// @@ -1270,7 +1270,7 @@ /// Absolute value of a map - /// This \ref concepts::ReadMap "read only map" returns the absolute + /// This \ref concepts::ReadMap "read-only map" returns the absolute /// value of the values of the given map. /// Its \c Key and \c Value are inherited from \c M. /// \c Value must be comparable to \c 0 and the unary \c - @@ -1311,10 +1311,190 @@ return AbsMap(m); } + /// @} + + // Logical maps and map adaptors: + + /// \addtogroup maps + /// @{ + + /// Constant \c true map. + + /// This \ref concepts::ReadMap "read-only map" assigns \c true to + /// each key. + /// + /// Note that + /// \code + /// TrueMap tm; + /// \endcode + /// is equivalent to + /// \code + /// ConstMap tm(true); + /// \endcode + /// + /// \sa FalseMap + /// \sa ConstMap + template + class TrueMap : public MapBase { + public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + + /// Gives back \c true. + Value operator[](const Key&) const { return true; } + }; + + /// Returns a \ref TrueMap class + + /// This function just returns a \ref TrueMap class. + /// \relates TrueMap + template + inline TrueMap trueMap() { + return TrueMap(); + } + + + /// Constant \c false map. + + /// This \ref concepts::ReadMap "read-only map" assigns \c false to + /// each key. + /// + /// Note that + /// \code + /// FalseMap fm; + /// \endcode + /// is equivalent to + /// \code + /// ConstMap fm(false); + /// \endcode + /// + /// \sa TrueMap + /// \sa ConstMap + template + class FalseMap : public MapBase { + public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + + /// Gives back \c false. + Value operator[](const Key&) const { return false; } + }; + + /// Returns a \ref FalseMap class + + /// This function just returns a \ref FalseMap class. + /// \relates FalseMap + template + inline FalseMap falseMap() { + return FalseMap(); + } + + /// @} + + /// \addtogroup map_adaptors + /// @{ + + /// Logical 'and' of two maps + + /// This \ref concepts::ReadMap "read-only map" returns the logical + /// 'and' of the values of the two given maps. + /// Its \c Key type is inherited from \c M1 and its \c Value type is + /// \c bool. \c M2::Key must be convertible to \c M1::Key. + /// + /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for + /// \code + /// AndMap am(m1,m2); + /// \endcode + /// am[x] will be equal to m1[x]&&m2[x]. + /// + /// The simplest way of using this map is through the andMap() + /// function. + /// + /// \sa OrMap + /// \sa NotMap, NotWriteMap + template + class AndMap : public MapBase { + const M1 &_m1; + const M2 &_m2; + public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + + /// Constructor + AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} + /// \e + Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; } + }; + + /// Returns an \ref AndMap class + + /// This function just returns an \ref AndMap class. + /// + /// For example, if \c m1 and \c m2 are both maps with \c bool values, + /// then andMap(m1,m2)[x] will be equal to + /// m1[x]&&m2[x]. + /// + /// \relates AndMap + template + inline AndMap andMap(const M1 &m1, const M2 &m2) { + return AndMap(m1,m2); + } + + + /// Logical 'or' of two maps + + /// This \ref concepts::ReadMap "read-only map" returns the logical + /// 'or' of the values of the two given maps. + /// Its \c Key type is inherited from \c M1 and its \c Value type is + /// \c bool. \c M2::Key must be convertible to \c M1::Key. + /// + /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for + /// \code + /// OrMap om(m1,m2); + /// \endcode + /// om[x] will be equal to m1[x]||m2[x]. + /// + /// The simplest way of using this map is through the orMap() + /// function. + /// + /// \sa AndMap + /// \sa NotMap, NotWriteMap + template + class OrMap : public MapBase { + const M1 &_m1; + const M2 &_m2; + public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + + /// Constructor + OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} + /// \e + Value operator[](const Key &k) const { return _m1[k]||_m2[k]; } + }; + + /// Returns an \ref OrMap class + + /// This function just returns an \ref OrMap class. + /// + /// For example, if \c m1 and \c m2 are both maps with \c bool values, + /// then orMap(m1,m2)[x] will be equal to + /// m1[x]||m2[x]. + /// + /// \relates OrMap + template + inline OrMap orMap(const M1 &m1, const M2 &m2) { + return OrMap(m1,m2); + } + /// Logical 'not' of a map - /// This \ref concepts::ReadMap "read only map" returns the logical + /// This \ref concepts::ReadMap "read-only map" returns the logical /// negation of the values of the given map. /// Its \c Key is inherited from \c M and its \c Value is \c bool. /// @@ -1391,6 +1571,102 @@ return NotWriteMap(m); } + + /// Combination of two maps using the \c == operator + + /// This \ref concepts::ReadMap "read-only map" assigns \c true to + /// the keys for which the corresponding values of the two maps are + /// equal. + /// Its \c Key type is inherited from \c M1 and its \c Value type is + /// \c bool. \c M2::Key must be convertible to \c M1::Key. + /// + /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for + /// \code + /// EqualMap em(m1,m2); + /// \endcode + /// em[x] will be equal to m1[x]==m2[x]. + /// + /// The simplest way of using this map is through the equalMap() + /// function. + /// + /// \sa LessMap + template + class EqualMap : public MapBase { + const M1 &_m1; + const M2 &_m2; + public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + + /// Constructor + EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} + /// \e + Value operator[](const Key &k) const { return _m1[k]==_m2[k]; } + }; + + /// Returns an \ref EqualMap class + + /// This function just returns an \ref EqualMap class. + /// + /// For example, if \c m1 and \c m2 are maps with keys and values of + /// the same type, then equalMap(m1,m2)[x] will be equal to + /// m1[x]==m2[x]. + /// + /// \relates EqualMap + template + inline EqualMap equalMap(const M1 &m1, const M2 &m2) { + return EqualMap(m1,m2); + } + + + /// Combination of two maps using the \c < operator + + /// This \ref concepts::ReadMap "read-only map" assigns \c true to + /// the keys for which the corresponding value of the first map is + /// less then the value of the second map. + /// Its \c Key type is inherited from \c M1 and its \c Value type is + /// \c bool. \c M2::Key must be convertible to \c M1::Key. + /// + /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for + /// \code + /// LessMap lm(m1,m2); + /// \endcode + /// lm[x] will be equal to m1[x]. + /// + /// The simplest way of using this map is through the lessMap() + /// function. + /// + /// \sa EqualMap + template + class LessMap : public MapBase { + const M1 &_m1; + const M2 &_m2; + public: + typedef MapBase Parent; + typedef typename Parent::Key Key; + typedef typename Parent::Value Value; + + /// Constructor + LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} + /// \e + Value operator[](const Key &k) const { return _m1[k]<_m2[k]; } + }; + + /// Returns an \ref LessMap class + + /// This function just returns an \ref LessMap class. + /// + /// For example, if \c m1 and \c m2 are maps with keys and values of + /// the same type, then lessMap(m1,m2)[x] will be equal to + /// m1[x]. + /// + /// \relates LessMap + template + inline LessMap lessMap(const M1 &m1, const M2 &m2) { + return LessMap(m1,m2); + } + /// @} }