COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/vector_map.h @ 986:e997802b855c

Last change on this file since 986:e997802b855c was 980:0f1044b7a3af, checked in by Balazs Dezso, 16 years ago

maxNodeId() and maxEdgeId() changed to maxId(Node) and maxId(Edge)
getNodeObserverRegistry() and getEdgeObserverRegistry() changed to
getObserverRegistry(Node) and getObserverRegistry(Edge)

IdMappableGraphExtender? erased

File size: 7.3 KB
RevLine 
[906]1/* -*- C++ -*-
[921]2 * src/lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
[906]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
[921]17#ifndef LEMON_VECTOR_MAP_H
18#define LEMON_VECTOR_MAP_H
[822]19
20#include <vector>
[946]21#include <algorithm>
[822]22
[946]23#include <lemon/alteration_observer_registry.h>
[822]24
25///\ingroup graphmaps
26///\file
27///\brief Vector based graph maps.
28
[921]29namespace lemon {
[822]30 
31  /// \addtogroup graphmaps
32  /// @{
33 
[946]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 _Value>
50  class VectorMap : public AlterationObserverRegistry<_Item>::ObserverBase {
[822]51  public:
52               
[946]53    /// The graph type of the map.
54    typedef _Graph Graph;
55    /// The key type of the map.
56    typedef _Item KeyType;
57    /// The id map type of the map.
58    typedef AlterationObserverRegistry<_Item> Registry;
59    /// The value type of the map.
60    typedef _Value Value;
[822]61
62    /// The map type.
63    typedef VectorMap Map;
[946]64    /// The base class of the map.
65    typedef typename Registry::ObserverBase Parent;
[822]66
67  private:
68               
69    /// The container type of the map.
70    typedef std::vector<Value> Container;       
71
72  public:
73
74    /// The value type of the map.
75    typedef Value ValueType;
76    /// The reference type of the map;
77    typedef typename Container::reference ReferenceType;
78    /// The pointer type of the map;
79    typedef typename Container::pointer PointerType;
80
81    /// The const value type of the map.
82    typedef const Value ConstValueType;
83    /// The const reference type of the map;
84    typedef typename Container::const_reference ConstReferenceType;
85    /// The pointer type of the map;
86    typedef typename Container::const_pointer ConstPointerType;
87
[946]88    /// Constructor to attach the new map into the registry.
[937]89
[946]90    /// It construates a map and attachs it into the registry.
91    /// It adds all the items of the graph to the map.
92     
[980]93    VectorMap(const Graph& _g) : graph(&_g) {
94      attach(_g.getObserverRegistry(_Item()));
[946]95      build();
96    }
[822]97
[937]98    /// Constructor uses given value to initialize the map.
99
[946]100    /// It construates a map uses a given value to initialize the map.
101    /// It adds all the items of the graph to the map.
102     
[980]103    VectorMap(const Graph& _g, const Value& _v) : graph(&_g) {
104      attach(_g.getObserverRegistry(_Item()));
105      container.resize(graph->maxId(_Item()) + 1, _v);
[946]106    }
[822]107
[980]108    VectorMap(const VectorMap& _copy) : graph(_copy.getGraph()) {
109      if (_copy.attached()) {
110        attach(*_copy.getRegistry());
111        container = _copy.container;
[822]112      }
113    }
114
[978]115    using Parent::attach;
116    using Parent::detach;
117    using Parent::attached;
[937]118
[946]119    /** Assign operator to copy a map of the same map type.
[822]120     */
[946]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        }
[897]131      }
[946]132      container = copy.container;
133
134      return *this;
135    }
136
137
138    virtual ~VectorMap() {
139      if (attached()) {
140        detach();
[822]141      }
[946]142    }
143
144    const Graph* getGraph() const {
145      return graph;
[822]146    }
[937]147
148    /// The subcript operator.
149
[946]150    /// The subscript operator. The map can be subscripted by the
151    /// actual items of the graph.
152     
[822]153    ReferenceType operator[](const KeyType& key) {
[980]154      return container[graph->id(key)];
[822]155    }
156               
[937]157    /// The const subcript operator.
158
[946]159    /// The const subscript operator. The map can be subscripted by the
160    /// actual items of the graph.
161     
[822]162    ConstReferenceType operator[](const KeyType& key) const {
[980]163      return container[graph->id(key)];
[822]164    }
165
[937]166
[946]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;
[822]174    }
[946]175
[937]176    /// Adds a new key to the map.
[822]177               
[946]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     
[822]181    void add(const KeyType& key) {
[980]182      int id = graph->id(key);
[822]183      if (id >= (int)container.size()) {
184        container.resize(id + 1);
185      }
186    }
[937]187
188    /// Erases a key from the map.
[822]189               
[946]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.     
[971]192    void erase(const KeyType&) {}
[822]193
[946]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.
[937]198
[946]199    void build() {
[980]200      container.resize(graph->maxId(_Item()) + 1);
[946]201    }
[937]202
[946]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.     
[822]207    void clear() {
208      container.clear();
209    }
210
211  private:
212               
213    Container container;
[946]214    const Graph *graph;
[822]215
[946]216  };
217
218
219  template <typename _Base>
220  class VectorMappableGraphExtender : public _Base {
[844]221  public:
[946]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>
[980]239    class NodeMap : public VectorMap<Graph, Node, _Value> {
[946]240    public:
241      typedef VectorMappableGraphExtender<_Base> Graph;
242
243      typedef typename Graph::Node Node;
244
[980]245      typedef VectorMap<Graph, Node, _Value> Parent;
[946]246
[979]247      //typedef typename Parent::Graph Graph;
[946]248      typedef typename Parent::Value Value;
249
250      NodeMap(const Graph& g)
[980]251        : Parent(g) {}
[946]252      NodeMap(const Graph& g, const Value& v)
[980]253        : Parent(g, v) {}
[946]254
255    };
256
257    template <typename _Value>
[980]258    class EdgeMap : public VectorMap<Graph, Edge, _Value> {
[946]259    public:
260      typedef VectorMappableGraphExtender<_Base> Graph;
261
262      typedef typename Graph::Edge Edge;
263
[980]264      typedef VectorMap<Graph, Edge, _Value> Parent;
[946]265
[979]266      //typedef typename Parent::Graph Graph;
[946]267      typedef typename Parent::Value Value;
268
269      EdgeMap(const Graph& g)
[980]270        : Parent(g) {}
[946]271      EdgeMap(const Graph& g, const Value& v)
[980]272        : Parent(g, v) {}
[946]273
274    };
275   
[822]276  };
277 
278  /// @}
279 
280}
281
282#endif
Note: See TracBrowser for help on using the repository browser.