lemon/bits/vector_map.h
author alpar
Wed, 15 Mar 2006 09:45:59 +0000
changeset 2008 0820d8168cbb
parent 1996 5dc13b93f8b4
child 2031 080d51024ac5
permissions -rw-r--r--
"Node shapes" 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@1999
    30
///\ingroup graphbits
deba@1669
    31
///
deba@822
    32
///\file
deba@822
    33
///\brief Vector based graph maps.
alpar@921
    34
namespace lemon {
deba@1669
    35
deba@1996
    36
  /// \ingroup graphbits
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@1999
    41
  /// automatically updates 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@1999
    46
  /// \param Notifier 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
  /// 
deba@1999
    50
  /// \author Balazs Dezso  	
deba@1999
    51
  template <typename _Graph, typename _Item, typename _Value>
deba@1999
    52
  class VectorMap 
deba@1999
    53
    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
deba@1719
    54
  private:
deba@1719
    55
		
deba@1719
    56
    /// The container type of the map.
deba@1719
    57
    typedef std::vector<_Value> Container;	
deba@1719
    58
deba@822
    59
  public:
deba@1703
    60
klao@946
    61
    /// The graph type of the map. 
klao@946
    62
    typedef _Graph Graph;
deba@1999
    63
    /// The item type of the map.
deba@1999
    64
    typedef _Item Item;
deba@1719
    65
    /// The reference map tag.
deba@1719
    66
    typedef True ReferenceMapTag;
deba@1719
    67
klao@946
    68
    /// The key type of the map.
alpar@987
    69
    typedef _Item Key;
klao@946
    70
    /// The value type of the map.
klao@946
    71
    typedef _Value Value;
deba@1719
    72
deba@1999
    73
    /// The notifier type.
deba@1999
    74
    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
deba@822
    75
deba@822
    76
    /// The map type.
deba@822
    77
    typedef VectorMap Map;
klao@946
    78
    /// The base class of the map.
deba@1999
    79
    typedef typename Notifier::ObserverBase Parent;
deba@822
    80
deba@822
    81
    /// The reference type of the map;
alpar@987
    82
    typedef typename Container::reference Reference;
deba@822
    83
    /// The const reference type of the map;
alpar@987
    84
    typedef typename Container::const_reference ConstReference;
deba@822
    85
deba@1267
    86
deba@1999
    87
    /// \brief Constructor to attach the new map into the notifier.
deba@1703
    88
    ///
deba@1999
    89
    /// It constructs a map and attachs it into the notifier.
klao@946
    90
    /// It adds all the items of the graph to the map.
deba@1999
    91
    VectorMap(const Graph& graph) {
deba@1999
    92
      Parent::attach(graph.getNotifier(Item()));
deba@1999
    93
      container.resize(Parent::getNotifier()->maxId() + 1);
klao@946
    94
    }
deba@822
    95
deba@1703
    96
    /// \brief Constructor uses given value to initialize the map. 
deba@1703
    97
    ///
deba@1703
    98
    /// It constructs a map uses a given value to initialize the map. 
klao@946
    99
    /// It adds all the items of the graph to the map.
deba@1999
   100
    VectorMap(const Graph& graph, const Value& value) {
deba@1999
   101
      Parent::attach(graph.getNotifier(Item()));
deba@1999
   102
      container.resize(Parent::getNotifier()->maxId() + 1, value);
klao@946
   103
    }
deba@822
   104
deba@1703
   105
    /// \brief Copy constructor
deba@1703
   106
    ///
deba@1703
   107
    /// Copy constructor.
deba@1999
   108
    VectorMap(const VectorMap& _copy) : Parent() {
deba@980
   109
      if (_copy.attached()) {
deba@1999
   110
	Parent::attach(*_copy.getNotifier());
deba@980
   111
	container = _copy.container;
deba@822
   112
      }
deba@822
   113
    }
deba@822
   114
deba@1669
   115
  private:
deba@1669
   116
deba@1669
   117
    VectorMap& operator=(const VectorMap&);
deba@1669
   118
deba@1669
   119
  public:
deba@1669
   120
deba@1703
   121
    /// \brief The subcript operator.
deba@1703
   122
    ///
klao@946
   123
    /// The subscript operator. The map can be subscripted by the
deba@1703
   124
    /// actual items of the graph.      
alpar@987
   125
    Reference operator[](const Key& key) {
deba@1999
   126
      return container[Parent::getNotifier()->id(key)];
deba@822
   127
    } 
deba@822
   128
		
deba@1703
   129
    /// \brief The const subcript operator.
deba@1703
   130
    ///
klao@946
   131
    /// The const subscript operator. The map can be subscripted by the
klao@946
   132
    /// actual items of the graph. 
alpar@987
   133
    ConstReference operator[](const Key& key) const {
deba@1999
   134
      return container[Parent::getNotifier()->id(key)];
deba@822
   135
    }
deba@822
   136
deba@937
   137
deba@1703
   138
    /// \brief The setter function of the map.
deba@1703
   139
    ///
klao@946
   140
    /// It the same as operator[](key) = value expression.
alpar@987
   141
    void set(const Key& key, const Value& value) {
klao@946
   142
      (*this)[key] = value;
deba@822
   143
    }
klao@946
   144
deba@1669
   145
  protected:
deba@1669
   146
deba@1669
   147
    /// \brief Adds a new key to the map.
deba@1669
   148
    ///		
deba@1999
   149
    /// It adds a new key to the map. It called by the observer notifier
deba@1703
   150
    /// and it overrides the add() member function of the observer base.     
deba@1703
   151
    virtual void add(const Key& key) {
deba@1999
   152
      int id = Parent::getNotifier()->id(key);
deba@822
   153
      if (id >= (int)container.size()) {
deba@822
   154
	container.resize(id + 1);
deba@822
   155
      }
deba@822
   156
    }
deba@937
   157
deba@1832
   158
    /// \brief Adds more new keys to the map.
deba@1832
   159
    ///		
deba@1999
   160
    /// It adds more new keys to the map. It called by the observer notifier
deba@1832
   161
    /// and it overrides the add() member function of the observer base.     
deba@1832
   162
    virtual void add(const std::vector<Key>& keys) {
deba@1999
   163
      int max = container.size() - 1;
deba@1832
   164
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1999
   165
        int id = Parent::getNotifier()->id(keys[i]);
deba@1999
   166
        if (id >= max) {
deba@1999
   167
          max = id;
deba@1999
   168
        }
deba@1832
   169
      }
deba@1999
   170
      container.resize(max + 1);
deba@1832
   171
    }
