lemon/bits/vector_map.h
author deba
Wed, 01 Mar 2006 10:25:30 +0000
changeset 1991 d7442141d9ef
parent 1946 17eb3eaad9f8
child 1993 2115143eceea
permissions -rw-r--r--
The graph adadptors can be alteration observed.
In most cases it uses the adapted graph alteration notifiers.
Only special case is now the UndirGraphAdaptor, where
we have to proxy the signals from the graph.

The SubBidirGraphAdaptor is removed, because it doest not
gives more feature than the EdgeSubGraphAdaptor<UndirGraphAdaptor<Graph>>.

The ResGraphAdaptor is based on this composition.
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     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_VECTOR_MAP_H
    20 #define LEMON_VECTOR_MAP_H
    21 
    22 #include <vector>
    23 #include <algorithm>
    24 
    25 #include <lemon/utility.h>
    26 #include <lemon/bits/map_extender.h>
    27 #include <lemon/bits/alteration_notifier.h>
    28 #include <lemon/concept_check.h>
    29 #include <lemon/concept/maps.h>
    30 
    31 /// \ingroup graphmapfactory
    32 ///
    33 ///\file
    34 ///\brief Vector based graph maps.
    35 
    36 namespace lemon {
    37 
    38   /// \ingroup graphmapfactory
    39   ///
    40   /// \brief Graph map based on the std::vector storage.
    41   ///
    42   /// The VectorMap template class is graph map structure what
    43   /// automatically indates the map when a key is added to or erased from
    44   /// the map. This map factory uses the allocators to implement 
    45   /// the container functionality. This map factory
    46   /// uses the std::vector to implement the container function.
    47   ///
    48   /// \param Registry The AlterationNotifier that will notify this map.
    49   /// \param Item The item type of the graph items.
    50   /// \param Value The value type of the map.
    51   /// 
    52   /// \author Balazs Dezso
    53   	
    54   template <
    55     typename _Graph, 
    56     typename _Item,    
    57     typename _Value
    58     >
    59   class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
    60   private:
    61 		
    62     /// The container type of the map.
    63     typedef std::vector<_Value> Container;	
    64 
    65   public:
    66 
    67     /// The graph type of the map. 
    68     typedef _Graph Graph;
    69     /// The reference map tag.
    70     typedef True ReferenceMapTag;
    71 
    72     /// The key type of the map.
    73     typedef _Item Key;
    74     /// The value type of the map.
    75     typedef _Value Value;
    76 
    77     typedef AlterationNotifier<_Item> Registry;
    78 
    79     /// The map type.
    80     typedef VectorMap Map;
    81     /// The base class of the map.
    82     typedef typename Registry::ObserverBase Parent;
    83 
    84     /// The reference type of the map;
    85     typedef typename Container::reference Reference;
    86     /// The pointer type of the map;
    87     typedef typename Container::pointer Pointer;
    88 
    89     /// The const value type of the map.
    90     typedef const Value ConstValue;
    91     /// The const reference type of the map;
    92     typedef typename Container::const_reference ConstReference;
    93     /// The pointer type of the map;
    94     typedef typename Container::const_pointer ConstPointer;
    95 
    96 
    97     /// \brief Constructor to attach the new map into the registry.
    98     ///
    99     /// It constructs a map and attachs it into the registry.
   100     /// It adds all the items of the graph to the map.
   101     VectorMap(const Graph& _g) : graph(&_g) {
   102       attach(_g.getNotifier(_Item()));
   103       build();
   104     }
   105 
   106     /// \brief Constructor uses given value to initialize the map. 
   107     ///
   108     /// It constructs a map uses a given value to initialize the map. 
   109     /// It adds all the items of the graph to the map.
   110     VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { 
   111       attach(_g.getNotifier(_Item()));
   112       container.resize(graph->maxId(_Item()) + 1, _v);
   113     }
   114 
   115     /// \brief Copy constructor
   116     ///
   117     /// Copy constructor.
   118     VectorMap(const VectorMap& _copy) 
   119       : Parent(), graph(_copy.getGraph()) {
   120       if (_copy.attached()) {
   121 	attach(*_copy.getRegistry());
   122 	container = _copy.container;
   123       }
   124     }
   125 
   126     /// \brief Destrcutor
   127     ///
   128     /// Destructor.
   129     virtual ~VectorMap() {
   130       if (attached()) {
   131 	detach();
   132       }
   133     }
   134 
   135 
   136   private:
   137 
   138     VectorMap& operator=(const VectorMap&);
   139 
   140   protected:
   141 
   142     using Parent::attach;
   143     using Parent::detach;
   144     using Parent::attached;
   145 
   146     const Graph* getGraph() const {
   147       return graph;
   148     }
   149 
   150   public:
   151 
   152     /// \brief The subcript operator.
   153     ///
   154     /// The subscript operator. The map can be subscripted by the
   155     /// actual items of the graph.      
   156     Reference operator[](const Key& key) {
   157       return container[graph->id(key)];
   158     } 
   159 		
   160     /// \brief The const subcript operator.
   161     ///
   162     /// The const subscript operator. The map can be subscripted by the
   163     /// actual items of the graph. 
   164     ConstReference operator[](const Key& key) const {
   165       return container[graph->id(key)];
   166     }
   167 
   168 
   169     /// \brief The setter function of the map.
   170     ///
   171     /// It the same as operator[](key) = value expression.
   172     void set(const Key& key, const Value& value) {
   173       (*this)[key] = value;
   174     }
   175 
   176   protected:
   177 
   178     /// \brief Adds a new key to the map.
   179     ///		
   180     /// It adds a new key to the map. It called by the observer registry
   181     /// and it overrides the add() member function of the observer base.     
   182     virtual void add(const Key& key) {
   183       int id = graph->id(key);
   184       if (id >= (int)container.size()) {
   185 	container.resize(id + 1);
   186       }
   187     }
   188 
   189     /// \brief Adds more new keys to the map.
   190     ///		
   191     /// It adds more new keys to the map. It called by the observer registry
   192     /// and it overrides the add() member function of the observer base.     
   193     virtual void add(const std::vector<Key>& keys) {
   194       for (int i = 0; i < (int)keys.size(); ++i) {
   195 	add(keys[i]);
   196       }
   197     }
   198 
   199     /// \brief Erase a key from the map.
   200     ///
   201     /// Erase a key from the map. It called by the observer registry
   202     /// and it overrides the erase() member function of the observer base.     
   203     virtual void erase(const Key&) {}
   204 
   205     /// \brief Erase more keys from the map.
   206     ///
   207     /// Erase more keys from the map. It called by the observer registry
   208     /// and it overrides the erase() member function of the observer base.     
   209     virtual void erase(const std::vector<Key>&) {}
   210     
   211     /// \brief Buildes the map.
   212     ///	
   213     /// It buildes the map. It called by the observer registry
   214     /// and it overrides the build() member function of the observer base.
   215     virtual void build() { 
   216       container.resize(graph->maxId(_Item()) + 1);
   217     }
   218 
   219     /// \brief Clear the map.
   220     ///
   221     /// It erase all items from the map. It called by the observer registry
   222     /// and it overrides the clear() member function of the observer base.     
   223     virtual void clear() { 
   224       container.clear();
   225     }
   226     
   227   private:
   228 		
   229     Container container;
   230     const Graph *graph;
   231 
   232   };
   233 
   234 }
   235 
   236 #endif