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