src/hugo/vector_map.h
author marci
Tue, 21 Sep 2004 21:28:43 +0000
changeset 894 68a18cd0505c
parent 844 9bf990cb066d
child 897 ef09eee53b09
permissions -rw-r--r--
todo for real comparison
deba@822
     1
// -*- c++ -*-
deba@822
     2
#ifndef VECTOR_MAP_H
deba@822
     3
#define VECTOR_MAP_H
deba@822
     4
deba@822
     5
#include <vector>
deba@822
     6
deba@822
     7
#include <hugo/map_iterator.h>
deba@844
     8
#include <hugo/map_bits.h>
deba@822
     9
deba@822
    10
///\ingroup graphmaps
deba@822
    11
///\file
deba@822
    12
///\brief Vector based graph maps.
deba@822
    13
deba@822
    14
namespace hugo {
deba@822
    15
  
deba@822
    16
  /// \addtogroup graphmaps
deba@822
    17
  /// @{
deba@822
    18
  
deba@822
    19
  /** The ArrayMap template class is graph map structure what
deba@822
    20
   *  automatically updates the map when a key is added to or erased from
deba@822
    21
   *  the map. This map factory uses the allocators to implement 
deba@822
    22
   *  the container functionality. This map factory
deba@822
    23
   *  uses the std::vector to implement the container function.
deba@822
    24
   *
deba@822
    25
   *  The template parameter is the MapRegistry that the maps
deba@822
    26
   *  will belong to and the ValueType.
deba@822
    27
   * 
deba@822
    28
   * \todo It should use a faster initialization using the maxNodeId() or
deba@822
    29
   * maxEdgeId() function of the graph instead of iterating through each
deba@822
    30
   * edge/node.
deba@822
    31
   */
deba@822
    32
	
deba@822
    33
  template <typename MapRegistry, typename Value>
deba@822
    34
  class VectorMap : public MapRegistry::MapBase {
deba@891
    35
    template <typename MR, typename T> friend class VectorMap;
deba@822
    36
  public:
deba@822
    37
		
deba@822
    38
    /// The graph type of the maps. 
deba@822
    39
    typedef typename MapRegistry::Graph Graph;
deba@822
    40
    /// The key type of the maps.
deba@822
    41
    typedef typename MapRegistry::KeyType KeyType;
deba@822
    42
    /// The iterator to iterate on the keys.
deba@822
    43
    typedef typename MapRegistry::KeyIt KeyIt;
deba@822
    44
deba@822
    45
    /// The map type.
deba@822
    46
    typedef VectorMap Map;
deba@822
    47
    /// The MapBase of the Map which implements the core regisitry function.
deba@822
    48
    typedef typename MapRegistry::MapBase MapBase;
deba@822
    49
deba@822
    50
  private:
deba@822
    51
		
deba@822
    52
    /// The container type of the map.
deba@822
    53
    typedef std::vector<Value> Container;	
deba@822
    54
deba@822
    55
  public:
deba@822
    56
deba@822
    57
deba@822
    58
    /// The value type of the map.
deba@822
    59
    typedef Value ValueType;
deba@822
    60
    /// The reference type of the map;
deba@822
    61
    typedef typename Container::reference ReferenceType;
deba@822
    62
    /// The pointer type of the map;
deba@822
    63
    typedef typename Container::pointer PointerType;
deba@822
    64
deba@822
    65
    /// The const value type of the map.
deba@822
    66
    typedef const Value ConstValueType;
deba@822
    67
    /// The const reference type of the map;
deba@822
    68
    typedef typename Container::const_reference ConstReferenceType;
deba@822
    69
    /// The pointer type of the map;
deba@822
    70
    typedef typename Container::const_pointer ConstPointerType;
deba@822
    71
deba@822
    72
    /** Default constructor for the map.
deba@822
    73
     */
deba@822
    74
    VectorMap() {}
deba@822
    75
		
deba@822
    76
    /** Graph and Registry initialized map constructor.
deba@822
    77
     */
deba@844
    78
    VectorMap(const Graph& g, MapRegistry& r) 
deba@844
    79
      : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {}
deba@822
    80
deba@822
    81
    /** Constructor to use default value to initialize the map. 
deba@822
    82
     */
deba@822
    83
    VectorMap(const Graph& g, MapRegistry& r, const Value& v) 
deba@844
    84
      : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {}
deba@822
    85
deba@891
    86
    /** Assign operator to copy a map of an other map type.
deba@822
    87
     */
deba@891
    88
    template <typename TT>
deba@891
    89
    VectorMap(const VectorMap<MapRegistry, TT>& c) 
deba@891
    90
      : MapBase(c), container(c.container.size()) {
deba@891
    91
      for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@891
    92
	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@891
    93
	container[id] = c.container[id];
deba@822
    94
      }
deba@822
    95
    }
deba@822
    96
deba@891
    97
    /** Assign operator to copy a map of an other map type.
deba@822
    98
     */
deba@891
    99
    template <typename TT>
deba@891
   100
    VectorMap& operator=(const VectorMap<MapRegistry, TT>& c) {
deba@891
   101
      container.resize(c.container.size());
deba@891
   102
      MapBase::operator=(c);
deba@891
   103
      for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@891
   104
	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@891
   105
	container[id] = c.container[id];
deba@822
   106
      }
deba@822
   107
      return *this;
deba@822
   108
    }
