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