3  * This file is a part of LEMON, a generic C++ optimization library
 
     5  * Copyright (C) 2003-2006
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     9  * Permission to use, modify and distribute this software is granted
 
    10  * provided that this copyright notice appears in all copies. For
 
    11  * precise terms see the accompanying LICENSE file.
 
    13  * This software is provided "AS IS" with no warranty of any kind,
 
    14  * express or implied, and with no claim as to its suitability for any
 
    19 #ifndef LEMON_BITS_VECTOR_MAP_H
 
    20 #define LEMON_BITS_VECTOR_MAP_H
 
    25 #include <lemon/bits/traits.h>
 
    26 #include <lemon/bits/utility.h>
 
    28 #include <lemon/bits/alteration_notifier.h>
 
    33 ///\brief Vector based graph maps.
 
    36   /// \ingroup graphbits
 
    38   /// \brief Graph map based on the std::vector storage.
 
    40   /// The VectorMap template class is graph map structure what
 
    41   /// automatically updates the map when a key is added to or erased from
 
    42   /// the map. This map factory uses the allocators to implement 
 
    43   /// the container functionality. This map factory
 
    44   /// uses the std::vector to implement the container function.
 
    46   /// \param Notifier The AlterationNotifier that will notify this map.
 
    47   /// \param Item The item type of the graph items.
 
    48   /// \param Value The value type of the map.
 
    50   /// \author Balazs Dezso  	
 
    51   template <typename _Graph, typename _Item, typename _Value>
 
    53     : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
 
    56     /// The container type of the map.
 
    57     typedef std::vector<_Value> Container;	
 
    61     /// The graph type of the map. 
 
    63     /// The item type of the map.
 
    65     /// The reference map tag.
 
    66     typedef True ReferenceMapTag;
 
    68     /// The key type of the map.
 
    70     /// The value type of the map.
 
    73     /// The notifier type.
 
    74     typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
 
    77     typedef VectorMap Map;
 
    78     /// The base class of the map.
 
    79     typedef typename Notifier::ObserverBase Parent;
 
    81     /// The reference type of the map;
 
    82     typedef typename Container::reference Reference;
 
    83     /// The const reference type of the map;
 
    84     typedef typename Container::const_reference ConstReference;
 
    87     /// \brief Constructor to attach the new map into the notifier.
 
    89     /// It constructs a map and attachs it into the notifier.
 
    90     /// It adds all the items of the graph to the map.
 
    91     VectorMap(const Graph& graph) {
 
    92       Parent::attach(graph.getNotifier(Item()));
 
    93       container.resize(Parent::getNotifier()->maxId() + 1);
 
    96     /// \brief Constructor uses given value to initialize the map. 
 
    98     /// It constructs a map uses a given value to initialize the map. 
 
    99     /// It adds all the items of the graph to the map.
 
   100     VectorMap(const Graph& graph, const Value& value) {
 
   101       Parent::attach(graph.getNotifier(Item()));
 
   102       container.resize(Parent::getNotifier()->maxId() + 1, value);
 
   105     /// \brief Copy constructor
 
   107     /// Copy constructor.
 
   108     VectorMap(const VectorMap& _copy) : Parent() {
 
   109       if (_copy.attached()) {
 
   110 	Parent::attach(*_copy.getNotifier());
 
   111 	container = _copy.container;
 
   117     VectorMap& operator=(const VectorMap&);
 
   121     /// \brief The subcript operator.
 
   123     /// The subscript operator. The map can be subscripted by the
 
   124     /// actual items of the graph.      
 
   125     Reference operator[](const Key& key) {
 
   126       return container[Parent::getNotifier()->id(key)];
 
   129     /// \brief The const subcript operator.
 
   131     /// The const subscript operator. The map can be subscripted by the
 
   132     /// actual items of the graph. 
 
   133     ConstReference operator[](const Key& key) const {
 
   134       return container[Parent::getNotifier()->id(key)];
 
   138     /// \brief The setter function of the map.
 
   140     /// It the same as operator[](key) = value expression.
 
   141     void set(const Key& key, const Value& value) {
 
   142       (*this)[key] = value;
 
   147     /// \brief Adds a new key to the map.
 
   149     /// It adds a new key to the map. It called by the observer notifier
 
   150     /// and it overrides the add() member function of the observer base.     
 
   151     virtual void add(const Key& key) {
 
   152       int id = Parent::getNotifier()->id(key);
 
   153       if (id >= (int)container.size()) {
 
   154 	container.resize(id + 1);
 
   158     /// \brief Adds more new keys to the map.
 
   160     /// It adds more new keys to the map. It called by the observer notifier
 
   161     /// and it overrides the add() member function of the observer base.     
 
   162     virtual void add(const std::vector<Key>& keys) {
 
   163       int max = container.size() - 1;
 
   164       for (int i = 0; i < (int)keys.size(); ++i) {
 
   165         int id = Parent::getNotifier()->id(keys[i]);
 
   170       container.resize(max + 1);
 
   173     /// \brief Erase a key from the map.
 
   175     /// Erase a key from the map. It called by the observer notifier
 
   176     /// and it overrides the erase() member function of the observer base.     
 
   177     virtual void erase(const Key& key) {
 
   178       container[Parent::getNotifier()->id(key)] = Value();
 
   181     /// \brief Erase more keys from the map.
 
   183     /// Erase more keys from the map. It called by the observer notifier
 
   184     /// and it overrides the erase() member function of the observer base.     
 
   185     virtual void erase(const std::vector<Key>& keys) {
 
   186       for (int i = 0; i < (int)keys.size(); ++i) {
 
   187 	container[Parent::getNotifier()->id(keys[i])] = Value();
 
   191     /// \brief Buildes the map.
 
   193     /// It buildes the map. It called by the observer notifier
 
   194     /// and it overrides the build() member function of the observer base.
 
   195     virtual void build() { 
 
   196       int size = Parent::getNotifier()->maxId() + 1;
 
   197       container.reserve(size);
 
   198       container.resize(size);
 
   201     /// \brief Clear the map.
 
   203     /// It erase all items from the map. It called by the observer notifier
 
   204     /// and it overrides the clear() member function of the observer base.     
 
   205     virtual void clear() {