alpar@906: /* -*- C++ -*- alpar@921: * src/lemon/vector_map.h - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@906: * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@906: * (Egervary Combinatorial Optimization Research Group, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@906: alpar@921: #ifndef LEMON_VECTOR_MAP_H alpar@921: #define LEMON_VECTOR_MAP_H deba@822: deba@822: #include deba@822: alpar@921: #include alpar@921: #include deba@822: deba@822: ///\ingroup graphmaps deba@822: ///\file deba@822: ///\brief Vector based graph maps. deba@822: alpar@921: namespace lemon { deba@822: deba@822: /// \addtogroup graphmaps deba@822: /// @{ deba@822: deba@937: /** The VectorMap template class is graph map structure what deba@822: * automatically updates the map when a key is added to or erased from deba@822: * the map. This map factory uses the allocators to implement deba@822: * the container functionality. This map factory deba@822: * uses the std::vector to implement the container function. deba@822: * deba@937: * \param MapRegistry The MapRegistry that the maps will belong to. deba@937: * \param Value The value type of the map. deba@822: * deba@937: * \author Balazs Dezso deba@822: */ deba@822: deba@822: template deba@822: class VectorMap : public MapRegistry::MapBase { deba@891: template friend class VectorMap; deba@822: public: deba@822: deba@822: /// The graph type of the maps. deba@822: typedef typename MapRegistry::Graph Graph; deba@822: /// The key type of the maps. deba@822: typedef typename MapRegistry::KeyType KeyType; deba@822: /// The iterator to iterate on the keys. deba@822: typedef typename MapRegistry::KeyIt KeyIt; deba@822: deba@822: /// The map type. deba@822: typedef VectorMap Map; deba@822: /// The MapBase of the Map which implements the core regisitry function. deba@822: typedef typename MapRegistry::MapBase MapBase; deba@822: deba@822: private: deba@822: deba@822: /// The container type of the map. deba@822: typedef std::vector Container; deba@822: deba@822: public: deba@822: deba@822: deba@822: /// The value type of the map. deba@822: typedef Value ValueType; deba@822: /// The reference type of the map; deba@822: typedef typename Container::reference ReferenceType; deba@822: /// The pointer type of the map; deba@822: typedef typename Container::pointer PointerType; deba@822: deba@822: /// The const value type of the map. deba@822: typedef const Value ConstValueType; deba@822: /// The const reference type of the map; deba@822: typedef typename Container::const_reference ConstReferenceType; deba@822: /// The pointer type of the map; deba@822: typedef typename Container::const_pointer ConstPointerType; deba@822: deba@937: /// Constructor to attach the new map into a registry. deba@937: deba@937: /** Constructor to attach the new map into a registry. deba@937: * It adds all the nodes or edges of the graph to the map. deba@822: */ deba@844: VectorMap(const Graph& g, MapRegistry& r) deba@844: : MapBase(g, r), container(KeyInfo::maxId(g)+1) {} deba@822: deba@937: /// Constructor uses given value to initialize the map. deba@937: deba@937: /** Constructor uses given value to initialize the map. deba@937: * It adds all the nodes or edges of the graph to the map. deba@822: */ deba@822: VectorMap(const Graph& g, MapRegistry& r, const Value& v) deba@844: : MapBase(g, r), container(KeyInfo::maxId(g)+1, v) {} deba@822: deba@937: /// Assign operator to copy a map of an other map type. deba@937: deba@891: /** Assign operator to copy a map of an other map type. deba@937: * This map's value type must be assignable by the other deba@937: * map type's value type. deba@822: */ deba@891: template deba@891: VectorMap(const VectorMap& c) deba@891: : MapBase(c), container(c.container.size()) { deba@891: for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { deba@891: int id = KeyInfo::id(*MapBase::getGraph(), it); deba@891: container[id] = c.container[id]; deba@822: } deba@822: } deba@822: deba@937: /// Assign operator to copy a map of an other map type. deba@937: deba@891: /** Assign operator to copy a map of an other map type. deba@937: * This map's value type must be assignable by the other deba@937: * map type's value type. deba@822: */ deba@891: template deba@891: VectorMap& operator=(const VectorMap& c) { deba@897: if (MapBase::getGraph() != c.getGraph()) { deba@897: MapBase::operator=(c); deba@897: container.resize(c.container.size()); deba@897: } deba@891: for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { deba@891: int id = KeyInfo::id(*MapBase::getGraph(), it); deba@891: container[id] = c.container[id]; deba@822: } deba@822: return *this; deba@822: } deba@937: deba@937: /// The subcript operator. deba@937: deba@822: /** deba@822: * The subscript operator. The map can be subscripted by the deba@822: * actual keys of the graph. deba@822: */ deba@822: ReferenceType operator[](const KeyType& key) { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), key); deba@822: return container[id]; deba@822: } deba@822: deba@937: /// The const subcript operator. deba@937: deba@822: /** deba@822: * The const subscript operator. The map can be subscripted by the deba@822: * actual keys of the graph. deba@822: */ deba@822: ConstReferenceType operator[](const KeyType& key) const { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), key); deba@822: return container[id]; deba@822: } deba@822: deba@937: ///Setter function of the map. deba@937: deba@822: /** Setter function of the map. Equivalent with map[key] = val. deba@822: * This is a compatibility feature with the not dereferable maps. deba@822: */ deba@822: void set(const KeyType& key, const ValueType& val) { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), key); deba@822: container[id] = val; deba@822: } deba@937: /// Adds a new key to the map. deba@822: deba@937: /** Adds a new key to the map. It called by the map registry deba@937: * and it overrides the \ref MapRegistry::MapBase MapBase's deba@937: * add() member function. deba@822: */ deba@822: void add(const KeyType& key) { deba@844: int id = KeyInfo::id(*MapBase::getGraph(), key); deba@822: if (id >= (int)container.size()) { deba@822: container.resize(id + 1); deba@822: } deba@822: } deba@937: deba@937: /// Erases a key from the map. deba@822: deba@937: /** Erase a key from the map. It called by the map registry deba@937: * and it overrides the \ref MapRegistry::MapBase MapBase's deba@937: * erase() member function. deba@822: */ deba@822: void erase(const KeyType& key) {} deba@822: deba@937: /// Makes empty the map. deba@937: deba@937: /** Makes empty the map. It called by the map registry deba@937: * and it overrides the \ref MapRegistry::MapBase MapBase's deba@937: * clear() member function. deba@822: */ deba@937: deba@822: void clear() { deba@822: container.clear(); deba@822: } deba@822: deba@822: /// The stl compatible pair iterator of the map. deba@822: typedef MapIterator Iterator; deba@822: /// The stl compatible const pair iterator of the map. deba@822: typedef MapConstIterator ConstIterator; deba@822: deba@822: /** Returns the begin iterator of the map. deba@822: */ deba@822: Iterator begin() { deba@822: return Iterator(*this, KeyIt(*MapBase::getGraph())); deba@822: } deba@822: deba@822: /** Returns the end iterator of the map. deba@822: */ deba@822: Iterator end() { deba@822: return Iterator(*this, INVALID); deba@822: } deba@822: deba@822: /** Returns the begin ConstIterator of the map. deba@822: */ deba@822: ConstIterator begin() const { deba@822: return ConstIterator(*this, KeyIt(*MapBase::getGraph())); deba@822: } deba@822: deba@822: /** Returns the end const_iterator of the map. deba@822: */ deba@822: ConstIterator end() const { deba@822: return ConstIterator(*this, INVALID); deba@822: } deba@822: deba@830: /// The KeySet of the Map. deba@830: typedef MapConstKeySet ConstKeySet; deba@830: deba@830: /// KeySet getter function. deba@830: ConstKeySet keySet() const { deba@830: return ConstKeySet(*this); deba@830: } deba@830: deba@830: /// The ConstValueSet of the Map. deba@830: typedef MapConstValueSet ConstValueSet; deba@830: deba@830: /// ConstValueSet getter function. deba@830: ConstValueSet valueSet() const { deba@830: return ConstValueSet(*this); deba@830: } deba@830: deba@830: /// The ValueSet of the Map. deba@830: typedef MapValueSet ValueSet; deba@830: deba@830: /// ValueSet getter function. deba@830: ValueSet valueSet() { deba@830: return ValueSet(*this); deba@830: } deba@830: deba@830: deba@822: private: deba@822: deba@822: Container container; deba@822: deba@844: public: deba@844: // STL compatibility typedefs. deba@844: typedef Iterator iterator; deba@844: typedef ConstIterator const_iterator; deba@844: typedef typename Iterator::PairValueType value_type; deba@844: typedef typename Iterator::KeyType key_type; deba@844: typedef typename Iterator::ValueType data_type; deba@844: typedef typename Iterator::PairReferenceType reference; deba@844: typedef typename Iterator::PairPointerType pointer; deba@844: typedef typename ConstIterator::PairReferenceType const_reference; deba@844: typedef typename ConstIterator::PairPointerType const_pointer; deba@844: typedef int difference_type; deba@822: }; deba@822: deba@822: /// @} deba@822: deba@822: } deba@822: deba@822: #endif