COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/vector_map_factory.h @ 803:c3d832275e69

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