lemon/bits/vector_map.h
author athos
Fri, 24 Jun 2005 21:02:47 +0000
changeset 1513 b2a79aaa6867
parent 1374 bcfa3980b432
child 1587 8f1c317ebeb4
permissions -rw-r--r--
Minor changes
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * 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@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, 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@1307
    24
#include <lemon/bits/map_iterator.h>
deba@1307
    25
#include <lemon/bits/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@1374
   112
    VectorMap(const VectorMap& _copy) 
deba@1374
   113
      : Parent(), graph(_copy.getGraph()) {
deba@980
   114
      if (_copy.attached()) {
deba@980
   115
	attach(*_copy.getRegistry());
deba@980
   116
	container = _copy.container;
deba@822
   117
      }
deba@822
   118
    }
deba@822
   119
klao@978
   120
    using Parent::attach;
klao@978
   121
    using Parent::detach;
klao@978
   122
    using Parent::attached;
deba@937
   123
klao@946
   124
    /** Assign operator to copy a map of the same map type.
deba@822
   125
     */
klao@946
   126
    VectorMap& operator=(const VectorMap& copy) {
klao@946
   127
      if (&copy == this) return *this;
klao@946
   128
      
klao@946
   129
      if (graph != copy.graph) {
klao@946
   130
	if (attached()) {
klao@946
   131
	  detach();
klao@946
   132
	}
klao@946
   133
	if (copy.attached()) {
klao@946
   134
	  attach(*copy.getRegistry());
klao@946
   135
	}
deba@897
   136
      }
klao@946
   137
      container = copy.container;
klao@946
   138
klao@946
   139
      return *this;
klao@946
   140
    }
klao@946
   141
klao@946
   142
klao@946
   143
    virtual ~VectorMap() {
klao@946
   144
      if (attached()) {
klao@946
   145
	detach();
deba@822
   146
      }
klao@946
   147
    }
klao@946
   148
klao@946
   149
    const Graph* getGraph() const {
klao@946
   150
      return graph;
deba@822
   151
    }
deba@937
   152
deba@937
   153
    /// The subcript operator.
deba@937
   154
klao@946
   155
    /// The subscript operator. The map can be subscripted by the
klao@946
   156
    /// actual items of the graph. 
klao@946
   157
     
alpar@987
   158
    Reference operator[](const Key& key) {
deba@980
   159
      return container[graph->id(key)];
deba@822
   160
    } 
deba@822
   161
		
deba@937
   162
    /// The const subcript operator.
deba@937
   163
klao@946
   164
    /// The const subscript operator. The map can be subscripted by the
klao@946
   165
    /// actual items of the graph. 
klao@946
   166
     
alpar@987
   167
    ConstReference operator[](const Key& key) const {
deba@980
   168
      return container[graph->id(key)];
deba@822
   169
    }
deba@822
   170
deba@937
   171
klao@946
   172
    /// The setter function of the map.
klao@946
   173
klao@946
   174
    /// It the same as operator[](key) = value expression.
klao@946
   175
    ///
klao@946
   176
     
alpar@987
   177
    void set(const Key& key, const Value& value) {
klao@946
   178
      (*this)[key] = value;
deba@822
   179
    }
klao@946
   180
deba@937
   181
    /// Adds a new key to the map.
deba@822
   182
		
klao@946
   183
    /// It adds a new key to the map. It called by the observer registry
klao@946
   184
    /// and it overrides the add() member function of the observer base.
klao@946
   185
     
alpar@987
   186
    void add(const Key& key) {
deba@980
   187
      int id = graph->id(key);
deba@822
   188
      if (id >= (int)container.size()) {
deba@822
   189
	container.resize(id + 1);
deba@822
   190
      }
deba@822
   191
    }
deba@937
   192
deba@937
   193
    /// Erases a key from the map.
deba@822
   194
		
klao@946
   195
    /// Erase a key from the map. It called by the observer registry
klao@946
   196
    /// and it overrides the erase() member function of the observer base.     
alpar@987
   197
    void erase(const Key&) {}
deba@822
   198
klao@946
   199
    /// Buildes the map.
klao@946
   200
		
