Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

vector_map.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  * src/lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
00003  *
00004  * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00005  * (Egervary Combinatorial Optimization Research Group, EGRES).
00006  *
00007  * Permission to use, modify and distribute this software is granted
00008  * provided that this copyright notice appears in all copies. For
00009  * precise terms see the accompanying LICENSE file.
00010  *
00011  * This software is provided "AS IS" with no warranty of any kind,
00012  * express or implied, and with no claim as to its suitability for any
00013  * purpose.
00014  *
00015  */
00016 
00017 #ifndef LEMON_VECTOR_MAP_H
00018 #define LEMON_VECTOR_MAP_H
00019 
00020 #include <vector>
00021 #include <algorithm>
00022 
00023 #include <lemon/alteration_notifier.h>
00024 
00028 
00029 namespace lemon {
00030   
00033   
00045         
00046 
00047   template <typename _Graph, 
00048             typename _Item,
00049             typename _Value>
00050   class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
00051   public:
00052                 
00054     typedef _Graph Graph;
00056     typedef _Item Key;
00058     typedef AlterationNotifier<_Item> Registry;
00060     typedef _Value Value;
00061 
00063     typedef VectorMap Map;
00065     typedef typename Registry::ObserverBase Parent;
00066 
00067   private:
00068                 
00070     typedef std::vector<Value> Container;       
00071 
00072   public:
00073 
00075     typedef typename Container::reference Reference;
00077     typedef typename Container::pointer Pointer;
00078 
00080     typedef const Value ConstValue;
00082     typedef typename Container::const_reference ConstReference;
00084     typedef typename Container::const_pointer ConstPointer;
00085 
00087 
00090      
00091     VectorMap(const Graph& _g) : graph(&_g) {
00092       attach(_g.getNotifier(_Item()));
00093       build();
00094     }
00095 
00097 
00100      
00101     VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { 
00102       attach(_g.getNotifier(_Item()));
00103       container.resize(graph->maxId(_Item()) + 1, _v);
00104     }
00105 
00106     VectorMap(const VectorMap& _copy) : graph(_copy.getGraph()) {
00107       if (_copy.attached()) {
00108         attach(*_copy.getRegistry());
00109         container = _copy.container;
00110       }
00111     }
00112 
00113     using Parent::attach;
00114     using Parent::detach;
00115     using Parent::attached;
00116 
00119     VectorMap& operator=(const VectorMap& copy) {
00120       if (&copy == this) return *this;
00121       
00122       if (graph != copy.graph) {
00123         if (attached()) {
00124           detach();
00125         }
00126         if (copy.attached()) {
00127           attach(*copy.getRegistry());
00128         }
00129       }
00130       container = copy.container;
00131 
00132       return *this;
00133     }
00134 
00135 
00136     virtual ~VectorMap() {
00137       if (attached()) {
00138         detach();
00139       }
00140     }
00141 
00142     const Graph* getGraph() const {
00143       return graph;
00144     }
00145 
00147 
00150      
00151     Reference operator[](const Key& key) {
00152       return container[graph->id(key)];
00153     } 
00154                 
00156 
00159      
00160     ConstReference operator[](const Key& key) const {
00161       return container[graph->id(key)];
00162     }
00163 
00164 
00166 
00169      
00170     void set(const Key& key, const Value& value) {
00171       (*this)[key] = value;
00172     }
00173 
00175                 
00178      
00179     void add(const Key& key) {
00180       int id = graph->id(key);
00181       if (id >= (int)container.size()) {
00182         container.resize(id + 1);
00183       }
00184     }
00185 
00187                 
00190     void erase(const Key&) {}
00191 
00193                 
00196 
00197     void build() { 
00198       container.resize(graph->maxId(_Item()) + 1);
00199     }
00200 
00202 
00205     void clear() { 
00206       container.clear();
00207     }
00208 
00209   private:
00210                 
00211     Container container;
00212     const Graph *graph;
00213 
00214   };
00215 
00216 
00217   template <typename _Base> 
00218   class VectorMappableGraphExtender : public _Base {
00219   public:
00220 
00221     typedef VectorMappableGraphExtender<_Base> Graph;
00222     typedef _Base Parent;
00223 
00224     typedef typename Parent::Node Node;
00225     typedef typename Parent::NodeIt NodeIt;
00226     typedef typename Parent::NodeIdMap NodeIdMap;
00227     typedef typename Parent::NodeNotifier NodeObserverRegistry;
00228 
00229     typedef typename Parent::Edge Edge;
00230     typedef typename Parent::EdgeIt EdgeIt;
00231     typedef typename Parent::EdgeIdMap EdgeIdMap;
00232     typedef typename Parent::EdgeNotifier EdgeObserverRegistry;
00233 
00234     
00235 
00236     template <typename _Value>
00237     class NodeMap : public VectorMap<Graph, Node, _Value> {
00238     public:
00239       typedef VectorMappableGraphExtender<_Base> Graph;
00240 
00241       typedef typename Graph::Node Node;
00242 
00243       typedef VectorMap<Graph, Node, _Value> Parent;
00244 
00245       //typedef typename Parent::Graph Graph;
00246       typedef typename Parent::Value Value;
00247 
00248       NodeMap(const Graph& g) 
00249         : Parent(g) {}
00250       NodeMap(const Graph& g, const Value& v) 
00251         : Parent(g, v) {}
00252 
00253     };
00254 
00255     template <typename _Value>
00256     class EdgeMap : public VectorMap<Graph, Edge, _Value> {
00257     public:
00258       typedef VectorMappableGraphExtender<_Base> Graph;
00259 
00260       typedef typename Graph::Edge Edge;
00261 
00262       typedef VectorMap<Graph, Edge, _Value> Parent;
00263 
00264       //typedef typename Parent::Graph Graph;
00265       typedef typename Parent::Value Value;
00266 
00267       EdgeMap(const Graph& g) 
00268         : Parent(g) {}
00269       EdgeMap(const Graph& g, const Value& v) 
00270         : Parent(g, v) {}
00271 
00272     };
00273     
00274   };
00275   
00277   
00278 }
00279 
00280 #endif

Generated on Sat Mar 19 10:58:41 2005 for LEMON by  doxygen 1.4.1