src/lemon/vector_map.h
author alpar
Mon, 01 Nov 2004 17:57:19 +0000
changeset 953 d9c115e2eeaf
parent 937 d4e911acef3d
child 971 643d3192ebc8
permissions -rw-r--r--
- Named parameters and traits for Dijkstra
(in src/work/alpar/dijkstra.h to be swithced to src/lemon)
- doc/named-param.dox: Doxygen page for named parameters.
     1 /* -*- C++ -*-
     2  * src/lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2004 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_observer_registry.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 AlterationObserverRegistry 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 _IdMap,
    50 	    typename _Value>
    51   class VectorMap : public AlterationObserverRegistry<_Item>::ObserverBase {
    52   public:
    53 		
    54     /// The graph type of the map. 
    55     typedef _Graph Graph;
    56     /// The key type of the map.
    57     typedef _Item KeyType;
    58     /// The id map type of the map.
    59     typedef _IdMap IdMap;
    60     /// The registry type of the map.
    61     typedef AlterationObserverRegistry<_Item> Registry;
    62     /// The value type of the map.
    63     typedef _Value Value;
    64 
    65     /// The map type.
    66     typedef VectorMap Map;
    67     /// The base class of the map.
    68     typedef typename Registry::ObserverBase Parent;
    69 
    70   private:
    71 		
    72     /// The container type of the map.
    73     typedef std::vector<Value> Container;	
    74 
    75   public:
    76 
    77     /// The value type of the map.
    78     typedef Value ValueType;
    79     /// The reference type of the map;
    80     typedef typename Container::reference ReferenceType;
    81     /// The pointer type of the map;
    82     typedef typename Container::pointer PointerType;
    83 
    84     /// The const value type of the map.
    85     typedef const Value ConstValueType;
    86     /// The const reference type of the map;
    87     typedef typename Container::const_reference ConstReferenceType;
    88     /// The pointer type of the map;
    89     typedef typename Container::const_pointer ConstPointerType;
    90 
    91     /// Constructor to attach the new map into the registry.
    92 
    93     /// It construates a map and attachs it into the registry.
    94     /// It adds all the items of the graph to the map.
    95      
    96     VectorMap(const Graph& _g, Registry& _r) : graph(&_g) {
    97       attach(_r);
    98       build();
    99     }
   100 
   101     /// Constructor uses given value to initialize the map. 
   102 
   103     /// It construates a map uses a given value to initialize the map. 
   104     /// It adds all the items of the graph to the map.
   105      
   106     VectorMap(const Graph& g, Registry& r, const Value& v) : graph(&g) { 
   107       attach(r);
   108       container.resize(IdMap(*graph).maxId() + 1, v);
   109     }
   110 
   111     VectorMap(const VectorMap& copy) : graph(copy.getGraph()) {
   112       if (copy.attached()) {
   113 	attach(*copy.getRegistry());
   114 	container = copy.container;
   115       }
   116     }
   117 
   118 
   119     /** Assign operator to copy a map of the same map type.
   120      */
   121     VectorMap& operator=(const VectorMap& copy) {
   122       if (&copy == this) return *this;
   123       
   124       if (graph != copy.graph) {
   125 	if (attached()) {
   126 	  detach();
   127 	}
   128 	if (copy.attached()) {
   129 	  attach(*copy.getRegistry());
   130 	}
   131       }
   132       container = copy.container;
   133 
   134       return *this;
   135     }
   136 
   137 
   138     virtual ~VectorMap() {
   139       if (attached()) {
   140 	detach();
   141       }
   142     }
   143 
   144     const Graph* getGraph() const {
   145       return graph;
   146     }
   147 
   148     /// The subcript operator.
   149 
   150     /// The subscript operator. The map can be subscripted by the
   151     /// actual items of the graph. 
   152      
   153     ReferenceType operator[](const KeyType& key) {
   154       return container[IdMap(*graph)[key]];
   155     } 
   156 		
   157     /// The const subcript operator.
   158 
   159     /// The const subscript operator. The map can be subscripted by the
   160     /// actual items of the graph. 
   161      
   162     ConstReferenceType operator[](const KeyType& key) const {
   163       return container[IdMap(*graph)[key]];
   164     }
   165 
   166 
   167     /// The setter function of the map.
   168 
   169     /// It the same as operator[](key) = value expression.
   170     ///
   171      
   172     void set(const KeyType& key, const ValueType& value) {
   173       (*this)[key] = value;
   174     }
   175 
   176     /// Adds a new key to the map.
   177 		
   178     /// It adds a new key to the map. It called by the observer registry
   179     /// and it overrides the add() member function of the observer base.
   180      
   181     void add(const KeyType& key) {
   182       int id = IdMap(*graph)[key];
   183       if (id >= (int)container.size()) {
   184 	container.resize(id + 1);
   185       }
   186     }
   187 
   188     /// Erases a key from the map.
   189 		
   190     /// Erase a key from the map. It called by the observer registry
   191     /// and it overrides the erase() member function of the observer base.     
   192     void erase(const KeyType& key) {}
   193 
   194     /// Buildes the map.
   195 		
   196     /// It buildes the map. It called by the observer registry
   197     /// and it overrides the build() member function of the observer base.
   198 
   199     void build() { 
   200       container.resize(IdMap(*graph).maxId() + 1);
   201     }
   202 
   203     /// Clear the map.
   204 
   205     /// It erase all items from the map. It called by the observer registry
   206     /// and it overrides the clear() member function of the observer base.     
   207     void clear() { 
   208       container.clear();
   209     }
   210 
   211   private:
   212 		
   213     Container container;
   214     const Graph *graph;
   215 
   216   };
   217 
   218 
   219   template <typename _Base> 
   220   class VectorMappableGraphExtender : public _Base {
   221   public:
   222 
   223     typedef VectorMappableGraphExtender<_Base> Graph;
   224     typedef _Base Parent;
   225 
   226     typedef typename Parent::Node Node;
   227     typedef typename Parent::NodeIt NodeIt;
   228     typedef typename Parent::NodeIdMap NodeIdMap;
   229     typedef typename Parent::NodeObserverRegistry NodeObserverRegistry;
   230 
   231     typedef typename Parent::Edge Edge;
   232     typedef typename Parent::EdgeIt EdgeIt;
   233     typedef typename Parent::EdgeIdMap EdgeIdMap;
   234     typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry;
   235 
   236     
   237 
   238     template <typename _Value>
   239     class NodeMap : public VectorMap<Graph, Node, NodeIdMap, _Value> {
   240     public:
   241       typedef VectorMappableGraphExtender<_Base> Graph;
   242 
   243       typedef typename Graph::Node Node;
   244       typedef typename Graph::NodeIdMap NodeIdMap;
   245 
   246       typedef VectorMap<Graph, Node, NodeIdMap, _Value> Parent;
   247 
   248       typedef typename Parent::Graph Graph;
   249       typedef typename Parent::Value Value;
   250 
   251       NodeMap(const Graph& g) 
   252 	: Parent(g, g.getNodeObserverRegistry()) {}
   253       NodeMap(const Graph& g, const Value& v) 
   254 	: Parent(g, g.getNodeObserverRegistry(), v) {}
   255 
   256     };
   257 
   258     template <typename _Value>
   259     class EdgeMap : public VectorMap<Graph, Edge, EdgeIdMap, _Value> {
   260     public:
   261       typedef VectorMappableGraphExtender<_Base> Graph;
   262 
   263       typedef typename Graph::Edge Edge;
   264       typedef typename Graph::EdgeIdMap EdgeIdMap;
   265 
   266       typedef VectorMap<Graph, Edge, EdgeIdMap, _Value> Parent;
   267 
   268       typedef typename Parent::Graph Graph;
   269       typedef typename Parent::Value Value;
   270 
   271       EdgeMap(const Graph& g) 
   272 	: Parent(g, g.getEdgeObserverRegistry()) {}
   273       EdgeMap(const Graph& g, const Value& v) 
   274 	: Parent(g, g.getEdgeObserverRegistry(), v) {}
   275 
   276     };
   277     
   278   };
   279   
   280   /// @}
   281   
   282 }
   283 
   284 #endif