| 1 | // -*- c++ -*- |
|---|
| 2 | #ifndef VECTOR_MAP_H |
|---|
| 3 | #define VECTOR_MAP_H |
|---|
| 4 | |
|---|
| 5 | #include <vector> |
|---|
| 6 | |
|---|
| 7 | #include <hugo/extended_pair.h> |
|---|
| 8 | |
|---|
| 9 | ///\ingroup graphmapfactory |
|---|
| 10 | ///\file |
|---|
| 11 | ///\brief Vector based graph maps. |
|---|
| 12 | |
|---|
| 13 | namespace hugo { |
|---|
| 14 | |
|---|
| 15 | /// \addtogroup graphmapfactory |
|---|
| 16 | /// @{ |
|---|
| 17 | |
|---|
| 18 | /** The VectorMapFactory template class is a factory class |
|---|
| 19 | * to create maps for the edge and nodes. This map factory |
|---|
| 20 | * uses the std::vector to implement the container function. |
|---|
| 21 | * |
|---|
| 22 | * The template parameter is the MapRegistry that the maps |
|---|
| 23 | * will belong to. |
|---|
| 24 | * |
|---|
| 25 | * \todo It should use a faster initialization using the maxNodeId() or |
|---|
| 26 | * maxEdgeId() function of the graph istead of iterating through each |
|---|
| 27 | * edge/node. |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | template <typename MapRegistry> |
|---|
| 31 | class VectorMapFactory { |
|---|
| 32 | public: |
|---|
| 33 | |
|---|
| 34 | /// The graph type of the maps. |
|---|
| 35 | typedef typename MapRegistry::Graph Graph; |
|---|
| 36 | /// The key type of the maps. |
|---|
| 37 | typedef typename MapRegistry::KeyType KeyType; |
|---|
| 38 | /// The iterator to iterate on the keys. |
|---|
| 39 | typedef typename MapRegistry::KeyIt KeyIt; |
|---|
| 40 | |
|---|
| 41 | /// The MapBase of the Map which imlements the core regisitry function. |
|---|
| 42 | typedef typename MapRegistry::MapBase MapBase; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /** The template Map type. |
|---|
| 46 | */ |
|---|
| 47 | template <typename V> |
|---|
| 48 | class Map : public MapBase { |
|---|
| 49 | |
|---|
| 50 | typedef std::vector<V> Container; |
|---|
| 51 | |
|---|
| 52 | public: |
|---|
| 53 | |
|---|
| 54 | /// The value type of the map. |
|---|
| 55 | typedef V ValueType; |
|---|
| 56 | |
|---|
| 57 | /// The value type of the map. |
|---|
| 58 | typedef V Value; |
|---|
| 59 | /// The reference type of the map; |
|---|
| 60 | typedef typename Container::reference Reference; |
|---|
| 61 | /// The pointer type of the map; |
|---|
| 62 | typedef typename Container::pointer Pointer; |
|---|
| 63 | |
|---|
| 64 | /// The const value type of the map. |
|---|
| 65 | typedef const Value ConstValue; |
|---|
| 66 | /// The const reference type of the map; |
|---|
| 67 | typedef typename Container::const_reference ConstReference; |
|---|
| 68 | /// The pointer type of the map; |
|---|
| 69 | typedef typename Container::const_pointer ConstPointer; |
|---|
| 70 | |
|---|
| 71 | /** Default constructor for the map. |
|---|
| 72 | */ |
|---|
| 73 | Map() {} |
|---|
| 74 | |
|---|
| 75 | /** Graph and Registry initialized map constructor. |
|---|
| 76 | */ |
|---|
| 77 | Map(const Graph& g, MapRegistry& r) : MapBase(g, r) { |
|---|
| 78 | init(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | /** Constructor to use default value to initialize the map. |
|---|
| 82 | */ |
|---|
| 83 | Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) { |
|---|
| 84 | for (KeyIt it(*getGraph()); it != INVALID; ++it) { |
|---|
| 85 | int id = getGraph()->id(it); |
|---|
| 86 | if (id >= (int)container.size()) { |
|---|
| 87 | container.resize(id + 1); |
|---|
| 88 | } |
|---|
| 89 | set(it, v); |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /** Constructor to copy a map of an other map type. |
|---|
| 94 | */ |
|---|
| 95 | template <typename CMap> Map(const CMap& copy) : MapBase(copy) { |
|---|
| 96 | if (getGraph()) { |
|---|
| 97 | for (KeyIt it(*getGraph()); it != INVALID; ++it) { |
|---|
| 98 | int id = getGraph()->id(it); |
|---|
| 99 | if (id >= (int)container.size()) { |
|---|
| 100 | container.resize(id + 1); |
|---|
| 101 | } |
|---|
| 102 | set(it, copy[it]); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** Assign operator to copy a map an other map type. |
|---|
| 108 | */ |
|---|
| 109 | template <typename CMap> Map& operator=(const CMap& copy) { |
|---|
| 110 | if (getGraph()) { |
|---|
| 111 | destroy(); |
|---|
| 112 | } |
|---|
| 113 | this->MapBase::operator=(copy); |
|---|
| 114 | if (getGraph()) { |
|---|
| 115 | for (KeyIt it(*getGraph()); it != INVALID; ++it) { |
|---|
| 116 | int id = getGraph()->id(it); |
|---|
| 117 | if (id >= (int)container.size()) { |
|---|
| 118 | container.resize(id + 1); |
|---|
| 119 | } |
|---|
| 120 | set(it, copy[it]); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | return *this; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** The destructor of the map. |
|---|
| 127 | */ |
|---|
| 128 | virtual ~Map() { |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * The subscript operator. The map can be subscripted by the |
|---|
| 133 | * actual keys of the graph. |
|---|
| 134 | */ |
|---|
| 135 | Reference operator[](const KeyType& key) { |
|---|
| 136 | int id = getGraph()->id(key); |
|---|
| 137 | return container[id]; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | * The const subscript operator. The map can be subscripted by the |
|---|
| 142 | * actual keys of the graph. |
|---|
| 143 | */ |
|---|
| 144 | ConstReference operator[](const KeyType& key) const { |
|---|
| 145 | int id = getGraph()->id(key); |
|---|
| 146 | return container[id]; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** Setter function of the map. Equivalent with map[key] = val. |
|---|
| 150 | * This is a compatibility feature with the not dereferable maps. |
|---|
| 151 | */ |
|---|
| 152 | void set(const KeyType& key, const Value& val) { |
|---|
| 153 | int id = getGraph()->id(key); |
|---|
| 154 | container[id] = val; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /** Add a new key to the map. It called by the map registry. |
|---|
| 158 | */ |
|---|
| 159 | void add(const KeyType& key) { |
|---|
| 160 | int id = getGraph()->id(key); |
|---|
| 161 | if (id >= (int)container.size()) { |
|---|
| 162 | container.resize(id + 1); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | /** Erase a key from the map. It called by the map registry. |
|---|
| 167 | */ |
|---|
| 168 | void erase(const KeyType& key) {} |
|---|
| 169 | |
|---|
| 170 | /** Clear the data structure. |
|---|
| 171 | */ |
|---|
| 172 | void clear() { |
|---|
| 173 | container.clear(); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | /** Compatible iterator with the stl maps' iterators. |
|---|
| 177 | * It iterates on pairs of a key and a value. |
|---|
| 178 | */ |
|---|
| 179 | class iterator { |
|---|
| 180 | friend class Map; |
|---|
| 181 | friend class const_iterator; |
|---|
| 182 | private: |
|---|
| 183 | |
|---|
| 184 | /** Private constructor to initalize the the iterators returned |
|---|
| 185 | * by the begin() and end(). |
|---|
| 186 | */ |
|---|
| 187 | iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {} |
|---|
| 188 | |
|---|
| 189 | public: |
|---|
| 190 | |
|---|
| 191 | /** Default constructor. |
|---|
| 192 | */ |
|---|
| 193 | iterator() {} |
|---|
| 194 | |
|---|
| 195 | typedef extended_pair<const KeyType&, const KeyType&, |
|---|
| 196 | Map::Reference, Map::Reference> Reference; |
|---|
| 197 | |
|---|
| 198 | /** Dereference operator for map. |
|---|
| 199 | */ |
|---|
| 200 | Reference operator*() { |
|---|
| 201 | return Reference(it, (*map)[it]); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | class Pointer { |
|---|
| 205 | friend class iterator; |
|---|
| 206 | private: |
|---|
| 207 | Reference data; |
|---|
| 208 | Pointer(const KeyType& key, Map::Reference val) : data(key, val) {} |
|---|
| 209 | public: |
|---|
| 210 | Reference* operator->() {return &data;} |
|---|
| 211 | }; |
|---|
| 212 | |
|---|
| 213 | /** Arrow operator for map. |
|---|
| 214 | */ |
|---|
| 215 | Pointer operator->() { |
|---|
| 216 | return Pointer(it, ((*map)[it])); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /** The pre increment operator of the map. |
|---|
| 220 | */ |
|---|
| 221 | iterator& operator++() { |
|---|
| 222 | ++it; |
|---|
| 223 | return *this; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | /** The post increment operator of the map. |
|---|
| 227 | */ |
|---|
| 228 | iterator operator++(int) { |
|---|
| 229 | iterator tmp(it); |
|---|
| 230 | ++it; |
|---|
| 231 | return tmp; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | /** The equality operator of the map. |
|---|
| 235 | */ |
|---|
| 236 | bool operator==(const_iterator p_it) { |
|---|
| 237 | return p_it.it == it; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | /** The not-equality operator of the map. |
|---|
| 241 | */ |
|---|
| 242 | bool operator!=(const_iterator p_it) { |
|---|
| 243 | return !(*this == p_it); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | private: |
|---|
| 248 | Map* map; |
|---|
| 249 | KeyIt it; |
|---|
| 250 | }; |
|---|
| 251 | |
|---|
| 252 | /** Returns the begin iterator of the map. |
|---|
| 253 | */ |
|---|
| 254 | iterator begin() { |
|---|
| 255 | return iterator(*this, KeyIt(*getGraph())); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | /** Returns the end iterator of the map. |
|---|
| 259 | */ |
|---|
| 260 | iterator end() { |
|---|
| 261 | return iterator(*this, INVALID); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | class const_iterator { |
|---|
| 265 | friend class Map; |
|---|
| 266 | friend class iterator; |
|---|
| 267 | private: |
|---|
| 268 | |
|---|
| 269 | /** Private constructor to initalize the the iterators returned |
|---|
| 270 | * by the begin() and end(). |
|---|
| 271 | */ |
|---|
| 272 | const_iterator (const Map& pmap, const KeyIt& pit) |
|---|
| 273 | : map(&pmap), it(pit) {} |
|---|
| 274 | |
|---|
| 275 | public: |
|---|
| 276 | |
|---|
| 277 | /** Default constructor. |
|---|
| 278 | */ |
|---|
| 279 | const_iterator() {} |
|---|
| 280 | |
|---|
| 281 | /** Constructor to convert iterator to const_iterator. |
|---|
| 282 | */ |
|---|
| 283 | const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {} |
|---|
| 284 | |
|---|
| 285 | typedef extended_pair<const KeyType&, const KeyType&, |
|---|
| 286 | Map::ConstReference, Map::ConstReference> Reference; |
|---|
| 287 | |
|---|
| 288 | /** Dereference operator for map. |
|---|
| 289 | */ |
|---|
| 290 | Reference operator*() { |
|---|
| 291 | return Reference(it, (*map)[it]); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | class Pointer { |
|---|
| 296 | friend class const_iterator; |
|---|
| 297 | private: |
|---|
| 298 | Reference data; |
|---|
| 299 | Pointer(const KeyType& key, Map::ConstReference val) |
|---|
| 300 | : data(key, val) {} |
|---|
| 301 | public: |
|---|
| 302 | Reference* operator->() {return &data;} |
|---|
| 303 | }; |
|---|
| 304 | |
|---|
| 305 | /** Arrow operator for map. |
|---|
| 306 | */ |
|---|
| 307 | Pointer operator->() { |
|---|
| 308 | return Pointer(it, ((*map)[it])); |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | /** The pre increment operator of the map. |
|---|
| 312 | */ |
|---|
| 313 | const_iterator& operator++() { |
|---|
| 314 | ++it; |
|---|
| 315 | return *this; |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | /** The post increment operator of the map. |
|---|
| 319 | */ |
|---|
| 320 | const_iterator operator++(int) { |
|---|
| 321 | const_iterator tmp(it); |
|---|
| 322 | ++it; |
|---|
| 323 | return tmp; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | /** The equality operator of the map. |
|---|
| 327 | */ |
|---|
| 328 | bool operator==(const_iterator p_it) { |
|---|
| 329 | return p_it.it == it; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | /** The not-equality operator of the map. |
|---|
| 333 | */ |
|---|
| 334 | bool operator!=(const_iterator p_it) { |
|---|
| 335 | return !(*this == p_it); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | private: |
|---|
| 340 | const Map* map; |
|---|
| 341 | KeyIt it; |
|---|
| 342 | }; |
|---|
| 343 | |
|---|
| 344 | /** Returns the begin const_iterator of the map. |
|---|
| 345 | */ |
|---|
| 346 | const_iterator begin() const { |
|---|
| 347 | return const_iterator(*this, KeyIt(*getGraph())); |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | /** Returns the end const_iterator of the map. |
|---|
| 351 | */ |
|---|
| 352 | const_iterator end() const { |
|---|
| 353 | return const_iterator(*this, INVALID); |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | private: |
|---|
| 357 | |
|---|
| 358 | Container container; |
|---|
| 359 | |
|---|
| 360 | }; |
|---|
| 361 | |
|---|
| 362 | }; |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | /// @} |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | #endif |
|---|