lemon/bits/vector_map.h
author ladanyi
Sun, 29 Jan 2006 22:06:45 +0000
changeset 1919 9704601de87f
parent 1875 98698b69a902
child 1946 17eb3eaad9f8
permissions -rw-r--r--
demo for simann
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1875
     4
 * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1910
     5
 * (Egervary Research Groin 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@1810
    24
#include <lemon/bits/map_extender.h>
deba@1307
    25
#include <lemon/bits/alteration_notifier.h>
deba@1669
    26
#include <lemon/concept_check.h>
deba@1669
    27
#include <lemon/concept/maps.h>
deba@822
    28
deba@1910
    29
/// \ingroin graphmapfactory
deba@1669
    30
///
deba@822
    31
///\file
deba@822
    32
///\brief Vector based graph maps.
deba@822
    33
alpar@921
    34
namespace lemon {
deba@1669
    35
deba@1910
    36
  /// \ingroin graphmapfactory
deba@1669
    37
  ///
deba@1669
    38
  /// \brief Graph map based on the std::vector storage.
deba@1669
    39
  ///
klao@946
    40
  /// The VectorMap template class is graph map structure what
deba@1910
    41
  /// automatically indates the map when a key is added to or erased from
klao@946
    42
  /// the map. This map factory uses the allocators to implement 
klao@946
    43
  /// the container functionality. This map factory
klao@946
    44
  /// uses the std::vector to implement the container function.
klao@946
    45
  ///
deba@1039
    46
  /// \param Registry The AlterationNotifier that will notify this map.
deba@1703
    47
  /// \param Item The item type of the graph items.
klao@946
    48
  /// \param Value The value type of the map.
klao@946
    49
  /// 
klao@946
    50
  /// \author Balazs Dezso
klao@946
    51
  	
deba@1267
    52
  template <
deba@1267
    53
    typename _Graph, 
deba@1267
    54
    typename _Item,    
deba@1267
    55
    typename _Value
deba@1267
    56
    >
deba@1039
    57
  class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
deba@1719
    58
  private:
deba@1719
    59
		
deba@1719
    60
    /// The container type of the map.
deba@1719
    61
    typedef std::vector<_Value> Container;	
deba@1719
    62
deba@822
    63
  public:
deba@1703
    64
klao@946
    65
    /// The graph type of the map. 
klao@946
    66
    typedef _Graph Graph;
deba@1719
    67
    /// The reference map tag.
deba@1719
    68
    typedef True ReferenceMapTag;
deba@1719
    69
klao@946
    70
    /// The key type of the map.
alpar@987
    71
    typedef _Item Key;
klao@946
    72
    /// The value type of the map.
klao@946
    73
    typedef _Value Value;
deba@1719
    74
deba@1719
    75
    typedef AlterationNotifier<_Item> Registry;
deba@822
    76
deba@822
    77
    /// The map type.
deba@822
    78
    typedef VectorMap Map;
klao@946
    79
    /// The base class of the map.
klao@946
    80
    typedef typename Registry::ObserverBase Parent;
deba@822
    81
deba@822
    82
    /// The reference type of the map;
alpar@987
    83
    typedef typename Container::reference Reference;
deba@822
    84
    /// The pointer type of the map;
alpar@987
    85
    typedef typename Container::pointer Pointer;
deba@822
    86
deba@822
    87
    /// The const value type of the map.
alpar@987
    88
    typedef const Value ConstValue;
deba@822
    89
    /// The const reference type of the map;
alpar@987
    90
    typedef typename Container::const_reference ConstReference;
deba@822
    91
    /// The pointer type of the map;
alpar@987
    92
    typedef typename Container::const_pointer ConstPointer;
deba@822
    93
deba@1267
    94
deba@1703
    95
    /// \brief Constructor to attach the new map into the registry.
deba@1703
    96
    ///
deba@1703
    97
    /// It constructs a map and attachs it into the registry.
klao@946
    98
    /// It adds all the items of the graph to the map.
deba@980
    99
    VectorMap(const Graph& _g) : graph(&_g) {
deba@1039
   100
      attach(_g.getNotifier(_Item()));
klao@946
   101
      build();
klao@946
   102
    }
deba@822
   103
deba@1703
   104
    /// \brief Constructor uses given value to initialize the map. 
deba@1703
   105
    ///
deba@1703
   106
    /// It constructs a map uses a given value to initialize the map. 
klao@946
   107
    /// It adds all the items of the graph to the map.
deba@980
   108
    VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { 
deba@1039
   109
      attach(_g.getNotifier(_Item()));
deba@980
   110
      container.resize(graph->maxId(_Item()) + 1, _v);
klao@946
   111
    }
deba@822
   112
deba@1703
   113
    /// \brief Copy constructor
deba@1703
   114
    ///
deba@1703
   115
    /// Copy constructor.
deba@1374
   116
    VectorMap(const VectorMap& _copy) 
deba@1374
   117
      : Parent(), graph(_copy.getGraph()) {
deba@980
   118
      if (_copy.attached()) {
deba@980
   119
	attach(*_copy.getRegistry());
deba@980
   120
	container = _copy.container;
deba@822
   121
      }
deba@822
   122
    }
deba@822
   123
deba@1703
   124
    /// \brief Destrcutor
deba@1703
   125
    ///
deba@1703
   126
    /// Destructor.
klao@946
   127
    virtual ~VectorMap() {
klao@946
   128
      if (attached()) {
klao@946
   129
	detach();
deba@822
   130
      }
klao@946
   131
    }
klao@946
   132
deba@1669
   133
deba@1669
   134
  private:
deba@1669
   135
deba@1669
   136
    VectorMap& operator=(const VectorMap&);
deba@1669
   137
deba@1669
   138
  protected:
deba@1669
   139
deba@1669
   140
    using Parent::attach;
deba@1669
   141
    using Parent::detach;
deba@1669
   142
    using Parent::attached;
deba@1669
   143
klao@946
   144
    const Graph* getGraph() const {
klao@946
   145
      return graph;
deba@822
   146
    }
deba@937
   147
deba@1669
   148
  public:
deba@1669
   149
deba@1703
   150
    /// \brief The subcript operator.
deba@1703
   151
    ///
klao@946
   152
    /// The subscript operator. The map can be subscripted by the
deba@1703
   153
    /// actual items of the graph.      
alpar@987
   154
    Reference operator[](const Key& key) {
deba@980
   155
      return container[graph->id(key)];
deba@822
   156
    } 
deba@822
   157
		
deba@1703
   158
    /// \brief The const subcript operator.
deba@1703
   159
    ///
klao@946
   160
    /// The const subscript operator. The map can be subscripted by the
klao@946
   161
    /// actual items of the graph. 
alpar@987
   162
    ConstReference operator[](const Key& key) const {
deba@980
   163
      return container[graph->id(key)];
deba@822
   164
    }
deba@822
   165
deba@937
   166
deba@1703
   167
    /// \brief The setter function of the map.
deba@1703
   168
    ///
klao@946
   169
    /// It the same as operator[](key) = value expression.
alpar@987
   170
    void set(const Key& key, const Value& value) {
klao@946
   171
      (*this)[key] = value;
deba@822
   172
    }
klao@946
   173
deba@1669
   174
  protected:
deba@1669
   175
deba@1669
   176
    /// \brief Adds a new key to the map.
deba@1669
   177
    ///		
klao@946
   178
    /// It adds a new key to the map. It called by the observer registry
deba@1703
   179
    /// and it overrides the add() member function of the observer base.     
deba@1703
   180
    virtual void add(const Key& key) {
deba@980
   181
      int id = graph->id(key);
deba@822
   182
      if (id >= (int)container.size()) {
deba@822
   183
	container.resize(id + 1);
deba@822
   184
      }
deba@822
   185
    }
deba@937
   186
deba@1832
   187
    /// \brief Adds more new keys to the map.
deba@1832
   188
    ///		
deba@1832
   189
    /// It adds more new keys to the map. It called by the observer registry
deba@1832
   190
    /// and it overrides the add() member function of the observer base.     
deba@1832
   191
    virtual void add(const std::vector<Key>& keys) {
deba@1832
   192
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1832
   193
	add(keys[i]);
deba@1832
   194
      }
deba@1832
   195
    }
deba@1832
   196
deba@1703
   197
    /// \brief Erase a key from the map.
deba@1703
   198
    ///
klao@946
   199
    /// Erase a key from the map. It called by the observer registry
klao@946
   200
    /// and it overrides the erase() member function of the observer base.     
deba@1703
   201
    virtual void erase(const Key&) {}
deba@822
   202
deba@1832
   203
    /// \brief Erase more keys from the map.
deba@1832
   204
    ///
deba@1832
   205
    /// Erase more keys from the map. It called by the observer registry
deba@1832
   206
    /// and it overrides the erase() member function of the observer base.     
deba@1832
   207
    virtual void erase(const std::vector<Key>&) {}
deba@1832
   208
    
deba@1703
   209
    /// \brief Buildes the map.
deba@1703
   210
    ///	
klao@946
   211
    /// It buildes the map. It called by the observer registry
klao@946
   212
    /// and it overrides the build() member function of the observer base.
deba@1703
   213
    virtual void build() { 
deba@980
   214
      container.resize(graph->maxId(_Item()) + 1);
klao@946
   215
    }
deba@937
   216
deba@1703
   217
    /// \brief Clear the map.
deba@1703
   218
    ///
klao@946
   219
    /// It erase all items from the map. It called by the observer registry
klao@946
   220
    /// and it overrides the clear() member function of the observer base.     
deba@1703
   221
    virtual void clear() { 
deba@822
   222
      container.clear();
deba@822
   223
    }
deba@1267
   224
    
deba@822
   225
  private:
deba@822
   226
		
deba@822
   227
    Container container;
klao@946
   228
    const Graph *graph;
deba@822
   229
klao@946
   230
  };
klao@946
   231
deba@822
   232
}
deba@822
   233
deba@822
   234
#endif