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