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