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