src/lemon/vector_map.h
author alpar
Fri, 04 Mar 2005 23:14:36 +0000
changeset 1196 4bebc22ab77c
parent 1039 bd01c5a3f989
child 1267 a93f94dbe3d3
permissions -rw-r--r--
- Bugfix in setting the previous Node
- Bugfix in DijkstraWizard _source member initialization
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 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>
klao@946
    21
#include <algorithm>
deba@822
    22
deba@1039
    23
#include <lemon/alteration_notifier.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
  
klao@946
    34
  /// The VectorMap template class is graph map structure what
klao@946
    35
  /// automatically updates the map when a key is added to or erased from
klao@946
    36
  /// the map. This map factory uses the allocators to implement 
klao@946
    37
  /// the container functionality. This map factory
klao@946
    38
  /// uses the std::vector to implement the container function.
klao@946
    39
  ///
deba@1039
    40
  /// \param Registry The AlterationNotifier that will notify this map.
klao@946
    41
  /// \param IdMap The IdMap type of the graph items.
klao@946
    42
  /// \param Value The value type of the map.
klao@946
    43
  /// 
klao@946
    44
  /// \author Balazs Dezso
klao@946
    45
  	
klao@946
    46
klao@946
    47
  template <typename _Graph, 
klao@946
    48
	    typename _Item,
klao@946
    49
	    typename _Value>
deba@1039
    50
  class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
deba@822
    51
  public:
deba@822
    52
		
klao@946
    53
    /// The graph type of the map. 
klao@946
    54
    typedef _Graph Graph;
klao@946
    55
    /// The key type of the map.
alpar@987
    56
    typedef _Item Key;
klao@946
    57
    /// The id map type of the map.
deba@1039
    58
    typedef AlterationNotifier<_Item> Registry;
klao@946
    59
    /// The value type of the map.
klao@946
    60
    typedef _Value Value;
deba@822
    61
deba@822
    62
    /// The map type.
deba@822
    63
    typedef VectorMap Map;
klao@946
    64
    /// The base class of the map.
klao@946
    65
    typedef typename Registry::ObserverBase Parent;
deba@822
    66
deba@822
    67
  private:
deba@822
    68
		
deba@822
    69
    /// The container type of the map.
deba@822
    70
    typedef std::vector<Value> Container;	
deba@822
    71
deba@822
    72
  public:
deba@822
    73
deba@822
    74
    /// The reference type of the map;
alpar@987
    75
    typedef typename Container::reference Reference;
deba@822
    76
    /// The pointer type of the map;
alpar@987
    77
    typedef typename Container::pointer Pointer;
deba@822
    78
deba@822
    79
    /// The const value type of the map.
alpar@987
    80
    typedef const Value ConstValue;
deba@822
    81
    /// The const reference type of the map;
alpar@987
    82
    typedef typename Container::const_reference ConstReference;
deba@822
    83
    /// The pointer type of the map;
alpar@987
    84
    typedef typename Container::const_pointer ConstPointer;
deba@822
    85
klao@946
    86
    /// Constructor to attach the new map into the registry.
deba@937
    87
klao@946
    88
    /// It construates a map and attachs it into the registry.
klao@946
    89
    /// It adds all the items of the graph to the map.
klao@946
    90
     
deba@980
    91
    VectorMap(const Graph& _g) : graph(&_g) {
deba@1039
    92
      attach(_g.getNotifier(_Item()));
klao@946
    93
      build();
klao@946
    94
    }
deba@822
    95
deba@937
    96
    /// Constructor uses given value to initialize the map. 
deba@937
    97
klao@946
    98
    /// It construates a map uses a given value to initialize the map. 
klao@946
    99
    /// It adds all the items of the graph to the map.
klao@946
   100
     
deba@980
   101
    VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { 
deba@1039
   102
      attach(_g.getNotifier(_Item()));
deba@980
   103
      container.resize(graph->maxId(_Item()) + 1, _v);
klao@946
   104
    }
