src/lemon/vector_map.h
author ladanyi
Mon, 11 Oct 2004 18:02:48 +0000
changeset 942 75fdd0c6866d
parent 921 818510fa3d99
child 946 c94ef40a22ce
permissions -rw-r--r--
Naming and coding style fixes and various other changes.
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@937
    34
  /** The VectorMap 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@937
    40
   *  \param MapRegistry The MapRegistry that the maps will belong to.
deba@937
    41
   *  \param Value The value type of the map.
deba@822
    42
   * 
deba@937
    43
   *  \author Balazs Dezso
deba@822
    44
   */
deba@822
    45
	
deba@822
    46
  template <typename MapRegistry, typename Value>
deba@822
    47
  class VectorMap : public MapRegistry::MapBase {
deba@891
    48
    template <typename MR, typename T> friend class VectorMap;
deba@822
    49
  public:
deba@822
    50
		
deba@822
    51
    /// The graph type of the maps. 
deba@822
    52
    typedef typename MapRegistry::Graph Graph;
deba@822
    53
    /// The key type of the maps.
deba@822
    54
    typedef typename MapRegistry::KeyType KeyType;
deba@822
    55
    /// The iterator to iterate on the keys.
deba@822
    56
    typedef typename MapRegistry::KeyIt KeyIt;
deba@822
    57
deba@822
    58
    /// The map type.
deba@822
    59
    typedef VectorMap Map;
deba@822
    60
    /// The MapBase of the Map which implements the core regisitry function.
deba@822
    61
    typedef typename MapRegistry::MapBase MapBase;
deba@822
    62
deba@822
    63
  private:
deba@822
    64
		
deba@822
    65
    /// The container type of the map.
deba@822
    66
    typedef std::vector<Value> Container;	
deba@822
    67
deba@822
    68
  public:
deba@822
    69
deba@822
    70
deba@822
    71
    /// The value type of the map.
deba@822
    72
    typedef Value ValueType;
deba@822
    73
    /// The reference type of the map;
deba@822
    74
    typedef typename Container::reference ReferenceType;
deba@822
    75
    /// The pointer type of the map;
deba@822
    76
    typedef typename Container::pointer PointerType;
deba@822
    77
deba@822
    78
    /// The const value type of the map.
deba@822
    79
    typedef const Value ConstValueType;
deba@822
    80
    /// The const reference type of the map;
deba@822
    81
    typedef typename Container::const_reference ConstReferenceType;
deba@822
    82
    /// The pointer type of the map;
deba@822
    83
    typedef typename Container::const_pointer ConstPointerType;
deba@822
    84
deba@937
    85
    /// Constructor to attach the new map into a registry.
deba@937
    86
deba@937
    87
    /** Constructor to attach the new map into a registry.
deba@937
    88
     *  It adds all the nodes or edges of the graph to the map.
deba@822
    89
     */
deba@844
    90
    VectorMap(const Graph& g, MapRegistry& r) 
deba@844
    91
      : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1) {}
deba@822
    92
deba@937
    93
    /// Constructor uses given value to initialize the map. 
deba@937
    94
deba@937
    95
    /** Constructor uses given value to initialize the map. 
deba@937
    96
     *  It adds all the nodes or edges of the graph to the map.
deba@822
    97
     */
deba@822
    98
    VectorMap(const Graph& g, MapRegistry& r, const Value& v) 
deba@844
    99
      : MapBase(g, r), container(KeyInfo<Graph, KeyIt>::maxId(g)+1, v) {}
deba@822
   100
deba@937
   101
    /// Assign operator to copy a map of an other map type.
deba@937
   102
deba@891
   103
    /** Assign operator to copy a map of an other map type.
deba@937
   104
     *  This map's value type must be assignable by the other
deba@937
   105
     *  map type's value type.
deba@822
   106
     */
deba@891
   107
    template <typename TT>
deba@891
   108
    VectorMap(const VectorMap<MapRegistry, TT>& c) 
deba@891
   109
      : MapBase(c), container(c.container.size()) {
deba@891
   110
      for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@891
   111
	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@891
   112
	container[id] = c.container[id];
deba@822
   113
      }
deba@822
   114
    }
deba@822
   115
deba@937
   116
    /// Assign operator to copy a map of an other map type.
deba@937
   117
deba@891
   118
    /** Assign operator to copy a map of an other map type.
deba@937
   119
     *  This map's value type must be assignable by the other
deba@937
   120
     *  map type's value type.
deba@822
   121
     */
deba@891
   122
    template <typename TT>
deba@891
   123
    VectorMap& operator=(const VectorMap<MapRegistry, TT>& c) {
deba@897
   124
      if (MapBase::getGraph() != c.getGraph()) {
deba@897
   125
	MapBase::operator=(c);
deba@897
   126
	container.resize(c.container.size());
deba@897
   127
      }
deba@891
   128
      for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@891
   129
	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@891
   130
	container[id] = c.container[id];
deba@822
   131
      }
deba@822
   132
      return *this;
deba@822
   133
    }
deba@937
   134
deba@937
   135
    /// The subcript operator.
deba@937
   136
deba@822
   137
    /**
deba@822
   138
     * The subscript operator. The map can be subscripted by the
deba@822
   139
     * actual keys of the graph. 
deba@822
   140
     */
deba@822
   141
    ReferenceType operator[](const KeyType& key) {
deba@844
   142
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   143
      return container[id];
deba@822
   144
    } 
deba@822
   145
		
deba@937
   146
    /// The const subcript operator.
deba@937
   147
deba@822
   148
    /**
deba@822
   149
     * The const subscript operator. The map can be subscripted by the
deba@822
   150
     * actual keys of the graph. 
deba@822
   151
     */
