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