diff -r c35afa9e89e7 -r ef88c0a30f85 lemon/maps.h --- a/lemon/maps.h Mon Jan 12 23:11:39 2009 +0100 +++ b/lemon/maps.h Thu Nov 05 15:48:01 2009 +0100 @@ -22,6 +22,7 @@ #include #include #include +#include #include @@ -29,8 +30,6 @@ ///\ingroup maps ///\brief Miscellaneous property maps -#include - namespace lemon { /// \addtogroup maps @@ -57,15 +56,16 @@ /// its type definitions, or if you have to provide a writable map, /// but data written to it is not required (i.e. it will be sent to /// /dev/null). - /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept. + /// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. /// /// \sa ConstMap template class NullMap : public MapBase { public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef K Key; + ///\e + typedef V Value; /// Gives back a default constructed element. Value operator[](const Key&) const { return Value(); } @@ -89,7 +89,7 @@ /// value to each key. /// /// In other aspects it is equivalent to \c NullMap. - /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" + /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" /// concept, but it absorbs the data written to it. /// /// The simplest way of using this map is through the constMap() @@ -102,9 +102,10 @@ private: V _value; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef K Key; + ///\e + typedef V Value; /// Default constructor @@ -157,7 +158,7 @@ /// value to each key. /// /// In other aspects it is equivalent to \c NullMap. - /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" + /// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" /// concept, but it absorbs the data written to it. /// /// The simplest way of using this map is through the constMap() @@ -168,9 +169,10 @@ template class ConstMap > : public MapBase { public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef K Key; + ///\e + typedef V Value; /// Constructor. ConstMap() {} @@ -202,9 +204,10 @@ template class IdentityMap : public MapBase { public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef T Key; + ///\e + typedef T Value; /// Gives back the given value without any modification. Value operator[](const Key &k) const { @@ -229,7 +232,7 @@ /// values to integer keys from the range [0..size-1]. /// It can be used with some data structures, for example /// \c UnionFind, \c BinHeap, when the used items are small - /// integers. This map conforms the \ref concepts::ReferenceMap + /// integers. This map conforms to the \ref concepts::ReferenceMap /// "ReferenceMap" concept. /// /// The simplest way of using this map is through the rangeMap() @@ -245,11 +248,10 @@ public: - typedef MapBase Parent; /// Key type - typedef typename Parent::Key Key; + typedef int Key; /// Value type - typedef typename Parent::Value Value; + typedef V Value; /// Reference type typedef typename Vector::reference Reference; /// Const reference type @@ -338,7 +340,7 @@ /// that you can specify a default value for the keys that are not /// stored actually. This value can be different from the default /// contructed value (i.e. \c %Value()). - /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap" + /// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap" /// concept. /// /// This map is useful if a default value should be assigned to most of @@ -353,17 +355,16 @@ /// /// The simplest way of using this map is through the sparseMap() /// function. - template > + template > class SparseMap : public MapBase { template friend class SparseMap; public: - typedef MapBase Parent; /// Key type - typedef typename Parent::Key Key; + typedef K Key; /// Value type - typedef typename Parent::Value Value; + typedef V Value; /// Reference type typedef Value& Reference; /// Const reference type @@ -373,7 +374,7 @@ private: - typedef std::map Map; + typedef std::map Map; Map _map; Value _value; @@ -489,14 +490,15 @@ const M1 &_m1; const M2 &_m2; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M2::Key Key; + ///\e + typedef typename M1::Value Value; /// Constructor ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} - /// \e + ///\e typename MapTraits::ConstReturnValue operator[](const Key &k) const { return _m1[_m2[k]]; } }; @@ -545,14 +547,15 @@ const M2 &_m2; F _f; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M1::Key Key; + ///\e + typedef V Value; /// Constructor CombineMap(const M1 &m1, const M2 &m2, const F &f = F()) : _m1(m1), _m2(m2), _f(f) {} - /// \e + ///\e Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); } }; @@ -615,13 +618,14 @@ class FunctorToMap : public MapBase { F _f; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef K Key; + ///\e + typedef V Value; /// Constructor FunctorToMap(const F &f = F()) : _f(f) {} - /// \e + ///\e Value operator[](const Key &k) const { return _f(k); } }; @@ -669,18 +673,19 @@ class MapToFunctor : public MapBase { const M &_m; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; - - typedef typename Parent::Key argument_type; - typedef typename Parent::Value result_type; + ///\e + typedef typename M::Key Key; + ///\e + typedef typename M::Value Value; + + typedef typename M::Key argument_type; + typedef typename M::Value result_type; /// Constructor MapToFunctor(const M &m) : _m(m) {} - /// \e + ///\e Value operator()(const Key &k) const { return _m[k]; } - /// \e + ///\e Value operator[](const Key &k) const { return _m[k]; } }; @@ -701,7 +706,7 @@ /// "readable map" to another type using the default conversion. /// The \c Key type of it is inherited from \c M and the \c Value /// type is \c V. - /// This type conforms the \ref concepts::ReadMap "ReadMap" concept. + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept. /// /// The simplest way of using this map is through the convertMap() /// function. @@ -709,9 +714,10 @@ class ConvertMap : public MapBase { const M &_m; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M::Key Key; + ///\e + typedef V Value; /// Constructor @@ -719,7 +725,7 @@ /// \param m The underlying map. ConvertMap(const M &m) : _m(m) {} - /// \e + ///\e Value operator[](const Key &k) const { return _m[k]; } }; @@ -751,9 +757,10 @@ M1 &_m1; M2 &_m2; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M1::Key Key; + ///\e + typedef typename M1::Value Value; /// Constructor ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {} @@ -797,13 +804,14 @@ const M1 &_m1; const M2 &_m2; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M1::Key Key; + ///\e + typedef typename M1::Value Value; /// Constructor AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} - /// \e + ///\e Value operator[](const Key &k) const { return _m1[k]+_m2[k]; } }; @@ -845,13 +853,14 @@ const M1 &_m1; const M2 &_m2; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M1::Key Key; + ///\e + typedef typename M1::Value Value; /// Constructor SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} - /// \e + ///\e Value operator[](const Key &k) const { return _m1[k]-_m2[k]; } }; @@ -894,13 +903,14 @@ const M1 &_m1; const M2 &_m2; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M1::Key Key; + ///\e + typedef typename M1::Value Value; /// Constructor MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {} - /// \e + ///\e Value operator[](const Key &k) const { return _m1[k]*_m2[k]; } }; @@ -942,13 +952,14 @@ const M1 &_m1; const M2 &_m2; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M1::Key Key; + ///\e + typedef typename M1::Value Value; /// Constructor DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {} - /// \e + ///\e Value operator[](const Key &k) const { return _m1[k]/_m2[k]; } }; @@ -992,9 +1003,10 @@ const M &_m; C _v; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M::Key Key; + ///\e + typedef typename M::Value Value; /// Constructor @@ -1002,7 +1014,7 @@ /// \param m The undelying map. /// \param v The constant value. ShiftMap(const M &m, const C &v) : _m(m), _v(v) {} - /// \e + ///\e Value operator[](const Key &k) const { return _m[k]+_v; } }; @@ -1022,9 +1034,10 @@ M &_m; C _v; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M::Key Key; + ///\e + typedef typename M::Value Value; /// Constructor @@ -1032,9 +1045,9 @@ /// \param m The undelying map. /// \param v The constant value. ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {} - /// \e + ///\e Value operator[](const Key &k) const { return _m[k]+_v; } - /// \e + ///\e void set(const Key &k, const Value &v) { _m.set(k, v-_v); } }; @@ -1093,9 +1106,10 @@ const M &_m; C _v; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M::Key Key; + ///\e + typedef typename M::Value Value; /// Constructor @@ -1103,7 +1117,7 @@ /// \param m The undelying map. /// \param v The constant value. ScaleMap(const M &m, const C &v) : _m(m), _v(v) {} - /// \e + ///\e Value operator[](const Key &k) const { return _v*_m[k]; } }; @@ -1124,9 +1138,10 @@ M &_m; C _v; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M::Key Key; + ///\e + typedef typename M::Value Value; /// Constructor @@ -1134,9 +1149,9 @@ /// \param m The undelying map. /// \param v The constant value. ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {} - /// \e + ///\e Value operator[](const Key &k) const { return _v*_m[k]; } - /// \e + ///\e void set(const Key &k, const Value &v) { _m.set(k, v/_v); } }; @@ -1193,13 +1208,14 @@ class NegMap : public MapBase { const M& _m; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M::Key Key; + ///\e + typedef typename M::Value Value; /// Constructor NegMap(const M &m) : _m(m) {} - /// \e + ///\e Value operator[](const Key &k) const { return -_m[k]; } }; @@ -1228,15 +1244,16 @@ class NegWriteMap : public MapBase { M &_m; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M::Key Key; + ///\e + typedef typename M::Value Value; /// Constructor NegWriteMap(M &m) : _m(m) {} - /// \e + ///\e Value operator[](const Key &k) const { return -_m[k]; } - /// \e + ///\e void set(const Key &k, const Value &v) { _m.set(k, -v); } }; @@ -1282,13 +1299,14 @@ class AbsMap : public MapBase { const M &_m; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M::Key Key; + ///\e + typedef typename M::Value Value; /// Constructor AbsMap(const M &m) : _m(m) {} - /// \e + ///\e Value operator[](const Key &k) const { Value tmp = _m[k]; return tmp >= 0 ? tmp : -tmp; @@ -1337,9 +1355,10 @@ template class TrueMap : public MapBase { public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef K Key; + ///\e + typedef bool Value; /// Gives back \c true. Value operator[](const Key&) const { return true; } @@ -1374,9 +1393,10 @@ template class FalseMap : public MapBase { public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef K Key; + ///\e + typedef bool Value; /// Gives back \c false. Value operator[](const Key&) const { return false; } @@ -1419,13 +1439,14 @@ const M1 &_m1; const M2 &_m2; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M1::Key Key; + ///\e + typedef bool Value; /// Constructor AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} - /// \e + ///\e Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; } }; @@ -1467,13 +1488,14 @@ const M1 &_m1; const M2 &_m2; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M1::Key Key; + ///\e + typedef bool Value; /// Constructor OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} - /// \e + ///\e Value operator[](const Key &k) const { return _m1[k]||_m2[k]; } }; @@ -1506,13 +1528,14 @@ class NotMap : public MapBase { const M &_m; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M::Key Key; + ///\e + typedef bool Value; /// Constructor NotMap(const M &m) : _m(m) {} - /// \e + ///\e Value operator[](const Key &k) const { return !_m[k]; } }; @@ -1532,15 +1555,16 @@ class NotWriteMap : public MapBase { M &_m; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M::Key Key; + ///\e + typedef bool Value; /// Constructor NotWriteMap(M &m) : _m(m) {} - /// \e + ///\e Value operator[](const Key &k) const { return !_m[k]; } - /// \e + ///\e void set(const Key &k, bool v) { _m.set(k, !v); } }; @@ -1595,13 +1619,14 @@ const M1 &_m1; const M2 &_m2; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M1::Key Key; + ///\e + typedef bool Value; /// Constructor EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} - /// \e + ///\e Value operator[](const Key &k) const { return _m1[k]==_m2[k]; } }; @@ -1643,13 +1668,14 @@ const M1 &_m1; const M2 &_m2; public: - typedef MapBase Parent; - typedef typename Parent::Key Key; - typedef typename Parent::Value Value; + ///\e + typedef typename M1::Key Key; + ///\e + typedef bool Value; /// Constructor LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} - /// \e + ///\e Value operator[](const Key &k) const { return _m1[k]<_m2[k]; } }; @@ -1705,24 +1731,27 @@ /// The simplest way of using this map is through the loggerBoolMap() /// function. /// - /// \tparam It The type of the iterator. - /// \tparam Ke The key type of the map. The default value set + /// \tparam IT The type of the iterator. + /// \tparam KEY The key type of the map. The default value set /// according to the iterator type should work in most cases. /// /// \note The container of the iterator must contain enough space /// for the elements or the iterator should be an inserter iterator. #ifdef DOXYGEN - template + template #else - template ::Value> + template ::Value> #endif - class LoggerBoolMap { + class LoggerBoolMap : public MapBase { public: - typedef It Iterator; - - typedef Ke Key; + + ///\e + typedef KEY Key; + ///\e typedef bool Value; + ///\e + typedef IT Iterator; /// Constructor LoggerBoolMap(Iterator it) @@ -1760,11 +1789,11 @@ /// order of Dfs algorithm, as the following examples show. /// \code /// std::vector v; - /// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run(); + /// dfs(g).processedMap(loggerBoolMap(std::back_inserter(v))).run(s); /// \endcode /// \code /// std::vector v(countNodes(g)); - /// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run(); + /// dfs(g).processedMap(loggerBoolMap(v.begin())).run(s); /// \endcode /// /// \note The container of the iterator must contain enough space @@ -1785,23 +1814,36 @@ /// \addtogroup graph_maps /// @{ - /// Provides an immutable and unique id for each item in the graph. - - /// The IdMap class provides a unique and immutable id for each item of the - /// same type (e.g. node) in the graph. This id is
  • \b unique: - /// different items (nodes) get different ids
  • \b immutable: the id of an - /// item (node) does not change (even if you delete other nodes).
