src/lemon/vector_map.h
author athos
Wed, 23 Mar 2005 11:51:40 +0000
changeset 1247 60708e1475ae
parent 1039 bd01c5a3f989
child 1267 a93f94dbe3d3
permissions -rw-r--r--
Completions.
     1 /* -*- C++ -*-
     2  * src/lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Combinatorial Optimization Research Group, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_VECTOR_MAP_H
    18 #define LEMON_VECTOR_MAP_H
    19 
    20 #include <vector>
    21 #include <algorithm>
    22 
    23 #include <lemon/alteration_notifier.h>
    24 
    25 ///\ingroup graphmaps
    26 ///\file
    27 ///\brief Vector based graph maps.
    28 
    29 namespace lemon {
    30   
    31   /// \addtogroup graphmaps
    32   /// @{
    33   
    34   /// The VectorMap template class is graph map structure what
    35   /// automatically updates the map when a key is added to or erased from
    36   /// the map. This map factory uses the allocators to implement 
    37   /// the container functionality. This map factory
    38   /// uses the std::vector to implement the container function.
    39   ///
    40   /// \param Registry The AlterationNotifier that will notify this map.
    41   /// \param IdMap The IdMap type of the graph items.
    42   /// \param Value The value type of the map.
    43   /// 
    44   /// \author Balazs Dezso
    45   	
    46 
    47   template <typename _Graph, 
    48 	    typename _Item,
    49 	    typename _Value>
    50   class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
    51   public:
    52 		
    53     /// The graph type of the map. 
    54     typedef _Graph Graph;
    55     /// The key type of the map.
    56     typedef _Item Key;
    57     /// The id map type of the map.
    58     typedef AlterationNotifier<_Item> Registry;
    59     /// The value type of the map.
    60     typedef _Value Value;
    61 
    62     /// The map type.
    63     typedef VectorMap Map;
    64     /// The base class of the map.
    65     typedef typename Registry::ObserverBase Parent;
    66 
    67   private:
    68 		
    69     /// The container type of the map.
    70     typedef std::vector<Value> Container;	
    71 
    72   public:
    73 
    74     /// The reference type of the map;
    75     typedef typename Container::reference Reference;
    76     /// The pointer type of the map;
    77     typedef typename Container::pointer Pointer;
    78 
    79     /// The const value type of the map.
    80     typedef const Value ConstValue;
    81     /// The const reference type of the map;
    82     typedef typename Container::const_reference ConstReference;
    83     /// The pointer type of the map;
    84     typedef typename Container::const_pointer ConstPointer;
    85 
    86     /// Constructor to attach the new map into the registry.
    87 
    88     /// It construates a map and attachs it into the registry.
    89     /// It adds all the items of the graph to the map.
    90      
    91     VectorMap(const Graph& _g) : graph(&_g) {
    92       attach(_g.getNotifier(_Item()));
    93       build();
    94     }
    95 
    96     /// Constructor uses given value to initialize the map. 
    97 
    98     /// It construates a map uses a given value to initialize the map. 
    99     /// It adds all the items of the graph to the map.
   100      
   101     VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { 
   102       attach(_g.getNotifier(_Item()));
   103       container.resize(graph->maxId(_Item()) + 1, _v);
   104     }
   105 
   106     VectorMap(const VectorMap& _copy) : graph(_copy.getGraph()) {
   107       if (_copy.attached()) {
   108 	attach(*_copy.getRegistry());
   109 	container = _copy.container;
   110       }
   111     }
   112 
   113     using Parent::attach;
   114     using Parent::detach;
   115     using Parent::attached;
   116 
   117     /** Assign operator to copy a map of the same map type.
   118      */
   119     VectorMap& operator=(const VectorMap& copy) {
   120       if (&copy == this) return *this;
   121       
   122       if (graph != copy.graph) {
   123 	if (attached()) {
   124 	  detach();
   125 	}
   126 	if (copy.attached()) {
   127 	  attach(*copy.getRegistry());
   128 	}
   129       }
   130       container = copy.container;
   131 
   132       return *this;
   133     }
   134 
   135 
   136     virtual ~VectorMap() {
   137       if (attached()) {
   138 	detach();
   139       }
   140     }
   141 
   142     const Graph* getGraph() const {
   143       return graph;
   144     }
   145 
   146     /// The subcript operator.
   147 
   148     /// The subscript operator. The map can be subscripted by the
   149     /// actual items of the graph. 
   150      
   151     Reference operator[](const Key& key) {
   152       return container[graph->id(key)];
   153     } 
   154 		
   155     /// The const subcript operator.
   156 
   157     /// The const subscript operator. The map can be subscripted by the
   158     /// actual items of the graph. 
   159      
   160     ConstReference operator[](const Key& key) const {
   161       return container[graph->id(key)];
   162     }
   163 
   164 
   165     /// The setter function of the map.
   166 
   167     /// It the same as operator[](key) = value expression.
   168     ///
   169      
   170     void set(const Key& key, const Value& value) {
   171       (*this)[key] = value;
   172     }
   173 
   174     /// Adds a new key to the map.
   175 		
   176     /// It adds a new key to the map. It called by the observer registry
   177     /// and it overrides the add() member function of the observer base.
   178      
   179     void add(const Key& key) {
   180       int id = graph->id(key);
   181       if (id >= (int)container.size()) {
   182 	container.resize(id + 1);
   183       }
   184     }
   185 
   186     /// Erases a key from the map.
   187 		
   188     /// Erase a key from the map. It called by the observer registry
   189     /// and it overrides the erase() member function of the observer base.     
   190     void erase(const Key&) {}
   191 
   192     /// Buildes the map.
   193 		
   194     /// It buildes the map. It called by the observer registry
   195     /// and it overrides the build() member function of the observer base.
   196 
   197     void build() { 
   198       container.resize(graph->maxId(_Item()) + 1);
   199     }
   200 
   201     /// Clear the map.
   202 
   203     /// It erase all items from the map. It called by the observer registry
   204     /// and it overrides the clear() member function of the observer base.     
   205     void clear() { 
   206       container.clear();
   207     }
   208 
   209   private:
   210 		
   211     Container container;
   212     const Graph *graph;
   213 
   214   };
   215 
   216 
   217   template <typename _Base> 
   218   class VectorMappableGraphExtender : public _Base {
   219   public:
   220 
   221     typedef VectorMappableGraphExtender<_Base> Graph;
   222     typedef _Base Parent;
   223 
   224     typedef typename Parent::Node Node;
   225     typedef typename Parent::NodeIt NodeIt;
   226     typedef typename Parent::NodeIdMap NodeIdMap;
   227     typedef typename Parent::NodeNotifier NodeObserverRegistry;
   228 
   229     typedef typename Parent::Edge Edge;
   230     typedef typename Parent::EdgeIt EdgeIt;
   231     typedef typename Parent::EdgeIdMap EdgeIdMap;
   232     typedef typename Parent::EdgeNotifier EdgeObserverRegistry;
   233 
   234     
   235 
   236     template <typename _Value>
   237     class NodeMap : public VectorMap<Graph, Node, _Value> {
   238     public:
   239       typedef VectorMappableGraphExtender<_Base> Graph;
   240 
   241       typedef typename Graph::Node Node;
   242 
   243       typedef VectorMap<Graph, Node, _Value> Parent;
   244 
   245       //typedef typename Parent::Graph Graph;
   246       typedef typename Parent::Value Value;
   247 
   248       NodeMap(const Graph& g) 
   249 	: Parent(g) {}
   250       NodeMap(const Graph& g, const Value& v) 
   251 	: Parent(g, v) {}
   252 
   253     };
   254 
   255     template <typename _Value>
   256     class EdgeMap : public VectorMap<Graph, Edge, _Value> {
   257     public:
   258       typedef VectorMappableGraphExtender<_Base> Graph;
   259 
   260       typedef typename Graph::Edge Edge;
   261 
   262       typedef VectorMap<Graph, Edge, _Value> Parent;
   263 
   264       //typedef typename Parent::Graph Graph;
   265       typedef typename Parent::Value Value;
   266 
   267       EdgeMap(const Graph& g) 
   268 	: Parent(g) {}
   269       EdgeMap(const Graph& g, const Value& v) 
   270 	: Parent(g, v) {}
   271 
   272     };
   273     
   274   };
   275   
   276   /// @}
   277   
   278 }
   279 
   280 #endif