deba@822
   105
deba@980
   106
    VectorMap(const VectorMap& _copy) : graph(_copy.getGraph()) {
deba@980
   107
      if (_copy.attached()) {
deba@980
   108
	attach(*_copy.getRegistry());
deba@980
   109
	container = _copy.container;
deba@822
   110
      }
deba@822
   111
    }
deba@822
   112
klao@978
   113
    using Parent::attach;
klao@978
   114
    using Parent::detach;
klao@978
   115
    using Parent::attached;
deba@937
   116
klao@946
   117
    /** Assign operator to copy a map of the same map type.
deba@822
   118
     */
klao@946
   119
    VectorMap& operator=(const VectorMap& copy) {
klao@946
   120
      if (&copy == this) return *this;
klao@946
   121
      
klao@946
   122
      if (graph != copy.graph) {
klao@946
   123
	if (attached()) {
klao@946
   124
	  detach();
klao@946
   125
	}
klao@946
   126
	if (copy.attached()) {
klao@946
   127
	  attach(*copy.getRegistry());
klao@946
   128
	}
deba@897
   129
      }
klao@946
   130
      container = copy.container;
klao@946
   131
klao@946
   132
      return *this;
klao@946
   133
    }
klao@946
   134
klao@946
   135
klao@946
   136
    virtual ~VectorMap() {
klao@946
   137
      if (attached()) {
klao@946
   138
	detach();
deba@822
   139
      }
klao@946
   140
    }
klao@946
   141
klao@946
   142
    const Graph* getGraph() const {
klao@946
   143
      return graph;
deba@822
   144
    }
deba@937
   145
deba@937
   146
    /// The subcript operator.
deba@937
   147
klao@946
   148
    /// The subscript operator. The map can be subscripted by the
klao@946
   149
    /// actual items of the graph. 
klao@946
   150
     
alpar@987
   151
    Reference operator[](const Key& key) {
deba@980
   152
      return container[graph->id(key)];
deba@822
   153
    } 
deba@822
   154
		
deba@937
   155
    /// The const subcript operator.
deba@937
   156
klao@946
   157
    /// The const subscript operator. The map can be subscripted by the
klao@946
   158
    /// actual items of the graph. 
klao@946
   159
     
alpar@987
   160
    ConstReference operator[](const Key& key) const {
deba@980
   161
      return container[graph->id(key)];
deba@822
   162
    }
deba@822
   163
deba@937
   164
klao@946
   165
    /// The setter function of the map.
klao@946
   166
klao@946
   167
    /// It the same as operator[](key) = value expression.
klao@946
   168
    ///
klao@946
   169
     
alpar@987
   170
    void set(const Key& key, const Value& value) {
klao@946
   171
      (*this)[key] = value;
deba@822
   172
    }
klao@946
   173
deba@937
   174
    /// Adds a new key to the map.
deba@822
   175
		
klao@946
   176
    /// It adds a new key to the map. It called by the observer registry
klao@946
   177
    /// and it overrides the add() member function of the observer base.
klao@946
   178
     
alpar@987
   179
    void add(const Key& key) {
deba@980
   180
      int id = graph->id(key);
deba@822
   181
      if (id >= (int)container.size()) {
deba@822
   182
	container.resize(id + 1);
deba@822
   183
      }
deba@822
   184
    }
deba@937
   185
deba@937
   186
    /// Erases a key from the map.
deba@822
   187
		
klao@946
   188
    /// Erase a key from the map. It called by the observer registry
klao@946
   189
    /// and it overrides the erase() member function of the observer base.     
alpar@987
   190
    void erase(const Key&) {}
deba@822
   191
klao@946
   192
    /// Buildes the map.
klao@946
   193
		
klao@946
   194
    /// It buildes the map. It called by the observer registry
klao@946
   195
    /// and it overrides the build() member function of the observer base.
