diff -r f2ea4aac9ada -r c94ef40a22ce src/lemon/vector_map.h --- a/src/lemon/vector_map.h Mon Oct 25 13:29:46 2004 +0000 +++ b/src/lemon/vector_map.h Wed Oct 27 22:38:50 2004 +0000 @@ -18,9 +18,9 @@ #define LEMON_VECTOR_MAP_H #include +#include -#include -#include +#include ///\ingroup graphmaps ///\file @@ -31,34 +31,41 @@ /// \addtogroup graphmaps /// @{ - /** The VectorMap template class is graph map structure what - * automatically updates the map when a key is added to or erased from - * the map. This map factory uses the allocators to implement - * the container functionality. This map factory - * uses the std::vector to implement the container function. - * - * \param MapRegistry The MapRegistry that the maps will belong to. - * \param Value The value type of the map. - * - * \author Balazs Dezso - */ - - template - class VectorMap : public MapRegistry::MapBase { - template friend class VectorMap; + /// The VectorMap template class is graph map structure what + /// automatically updates the map when a key is added to or erased from + /// the map. This map factory uses the allocators to implement + /// the container functionality. This map factory + /// uses the std::vector to implement the container function. + /// + /// \param Registry The AlterationObserverRegistry that will notify this map. + /// \param IdMap The IdMap type of the graph items. + /// \param Value The value type of the map. + /// + /// \author Balazs Dezso + + + template + class VectorMap : public AlterationObserverRegistry<_Item>::ObserverBase { public: - /// The graph type of the maps. - typedef typename MapRegistry::Graph Graph; - /// The key type of the maps. - typedef typename MapRegistry::KeyType KeyType; - /// The iterator to iterate on the keys. - typedef typename MapRegistry::KeyIt KeyIt; + /// The graph type of the map. + typedef _Graph Graph; + /// The key type of the map. + typedef _Item KeyType; + /// The id map type of the map. + typedef _IdMap IdMap; + /// The registry type of the map. + typedef AlterationObserverRegistry<_Item> Registry; + /// The value type of the map. + typedef _Value Value; /// The map type. typedef VectorMap Map; - /// The MapBase of the Map which implements the core regisitry function. - typedef typename MapRegistry::MapBase MapBase; + /// The base class of the map. + typedef typename Registry::ObserverBase Parent; private: @@ -67,7 +74,6 @@ public: - /// The value type of the map. typedef Value ValueType; /// The reference type of the map; @@ -82,95 +88,98 @@ /// The pointer type of the map; typedef typename Container::const_pointer ConstPointerType; - /// Constructor to attach the new map into a registry. + /// Constructor to attach the new map into the registry. - /** Constructor to attach the new map into a registry. - * It adds all the nodes or edges of the graph to the map. - */ - VectorMap(const Graph& g, MapRegistry& r) - : MapBase(g, r), container(KeyInfo::maxId(g)+1) {} + /// It construates a map and attachs it into the registry. + /// It adds all the items of the graph to the map. + + VectorMap(const Graph& _g, Registry& _r) : graph(&_g) { + attach(_r); + build(); + } /// Constructor uses given value to initialize the map. - /** Constructor uses given value to initialize the map. - * It adds all the nodes or edges of the graph to the map. - */ - VectorMap(const Graph& g, MapRegistry& r, const Value& v) - : MapBase(g, r), container(KeyInfo::maxId(g)+1, v) {} + /// It construates a map uses a given value to initialize the map. + /// It adds all the items of the graph to the map. + + VectorMap(const Graph& g, Registry& r, const Value& v) : graph(&g) { + attach(r); + container.resize(IdMap(*graph).maxId() + 1, v); + } - /// Assign operator to copy a map of an other map type. - - /** Assign operator to copy a map of an other map type. - * This map's value type must be assignable by the other - * map type's value type. - */ - template - VectorMap(const VectorMap& c) - : MapBase(c), container(c.container.size()) { - for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { - int id = KeyInfo::id(*MapBase::getGraph(), it); - container[id] = c.container[id]; + VectorMap(const VectorMap& copy) : graph(copy.getGraph()) { + if (copy.attached()) { + attach(*copy.getRegistry()); + container = copy.container; } } - /// Assign operator to copy a map of an other map type. - /** Assign operator to copy a map of an other map type. - * This map's value type must be assignable by the other - * map type's value type. + /** Assign operator to copy a map of the same map type. */ - template - VectorMap& operator=(const VectorMap& c) { - if (MapBase::getGraph() != c.getGraph()) { - MapBase::operator=(c); - container.resize(c.container.size()); + VectorMap& operator=(const VectorMap& copy) { + if (© == this) return *this; + + if (graph != copy.graph) { + if (attached()) { + detach(); + } + if (copy.attached()) { + attach(*copy.getRegistry()); + } } - for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { - int id = KeyInfo::id(*MapBase::getGraph(), it); - container[id] = c.container[id]; + container = copy.container; + + return *this; + } + + + virtual ~VectorMap() { + if (attached()) { + detach(); } - return *this; + } + + const Graph* getGraph() const { + return graph; } /// The subcript operator. - /** - * The subscript operator. The map can be subscripted by the - * actual keys of the graph. - */ + /// The subscript operator. The map can be subscripted by the + /// actual items of the graph. + ReferenceType operator[](const KeyType& key) { - int id = KeyInfo::id(*MapBase::getGraph(), key); - return container[id]; + return container[IdMap(*graph)[key]]; } /// The const subcript operator. - /** - * The const subscript operator. The map can be subscripted by the - * actual keys of the graph. - */ + /// The const subscript operator. The map can be subscripted by the + /// actual items of the graph. + ConstReferenceType operator[](const KeyType& key) const { - int id = KeyInfo::id(*MapBase::getGraph(), key); - return container[id]; + return container[IdMap(*graph)[key]]; } - ///Setter function of the map. - /** Setter function of the map. Equivalent with map[key] = val. - * This is a compatibility feature with the not dereferable maps. - */ - void set(const KeyType& key, const ValueType& val) { - int id = KeyInfo::id(*MapBase::getGraph(), key); - container[id] = val; + /// The setter function of the map. + + /// It the same as operator[](key) = value expression. + /// + + void set(const KeyType& key, const ValueType& value) { + (*this)[key] = value; } + /// Adds a new key to the map. - /** Adds a new key to the map. It called by the map registry - * and it overrides the \ref MapRegistry::MapBase MapBase's - * add() member function. - */ + /// It adds a new key to the map. It called by the observer registry + /// and it overrides the add() member function of the observer base. + void add(const KeyType& key) { - int id = KeyInfo::id(*MapBase::getGraph(), key); + int id = IdMap(*graph)[key]; if (id >= (int)container.size()) { container.resize(id + 1); } @@ -178,93 +187,94 @@ /// Erases a key from the map. - /** Erase a key from the map. It called by the map registry - * and it overrides the \ref MapRegistry::MapBase MapBase's - * erase() member function. - */ + /// Erase a key from the map. It called by the observer registry + /// and it overrides the erase() member function of the observer base. void erase(const KeyType& key) {} - /// Makes empty the map. + /// Buildes the map. + + /// It buildes the map. It called by the observer registry + /// and it overrides the build() member function of the observer base. - /** Makes empty the map. It called by the map registry - * and it overrides the \ref MapRegistry::MapBase MapBase's - * clear() member function. - */ + void build() { + container.resize(IdMap(*graph).maxId() + 1); + } + /// Clear the map. + + /// It erase all items from the map. It called by the observer registry + /// and it overrides the clear() member function of the observer base. void clear() { container.clear(); } - /// The stl compatible pair iterator of the map. - typedef MapIterator Iterator; - /// The stl compatible const pair iterator of the map. - typedef MapConstIterator ConstIterator; - - /** Returns the begin iterator of the map. - */ - Iterator begin() { - return Iterator(*this, KeyIt(*MapBase::getGraph())); - } - - /** Returns the end iterator of the map. - */ - Iterator end() { - return Iterator(*this, INVALID); - } - - /** Returns the begin ConstIterator of the map. - */ - ConstIterator begin() const { - return ConstIterator(*this, KeyIt(*MapBase::getGraph())); - } - - /** Returns the end const_iterator of the map. - */ - ConstIterator end() const { - return ConstIterator(*this, INVALID); - } - - /// The KeySet of the Map. - typedef MapConstKeySet ConstKeySet; - - /// KeySet getter function. - ConstKeySet keySet() const { - return ConstKeySet(*this); - } - - /// The ConstValueSet of the Map. - typedef MapConstValueSet ConstValueSet; - - /// ConstValueSet getter function. - ConstValueSet valueSet() const { - return ConstValueSet(*this); - } - - /// The ValueSet of the Map. - typedef MapValueSet ValueSet; - - /// ValueSet getter function. - ValueSet valueSet() { - return ValueSet(*this); - } - - private: Container container; + const Graph *graph; + }; + + + template + class VectorMappableGraphExtender : public _Base { public: - // STL compatibility typedefs. - typedef Iterator iterator; - typedef ConstIterator const_iterator; - typedef typename Iterator::PairValueType value_type; - typedef typename Iterator::KeyType key_type; - typedef typename Iterator::ValueType data_type; - typedef typename Iterator::PairReferenceType reference; - typedef typename Iterator::PairPointerType pointer; - typedef typename ConstIterator::PairReferenceType const_reference; - typedef typename ConstIterator::PairPointerType const_pointer; - typedef int difference_type; + + typedef VectorMappableGraphExtender<_Base> Graph; + typedef _Base Parent; + + typedef typename Parent::Node Node; + typedef typename Parent::NodeIt NodeIt; + typedef typename Parent::NodeIdMap NodeIdMap; + typedef typename Parent::NodeObserverRegistry NodeObserverRegistry; + + typedef typename Parent::Edge Edge; + typedef typename Parent::EdgeIt EdgeIt; + typedef typename Parent::EdgeIdMap EdgeIdMap; + typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry; + + + + template + class NodeMap : public VectorMap { + public: + typedef VectorMappableGraphExtender<_Base> Graph; + + typedef typename Graph::Node Node; + typedef typename Graph::NodeIdMap NodeIdMap; + + typedef VectorMap Parent; + + typedef typename Parent::Graph Graph; + typedef typename Parent::Value Value; + + NodeMap(const Graph& g) + : Parent(g, g.getNodeObserverRegistry()) {} + NodeMap(const Graph& g, const Value& v) + : Parent(g, g.getNodeObserverRegistry(), v) {} + + }; + + template + class EdgeMap : public VectorMap { + public: + typedef VectorMappableGraphExtender<_Base> Graph; + + typedef typename Graph::Edge Edge; + typedef typename Graph::EdgeIdMap EdgeIdMap; + + typedef VectorMap Parent; + + typedef typename Parent::Graph Graph; + typedef typename Parent::Value Value; + + EdgeMap(const Graph& g) + : Parent(g, g.getEdgeObserverRegistry()) {} + EdgeMap(const Graph& g, const Value& v) + : Parent(g, g.getEdgeObserverRegistry(), v) {} + + }; + }; /// @}