src/hugo/vector_map.h
author alpar
Wed, 22 Sep 2004 09:58:17 +0000
changeset 900 fc7bc2dacee5
parent 891 74589d20dbc3
child 901 69a8e672acb1
permissions -rw-r--r--
'iff' changed to 'if and only if'
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
    /** Graph and Registry initialized map constructor.
deba@822
    73
     */
deba@844
    74
    VectorMap(const Graph& g, MapRegistry& r) 
deba@844
    75
      : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {}
deba@822
    76
deba@822
    77
    /** Constructor to use default value to initialize the map. 
deba@822
    78
     */
deba@822
    79
    VectorMap(const Graph& g, MapRegistry& r, const Value& v) 
deba@844
    80
      : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {}
deba@822
    81
deba@891
    82
    /** Assign operator to copy a map of an other map type.
deba@822
    83
     */
deba@891
    84
    template <typename TT>
deba@891
    85
    VectorMap(const VectorMap<MapRegistry, TT>& c) 
deba@891
    86
      : MapBase(c), container(c.container.size()) {
deba@891
    87
      for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@891
    88
	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@891
    89
	container[id] = c.container[id];
deba@822
    90
      }
deba@822
    91
    }
deba@822
    92
deba@891
    93
    /** Assign operator to copy a map of an other map type.
deba@822
    94
     */
deba@891
    95
    template <typename TT>
deba@891
    96
    VectorMap& operator=(const VectorMap<MapRegistry, TT>& c) {
deba@897
    97
      if (MapBase::getGraph() != c.getGraph()) {
deba@897
    98
	MapBase::operator=(c);
deba@897
    99
	container.resize(c.container.size());
deba@897
   100
      }
deba@891
   101
      for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@891
   102
	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@891
   103
	container[id] = c.container[id];
deba@822
   104
      }
deba@822
   105
      return *this;
deba@822
   106
    }
deba@822
   107
    /**
deba@822
   108
     * The subscript operator. The map can be subscripted by the
deba@822
   109
     * actual keys of the graph. 
deba@822
   110
     */
deba@822
   111
    ReferenceType operator[](const KeyType& key) {
deba@844
   112
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   113
      return container[id];
deba@822
   114
    } 
deba@822
   115
		
deba@822
   116
    /**
deba@822
   117
     * The const subscript operator. The map can be subscripted by the
deba@822
   118
     * actual keys of the graph. 
deba@822
   119
     */
deba@822
   120
    ConstReferenceType operator[](const KeyType& key) const {
deba@844
   121
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   122
      return container[id];
deba@822
   123
    }
deba@822
   124
deba@822
   125
    /** Setter function of the map. Equivalent with map[key] = val.
deba@822
   126
     *  This is a compatibility feature with the not dereferable maps.
deba@822
   127
     */
deba@822
   128
    void set(const KeyType& key, const ValueType& val) {
deba@844
   129
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   130
      container[id] = val;
deba@822
   131
    }
deba@822
   132
		
deba@822
   133
    /** Add a new key to the map. It called by the map registry.
deba@822
   134
     */
deba@822
   135
    void add(const KeyType& key) {
deba@844
   136
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   137
      if (id >= (int)container.size()) {
deba@822
   138
	container.resize(id + 1);
deba@822
   139
      }
deba@822
   140
    }
deba@822
   141
		
deba@822
   142
    /** Erase a key from the map. It called by the map registry.
deba@822
   143
     */
deba@822
   144
    void erase(const KeyType& key) {}
deba@822
   145
deba@822
   146
    /** Clear the data structure.
deba@822
   147
     */
deba@822
   148
    void clear() { 
deba@822
   149
      container.clear();
deba@822
   150
    }
deba@822
   151
deba@822
   152
    /// The stl compatible pair iterator of the map.
deba@822
   153
    typedef MapIterator<VectorMap> Iterator;
deba@822
   154
    /// The stl compatible const pair iterator of the map.
deba@822
   155
    typedef MapConstIterator<VectorMap> ConstIterator;
deba@822
   156
deba@822
   157
    /** Returns the begin iterator of the map.
deba@822
   158
     */
deba@822
   159
    Iterator begin() {
deba@822
   160
      return Iterator(*this, KeyIt(*MapBase::getGraph()));
deba@822
   161
    }
deba@822
   162
deba@822
   163
    /** Returns the end iterator of the map.
deba@822
   164
     */
deba@822
   165
    Iterator end() {
deba@822
   166
      return Iterator(*this, INVALID);
deba@822
   167
    }
deba@822
   168
deba@822
   169
    /** Returns the begin ConstIterator of the map.
deba@822
   170
     */
deba@822
   171
    ConstIterator begin() const {
deba@822
   172
      return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
deba@822
   173
    }
deba@822
   174
deba@822
   175
    /** Returns the end const_iterator of the map.
deba@822
   176
     */
deba@822
   177
    ConstIterator end() const {
deba@822
   178
      return ConstIterator(*this, INVALID);
deba@822
   179
    }
deba@822
   180
deba@830
   181
    /// The KeySet of the Map.
deba@830
   182
    typedef MapConstKeySet<VectorMap> ConstKeySet;
deba@830
   183
deba@830
   184
    /// KeySet getter function.
deba@830
   185
    ConstKeySet keySet() const {
deba@830
   186
      return ConstKeySet(*this); 
deba@830
   187
    }
deba@830
   188
deba@830
   189
    /// The ConstValueSet of the Map.
deba@830
   190
    typedef MapConstValueSet<VectorMap> ConstValueSet;
deba@830
   191
deba@830
   192
    /// ConstValueSet getter function.
deba@830
   193
    ConstValueSet valueSet() const {
deba@830
   194
      return ConstValueSet(*this);
deba@830
   195
    }
deba@830
   196
deba@830
   197
    /// The ValueSet of the Map.
deba@830
   198
    typedef MapValueSet<VectorMap> ValueSet;
deba@830
   199
deba@830
   200
    /// ValueSet getter function.
deba@830
   201
    ValueSet valueSet() {
deba@830
   202
      return ValueSet(*this);
deba@830
   203
    }
deba@830
   204
deba@830
   205
deba@822
   206
  private:
deba@822
   207
		
deba@822
   208
    Container container;
deba@822
   209
deba@844
   210
  public:
deba@844
   211
    // STL  compatibility typedefs.
deba@844
   212
    typedef Iterator iterator;
deba@844
   213
    typedef ConstIterator const_iterator;
deba@844
   214
    typedef typename Iterator::PairValueType value_type;
deba@844
   215
    typedef typename Iterator::KeyType key_type;
deba@844
   216
    typedef typename Iterator::ValueType data_type;
deba@844
   217
    typedef typename Iterator::PairReferenceType reference;
deba@844
   218
    typedef typename Iterator::PairPointerType pointer;
deba@844
   219
    typedef typename ConstIterator::PairReferenceType const_reference;
deba@844
   220
    typedef typename ConstIterator::PairPointerType const_pointer;
deba@844
   221
    typedef int difference_type;		
deba@822
   222
  };
deba@822
   223
  
deba@822
   224
  /// @}
deba@822
   225
  
deba@822
   226
}
deba@822
   227
deba@822
   228
#endif