| [822] | 1 | // -*- c++ -*- |
|---|
| 2 | #ifndef VECTOR_MAP_H |
|---|
| 3 | #define VECTOR_MAP_H |
|---|
| 4 | |
|---|
| 5 | #include <vector> |
|---|
| 6 | |
|---|
| 7 | #include <hugo/map_iterator.h> |
|---|
| [844] | 8 | #include <hugo/map_bits.h> |
|---|
| [822] | 9 | |
|---|
| 10 | ///\ingroup graphmaps |
|---|
| 11 | ///\file |
|---|
| 12 | ///\brief Vector based graph maps. |
|---|
| 13 | |
|---|
| 14 | namespace hugo { |
|---|
| 15 | |
|---|
| 16 | /// \addtogroup graphmaps |
|---|
| 17 | /// @{ |
|---|
| 18 | |
|---|
| 19 | /** The ArrayMap template class is graph map structure what |
|---|
| 20 | * automatically updates the map when a key is added to or erased from |
|---|
| 21 | * the map. This map factory uses the allocators to implement |
|---|
| 22 | * the container functionality. This map factory |
|---|
| 23 | * uses the std::vector to implement the container function. |
|---|
| 24 | * |
|---|
| 25 | * The template parameter is the MapRegistry that the maps |
|---|
| 26 | * will belong to and the ValueType. |
|---|
| 27 | * |
|---|
| 28 | * \todo It should use a faster initialization using the maxNodeId() or |
|---|
| 29 | * maxEdgeId() function of the graph instead of iterating through each |
|---|
| 30 | * edge/node. |
|---|
| 31 | */ |
|---|
| 32 | |
|---|
| 33 | template <typename MapRegistry, typename Value> |
|---|
| 34 | class VectorMap : public MapRegistry::MapBase { |
|---|
| [891] | 35 | template <typename MR, typename T> friend class VectorMap; |
|---|
| [822] | 36 | public: |
|---|
| 37 | |
|---|
| 38 | /// The graph type of the maps. |
|---|
| 39 | typedef typename MapRegistry::Graph Graph; |
|---|
| 40 | /// The key type of the maps. |
|---|
| 41 | typedef typename MapRegistry::KeyType KeyType; |
|---|
| 42 | /// The iterator to iterate on the keys. |
|---|
| 43 | typedef typename MapRegistry::KeyIt KeyIt; |
|---|
| 44 | |
|---|
| 45 | /// The map type. |
|---|
| 46 | typedef VectorMap Map; |
|---|
| 47 | /// The MapBase of the Map which implements the core regisitry function. |
|---|
| 48 | typedef typename MapRegistry::MapBase MapBase; |
|---|
| 49 | |
|---|
| 50 | private: |
|---|
| 51 | |
|---|
| 52 | /// The container type of the map. |
|---|
| 53 | typedef std::vector<Value> Container; |
|---|
| 54 | |
|---|
| 55 | public: |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | /// The value type of the map. |
|---|
| 59 | typedef Value ValueType; |
|---|
| 60 | /// The reference type of the map; |
|---|
| 61 | typedef typename Container::reference ReferenceType; |
|---|
| 62 | /// The pointer type of the map; |
|---|
| 63 | typedef typename Container::pointer PointerType; |
|---|
| 64 | |
|---|
| 65 | /// The const value type of the map. |
|---|
| 66 | typedef const Value ConstValueType; |
|---|
| 67 | /// The const reference type of the map; |
|---|
| 68 | typedef typename Container::const_reference ConstReferenceType; |
|---|
| 69 | /// The pointer type of the map; |
|---|
| 70 | typedef typename Container::const_pointer ConstPointerType; |
|---|
| 71 | |
|---|
| 72 | /** Graph and Registry initialized map constructor. |
|---|
| 73 | */ |
|---|
| [844] | 74 | VectorMap(const Graph& g, MapRegistry& r) |
|---|
| 75 | : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {} |
|---|
| [822] | 76 | |
|---|
| 77 | /** Constructor to use default value to initialize the map. |
|---|
| 78 | */ |
|---|
| 79 | VectorMap(const Graph& g, MapRegistry& r, const Value& v) |
|---|
| [844] | 80 | : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {} |
|---|
| [822] | 81 | |
|---|
| [891] | 82 | /** Assign operator to copy a map of an other map type. |
|---|
| [822] | 83 | */ |
|---|
| [891] | 84 | template <typename TT> |
|---|
| 85 | VectorMap(const VectorMap<MapRegistry, TT>& c) |
|---|
| 86 | : MapBase(c), container(c.container.size()) { |
|---|
| 87 | for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { |
|---|
| 88 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it); |
|---|
| 89 | container[id] = c.container[id]; |
|---|
| [822] | 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| [891] | 93 | /** Assign operator to copy a map of an other map type. |
|---|
| [822] | 94 | */ |
|---|
| [891] | 95 | template <typename TT> |
|---|
| 96 | VectorMap& operator=(const VectorMap<MapRegistry, TT>& c) { |
|---|
| [897] | 97 | if (MapBase::getGraph() != c.getGraph()) { |
|---|
| 98 | MapBase::operator=(c); |
|---|
| 99 | container.resize(c.container.size()); |
|---|
| 100 | } |
|---|
| [891] | 101 | for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) { |
|---|
| 102 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it); |
|---|
| 103 | container[id] = c.container[id]; |
|---|
| [822] | 104 | } |
|---|
| 105 | return *this; |
|---|
| 106 | } |
|---|
| 107 | /** |
|---|
| 108 | * The subscript operator. The map can be subscripted by the |
|---|
| 109 | * actual keys of the graph. |
|---|
| 110 | */ |
|---|
| 111 | ReferenceType operator[](const KeyType& key) { |
|---|
| [844] | 112 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); |
|---|
| [822] | 113 | return container[id]; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * The const subscript operator. The map can be subscripted by the |
|---|
| 118 | * actual keys of the graph. |
|---|
| 119 | */ |
|---|
| 120 | ConstReferenceType operator[](const KeyType& key) const { |
|---|
| [844] | 121 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); |
|---|
| [822] | 122 | return container[id]; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | /** Setter function of the map. Equivalent with map[key] = val. |
|---|
| 126 | * This is a compatibility feature with the not dereferable maps. |
|---|
| 127 | */ |
|---|
| 128 | void set(const KeyType& key, const ValueType& val) { |
|---|
| [844] | 129 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); |
|---|
| [822] | 130 | container[id] = val; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /** Add a new key to the map. It called by the map registry. |
|---|
| 134 | */ |
|---|
| 135 | void add(const KeyType& key) { |
|---|
| [844] | 136 | int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key); |
|---|
| [822] | 137 | if (id >= (int)container.size()) { |
|---|
| 138 | container.resize(id + 1); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /** Erase a key from the map. It called by the map registry. |
|---|
| 143 | */ |
|---|
| 144 | void erase(const KeyType& key) {} |
|---|
| 145 | |
|---|
| 146 | /** Clear the data structure. |
|---|
| 147 | */ |
|---|
| 148 | void clear() { |
|---|
| 149 | container.clear(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /// The stl compatible pair iterator of the map. |
|---|
| 153 | typedef MapIterator<VectorMap> Iterator; |
|---|
| 154 | /// The stl compatible const pair iterator of the map. |
|---|
| 155 | typedef MapConstIterator<VectorMap> ConstIterator; |
|---|
| 156 | |
|---|
| 157 | /** Returns the begin iterator of the map. |
|---|
| 158 | */ |
|---|
| 159 | Iterator begin() { |
|---|
| 160 | return Iterator(*this, KeyIt(*MapBase::getGraph())); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | /** Returns the end iterator of the map. |
|---|
| 164 | */ |
|---|
| 165 | Iterator end() { |
|---|
| 166 | return Iterator(*this, INVALID); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** Returns the begin ConstIterator of the map. |
|---|
| 170 | */ |
|---|
| 171 | ConstIterator begin() const { |
|---|
| 172 | return ConstIterator(*this, KeyIt(*MapBase::getGraph())); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | /** Returns the end const_iterator of the map. |
|---|
| 176 | */ |
|---|
| 177 | ConstIterator end() const { |
|---|
| 178 | return ConstIterator(*this, INVALID); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| [830] | 181 | /// The KeySet of the Map. |
|---|
| 182 | typedef MapConstKeySet<VectorMap> ConstKeySet; |
|---|
| 183 | |
|---|
| 184 | /// KeySet getter function. |
|---|
| 185 | ConstKeySet keySet() const { |
|---|
| 186 | return ConstKeySet(*this); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /// The ConstValueSet of the Map. |
|---|
| 190 | typedef MapConstValueSet<VectorMap> ConstValueSet; |
|---|
| 191 | |
|---|
| 192 | /// ConstValueSet getter function. |
|---|
| 193 | ConstValueSet valueSet() const { |
|---|
| 194 | return ConstValueSet(*this); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /// The ValueSet of the Map. |
|---|
| 198 | typedef MapValueSet<VectorMap> ValueSet; |
|---|
| 199 | |
|---|
| 200 | /// ValueSet getter function. |
|---|
| 201 | ValueSet valueSet() { |
|---|
| 202 | return ValueSet(*this); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | |
|---|
| [822] | 206 | private: |
|---|
| 207 | |
|---|
| 208 | Container container; |
|---|
| 209 | |
|---|
| [844] | 210 | public: |
|---|
| 211 | // STL compatibility typedefs. |
|---|
| 212 | typedef Iterator iterator; |
|---|
| 213 | typedef ConstIterator const_iterator; |
|---|
| 214 | typedef typename Iterator::PairValueType value_type; |
|---|
| 215 | typedef typename Iterator::KeyType key_type; |
|---|
| 216 | typedef typename Iterator::ValueType data_type; |
|---|
| 217 | typedef typename Iterator::PairReferenceType reference; |
|---|
| 218 | typedef typename Iterator::PairPointerType pointer; |
|---|
| 219 | typedef typename ConstIterator::PairReferenceType const_reference; |
|---|
| 220 | typedef typename ConstIterator::PairPointerType const_pointer; |
|---|
| 221 | typedef int difference_type; |
|---|
| [822] | 222 | }; |
|---|
| 223 | |
|---|
| 224 | /// @} |
|---|
| 225 | |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | #endif |
|---|