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