lemon/bits/vector_map.h
author deba
Tue, 30 Oct 2007 10:51:07 +0000
changeset 2504 46a82ce84cc6
parent 2386 81b47fc5c444
child 2553 bfced05fa852
permissions -rw-r--r--
Bug fix
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@2391
     5
 * Copyright (C) 2003-2007
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
deba@1999
    19
#ifndef LEMON_BITS_VECTOR_MAP_H
deba@1999
    20
#define LEMON_BITS_VECTOR_MAP_H
deba@822
    21
deba@822
    22
#include <vector>
klao@946
    23
#include <algorithm>
deba@822
    24
deba@1999
    25
#include <lemon/bits/traits.h>
deba@1993
    26
#include <lemon/bits/utility.h>
deba@1999
    27
deba@1307
    28
#include <lemon/bits/alteration_notifier.h>
deba@822
    29
deba@2031
    30
#include <lemon/concept_check.h>
alpar@2260
    31
#include <lemon/concepts/maps.h>
deba@2031
    32
deba@1999
    33
///\ingroup graphbits
deba@1669
    34
///
deba@822
    35
///\file
deba@822
    36
///\brief Vector based graph maps.
alpar@921
    37
namespace lemon {
deba@1669
    38
deba@1996
    39
  /// \ingroup graphbits
deba@1669
    40
  ///
deba@1669
    41
  /// \brief Graph map based on the std::vector storage.
deba@1669
    42
  ///
klao@946
    43
  /// The VectorMap template class is graph map structure what
deba@1999
    44
  /// automatically updates the map when a key is added to or erased from
deba@2202
    45
  /// the map. This map type uses the std::vector to store the values.
klao@946
    46
  ///
deba@1999
    47
  /// \param Notifier The AlterationNotifier that will notify this map.
deba@1703
    48
  /// \param Item The item type of the graph items.
klao@946
    49
  /// \param Value The value type of the map.
klao@946
    50
  /// 
deba@1999
    51
  /// \author Balazs Dezso  	
deba@1999
    52
  template <typename _Graph, typename _Item, typename _Value>
deba@1999
    53
  class VectorMap 
deba@1999
    54
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
deba@1719
    55
  private:
deba@1719
    56
		
deba@1719
    57
    /// The container type of the map.
deba@1719
    58
    typedef std::vector<_Value> Container;	
deba@1719
    59
deba@822
    60
  public:
deba@1703
    61
klao@946
    62
    /// The graph type of the map. 
klao@946
    63
    typedef _Graph Graph;
deba@1999
    64
    /// The item type of the map.
deba@1999
    65
    typedef _Item Item;
deba@1719
    66
    /// The reference map tag.
deba@1719
    67
    typedef True ReferenceMapTag;
deba@1719
    68
klao@946
    69
    /// The key type of the map.
alpar@987
    70
    typedef _Item Key;
klao@946
    71
    /// The value type of the map.
klao@946
    72
    typedef _Value Value;
deba@1719
    73
deba@1999
    74
    /// The notifier type.
deba@1999
    75
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
deba@822
    76
deba@822
    77
    /// The map type.
deba@822
    78
    typedef VectorMap Map;
klao@946
    79
    /// The base class of the map.
deba@1999
    80
    typedef typename Notifier::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 const reference type of the map;
alpar@987
    85
    typedef typename Container::const_reference ConstReference;
deba@822
    86
deba@1267
    87
deba@1999
    88
    /// \brief Constructor to attach the new map into the notifier.
deba@1703
    89
    ///
deba@1999
    90
    /// It constructs a map and attachs it into the notifier.
klao@946
    91
    /// It adds all the items of the graph to the map.
deba@1999
    92
    VectorMap(const Graph& graph) {
deba@2384
    93
      Parent::attach(graph.notifier(Item()));
deba@2384
    94
      container.resize(Parent::notifier()->maxId() + 1);
klao@946
    95
    }
deba@822
    96
deba@1703
    97
    /// \brief Constructor uses given value to initialize the map. 
deba@1703
    98
    ///
deba@1703
    99
    /// It constructs a map uses a given value to initialize the map. 
klao@946
   100
    /// It adds all the items of the graph to the map.
deba@1999
   101
    VectorMap(const Graph& graph, const Value& value) {
deba@2384
   102
      Parent::attach(graph.notifier(Item()));
deba@2384
   103
      container.resize(Parent::notifier()->maxId() + 1, value);
klao@946
   104
    }
deba@822
   105
deba@1703
   106
    /// \brief Copy constructor
deba@1703
   107
    ///
deba@1703
   108
    /// Copy constructor.
deba@1999
   109
    VectorMap(const VectorMap& _copy) : Parent() {
deba@980
   110
      if (_copy.attached()) {
deba@2384
   111
	Parent::attach(*_copy.notifier());
deba@980
   112
	container = _copy.container;
deba@822
   113
      }
deba@822
   114
    }
deba@822
   115
deba@2031
   116
    /// \brief Assign operator.
deba@2031
   117
    ///
deba@2031
   118
    /// This operator assigns for each item in the map the
deba@2031
   119
    /// value mapped to the same item in the copied map.  
deba@2031
   120
    /// The parameter map should be indiced with the same
deba@2031
   121
    /// itemset because this assign operator does not change
deba@2031
   122
    /// the container of the map. 
deba@2031
   123
    VectorMap& operator=(const VectorMap& cmap) {
deba@2031
   124
      return operator=<VectorMap>(cmap);
deba@2031
   125
    }
deba@1669
   126
deba@1669
   127
deba@2031
   128
    /// \brief Template assign operator.
deba@2031
   129
    ///
deba@2031
   130
    /// The given parameter should be conform to the ReadMap
deba@2031
   131
    /// concecpt and could be indiced by the current item set of
deba@2031
   132
    /// the NodeMap. In this case the value for each item
deba@2031
   133
    /// is assigned by the value of the given ReadMap. 
deba@2031
   134
    template <typename CMap>
deba@2031
   135
    VectorMap& operator=(const CMap& cmap) {
alpar@2260
   136
      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
deba@2386
   137
      const typename Parent::Notifier* nf = Parent::notifier();
deba@2031
   138
      Item it;
deba@2386
   139
      for (nf->first(it); it != INVALID; nf->next(it)) {
deba@2031
   140
        set(it, cmap[it]);
deba@2031
   141
      }
deba@2031
   142
      return *this;
deba@2031
   143
    }
deba@2031
   144
    
deba@1669
   145
  public:
deba@1669
   146
deba@1703
   147
    /// \brief The subcript operator.
deba@1703
   148
    ///
klao@946
   149
    /// The subscript operator. The map can be subscripted by the
deba@1703
   150
    /// actual items of the graph.      
alpar@987
   151
    Reference operator[](const Key& key) {
deba@2384
   152
      return container[Parent::notifier()->id(key)];
deba@822
   153
    } 
deba@822
   154
		
deba@1703
   155
    /// \brief The const subcript operator.
deba@1703
   156
    ///
klao@946
   157
    /// The const subscript operator. The map can be subscripted by the
klao@946
   158
    /// actual items of the graph. 
alpar@987
   159
    ConstReference operator[](const Key& key) const {
deba@2384
   160
      return container[Parent::notifier()->id(key)];
deba@822
   161
    }
deba@822
   162
deba@937
   163
deba@1703
   164
    /// \brief The setter function of the map.
deba@1703
   165
    ///
klao@946
   166
    /// It the same as operator[](key) = value expression.
alpar@987
   167
    void set(const Key& key, const Value& value) {
klao@946
   168
      (*this)[key] = value;
deba@822
   169
    }
klao@946
   170
deba@1669
   171
  protected:
deba@1669
   172
deba@1669
   173
    /// \brief Adds a new key to the map.
deba@1669
   174
    ///		
deba@1999
   175
    /// It adds a new key to the map. It called by the observer notifier
deba@1703
   176
    /// and it overrides the add() member function of the observer base.     
deba@1703
   177
    virtual void add(const Key& key) {
deba@2384
   178
      int id = Parent::notifier()->id(key);
deba@2386
   179
      if (id >= int(container.size())) {
deba@822
   180
	container.resize(id + 1);
deba@822
   181
      }
deba@822
   182
    }
deba@937
   183
deba@1832
   184
    /// \brief Adds more new keys to the map.
deba@1832
   185
    ///		
deba@1999
   186
    /// It adds more new keys to the map. It called by the observer notifier
deba@1832
   187
    /// and it overrides the add() member function of the observer base.     
deba@1832
   188
    virtual void add(const std::vector<Key>& keys) {
deba@1999
   189
      int max = container.size() - 1;
deba@2386
   190
      for (int i = 0; i < int(keys.size()); ++i) {
deba@2384
   191
        int id = Parent::notifier()->id(keys[i]);
deba@1999
   192
        if (id >= max) {
deba@1999
   193
          max = id;
deba@1999
   194
        }
deba@1832
   195
      }
deba@1999
   196
      container.resize(max + 1);
deba@1832
   197
    }
deba@1832
   198
deba@1703
   199
    /// \brief Erase a key from the map.
deba@1703
   200
    ///
deba@1999
   201
    /// Erase a key from the map. It called by the observer notifier
klao@946
   202
    /// and it overrides the erase() member function of the observer base.     
deba@1999
   203
    virtual void erase(const Key& key) {
deba@2384
   204
      container[Parent::notifier()->id(key)] = Value();
deba@1999
   205
    }
deba@822
   206
deba@1832
   207
    /// \brief Erase more keys from the map.
deba@1832
   208
    ///
deba@1999
   209
    /// Erase more keys from the map. It called by the observer notifier
deba@1832
   210
    /// and it overrides the erase() member function of the observer base.     
deba@1999
   211
    virtual void erase(const std::vector<Key>& keys) {
deba@2386
   212
      for (int i = 0; i < int(keys.size()); ++i) {
deba@2384
   213
	container[Parent::notifier()->id(keys[i])] = Value();
deba@1999
   214
      }
deba@1999
   215
    }
deba@1832
   216
    
deba@1703
   217
    /// \brief Buildes the map.
deba@1703
   218
    ///	
deba@1999
   219
    /// It buildes the map. It called by the observer notifier
klao@946
   220
    /// and it overrides the build() member function of the observer base.
deba@1703
   221
    virtual void build() { 
deba@2384
   222
      int size = Parent::notifier()->maxId() + 1;
deba@1999
   223
      container.reserve(size);
deba@1999
   224
      container.resize(size);
klao@946
   225
    }
deba@937
   226
deba@1703
   227
    /// \brief Clear the map.
deba@1703
   228
    ///
deba@1999
   229
    /// It erase all items from the map. It called by the observer notifier
klao@946
   230
    /// and it overrides the clear() member function of the observer base.     
deba@1703
   231
    virtual void clear() { 
deba@822
   232
      container.clear();
deba@822
   233
    }
deba@1267
   234
    
deba@822
   235
  private:
deba@822
   236
		
deba@822
   237
    Container container;
deba@822
   238
klao@946
   239
  };
klao@946
   240
deba@822
   241
}
deba@822
   242
deba@822
   243
#endif