deba@937
   196
klao@946
   197
    void build() { 
deba@980
   198
      container.resize(graph->maxId(_Item()) + 1);
klao@946
   199
    }
deba@937
   200
klao@946
   201
    /// Clear the map.
klao@946
   202
klao@946
   203
    /// It erase all items from the map. It called by the observer registry
klao@946
   204
    /// and it overrides the clear() member function of the observer base.     
deba@822
   205
    void clear() { 
deba@822
   206
      container.clear();
deba@822
   207
    }
deba@822
   208
deba@822
   209
  private:
deba@822
   210
		
deba@822
   211
    Container container;
klao@946
   212
    const Graph *graph;
deba@822
   213
klao@946
   214
  };
klao@946
   215
klao@946
   216
klao@946
   217
  template <typename _Base> 
klao@946
   218
  class VectorMappableGraphExtender : public _Base {
deba@844
   219
  public:
klao@946
   220
klao@946
   221
    typedef VectorMappableGraphExtender<_Base> Graph;
klao@946
   222
    typedef _Base Parent;
klao@946
   223
klao@946
   224
    typedef typename Parent::Node Node;
klao@946
   225
    typedef typename Parent::NodeIt NodeIt;
klao@946
   226
    typedef typename Parent::NodeIdMap NodeIdMap;
deba@1039
   227
    typedef typename Parent::NodeNotifier NodeObserverRegistry;
klao@946
   228
klao@946
   229
    typedef typename Parent::Edge Edge;
klao@946
   230
    typedef typename Parent::EdgeIt EdgeIt;
klao@946
   231
    typedef typename Parent::EdgeIdMap EdgeIdMap;
deba@1039
   232
    typedef typename Parent::EdgeNotifier EdgeObserverRegistry;
klao@946
   233
klao@946
   234
    
klao@946
   235
klao@946
   236
    template <typename _Value>
deba@980
   237
    class NodeMap : public VectorMap<Graph, Node, _Value> {
klao@946
   238
    public:
klao@946
   239
      typedef VectorMappableGraphExtender<_Base> Graph;
klao@946
   240
klao@946
   241
      typedef typename Graph::Node Node;
klao@946
   242
deba@980
   243
      typedef VectorMap<Graph, Node, _Value> Parent;
klao@946
   244
klao@979
   245
      //typedef typename Parent::Graph Graph;
klao@946
   246
      typedef typename Parent::Value Value;
klao@946
   247
klao@946
   248
      NodeMap(const Graph& g) 
deba@980
   249
	: Parent(g) {}
klao@946
   250
      NodeMap(const Graph& g, const Value& v) 
deba@980
   251
	: Parent(g, v) {}
klao@946
   252
klao@946
   253
    };
klao@946
   254
klao@946
   255
    template <typename _Value>
deba@980
   256
    class EdgeMap : public VectorMap<Graph, Edge, _Value> {
klao@946
   257
    public:
klao@946
   258
      typedef VectorMappableGraphExtender<_Base> Graph;
klao@946
   259
klao@946
   260
      typedef typename Graph::Edge Edge;
klao@946
   261
deba@980
   262
      typedef VectorMap<Graph, Edge, _Value> Parent;
klao@946
   263
klao@979
   264
      //typedef typename Parent::Graph Graph;
klao@946
   265
      typedef typename Parent::Value Value;
klao@946
   266
klao@946
   267
      EdgeMap(const Graph& g) 
deba@980
   268
	: Parent(g) {}
klao@946
   269
      EdgeMap(const Graph& g, const Value& v) 
deba@980
   270
	: Parent(g, v) {}
klao@946
   271
klao@946
   272
    };
klao@946
   273
    
deba@822
   274
  };
deba@822
   275
  
deba@822
   276
  /// @}
deba@822
   277
  
deba@822
   278
}
deba@822
   279
deba@822
   280
#endif