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