klao@946
   201
    /// It buildes the map. It called by the observer registry
klao@946
   202
    /// and it overrides the build() member function of the observer base.
deba@937
   203
klao@946
   204
    void build() { 
deba@980
   205
      container.resize(graph->maxId(_Item()) + 1);
klao@946
   206
    }
deba@937
   207
klao@946
   208
    /// Clear the map.
klao@946
   209
klao@946
   210
    /// It erase all items from the map. It called by the observer registry
klao@946
   211
    /// and it overrides the clear() member function of the observer base.     
deba@822
   212
    void clear() { 
deba@822
   213
      container.clear();
deba@822
   214
    }
deba@1267
   215
    
deba@822
   216
  private:
deba@822
   217
		
deba@822
   218
    Container container;
klao@946
   219
    const Graph *graph;
deba@822
   220
klao@946
   221
  };
klao@946
   222
klao@946
   223
klao@946
   224
  template <typename _Base> 
klao@946
   225
  class VectorMappableGraphExtender : public _Base {
deba@844
   226
  public:
klao@946
   227
klao@946
   228
    typedef VectorMappableGraphExtender<_Base> Graph;
klao@946
   229
    typedef _Base Parent;
klao@946
   230
klao@946
   231
    typedef typename Parent::Node Node;
klao@946
   232
    typedef typename Parent::NodeIt NodeIt;
klao@946
   233
    typedef typename Parent::NodeIdMap NodeIdMap;
deba@1039
   234
    typedef typename Parent::NodeNotifier NodeObserverRegistry;
klao@946
   235
klao@946
   236
    typedef typename Parent::Edge Edge;
klao@946
   237
    typedef typename Parent::EdgeIt EdgeIt;
klao@946
   238
    typedef typename Parent::EdgeIdMap EdgeIdMap;
deba@1039
   239
    typedef typename Parent::EdgeNotifier EdgeObserverRegistry;
klao@946
   240
klao@946
   241
    
klao@946
   242
    template <typename _Value>
deba@1267
   243
    class NodeMap : 
deba@1267
   244
      public IterableMapExtender<VectorMap<Graph, Node, _Value> > {
klao@946
   245
    public:
klao@946
   246
      typedef VectorMappableGraphExtender<_Base> Graph;
klao@946
   247
klao@946
   248
      typedef typename Graph::Node Node;
klao@946
   249
deba@1267
   250
      typedef IterableMapExtender<VectorMap<Graph, Node, _Value> > Parent;
klao@946
   251
klao@979
   252
      //typedef typename Parent::Graph Graph;
klao@946
   253
      typedef typename Parent::Value Value;
klao@946
   254
klao@946
   255
      NodeMap(const Graph& g) 
deba@980
   256
	: Parent(g) {}
klao@946
   257
      NodeMap(const Graph& g, const Value& v) 
deba@980
   258
	: Parent(g, v) {}
klao@946
   259
klao@946
   260
    };
klao@946
   261
klao@946
   262
    template <typename _Value>
deba@1267
   263
    class EdgeMap 
deba@1267
   264
      : public IterableMapExtender<VectorMap<Graph, Edge, _Value> > {
klao@946
   265
    public:
klao@946
   266
      typedef VectorMappableGraphExtender<_Base> Graph;
klao@946
   267
klao@946
   268
      typedef typename Graph::Edge Edge;
klao@946
   269
deba@1267
   270
      typedef IterableMapExtender<VectorMap<Graph, Edge, _Value> > Parent;
klao@946
   271
klao@979
   272
      //typedef typename Parent::Graph Graph;
klao@946
   273
      typedef typename Parent::Value Value;
klao@946
   274
klao@946
   275
      EdgeMap(const Graph& g) 
deba@980
   276
	: Parent(g) {}
klao@946
   277
      EdgeMap(const Graph& g, const Value& v) 
deba@980
   278
	: Parent(g, v) {}
klao@946
   279
klao@946
   280
    };
klao@946
   281
    
deba@822
   282
  };
deba@822
   283
  
deba@822
   284
  /// @}
deba@822
   285
  
deba@822
   286
}
deba@822
   287
deba@822
   288
#endif