lemon/bits/vector_map.h
author Akos Ladanyi <ladanyi@tmit.bme.hu>
Wed, 02 Jul 2008 13:51:20 +0100
changeset 187 84c2a2e5cfee
parent 107 31a2e6d28f61
child 209 765619b7cbb2
permissions -rw-r--r--
Fix bug caused by m4 consuming pairs of square brackets (#108).
deba@57
     1
/* -*- C++ -*-
deba@57
     2
 *
deba@57
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@57
     4
 *
alpar@107
     5
 * Copyright (C) 2003-2008
deba@57
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57
     8
 *
deba@57
     9
 * Permission to use, modify and distribute this software is granted
deba@57
    10
 * provided that this copyright notice appears in all copies. For
deba@57
    11
 * precise terms see the accompanying LICENSE file.
deba@57
    12
 *
deba@57
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@57
    14
 * express or implied, and with no claim as to its suitability for any
deba@57
    15
 * purpose.
deba@57
    16
 *
deba@57
    17
 */
deba@57
    18
deba@57
    19
#ifndef LEMON_BITS_VECTOR_MAP_H
deba@57
    20
#define LEMON_BITS_VECTOR_MAP_H
deba@57
    21
deba@57
    22
#include <vector>
deba@57
    23
#include <algorithm>
deba@57
    24
deba@57
    25
#include <lemon/bits/traits.h>
deba@57
    26
#include <lemon/bits/utility.h>
deba@57
    27
deba@57
    28
#include <lemon/bits/alteration_notifier.h>
deba@57
    29
deba@57
    30
#include <lemon/concept_check.h>
deba@57
    31
#include <lemon/concepts/maps.h>
deba@57
    32
deba@57
    33
///\ingroup graphbits
deba@57
    34
///
deba@57
    35
///\file
deba@57
    36
///\brief Vector based graph maps.
deba@57
    37
namespace lemon {
deba@57
    38
deba@57
    39
  /// \ingroup graphbits
deba@57
    40
  ///
deba@57
    41
  /// \brief Graph map based on the std::vector storage.
deba@57
    42
  ///
deba@57
    43
  /// The VectorMap template class is graph map structure what
deba@57
    44
  /// automatically updates the map when a key is added to or erased from
deba@57
    45
  /// the map. This map type uses the std::vector to store the values.
deba@57
    46
  ///
kpeter@157
    47
  /// \tparam _Notifier The AlterationNotifier that will notify this map.
kpeter@157
    48
  /// \tparam _Item The item type of the graph items.
kpeter@157
    49
  /// \tparam _Value The value type of the map.
kpeter@157
    50
  /// \todo Fix the doc: there is _Graph parameter instead of _Notifier.
deba@57
    51
  template <typename _Graph, typename _Item, typename _Value>
deba@57
    52
  class VectorMap 
deba@57
    53
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
deba@57
    54
  private:
deba@57
    55
		
deba@57
    56
    /// The container type of the map.
deba@57
    57
    typedef std::vector<_Value> Container;	
deba@57
    58
deba@57
    59
  public:
deba@57
    60
deba@57
    61
    /// The graph type of the map. 
deba@57
    62
    typedef _Graph Graph;
deba@57
    63
    /// The item type of the map.
deba@57
    64
    typedef _Item Item;
deba@57
    65
    /// The reference map tag.
deba@57
    66
    typedef True ReferenceMapTag;
deba@57
    67
deba@57
    68
    /// The key type of the map.
deba@57
    69
    typedef _Item Key;
deba@57
    70
    /// The value type of the map.
deba@57
    71
    typedef _Value Value;
deba@57
    72
deba@57
    73
    /// The notifier type.
deba@57
    74
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
deba@57
    75
deba@57
    76
    /// The map type.
deba@57
    77
    typedef VectorMap Map;
deba@57
    78
    /// The base class of the map.
deba@57
    79
    typedef typename Notifier::ObserverBase Parent;
deba@57
    80
deba@57
    81
    /// The reference type of the map;
deba@57
    82
    typedef typename Container::reference Reference;
deba@57
    83
    /// The const reference type of the map;
deba@57
    84
    typedef typename Container::const_reference ConstReference;
deba@57
    85
deba@57
    86
deba@57
    87
    /// \brief Constructor to attach the new map into the notifier.
deba@57
    88
    ///
deba@57
    89
    /// It constructs a map and attachs it into the notifier.
deba@57
    90
    /// It adds all the items of the graph to the map.
deba@57
    91
    VectorMap(const Graph& graph) {
deba@57
    92
      Parent::attach(graph.notifier(Item()));
deba@57
    93
      container.resize(Parent::notifier()->maxId() + 1);
deba@57
    94
    }
deba@57
    95
deba@57
    96
    /// \brief Constructor uses given value to initialize the map. 
deba@57
    97
    ///
deba@57
    98
    /// It constructs a map uses a given value to initialize the map. 
deba@57
    99
    /// It adds all the items of the graph to the map.
deba@57
   100
    VectorMap(const Graph& graph, const Value& value) {
deba@57
   101
      Parent::attach(graph.notifier(Item()));
deba@57
   102
      container.resize(Parent::notifier()->maxId() + 1, value);
deba@57
   103
    }
deba@57
   104
deba@57
   105
    /// \brief Copy constructor
deba@57
   106
    ///
deba@57
   107
    /// Copy constructor.
deba@57
   108
    VectorMap(const VectorMap& _copy) : Parent() {
deba@57
   109
      if (_copy.attached()) {
deba@57
   110
	Parent::attach(*_copy.notifier());
deba@57
   111
	container = _copy.container;
deba@57
   112
      }
deba@57
   113
    }
deba@57
   114
deba@57
   115
    /// \brief Assign operator.
deba@57
   116
    ///
deba@57
   117
    /// This operator assigns for each item in the map the
deba@57
   118
    /// value mapped to the same item in the copied map.  
deba@57
   119
    /// The parameter map should be indiced with the same
deba@57
   120
    /// itemset because this assign operator does not change
deba@57
   121
    /// the container of the map. 
deba@57
   122
    VectorMap& operator=(const VectorMap& cmap) {
deba@57
   123
      return operator=<VectorMap>(cmap);
deba@57
   124
    }
deba@57
   125
deba@57
   126
deba@57
   127
    /// \brief Template assign operator.
deba@57
   128
    ///
deba@57
   129
    /// The given parameter should be conform to the ReadMap
deba@57
   130
    /// concecpt and could be indiced by the current item set of
deba@57
   131
    /// the NodeMap. In this case the value for each item
deba@57
   132
    /// is assigned by the value of the given ReadMap. 
deba@57
   133
    template <typename CMap>
deba@57
   134
    VectorMap& operator=(const CMap& cmap) {
deba@57
   135
      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
deba@57
   136
      const typename Parent::Notifier* nf = Parent::notifier();
deba@57
   137
      Item it;
deba@57
   138
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@57
   139
        set(it, cmap[it]);
deba@57
   140
      }
deba@57
   141
      return *this;
deba@57
   142
    }
deba@57
   143
    
deba@57
   144
  public:
deba@57
   145
deba@57
   146
    /// \brief The subcript operator.
deba@57
   147
    ///
deba@57
   148
    /// The subscript operator. The map can be subscripted by the
deba@57
   149
    /// actual items of the graph.      
deba@57
   150
    Reference operator[](const Key& key) {
deba@57
   151
      return container[Parent::notifier()->id(key)];
deba@57
   152
    } 
deba@57
   153
		
deba@57
   154
    /// \brief The const subcript operator.
deba@57
   155
    ///
deba@57
   156
    /// The const subscript operator. The map can be subscripted by the
deba@57
   157
    /// actual items of the graph. 
deba@57
   158
    ConstReference operator[](const Key& key) const {
deba@57
   159
      return container[Parent::notifier()->id(key)];
deba@57
   160
    }
deba@57
   161
deba@57
   162
deba@57
   163
    /// \brief The setter function of the map.
deba@57
   164
    ///
deba@57
   165
    /// It the same as operator[](key) = value expression.
deba@57
   166
    void set(const Key& key, const Value& value) {
deba@57
   167
      (*this)[key] = value;
deba@57
   168
    }
deba@57
   169
deba@57
   170
  protected:
deba@57
   171
deba@57
   172
    /// \brief Adds a new key to the map.
deba@57
   173
    ///		
deba@57
   174
    /// It adds a new key to the map. It called by the observer notifier
deba@57
   175
    /// and it overrides the add() member function of the observer base.     
deba@57
   176
    virtual void add(const Key& key) {
deba@57
   177
      int id = Parent::notifier()->id(key);
deba@57
   178
      if (id >= int(container.size())) {
deba@57
   179
	container.resize(id + 1);
deba@57
   180
      }
deba@57
   181
    }
deba@57
   182
deba@57
   183
    /// \brief Adds more new keys to the map.
deba@57
   184
    ///		
deba@57
   185
    /// It adds more new keys to the map. It called by the observer notifier
deba@57
   186
    /// and it overrides the add() member function of the observer base.     
deba@57
   187
    virtual void add(const std::vector<Key>& keys) {
deba@57
   188
      int max = container.size() - 1;
deba@57
   189
      for (int i = 0; i < int(keys.size()); ++i) {
deba@57
   190
        int id = Parent::notifier()->id(keys[i]);
deba@57
   191
        if (id >= max) {
deba@57
   192
          max = id;
deba@57
   193
        }
deba@57
   194
      }
deba@57
   195
      container.resize(max + 1);
deba@57
   196
    }
deba@57
   197
deba@57
   198
    /// \brief Erase a key from the map.
deba@57
   199
    ///
deba@57
   200
    /// Erase a key from the map. It called by the observer notifier
deba@57
   201
    /// and it overrides the erase() member function of the observer base.     
deba@57
   202
    virtual void erase(const Key& key) {
deba@57
   203
      container[Parent::notifier()->id(key)] = Value();
deba@57
   204
    }
deba@57
   205
deba@57
   206
    /// \brief Erase more keys from the map.
deba@57
   207
    ///
deba@57
   208
    /// Erase more keys from the map. It called by the observer notifier
deba@57
   209
    /// and it overrides the erase() member function of the observer base.     
deba@57
   210
    virtual void erase(const std::vector<Key>& keys) {
deba@57
   211
      for (int i = 0; i < int(keys.size()); ++i) {
deba@57
   212
	container[Parent::notifier()->id(keys[i])] = Value();
deba@57
   213
      }
deba@57
   214
    }
deba@57
   215
    
deba@57
   216
    /// \brief Buildes the map.
deba@57
   217
    ///	
deba@57
   218
    /// It buildes the map. It called by the observer notifier
deba@57
   219
    /// and it overrides the build() member function of the observer base.
deba@57
   220
    virtual void build() { 
deba@57
   221
      int size = Parent::notifier()->maxId() + 1;
deba@57
   222
      container.reserve(size);
deba@57
   223
      container.resize(size);
deba@57
   224
    }
deba@57
   225
deba@57
   226
    /// \brief Clear the map.
deba@57
   227
    ///
deba@57
   228
    /// It erase all items from the map. It called by the observer notifier
deba@57
   229
    /// and it overrides the clear() member function of the observer base.     
deba@57
   230
    virtual void clear() { 
deba@57
   231
      container.clear();
deba@57
   232
    }
deba@57
   233
    
deba@57
   234
  private:
deba@57
   235
		
deba@57
   236
    Container container;
deba@57
   237
deba@57
   238
  };
deba@57
   239
deba@57
   240
}
deba@57
   241
deba@57
   242
#endif