deba@822
   109
    /**
deba@822
   110
     * The subscript operator. The map can be subscripted by the
deba@822
   111
     * actual keys of the graph. 
deba@822
   112
     */
deba@822
   113
    ReferenceType operator[](const KeyType& key) {
deba@844
   114
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   115
      return container[id];
deba@822
   116
    } 
deba@822
   117
		
deba@822
   118
    /**
deba@822
   119
     * The const subscript operator. The map can be subscripted by the
deba@822
   120
     * actual keys of the graph. 
deba@822
   121
     */
deba@822
   122
    ConstReferenceType operator[](const KeyType& key) const {
deba@844
   123
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   124
      return container[id];
deba@822
   125
    }
deba@822
   126
deba@822
   127
    /** Setter function of the map. Equivalent with map[key] = val.
deba@822
   128
     *  This is a compatibility feature with the not dereferable maps.
deba@822
   129
     */
deba@822
   130
    void set(const KeyType& key, const ValueType& val) {
deba@844
   131
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   132
      container[id] = val;
deba@822
   133
    }
deba@822
   134
		
deba@822
   135
    /** Add a new key to the map. It called by the map registry.
deba@822
   136
     */
deba@822
   137
    void add(const KeyType& key) {
deba@844
   138
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   139
      if (id >= (int)container.size()) {
deba@822
   140
	container.resize(id + 1);
deba@822
   141
      }
deba@822
   142
    }
deba@822
   143
		
deba@822
   144
    /** Erase a key from the map. It called by the map registry.
deba@822
   145
     */
deba@822
   146
    void erase(const KeyType& key) {}
deba@822
   147
deba@822
   148
    /** Clear the data structure.
deba@822
   149
     */
deba@822
   150
    void clear() { 
deba@822
   151
      container.clear();
deba@822
   152
    }
deba@822
   153
deba@822
   154
    /// The stl compatible pair iterator of the map.
deba@822
   155
    typedef MapIterator<VectorMap> Iterator;
deba@822
   156
    /// The stl compatible const pair iterator of the map.
deba@822
   157
    typedef MapConstIterator<VectorMap> ConstIterator;
deba@822
   158
deba@822
   159
    /** Returns the begin iterator of the map.
deba@822
   160
     */
deba@822
   161
    Iterator begin() {
deba@822
   162
      return Iterator(*this, KeyIt(*MapBase::getGraph()));
deba@822
   163
    }
deba@822
   164
deba@822
   165
    /** Returns the end iterator of the map.
deba@822
   166
     */
deba@822
   167
    Iterator end() {
deba@822
   168
      return Iterator(*this, INVALID);
deba@822
   169
    }
deba@822
   170
deba@822
   171
    /** Returns the begin ConstIterator of the map.
deba@822
   172
     */
deba@822
   173
    ConstIterator begin() const {
deba@822
   174
      return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
deba@822
   175
    }
deba@822
   176
deba@822
   177
    /** Returns the end const_iterator of the map.
deba@822
   178
     */
deba@822
   179
    ConstIterator end() const {
deba@822
   180
      return ConstIterator(*this, INVALID);
deba@822
   181
    }
deba@822
   182
deba@830
   183
    /// The KeySet of the Map.
deba@830
   184
    typedef MapConstKeySet<VectorMap> ConstKeySet;
deba@830
   185
deba@830
   186
    /// KeySet getter function.
deba@830
   187
    ConstKeySet keySet() const {
deba@830
   188
      return ConstKeySet(*this); 
deba@830
   189
    }
deba@830
   190
deba@830
   191
    /// The ConstValueSet of the Map.
deba@830
   192
    typedef MapConstValueSet<VectorMap> ConstValueSet;
deba@830
   193
deba@830
   194
    /// ConstValueSet getter function.
deba@830
   195
    ConstValueSet valueSet() const {
deba@830
   196
      return ConstValueSet(*this);
deba@830
   197
    }
deba@830
   198
deba@830
   199
    /// The ValueSet of the Map.
deba@830
   200
    typedef MapValueSet<VectorMap> ValueSet;
deba@830
   201
deba@830
   202
    /// ValueSet getter function.
deba@830
   203
    ValueSet valueSet() {
deba@830
   204
      return ValueSet(*this);
deba@830
   205
    }
deba@830
   206
deba@830
   207
deba@822
   208
  private:
deba@822
   209
		
deba@822
   210
    Container container;
deba@822
   211
deba@844
   212
  public:
deba@844
   213
    // STL  compatibility typedefs.
deba@844
   214
    typedef Iterator iterator;
deba@844
   215
    typedef ConstIterator const_iterator;
deba@844
   216
    typedef typename Iterator::PairValueType value_type;
deba@844
   217
    typedef typename Iterator::KeyType key_type;
deba@844
   218
    typedef typename Iterator::ValueType data_type;
deba@844
   219
    typedef typename Iterator::PairReferenceType reference;
deba@844
   220
    typedef typename Iterator::PairPointerType pointer;
deba@844
   221
    typedef typename ConstIterator::PairReferenceType const_reference;
deba@844
   222
    typedef typename ConstIterator::PairPointerType const_pointer;
deba@844
   223
    typedef int difference_type;		
deba@822
   224
  };
deba@822
   225
  
deba@822
   226
  /// @}
deba@822
   227
  
deba@822
   228
}
deba@822
   229
deba@822
   230
#endif