- /// Through this map you get access (i.e. can read) the inner id values of - /// the items stored in the graph. This map can be inverted with its member - /// class \c InverseMap or with the \c operator() member. + /// \brief Provides an immutable and unique id for each item in a graph. /// - template - class IdMap { + /// IdMap provides a unique and immutable id for each item of the + /// same type (\c Node, \c Arc or \c Edge) in a graph. This id is + /// - \b unique: different items get different ids, + /// - \b immutable: the id of an item does not change (even if you + /// delete other nodes). + /// + /// Using this map you get access (i.e. can read) the inner id values of + /// the items stored in the graph, which is returned by the \c id() + /// function of the graph. This map can be inverted with its member + /// class \c InverseMap or with the \c operator()() member. + /// + /// \tparam GR The graph type. + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or + /// \c GR::Edge). + /// + /// \see RangeIdMap + template + class IdMap : public MapBase { public: - typedef _Graph Graph; + /// The graph type of IdMap. + typedef GR Graph; + typedef GR Digraph; + /// The key type of IdMap (\c Node, \c Arc or \c Edge). + typedef K Item; + /// The key type of IdMap (\c Node, \c Arc or \c Edge). + typedef K Key; + /// The value type of IdMap. typedef int Value; - typedef _Item Item; - typedef _Item Key; /// \brief Constructor. /// @@ -1813,9 +1855,9 @@ /// Gives back the immutable and unique \e id of the item. int operator[](const Item& item) const { return _graph->id(item);} - /// \brief Gives back the item by its id. + /// \brief Gives back the \e item by its id. /// - /// Gives back the item by its id. + /// Gives back the \e item by its id. Item operator()(int id) { return _graph->fromId(id, Item()); } private: @@ -1823,9 +1865,11 @@ public: - /// \brief The class represents the inverse of its owner (IdMap). + /// \brief The inverse map type of IdMap. /// - /// The class represents the inverse of its owner (IdMap). + /// The inverse map type of IdMap. The subscript operator gives back + /// an item by its id. + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept. /// \see inverse() class InverseMap { public: @@ -1840,10 +1884,9 @@ /// Constructor for creating an id-to-item map. explicit InverseMap(const IdMap& map) : _graph(map._graph) {} - /// \brief Gives back the given item from its id. + /// \brief Gives back an item by its id. /// - /// Gives back the given item from its id. - /// + /// Gives back an item by its id. Item operator[](int id) const { return _graph->fromId(id, Item());} private: @@ -1854,165 +1897,220 @@ /// /// Gives back the inverse of the IdMap. InverseMap inverse() const { return InverseMap(*_graph);} - }; - - /// \brief General invertable graph-map type. - - /// This type provides simple invertable graph-maps. - /// The InvertableMap wraps an arbitrary ReadWriteMap - /// and if a key is set to a new value then store it - /// in the inverse map. + /// \brief Returns an \c IdMap class. /// - /// The values of the map can be accessed - /// with stl compatible forward iterator. + /// This function just returns an \c IdMap class. + /// \relates IdMap + template + inline IdMap idMap(const GR& graph) { + return IdMap(graph); + } + + /// \brief General cross reference graph map type. + + /// This class provides simple invertable graph maps. + /// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap) + /// and if a key is set to a new value, then stores it in the inverse map. + /// The graph items can be accessed by their values either using + /// \c InverseMap or \c operator()(), and the values of the map can be + /// accessed with an STL compatible forward iterator (\c ValueIt). + /// + /// This map is intended to be used when all associated values are + /// different (the map is actually invertable) or there are only a few + /// items with the same value. + /// Otherwise consider to use \c IterableValueMap, which is more + /// suitable and more efficient for such cases. It provides iterators + /// to traverse the items with the same associated value, however + /// it does not have \c InverseMap. /// - /// \tparam _Graph The graph type. - /// \tparam _Item The item type of the graph. - /// \tparam _Value The value type of the map. + /// This type is not reference map, so it cannot be modified with + /// the subscript operator. + /// + /// \tparam GR The graph type. + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or + /// \c GR::Edge). + /// \tparam V The value type of the map. /// /// \see IterableValueMap - template - class InvertableMap - : protected ItemSetTraits<_Graph, _Item>::template Map<_Value>::Type { + template + class CrossRefMap + : protected ItemSetTraits::template Map::Type { private: - typedef typename ItemSetTraits<_Graph, _Item>:: - template Map<_Value>::Type Map; - typedef _Graph Graph; - - typedef std::map<_Value, _Item> Container; + typedef typename ItemSetTraits:: + template Map::Type Map; + + typedef std::multimap Container; Container _inv_map; public: - /// The key type of InvertableMap (Node, Arc, Edge). - typedef typename Map::Key Key; - /// The value type of the InvertableMap. - typedef typename Map::Value Value; + /// The graph type of CrossRefMap. + typedef GR Graph; + typedef GR Digraph; + /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge). + typedef K Item; + /// The key type of CrossRefMap (\c Node, \c Arc or \c Edge). + typedef K Key; + /// The value type of CrossRefMap. + typedef V Value; /// \brief Constructor. /// - /// Construct a new InvertableMap for the graph. - /// - explicit InvertableMap(const Graph& graph) : Map(graph) {} + /// Construct a new CrossRefMap for the given graph. + explicit CrossRefMap(const Graph& graph) : Map(graph) {} /// \brief Forward iterator for values. /// - /// This iterator is an stl compatible forward + /// This iterator is an STL compatible forward /// iterator on the values of the map. The values can - /// be accessed in the [beginValue, endValue) range. - /// - class ValueIterator + /// be accessed in the [beginValue, endValue) range. + /// They are considered with multiplicity, so each value is + /// traversed for each item it is assigned to. + class ValueIt : public std::iterator { - friend class InvertableMap; + friend class CrossRefMap; private: - ValueIterator(typename Container::const_iterator _it) + ValueIt(typename Container::const_iterator _it) : it(_it) {} public: - ValueIterator() {} - - ValueIterator& operator++() { ++it; return *this; } - ValueIterator operator++(int) { - ValueIterator tmp(*this); + /// Constructor + ValueIt() {} + + /// \e + ValueIt& operator++() { ++it; return *this; } + /// \e + ValueIt operator++(int) { + ValueIt tmp(*this); operator++(); return tmp; } + /// \e const Value& operator*() const { return it->first; } + /// \e const Value* operator->() const { return &(it->first); } - bool operator==(ValueIterator jt) const { return it == jt.it; } - bool operator!=(ValueIterator jt) const { return it != jt.it; } + /// \e + bool operator==(ValueIt jt) const { return it == jt.it; } + /// \e + bool operator!=(ValueIt jt) const { return it != jt.it; } private: typename Container::const_iterator it; }; + + /// Alias for \c ValueIt + typedef ValueIt ValueIterator; /// \brief Returns an iterator to the first value. /// - /// Returns an stl compatible iterator to the + /// Returns an STL compatible iterator to the /// first value of the map. The values of the - /// map can be accessed in the [beginValue, endValue) + /// map can be accessed in the [beginValue, endValue) /// range. - ValueIterator beginValue() const { - return ValueIterator(_inv_map.begin()); + ValueIt beginValue() const { + return ValueIt(_inv_map.begin()); } /// \brief Returns an iterator after the last value. /// - /// Returns an stl compatible iterator after the + /// Returns an STL compatible iterator after the /// last value of the map. The values of the - /// map can be accessed in the [beginValue, endValue) + /// map can be accessed in the [beginValue, endValue) /// range. - ValueIterator endValue() const { - return ValueIterator(_inv_map.end()); + ValueIt endValue() const { + return ValueIt(_inv_map.end()); } - /// \brief The setter function of the map. + /// \brief Sets the value associated with the given key. /// - /// Sets the mapped value. + /// Sets the value associated with the given key. void set(const Key& key, const Value& val) { Value oldval = Map::operator[](key); - typename Container::iterator it = _inv_map.find(oldval); - if (it != _inv_map.end() && it->second == key) { - _inv_map.erase(it); + typename Container::iterator it; + for (it = _inv_map.equal_range(oldval).first; + it != _inv_map.equal_range(oldval).second; ++it) { + if (it->second == key) { + _inv_map.erase(it); + break; + } } - _inv_map.insert(make_pair(val, key)); + _inv_map.insert(std::make_pair(val, key)); Map::set(key, val); } - /// \brief The getter function of the map. + /// \brief Returns the value associated with the given key. /// - /// It gives back the value associated with the key. + /// Returns the value associated with the given key. typename MapTraits::ConstReturnValue operator[](const Key& key) const { return Map::operator[](key); } - /// \brief Gives back the item by its value. + /// \brief Gives back an item by its value. /// - /// Gives back the item by its value. - Key operator()(const Value& key) const { - typename Container::const_iterator it = _inv_map.find(key); + /// This function gives back an item that is assigned to + /// the given value or \c INVALID if no such item exists. + /// If there are more items with the same associated value, + /// only one of them is returned. + Key operator()(const Value& val) const { + typename Container::const_iterator it = _inv_map.find(val); return it != _inv_map.end() ? it->second : INVALID; } + + /// \brief Returns the number of items with the given value. + /// + /// This function returns the number of items with the given value + /// associated with it. + int count(const Value &val) const { + return _inv_map.count(val); + } protected: - /// \brief Erase the key from the map. + /// \brief Erase the key from the map and the inverse map. /// - /// Erase the key to the map. It is called by the + /// Erase the key from the map and the inverse map. It is called by the /// \c AlterationNotifier. virtual void erase(const Key& key) { Value val = Map::operator[](key); - typename Container::iterator it = _inv_map.find(val); - if (it != _inv_map.end() && it->second == key) { - _inv_map.erase(it); + typename Container::iterator it; + for (it = _inv_map.equal_range(val).first; + it != _inv_map.equal_range(val).second; ++it) { + if (it->second == key) { + _inv_map.erase(it); + break; + } } Map::erase(key); } - /// \brief Erase more keys from the map. + /// \brief Erase more keys from the map and the inverse map. /// - /// Erase more keys from the map. It is called by the + /// Erase more keys from the map and the inverse map. It is called by the /// \c AlterationNotifier. virtual void erase(const std::vector& keys) { for (int i = 0; i < int(keys.size()); ++i) { Value val = Map::operator[](keys[i]); - typename Container::iterator it = _inv_map.find(val); - if (it != _inv_map.end() && it->second == keys[i]) { - _inv_map.erase(it); + typename Container::iterator it; + for (it = _inv_map.equal_range(val).first; + it != _inv_map.equal_range(val).second; ++it) { + if (it->second == keys[i]) { + _inv_map.erase(it); + break; + } } } Map::erase(keys); } - /// \brief Clear the keys from the map and inverse map. + /// \brief Clear the keys from the map and the inverse map. /// - /// Clear the keys from the map and inverse map. It is called by the + /// Clear the keys from the map and the inverse map. It is called by the /// \c AlterationNotifier. virtual void clear() { _inv_map.clear(); @@ -2021,79 +2119,90 @@ public: - /// \brief The inverse map type. + /// \brief The inverse map type of CrossRefMap. /// - /// The inverse of this map. The subscript operator of the map - /// gives back always the item what was last assigned to the value. + /// The inverse map type of CrossRefMap. The subscript operator gives + /// back an item by its value. + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept. + /// \see inverse() class InverseMap { public: - /// \brief Constructor of the InverseMap. + /// \brief Constructor /// /// Constructor of the InverseMap. - explicit InverseMap(const InvertableMap& inverted) + explicit InverseMap(const CrossRefMap& inverted) : _inverted(inverted) {} /// The value type of the InverseMap. - typedef typename InvertableMap::Key Value; + typedef typename CrossRefMap::Key Value; /// The key type of the InverseMap. - typedef typename InvertableMap::Value Key; + typedef typename CrossRefMap::Value Key; /// \brief Subscript operator. /// - /// Subscript operator. It gives back always the item - /// what was last assigned to the value. + /// Subscript operator. It gives back an item + /// that is assigned to the given value or \c INVALID + /// if no such item exists. Value operator[](const Key& key) const { return _inverted(key); } private: - const InvertableMap& _inverted; + const CrossRefMap& _inverted; }; - /// \brief It gives back the just readable inverse map. + /// \brief Gives back the inverse of the map. /// - /// It gives back the just readable inverse map. + /// Gives back the inverse of the CrossRefMap. InverseMap inverse() const { return InverseMap(*this); } }; - /// \brief Provides a mutable, continuous and unique descriptor for each - /// item in the graph. + /// \brief Provides continuous and unique id for the + /// items of a graph. /// - /// The DescriptorMap class provides a unique and continuous (but mutable) - /// descriptor (id) for each item of the same type (e.g. node) in the - /// graph. This id is
  • \b unique: different items (nodes) get - /// different ids
  • \b continuous: the range of the ids is the set of - /// integers between 0 and \c n-1, where \c n is the number of the items of - /// this type (e.g. nodes) (so the id of a node can change if you delete an - /// other node, i.e. this id is mutable).
