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