deba@1832
   172
deba@1703
   173
    /// \brief Erase a key from the map.
deba@1703
   174
    ///
deba@1999
   175
    /// Erase a key from the map. It called by the observer notifier
klao@946
   176
    /// and it overrides the erase() member function of the observer base.     
deba@1999
   177
    virtual void erase(const Key& key) {
deba@1999
   178
      container[Parent::getNotifier()->id(key)] = Value();
deba@1999
   179
    }
deba@822
   180
deba@1832
   181
    /// \brief Erase more keys from the map.
deba@1832
   182
    ///
deba@1999
   183
    /// Erase more keys from the map. It called by the observer notifier
deba@1832
   184
    /// and it overrides the erase() member function of the observer base.     
deba@1999
   185
    virtual void erase(const std::vector<Key>& keys) {
deba@1999
   186
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1999
   187
	container[Parent::getNotifier()->id(keys[i])] = Value();
deba@1999
   188
      }
deba@1999
   189
    }
deba@1832
   190
    
deba@1703
   191
    /// \brief Buildes the map.
deba@1703
   192
    ///	
deba@1999
   193
    /// It buildes the map. It called by the observer notifier
klao@946
   194
    /// and it overrides the build() member function of the observer base.
deba@1703
   195
    virtual void build() { 
deba@1999
   196
      int size = Parent::getNotifier()->maxId() + 1;
deba@1999
   197
      container.reserve(size);
deba@1999
   198
      container.resize(size);
klao@946
   199
    }
deba@937
   200
deba@1703
   201
    /// \brief Clear the map.
deba@1703
   202
    ///
deba@1999
   203
    /// It erase all items from the map. It called by the observer notifier
klao@946
   204
    /// and it overrides the clear() member function of the observer base.     
deba@1703
   205
    virtual void clear() { 
deba@822
   206
      container.clear();
deba@822
   207
    }
deba@1267
   208
    
deba@822
   209
  private:
deba@822
   210
		
deba@822
   211
    Container container;
deba@822
   212
klao@946
   213
  };
klao@946
   214
deba@822
   215
}
deba@822
   216
deba@822
   217
#endif