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