This map can be inverted - /// with its member class \c InverseMap, or with the \c operator() member. + /// RangeIdMap provides a unique and continuous + /// id for each item of a given type (\c Node, \c Arc or + /// \c Edge) in a graph. This id is + /// - \b unique: different items get different ids, + /// - \b continuous: the range of the ids is the set of integers + /// between 0 and \c n-1, where \c n is the number of the items of + /// this type (\c Node, \c Arc or \c Edge). + /// - So, the ids can change when deleting an item of the same type. /// - /// \tparam _Graph The graph class the \c DescriptorMap belongs to. - /// \tparam _Item The Item is the Key of the Map. It may be Node, Arc or - /// Edge. - template - class DescriptorMap - : protected ItemSetTraits<_Graph, _Item>::template Map::Type { - - typedef _Item Item; - typedef typename ItemSetTraits<_Graph, _Item>::template Map::Type Map; + /// Thus this id is not (necessarily) the same as what can get using + /// the \c id() function of the graph or \ref IdMap. + /// This map can be inverted with its member class \c InverseMap, + /// or with the \c operator()() member. + /// + /// \tparam GR The graph type. + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or + /// \c GR::Edge). + /// + /// \see IdMap + template + class RangeIdMap + : protected ItemSetTraits::template Map::Type { + + typedef typename ItemSetTraits::template Map::Type Map; public: - /// The graph class of DescriptorMap. - typedef _Graph Graph; - - /// The key type of DescriptorMap (Node, Arc, Edge). - typedef typename Map::Key Key; - /// The value type of DescriptorMap. - typedef typename Map::Value Value; + /// The graph type of RangeIdMap. + typedef GR Graph; + typedef GR Digraph; + /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge). + typedef K Item; + /// The key type of RangeIdMap (\c Node, \c Arc or \c Edge). + typedef K Key; + /// The value type of RangeIdMap. + typedef int Value; /// \brief Constructor. /// - /// Constructor for descriptor map. - explicit DescriptorMap(const Graph& _graph) : Map(_graph) { + /// Constructor. + explicit RangeIdMap(const Graph& gr) : Map(gr) { Item it; const typename Map::Notifier* nf = Map::notifier(); for (nf->first(it); it != INVALID; nf->next(it)) { @@ -2104,7 +2213,7 @@ protected: - /// \brief Add a new key to the map. + /// \brief Adds a new key to the map. /// /// Add a new key to the map. It is called by the /// \c AlterationNotifier. @@ -2194,16 +2303,16 @@ _inv_map[pi] = q; } - /// \brief Gives back the \e descriptor of the item. + /// \brief Gives back the \e range \e id of the item /// - /// Gives back the mutable and unique \e descriptor of the map. + /// Gives back the \e range \e id of the item. int operator[](const Item& item) const { return Map::operator[](item); } - /// \brief Gives back the item by its descriptor. + /// \brief Gives back the item belonging to a \e range \e id /// - /// Gives back th item by its descriptor. + /// Gives back the item belonging to the given \e range \e id. Item operator()(int id) const { return _inv_map[id]; } @@ -2214,27 +2323,30 @@ Container _inv_map; public: - /// \brief The inverse map type of DescriptorMap. + + /// \brief The inverse map type of RangeIdMap. /// - /// The inverse map type of DescriptorMap. + /// The inverse map type of RangeIdMap. The subscript operator gives + /// back an item by its \e range \e id. + /// This type conforms to the \ref concepts::ReadMap "ReadMap" concept. class InverseMap { public: - /// \brief Constructor of the InverseMap. + /// \brief Constructor /// /// Constructor of the InverseMap. - explicit InverseMap(const DescriptorMap& inverted) + explicit InverseMap(const RangeIdMap& inverted) : _inverted(inverted) {} /// The value type of the InverseMap. - typedef typename DescriptorMap::Key Value; + typedef typename RangeIdMap::Key Value; /// The key type of the InverseMap. - typedef typename DescriptorMap::Value Key; + typedef typename RangeIdMap::Value Key; /// \brief Subscript operator. /// /// Subscript operator. It gives back the item - /// that the descriptor belongs to currently. + /// that the given \e range \e id currently belongs to. Value operator[](const Key& key) const { return _inverted(key); } @@ -2247,241 +2359,1134 @@ } private: - const DescriptorMap& _inverted; + const RangeIdMap& _inverted; }; /// \brief Gives back the inverse of the map. /// - /// Gives back the inverse of the map. + /// Gives back the inverse of the RangeIdMap. const InverseMap inverse() const { return InverseMap(*this); } }; - /// \brief Returns the source of the given arc. + /// \brief Returns a \c RangeIdMap class. /// - /// The SourceMap gives back the source Node of the given arc. + /// This function just returns an \c RangeIdMap class. + /// \relates RangeIdMap + template + inline RangeIdMap rangeIdMap(const GR& graph) { + return RangeIdMap(graph); + } + + /// \brief Dynamic iterable \c bool map. + /// + /// This class provides a special graph map type which can store a + /// \c bool value for graph items (\c Node, \c Arc or \c Edge). + /// For both \c true and \c false values it is possible to iterate on + /// the keys mapped to the value. + /// + /// This type is a reference map, so it can be modified with the + /// subscript operator. + /// + /// \tparam GR The graph type. + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or + /// \c GR::Edge). + /// + /// \see IterableIntMap, IterableValueMap + /// \see CrossRefMap + template + class IterableBoolMap + : protected ItemSetTraits::template Map::Type { + private: + typedef GR Graph; + + typedef typename ItemSetTraits::ItemIt KeyIt; + typedef typename ItemSetTraits::template Map::Type Parent; + + std::vector _array; + int _sep; + + public: + + /// Indicates that the map is reference map. + typedef True ReferenceMapTag; + + /// The key type + typedef K Key; + /// The value type + typedef bool Value; + /// The const reference type. + typedef const Value& ConstReference; + + private: + + int position(const Key& key) const { + return Parent::operator[](key); + } + + public: + + /// \brief Reference to the value of the map. + /// + /// This class is similar to the \c bool type. It can be converted to + /// \c bool and it provides the same operators. + class Reference { + friend class IterableBoolMap; + private: + Reference(IterableBoolMap& map, const Key& key) + : _key(key), _map(map) {} + public: + + Reference& operator=(const Reference& value) { + _map.set(_key, static_cast(value)); + return *this; + } + + operator bool() const { + return static_cast(_map)[_key]; + } + + Reference& operator=(bool value) { + _map.set(_key, value); + return *this; + } + Reference& operator&=(bool value) { + _map.set(_key, _map[_key] & value); + return *this; + } + Reference& operator|=(bool value) { + _map.set(_key, _map[_key] | value); + return *this; + } + Reference& operator^=(bool value) { + _map.set(_key, _map[_key] ^ value); + return *this; + } + private: + Key _key; + IterableBoolMap& _map; + }; + + /// \brief Constructor of the map with a default value. + /// + /// Constructor of the map with a default value. + explicit IterableBoolMap(const Graph& graph, bool def = false) + : Parent(graph) { + typename Parent::Notifier* nf = Parent::notifier(); + Key it; + for (nf->first(it); it != INVALID; nf->next(it)) { + Parent::set(it, _array.size()); + _array.push_back(it); + } + _sep = (def ? _array.size() : 0); + } + + /// \brief Const subscript operator of the map. + /// + /// Const subscript operator of the map. + bool operator[](const Key& key) const { + return position(key) < _sep; + } + + /// \brief Subscript operator of the map. + /// + /// Subscript operator of the map. + Reference operator[](const Key& key) { + return Reference(*this, key); + } + + /// \brief Set operation of the map. + /// + /// Set operation of the map. + void set(const Key& key, bool value) { + int pos = position(key); + if (value) { + if (pos < _sep) return; + Key tmp = _array[_sep]; + _array[_sep] = key; + Parent::set(key, _sep); + _array[pos] = tmp; + Parent::set(tmp, pos); + ++_sep; + } else { + if (pos >= _sep) return; + --_sep; + Key tmp = _array[_sep]; + _array[_sep] = key; + Parent::set(key, _sep); + _array[pos] = tmp; + Parent::set(tmp, pos); + } + } + + /// \brief Set all items. + /// + /// Set all items in the map. + /// \note Constant time operation. + void setAll(bool value) { + _sep = (value ? _array.size() : 0); + } + + /// \brief Returns the number of the keys mapped to \c true. + /// + /// Returns the number of the keys mapped to \c true. + int trueNum() const { + return _sep; + } + + /// \brief Returns the number of the keys mapped to \c false. + /// + /// Returns the number of the keys mapped to \c false. + int falseNum() const { + return _array.size() - _sep; + } + + /// \brief Iterator for the keys mapped to \c true. + /// + /// Iterator for the keys mapped to \c true. It works + /// like a graph item iterator, it can be converted to + /// the key type of the map, incremented with \c ++ operator, and + /// if the iterator leaves the last valid key, it will be equal to + /// \c INVALID. + class TrueIt : public Key { + public: + typedef Key Parent; + + /// \brief Creates an iterator. + /// + /// Creates an iterator. It iterates on the + /// keys mapped to \c true. + /// \param map The IterableBoolMap. + explicit TrueIt(const IterableBoolMap& map) + : Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID), + _map(&map) {} + + /// \brief Invalid constructor \& conversion. + /// + /// This constructor initializes the iterator to be invalid. + /// \sa Invalid for more details. + TrueIt(Invalid) : Parent(INVALID), _map(0) {} + + /// \brief Increment operator. + /// + /// Increment operator. + TrueIt& operator++() { + int pos = _map->position(*this); + Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID); + return *this; + } + + private: + const IterableBoolMap* _map; + }; + + /// \brief Iterator for the keys mapped to \c false. + /// + /// Iterator for the keys mapped to \c false. It works + /// like a graph item iterator, it can be converted to + /// the key type of the map, incremented with \c ++ operator, and + /// if the iterator leaves the last valid key, it will be equal to + /// \c INVALID. + class FalseIt : public Key { + public: + typedef Key Parent; + + /// \brief Creates an iterator. + /// + /// Creates an iterator. It iterates on the + /// keys mapped to \c false. + /// \param map The IterableBoolMap. + explicit FalseIt(const IterableBoolMap& map) + : Parent(map._sep < int(map._array.size()) ? + map._array.back() : INVALID), _map(&map) {} + + /// \brief Invalid constructor \& conversion. + /// + /// This constructor initializes the iterator to be invalid. + /// \sa Invalid for more details. + FalseIt(Invalid) : Parent(INVALID), _map(0) {} + + /// \brief Increment operator. + /// + /// Increment operator. + FalseIt& operator++() { + int pos = _map->position(*this); + Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID); + return *this; + } + + private: + const IterableBoolMap* _map; + }; + + /// \brief Iterator for the keys mapped to a given value. + /// + /// Iterator for the keys mapped to a given value. It works + /// like a graph item iterator, it can be converted to + /// the key type of the map, incremented with \c ++ operator, and + /// if the iterator leaves the last valid key, it will be equal to + /// \c INVALID. + class ItemIt : public Key { + public: + typedef Key Parent; + + /// \brief Creates an iterator with a value. + /// + /// Creates an iterator with a value. It iterates on the + /// keys mapped to the given value. + /// \param map The IterableBoolMap. + /// \param value The value. + ItemIt(const IterableBoolMap& map, bool value) + : Parent(value ? + (map._sep > 0 ? + map._array[map._sep - 1] : INVALID) : + (map._sep < int(map._array.size()) ? + map._array.back() : INVALID)), _map(&map) {} + + /// \brief Invalid constructor \& conversion. + /// + /// This constructor initializes the iterator to be invalid. + /// \sa Invalid for more details. + ItemIt(Invalid) : Parent(INVALID), _map(0) {} + + /// \brief Increment operator. + /// + /// Increment operator. + ItemIt& operator++() { + int pos = _map->position(*this); + int _sep = pos >= _map->_sep ? _map->_sep : 0; + Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID); + return *this; + } + + private: + const IterableBoolMap* _map; + }; + + protected: + + virtual void add(const Key& key) { + Parent::add(key); + Parent::set(key, _array.size()); + _array.push_back(key); + } + + virtual void add(const std::vector& keys) { + Parent::add(keys); + for (int i = 0; i < int(keys.size()); ++i) { + Parent::set(keys[i], _array.size()); + _array.push_back(keys[i]); + } + } + + virtual void erase(const Key& key) { + int pos = position(key); + if (pos < _sep) { + --_sep; + Parent::set(_array[_sep], pos); + _array[pos] = _array[_sep]; + Parent::set(_array.back(), _sep); + _array[_sep] = _array.back(); + _array.pop_back(); + } else { + Parent::set(_array.back(), pos); + _array[pos] = _array.back(); + _array.pop_back(); + } + Parent::erase(key); + } + + virtual void erase(const std::vector& keys) { + for (int i = 0; i < int(keys.size()); ++i) { + int pos = position(keys[i]); + if (pos < _sep) { + --_sep; + Parent::set(_array[_sep], pos); + _array[pos] = _array[_sep]; + Parent::set(_array.back(), _sep); + _array[_sep] = _array.back(); + _array.pop_back(); + } else { + Parent::set(_array.back(), pos); + _array[pos] = _array.back(); + _array.pop_back(); + } + } + Parent::erase(keys); + } + + virtual void build() { + Parent::build(); + typename Parent::Notifier* nf = Parent::notifier(); + Key it; + for (nf->first(it); it != INVALID; nf->next(it)) { + Parent::set(it, _array.size()); + _array.push_back(it); + } + _sep = 0; + } + + virtual void clear() { + _array.clear(); + _sep = 0; + Parent::clear(); + } + + }; + + + namespace _maps_bits { + template + struct IterableIntMapNode { + IterableIntMapNode() : value(-1) {} + IterableIntMapNode(int _value) : value(_value) {} + Item prev, next; + int value; + }; + } + + /// \brief Dynamic iterable integer map. + /// + /// This class provides a special graph map type which can store an + /// integer value for graph items (\c Node, \c Arc or \c Edge). + /// For each non-negative value it is possible to iterate on the keys + /// mapped to the value. + /// + /// This map is intended to be used with small integer values, for which + /// it is efficient, and supports iteration only for non-negative values. + /// If you need large values and/or iteration for negative integers, + /// consider to use \ref IterableValueMap instead. + /// + /// This type is a reference map, so it can be modified with the + /// subscript operator. + /// + /// \note The size of the data structure depends on the largest + /// value in the map. + /// + /// \tparam GR The graph type. + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or + /// \c GR::Edge). + /// + /// \see IterableBoolMap, IterableValueMap + /// \see CrossRefMap + template + class IterableIntMap + : protected ItemSetTraits:: + template Map<_maps_bits::IterableIntMapNode >::Type { + public: + typedef typename ItemSetTraits:: + template Map<_maps_bits::IterableIntMapNode >::Type Parent; + + /// The key type + typedef K Key; + /// The value type + typedef int Value; + /// The graph type + typedef GR Graph; + + /// \brief Constructor of the map. + /// + /// Constructor of the map. It sets all values to -1. + explicit IterableIntMap(const Graph& graph) + : Parent(graph) {} + + /// \brief Constructor of the map with a given value. + /// + /// Constructor of the map with a given value. + explicit IterableIntMap(const Graph& graph, int value) + : Parent(graph, _maps_bits::IterableIntMapNode(value)) { + if (value >= 0) { + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { + lace(it); + } + } + } + + private: + + void unlace(const Key& key) { + typename Parent::Value& node = Parent::operator[](key); + if (node.value < 0) return; + if (node.prev != INVALID) { + Parent::operator[](node.prev).next = node.next; + } else { + _first[node.value] = node.next; + } + if (node.next != INVALID) { + Parent::operator[](node.next).prev = node.prev; + } + while (!_first.empty() && _first.back() == INVALID) { + _first.pop_back(); + } + } + + void lace(const Key& key) { + typename Parent::Value& node = Parent::operator[](key); + if (node.value < 0) return; + if (node.value >= int(_first.size())) { + _first.resize(node.value + 1, INVALID); + } + node.prev = INVALID; + node.next = _first[node.value]; + if (node.next != INVALID) { + Parent::operator[](node.next).prev = key; + } + _first[node.value] = key; + } + + public: + + /// Indicates that the map is reference map. + typedef True ReferenceMapTag; + + /// \brief Reference to the value of the map. + /// + /// This class is similar to the \c int type. It can + /// be converted to \c int and it has the same operators. + class Reference { + friend class IterableIntMap; + private: + Reference(IterableIntMap& map, const Key& key) + : _key(key), _map(map) {} + public: + + Reference& operator=(const Reference& value) { + _map.set(_key, static_cast(value)); + return *this; + } + + operator const int&() const { + return static_cast(_map)[_key]; + } + + Reference& operator=(int value) { + _map.set(_key, value); + return *this; + } + Reference& operator++() { + _map.set(_key, _map[_key] + 1); + return *this; + } + int operator++(int) { + int value = _map[_key]; + _map.set(_key, value + 1); + return value; + } + Reference& operator--() { + _map.set(_key, _map[_key] - 1); + return *this; + } + int operator--(int) { + int value = _map[_key]; + _map.set(_key, value - 1); + return value; + } + Reference& operator+=(int value) { + _map.set(_key, _map[_key] + value); + return *this; + } + Reference& operator-=(int value) { + _map.set(_key, _map[_key] - value); + return *this; + } + Reference& operator*=(int value) { + _map.set(_key, _map[_key] * value); + return *this; + } + Reference& operator/=(int value) { + _map.set(_key, _map[_key] / value); + return *this; + } + Reference& operator%=(int value) { + _map.set(_key, _map[_key] % value); + return *this; + } + Reference& operator&=(int value) { + _map.set(_key, _map[_key] & value); + return *this; + } + Reference& operator|=(int value) { + _map.set(_key, _map[_key] | value); + return *this; + } + Reference& operator^=(int value) { + _map.set(_key, _map[_key] ^ value); + return *this; + } + Reference& operator<<=(int value) { + _map.set(_key, _map[_key] << value); + return *this; + } + Reference& operator>>=(int value) { + _map.set(_key, _map[_key] >> value); + return *this; + } + + private: + Key _key; + IterableIntMap& _map; + }; + + /// The const reference type. + typedef const Value& ConstReference; + + /// \brief Gives back the maximal value plus one. + /// + /// Gives back the maximal value plus one. + int size() const { + return _first.size(); + } + + /// \brief Set operation of the map. + /// + /// Set operation of the map. + void set(const Key& key, const Value& value) { + unlace(key); + Parent::operator[](key).value = value; + lace(key); + } + + /// \brief Const subscript operator of the map. + /// + /// Const subscript operator of the map. + const Value& operator[](const Key& key) const { + return Parent::operator[](key).value; + } + + /// \brief Subscript operator of the map. + /// + /// Subscript operator of the map. + Reference operator[](const Key& key) { + return Reference(*this, key); + } + + /// \brief Iterator for the keys with the same value. + /// + /// Iterator for the keys with the same value. It works + /// like a graph item iterator, it can be converted to + /// the item type of the map, incremented with \c ++ operator, and + /// if the iterator leaves the last valid item, it will be equal to + /// \c INVALID. + class ItemIt : public Key { + public: + typedef Key Parent; + + /// \brief Invalid constructor \& conversion. + /// + /// This constructor initializes the iterator to be invalid. + /// \sa Invalid for more details. + ItemIt(Invalid) : Parent(INVALID), _map(0) {} + + /// \brief Creates an iterator with a value. + /// + /// Creates an iterator with a value. It iterates on the + /// keys mapped to the given value. + /// \param map The IterableIntMap. + /// \param value The value. + ItemIt(const IterableIntMap& map, int value) : _map(&map) { + if (value < 0 || value >= int(_map->_first.size())) { + Parent::operator=(INVALID); + } else { + Parent::operator=(_map->_first[value]); + } + } + + /// \brief Increment operator. + /// + /// Increment operator. + ItemIt& operator++() { + Parent::operator=(_map->IterableIntMap::Parent:: + operator[](static_cast(*this)).next); + return *this; + } + + private: + const IterableIntMap* _map; + }; + + protected: + + virtual void erase(const Key& key) { + unlace(key); + Parent::erase(key); + } + + virtual void erase(const std::vector& keys) { + for (int i = 0; i < int(keys.size()); ++i) { + unlace(keys[i]); + } + Parent::erase(keys); + } + + virtual void clear() { + _first.clear(); + Parent::clear(); + } + + private: + std::vector _first; + }; + + namespace _maps_bits { + template + struct IterableValueMapNode { + IterableValueMapNode(Value _value = Value()) : value(_value) {} + Item prev, next; + Value value; + }; + } + + /// \brief Dynamic iterable map for comparable values. + /// + /// This class provides a special graph map type which can store a + /// comparable value for graph items (\c Node, \c Arc or \c Edge). + /// For each value it is possible to iterate on the keys mapped to + /// the value (\c ItemIt), and the values of the map can be accessed + /// with an STL compatible forward iterator (\c ValueIt). + /// The map stores a linked list for each value, which contains + /// the items mapped to the value, and the used values are stored + /// in balanced binary tree (\c std::map). + /// + /// \ref IterableBoolMap and \ref IterableIntMap are similar classes + /// specialized for \c bool and \c int values, respectively. + /// + /// This type is not reference map, so it cannot be modified with + /// the subscript operator. + /// + /// \tparam GR The graph type. + /// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or + /// \c GR::Edge). + /// \tparam V The value type of the map. It can be any comparable + /// value type. + /// + /// \see IterableBoolMap, IterableIntMap + /// \see CrossRefMap + template + class IterableValueMap + : protected ItemSetTraits:: + template Map<_maps_bits::IterableValueMapNode >::Type { + public: + typedef typename ItemSetTraits:: + template Map<_maps_bits::IterableValueMapNode >::Type Parent; + + /// The key type + typedef K Key; + /// The value type + typedef V Value; + /// The graph type + typedef GR Graph; + + public: + + /// \brief Constructor of the map with a given value. + /// + /// Constructor of the map with a given value. + explicit IterableValueMap(const Graph& graph, + const Value& value = Value()) + : Parent(graph, _maps_bits::IterableValueMapNode(value)) { + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { + lace(it); + } + } + + protected: + + void unlace(const Key& key) { + typename Parent::Value& node = Parent::operator[](key); + if (node.prev != INVALID) { + Parent::operator[](node.prev).next = node.next; + } else { + if (node.next != INVALID) { + _first[node.value] = node.next; + } else { + _first.erase(node.value); + } + } + if (node.next != INVALID) { + Parent::operator[](node.next).prev = node.prev; + } + } + + void lace(const Key& key) { + typename Parent::Value& node = Parent::operator[](key); + typename std::map::iterator it = _first.find(node.value); + if (it == _first.end()) { + node.prev = node.next = INVALID; + _first.insert(std::make_pair(node.value, key)); + } else { + node.prev = INVALID; + node.next = it->second; + if (node.next != INVALID) { + Parent::operator[](node.next).prev = key; + } + it->second = key; + } + } + + public: + + /// \brief Forward iterator for values. + /// + /// This iterator is an STL compatible forward + /// iterator on the values of the map. The values can + /// be accessed in the [beginValue, endValue) range. + class ValueIt + : public std::iterator { + friend class IterableValueMap; + private: + ValueIt(typename std::map::const_iterator _it) + : it(_it) {} + public: + + /// Constructor + ValueIt() {} + + /// \e + ValueIt& operator++() { ++it; return *this; } + /// \e + ValueIt operator++(int) { + ValueIt tmp(*this); + operator++(); + return tmp; + } + + /// \e + const Value& operator*() const { return it->first; } + /// \e + const Value* operator->() const { return &(it->first); } + + /// \e + bool operator==(ValueIt jt) const { return it == jt.it; } + /// \e + bool operator!=(ValueIt jt) const { return it != jt.it; } + + private: + typename std::map::const_iterator it; + }; + + /// \brief Returns an iterator to the first value. + /// + /// Returns an STL compatible iterator to the + /// first value of the map. The values of the + /// map can be accessed in the [beginValue, endValue) + /// range. + ValueIt beginValue() const { + return ValueIt(_first.begin()); + } + + /// \brief Returns an iterator after the last value. + /// + /// Returns an STL compatible iterator after the + /// last value of the map. The values of the + /// map can be accessed in the [beginValue, endValue) + /// range. + ValueIt endValue() const { + return ValueIt(_first.end()); + } + + /// \brief Set operation of the map. + /// + /// Set operation of the map. + void set(const Key& key, const Value& value) { + unlace(key); + Parent::operator[](key).value = value; + lace(key); + } + + /// \brief Const subscript operator of the map. + /// + /// Const subscript operator of the map. + const Value& operator[](const Key& key) const { + return Parent::operator[](key).value; + } + + /// \brief Iterator for the keys with the same value. + /// + /// Iterator for the keys with the same value. It works + /// like a graph item iterator, it can be converted to + /// the item type of the map, incremented with \c ++ operator, and + /// if the iterator leaves the last valid item, it will be equal to + /// \c INVALID. + class ItemIt : public Key { + public: + typedef Key Parent; + + /// \brief Invalid constructor \& conversion. + /// + /// This constructor initializes the iterator to be invalid. + /// \sa Invalid for more details. + ItemIt(Invalid) : Parent(INVALID), _map(0) {} + + /// \brief Creates an iterator with a value. + /// + /// Creates an iterator with a value. It iterates on the + /// keys which have the given value. + /// \param map The IterableValueMap + /// \param value The value + ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) { + typename std::map::const_iterator it = + map._first.find(value); + if (it == map._first.end()) { + Parent::operator=(INVALID); + } else { + Parent::operator=(it->second); + } + } + + /// \brief Increment operator. + /// + /// Increment Operator. + ItemIt& operator++() { + Parent::operator=(_map->IterableValueMap::Parent:: + operator[](static_cast(*this)).next); + return *this; + } + + + private: + const IterableValueMap* _map; + }; + + protected: + + virtual void add(const Key& key) { + Parent::add(key); + unlace(key); + } + + virtual void add(const std::vector& keys) { + Parent::add(keys); + for (int i = 0; i < int(keys.size()); ++i) { + lace(keys[i]); + } + } + + virtual void erase(const Key& key) { + unlace(key); + Parent::erase(key); + } + + virtual void erase(const std::vector& keys) { + for (int i = 0; i < int(keys.size()); ++i) { + unlace(keys[i]); + } + Parent::erase(keys); + } + + virtual void build() { + Parent::build(); + for (typename Parent::ItemIt it(*this); it != INVALID; ++it) { + lace(it); + } + } + + virtual void clear() { + _first.clear(); + Parent::clear(); + } + + private: + std::map _first; + }; + + /// \brief Map of the source nodes of arcs in a digraph. + /// + /// SourceMap provides access for the source node of each arc in a digraph, + /// which is returned by the \c source() function of the digraph. + /// \tparam GR The digraph type. /// \see TargetMap - template + template class SourceMap { public: - typedef typename Digraph::Node Value; - typedef typename Digraph::Arc Key; + /// The key type (the \c Arc type of the digraph). + typedef typename GR::Arc Key; + /// The value type (the \c Node type of the digraph). + typedef typename GR::Node Value; /// \brief Constructor /// - /// Constructor + /// Constructor. /// \param digraph The digraph that the map belongs to. - explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {} - - /// \brief The subscript operator. + explicit SourceMap(const GR& digraph) : _graph(digraph) {} + + /// \brief Returns the source node of the given arc. /// - /// The subscript operator. - /// \param arc The arc - /// \return The source of the arc + /// Returns the source node of the given arc. Value operator[](const Key& arc) const { - return _digraph.source(arc); + return _graph.source(arc); } private: - const Digraph& _digraph; + const GR& _graph; }; /// \brief Returns a \c SourceMap class. /// /// This function just returns an \c SourceMap class. /// \relates SourceMap - template - inline SourceMap sourceMap(const Digraph& digraph) { - return SourceMap(digraph); + template + inline SourceMap sourceMap(const GR& graph) { + return SourceMap(graph); } - /// \brief Returns the target of the given arc. + /// \brief Map of the target nodes of arcs in a digraph. /// - /// The TargetMap gives back the target Node of the given arc. + /// TargetMap provides access for the target node of each arc in a digraph, + /// which is returned by the \c target() function of the digraph. + /// \tparam GR The digraph type. /// \see SourceMap - template + template class TargetMap { public: - typedef typename Digraph::Node Value; - typedef typename Digraph::Arc Key; + /// The key type (the \c Arc type of the digraph). + typedef typename GR::Arc Key; + /// The value type (the \c Node type of the digraph). + typedef typename GR::Node Value; /// \brief Constructor /// - /// Constructor + /// Constructor. /// \param digraph The digraph that the map belongs to. - explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {} - - /// \brief The subscript operator. + explicit TargetMap(const GR& digraph) : _graph(digraph) {} + + /// \brief Returns the target node of the given arc. /// - /// The subscript operator. - /// \param e The arc - /// \return The target of the arc + /// Returns the target node of the given arc. Value operator[](const Key& e) const { - return _digraph.target(e); + return _graph.target(e); } private: - const Digraph& _digraph; + const GR& _graph; }; /// \brief Returns a \c TargetMap class. /// /// This function just returns a \c TargetMap class. /// \relates TargetMap - template - inline TargetMap targetMap(const Digraph& digraph) { - return TargetMap(digraph); + template + inline TargetMap targetMap(const GR& graph) { + return TargetMap(graph); } - /// \brief Returns the "forward" directed arc view of an edge. + /// \brief Map of the "forward" directed arc view of edges in a graph. /// - /// Returns the "forward" directed arc view of an edge. + /// ForwardMap provides access for the "forward" directed arc view of + /// each edge in a graph, which is returned by the \c direct() function + /// of the graph with \c true parameter. + /// \tparam GR The graph type. /// \see BackwardMap - template + template class ForwardMap { public: - typedef typename Graph::Arc Value; - typedef typename Graph::Edge Key; + /// The key type (the \c Edge type of the digraph). + typedef typename GR::Edge Key; + /// The value type (the \c Arc type of the digraph). + typedef typename GR::Arc Value; /// \brief Constructor /// - /// Constructor + /// Constructor. /// \param graph The graph that the map belongs to. - explicit ForwardMap(const Graph& graph) : _graph(graph) {} - - /// \brief The subscript operator. + explicit ForwardMap(const GR& graph) : _graph(graph) {} + + /// \brief Returns the "forward" directed arc view of the given edge. /// - /// The subscript operator. - /// \param key An edge - /// \return The "forward" directed arc view of edge + /// Returns the "forward" directed arc view of the given edge. Value operator[](const Key& key) const { return _graph.direct(key, true); } private: - const Graph& _graph; + const GR& _graph; }; /// \brief Returns a \c ForwardMap class. /// /// This function just returns an \c ForwardMap class. /// \relates ForwardMap - template - inline ForwardMap forwardMap(const Graph& graph) { - return ForwardMap(graph); + template + inline ForwardMap forwardMap(const GR& graph) { + return ForwardMap(graph); } - /// \brief Returns the "backward" directed arc view of an edge. + /// \brief Map of the "backward" directed arc view of edges in a graph. /// - /// Returns the "backward" directed arc view of an edge. + /// BackwardMap provides access for the "backward" directed arc view of + /// each edge in a graph, which is returned by the \c direct() function + /// of the graph with \c false parameter. + /// \tparam GR The graph type. /// \see ForwardMap - template + template class BackwardMap { public: - typedef typename Graph::Arc Value; - typedef typename Graph::Edge Key; + /// The key type (the \c Edge type of the digraph). + typedef typename GR::Edge Key; + /// The value type (the \c Arc type of the digraph). + typedef typename GR::Arc Value; /// \brief Constructor /// - /// Constructor + /// Constructor. /// \param graph The graph that the map belongs to. - explicit BackwardMap(const Graph& graph) : _graph(graph) {} - - /// \brief The subscript operator. + explicit BackwardMap(const GR& graph) : _graph(graph) {} + + /// \brief Returns the "backward" directed arc view of the given edge. /// - /// The subscript operator. - /// \param key An edge - /// \return The "backward" directed arc view of edge + /// Returns the "backward" directed arc view of the given edge. Value operator[](const Key& key) const { return _graph.direct(key, false); } private: - const Graph& _graph; + const GR& _graph; }; /// \brief Returns a \c BackwardMap class /// This function just returns a \c BackwardMap class. /// \relates BackwardMap - template - inline BackwardMap backwardMap(const Graph& graph) { - return BackwardMap(graph); + template + inline BackwardMap backwardMap(const GR& graph) { + return BackwardMap(graph); } - /// \brief Potential difference map - /// - /// If there is an potential map on the nodes then we - /// can get an arc map as we get the substraction of the - /// values of the target and source. - template - class PotentialDifferenceMap { - public: - typedef typename Digraph::Arc Key; - typedef typename NodeMap::Value Value; - - /// \brief Constructor - /// - /// Contructor of the map - explicit PotentialDifferenceMap(const Digraph& digraph, - const NodeMap& potential) - : _digraph(digraph), _potential(potential) {} - - /// \brief Const subscription operator - /// - /// Const subscription operator - Value operator[](const Key& arc) const { - return _potential[_digraph.target(arc)] - - _potential[_digraph.source(arc)]; - } - - private: - const Digraph& _digraph; - const NodeMap& _potential; - }; - - /// \brief Returns a PotentialDifferenceMap. - /// - /// This function just returns a PotentialDifferenceMap. - /// \relates PotentialDifferenceMap - template - PotentialDifferenceMap - potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) { - return PotentialDifferenceMap(digraph, potential); - } - - /// \brief Map of the node in-degrees. + /// \brief Map of the in-degrees of nodes in a digraph. /// /// This map returns the in-degree of a node. Once it is constructed, - /// the degrees are stored in a standard NodeMap, so each query is done + /// the degrees are stored in a standard \c NodeMap, so each query is done /// in constant time. On the other hand, the values are updated automatically /// whenever the digraph changes. /// - /// \warning Besides addNode() and addArc(), a digraph structure may provide - /// alternative ways to modify the digraph. The correct behavior of InDegMap - /// is not guarantied if these additional features are used. For example - /// the functions \ref ListDigraph::changeSource() "changeSource()", + /// \warning Besides \c addNode() and \c addArc(), a digraph structure + /// may provide alternative ways to modify the digraph. + /// The correct behavior of InDegMap is not guarantied if these additional + /// features are used. For example the functions + /// \ref ListDigraph::changeSource() "changeSource()", /// \ref ListDigraph::changeTarget() "changeTarget()" and /// \ref ListDigraph::reverseArc() "reverseArc()" /// of \ref ListDigraph will \e not update the degree values correctly. /// /// \sa OutDegMap - - template + template class InDegMap - : protected ItemSetTraits<_Digraph, typename _Digraph::Arc> + : protected ItemSetTraits ::ItemNotifier::ObserverBase { public: - typedef _Digraph Digraph; + /// The graph type of InDegMap + typedef GR Graph; + typedef GR Digraph; + /// The key type + typedef typename Digraph::Node Key; + /// The value type typedef int Value; - typedef typename Digraph::Node Key; typedef typename ItemSetTraits ::ItemNotifier::ObserverBase Parent; @@ -2523,9 +3528,9 @@ /// \brief Constructor. /// - /// Constructor for creating in-degree map. - explicit InDegMap(const Digraph& digraph) - : _digraph(digraph), _deg(digraph) { + /// Constructor for creating an in-degree map. + explicit InDegMap(const Digraph& graph) + : _digraph(graph), _deg(graph) { Parent::attach(_digraph.notifier(typename Digraph::Arc())); for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { @@ -2533,6 +3538,8 @@ } } + /// \brief Gives back the in-degree of a Node. + /// /// Gives back the in-degree of a Node. int operator[](const Key& key) const { return _deg[key]; @@ -2579,33 +3586,37 @@ AutoNodeMap _deg; }; - /// \brief Map of the node out-degrees. + /// \brief Map of the out-degrees of nodes in a digraph. /// /// This map returns the out-degree of a node. Once it is constructed, - /// the degrees are stored in a standard NodeMap, so each query is done + /// the degrees are stored in a standard \c NodeMap, so each query is done /// in constant time. On the other hand, the values are updated automatically /// whenever the digraph changes. /// - /// \warning Besides addNode() and addArc(), a digraph structure may provide - /// alternative ways to modify the digraph. The correct behavior of OutDegMap - /// is not guarantied if these additional features are used. For example - /// the functions \ref ListDigraph::changeSource() "changeSource()", + /// \warning Besides \c addNode() and \c addArc(), a digraph structure + /// may provide alternative ways to modify the digraph. + /// The correct behavior of OutDegMap is not guarantied if these additional + /// features are used. For example the functions + /// \ref ListDigraph::changeSource() "changeSource()", /// \ref ListDigraph::changeTarget() "changeTarget()" and /// \ref ListDigraph::reverseArc() "reverseArc()" /// of \ref ListDigraph will \e not update the degree values correctly. /// /// \sa InDegMap - - template + template class OutDegMap - : protected ItemSetTraits<_Digraph, typename _Digraph::Arc> + : protected ItemSetTraits ::ItemNotifier::ObserverBase { public: - typedef _Digraph Digraph; + /// The graph type of OutDegMap + typedef GR Graph; + typedef GR Digraph; + /// The key type + typedef typename Digraph::Node Key; + /// The value type typedef int Value; - typedef typename Digraph::Node Key; typedef typename ItemSetTraits ::ItemNotifier::ObserverBase Parent; @@ -2645,9 +3656,9 @@ /// \brief Constructor. /// - /// Constructor for creating out-degree map. - explicit OutDegMap(const Digraph& digraph) - : _digraph(digraph), _deg(digraph) { + /// Constructor for creating an out-degree map. + explicit OutDegMap(const Digraph& graph) + : _digraph(graph), _deg(graph) { Parent::attach(_digraph.notifier(typename Digraph::Arc())); for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { @@ -2655,6 +3666,8 @@ } } + /// \brief Gives back the out-degree of a Node. + /// /// Gives back the out-degree of a Node. int operator[](const Key& key) const { return _deg[key]; @@ -2701,6 +3714,56 @@ AutoNodeMap _deg; }; + /// \brief Potential difference map + /// + /// PotentialDifferenceMap returns the difference between the potentials of + /// the source and target nodes of each arc in a digraph, i.e. it returns + /// \code + /// potential[gr.target(arc)] - potential[gr.source(arc)]. + /// \endcode + /// \tparam GR The digraph type. + /// \tparam POT A node map storing the potentials. + template + class PotentialDifferenceMap { + public: + /// Key type + typedef typename GR::Arc Key; + /// Value type + typedef typename POT::Value Value; + + /// \brief Constructor + /// + /// Contructor of the map. + explicit PotentialDifferenceMap(const GR& gr, + const POT& potential) + : _digraph(gr), _potential(potential) {} + + /// \brief Returns the potential difference for the given arc. + /// + /// Returns the potential difference for the given arc, i.e. + /// \code + /// potential[gr.target(arc)] - potential[gr.source(arc)]. + /// \endcode + Value operator[](const Key& arc) const { + return _potential[_digraph.target(arc)] - + _potential[_digraph.source(arc)]; + } + + private: + const GR& _digraph; + const POT& _potential; + }; + + /// \brief Returns a PotentialDifferenceMap. + /// + /// This function just returns a PotentialDifferenceMap. + /// \relates PotentialDifferenceMap + template + PotentialDifferenceMap + potentialDifferenceMap(const GR& gr, const POT& potential) { + return PotentialDifferenceMap(gr, potential); + } + /// @} }