deba@822: // -*- c++ -*- deba@822: #ifndef VECTOR_MAP_H deba@822: #define VECTOR_MAP_H deba@822: deba@822: #include deba@822: deba@822: #include deba@822: deba@822: ///\ingroup graphmaps deba@822: ///\file deba@822: ///\brief Vector based graph maps. deba@822: deba@822: namespace hugo { deba@822: deba@822: /// \addtogroup graphmaps deba@822: /// @{ deba@822: deba@822: /** The ArrayMap 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@822: * The template parameter is the MapRegistry that the maps deba@822: * will belong to and the ValueType. deba@822: * deba@822: * \todo It should use a faster initialization using the maxNodeId() or deba@822: * maxEdgeId() function of the graph instead of iterating through each deba@822: * edge/node. deba@822: */ deba@822: deba@822: template deba@822: class VectorMap : public MapRegistry::MapBase { 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@822: /** Default constructor for the map. deba@822: */ deba@822: VectorMap() {} deba@822: deba@822: /** Graph and Registry initialized map constructor. deba@822: */ deba@822: VectorMap(const Graph& g, MapRegistry& r) : MapBase(g, r) { deba@822: init(); deba@822: } deba@822: deba@822: /** Constructor to use default value to initialize the map. deba@822: */ deba@822: VectorMap(const Graph& g, MapRegistry& r, const Value& v) deba@822: : MapBase(g, r) { deba@822: for (KeyIt it(*getGraph()); it != INVALID; ++it) { deba@822: int id = getGraph()->id(it); deba@822: if (id >= (int)container.size()) { deba@822: container.resize(id + 1); deba@822: } deba@822: set(it, v); deba@822: } deba@822: } deba@822: deba@822: /** Constructor to copy a map of an other map type. deba@822: */ deba@822: template VectorMap(const CMap& copy) : MapBase(copy) { deba@822: if (getGraph()) { deba@822: for (KeyIt it(*getGraph()); it != INVALID; ++it) { deba@822: int id = getGraph()->id(it); deba@822: if (id >= (int)container.size()) { deba@822: container.resize(id + 1); deba@822: } deba@822: set(it, copy[it]); deba@822: } deba@822: } deba@822: } deba@822: deba@822: /** Assign operator to copy a map an other map type. deba@822: */ deba@822: template VectorMap& operator=(const CMap& copy) { deba@822: if (getGraph()) { deba@822: destroy(); deba@822: } deba@822: this->MapBase::operator=(copy); deba@822: if (getGraph()) { deba@822: for (KeyIt it(*getGraph()); it != INVALID; ++it) { deba@822: int id = getGraph()->id(it); deba@822: if (id >= (int)container.size()) { deba@822: container.resize(id + 1); deba@822: } deba@822: set(it, copy[it]); deba@822: } deba@822: } deba@822: return *this; deba@822: } deba@822: deba@822: /** The destructor of the map. deba@822: */ deba@822: virtual ~VectorMap() { deba@822: } deba@822: 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@822: int id = getGraph()->id(key); deba@822: return container[id]; deba@822: } deba@822: 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@822: int id = getGraph()->id(key); deba@822: return container[id]; deba@822: } deba@822: 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@822: int id = getGraph()->id(key); deba@822: container[id] = val; deba@822: } deba@822: deba@822: /** Add a new key to the map. It called by the map registry. deba@822: */ deba@822: void add(const KeyType& key) { deba@822: int id = getGraph()->id(key); deba@822: if (id >= (int)container.size()) { deba@822: container.resize(id + 1); deba@822: } deba@822: } deba@822: deba@822: /** Erase a key from the map. It called by the map registry. deba@822: */ deba@822: void erase(const KeyType& key) {} deba@822: deba@822: /** Clear the data structure. deba@822: */ 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@822: deba@822: }; deba@822: deba@822: /// @} deba@822: deba@822: } deba@822: deba@822: #endif