deba@822
   152
    ConstReferenceType operator[](const KeyType& key) const {
deba@844
   153
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   154
      return container[id];
deba@822
   155
    }
deba@822
   156
deba@937
   157
    ///Setter function of the map.
deba@937
   158
deba@822
   159
    /** Setter function of the map. Equivalent with map[key] = val.
deba@822
   160
     *  This is a compatibility feature with the not dereferable maps.
deba@822
   161
     */
deba@822
   162
    void set(const KeyType& key, const ValueType& val) {
deba@844
   163
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   164
      container[id] = val;
deba@822
   165
    }
deba@937
   166
    /// Adds a new key to the map.
deba@822
   167
		
deba@937
   168
    /** Adds a new key to the map. It called by the map registry
deba@937
   169
     *  and it overrides the \ref MapRegistry::MapBase MapBase's
deba@937
   170
     *  add() member function.
deba@822
   171
     */
deba@822
   172
    void add(const KeyType& key) {
deba@844
   173
      int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822
   174
      if (id >= (int)container.size()) {
deba@822
   175
	container.resize(id + 1);
deba@822
   176
      }
deba@822
   177
    }
deba@937
   178
deba@937
   179
    /// Erases a key from the map.
deba@822
   180
		
deba@937
   181
    /** Erase a key from the map. It called by the map registry 
deba@937
   182
     *  and it overrides the \ref MapRegistry::MapBase MapBase's
deba@937
   183
     *  erase() member function.
deba@822
   184
     */
deba@822
   185
    void erase(const KeyType& key) {}
deba@822
   186
deba@937
   187
    /// Makes empty the map.
deba@937
   188
deba@937
   189
    /** Makes empty the map. It called by the map registry 
deba@937
   190
     *  and it overrides the \ref MapRegistry::MapBase MapBase's
deba@937
   191
     *  clear() member function.
deba@822
   192
     */
deba@937
   193
deba@822
   194
    void clear() { 
deba@822
   195
      container.clear();
deba@822
   196
    }
deba@822
   197
deba@822
   198
    /// The stl compatible pair iterator of the map.
deba@822
   199
    typedef MapIterator<VectorMap> Iterator;
deba@822
   200
    /// The stl compatible const pair iterator of the map.
deba@822
   201
    typedef MapConstIterator<VectorMap> ConstIterator;
deba@822
   202
deba@822
   203
    /** Returns the begin iterator of the map.
deba@822
   204
     */
deba@822
   205
    Iterator begin() {
deba@822
   206
      return Iterator(*this, KeyIt(*MapBase::getGraph()));
deba@822
   207
    }
deba@822
   208
deba@822
   209
    /** Returns the end iterator of the map.
deba@822
   210
     */
deba@822
   211
    Iterator end() {
deba@822
   212
      return Iterator(*this, INVALID);
deba@822
   213
    }
deba@822
   214
deba@822
   215
    /** Returns the begin ConstIterator of the map.
deba@822
   216
     */
deba@822
   217
    ConstIterator begin() const {
deba@822
   218
      return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
deba@822
   219
    }
deba@822
   220
deba@822
   221
    /** Returns the end const_iterator of the map.
deba@822
   222
     */
deba@822
   223
    ConstIterator end() const {
deba@822
   224
      return ConstIterator(*this, INVALID);
deba@822
   225
    }
deba@822
   226
deba@830
   227
    /// The KeySet of the Map.
deba@830
   228
    typedef MapConstKeySet<VectorMap> ConstKeySet;
deba@830
   229
deba@830
   230
    /// KeySet getter function.
deba@830
   231
    ConstKeySet keySet() const {
deba@830
   232
      return ConstKeySet(*this); 
deba@830
   233
    }
deba@830
   234
deba@830
   235
    /// The ConstValueSet of the Map.
deba@830
   236
    typedef MapConstValueSet<VectorMap> ConstValueSet;
deba@830
   237
deba@830
   238
    /// ConstValueSet getter function.
deba@830
   239
    ConstValueSet valueSet() const {
deba@830
   240
      return ConstValueSet(*this);
deba@830
   241
    }
deba@830
   242
deba@830
   243
    /// The ValueSet of the Map.
deba@830
   244
    typedef MapValueSet<VectorMap> ValueSet;
deba@830
   245
deba@830
   246
    /// ValueSet getter function.
deba@830
   247
    ValueSet valueSet() {
deba@830
   248
      return ValueSet(*this);
deba@830
   249
    }
deba@830
   250
deba@830
   251
deba@822
   252
  private:
deba@822
   253
		
deba@822
   254
    Container container;
deba@822
   255
deba@844
   256
  public:
deba@844
   257
    // STL  compatibility typedefs.
deba@844
   258
    typedef Iterator iterator;
deba@844
   259
    typedef ConstIterator const_iterator;
deba@844
   260
    typedef typename Iterator::PairValueType value_type;
deba@844
   261
    typedef typename Iterator::KeyType key_type;
deba@844
   262
    typedef typename Iterator::ValueType data_type;
deba@844
   263
    typedef typename Iterator::PairReferenceType reference;
deba@844
   264
    typedef typename Iterator::PairPointerType pointer;
deba@844
   265
    typedef typename ConstIterator::PairReferenceType const_reference;
deba@844
   266
    typedef typename ConstIterator::PairPointerType const_pointer;
deba@844
   267
    typedef int difference_type;		
deba@822
   268
  };
deba@822
   269
  
deba@822
   270
  /// @}
deba@822
   271
  
deba@822
   272
}
deba@822
   273
deba@822
   274
#endif