COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/vector_map.h @ 1164:80bb73097736

Last change on this file since 1164:80bb73097736 was 1164:80bb73097736, checked in by Alpar Juttner, 15 years ago

A year has passed again.

File size: 7.1 KB
RevLine 
[906]1/* -*- C++ -*-
[921]2 * src/lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
[906]3 *
[1164]4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[906]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
[1039]23#include <lemon/alteration_notifier.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  ///
[1039]40  /// \param Registry The AlterationNotifier that will notify this map.
[946]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>
[1039]50  class VectorMap : public AlterationNotifier<_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.
[987]56    typedef _Item Key;
[946]57    /// The id map type of the map.
[1039]58    typedef AlterationNotifier<_Item> Registry;
[946]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 reference type of the map;
[987]75    typedef typename Container::reference Reference;
[822]76    /// The pointer type of the map;
[987]77    typedef typename Container::pointer Pointer;
[822]78
79    /// The const value type of the map.
[987]80    typedef const Value ConstValue;
[822]81    /// The const reference type of the map;
[987]82    typedef typename Container::const_reference ConstReference;
[822]83    /// The pointer type of the map;
[987]84    typedef typename Container::const_pointer ConstPointer;
[822]85
[946]86    /// Constructor to attach the new map into the registry.
[937]87
[946]88    /// It construates a map and attachs it into the registry.
89    /// It adds all the items of the graph to the map.
90     
[980]91    VectorMap(const Graph& _g) : graph(&_g) {
[1039]92      attach(_g.getNotifier(_Item()));
[946]93      build();
94    }
[822]95
[937]96    /// Constructor uses given value to initialize the map.
97
[946]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     
[980]101    VectorMap(const Graph& _g, const Value& _v) : graph(&_g) {
[1039]102      attach(_g.getNotifier(_Item()));
[980]103      container.resize(graph->maxId(_Item()) + 1, _v);
[946]104    }
[822]105
[980]106    VectorMap(const VectorMap& _copy) : graph(_copy.getGraph()) {
107      if (_copy.attached()) {
108        attach(*_copy.getRegistry());
109        container = _copy.container;
[822]110      }
111    }
112
[978]113    using Parent::attach;
114    using Parent::detach;
115    using Parent::attached;
[937]116
[946]117    /** Assign operator to copy a map of the same map type.
[822]118     */
[946]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        }
[897]129      }
[946]130      container = copy.container;
131
132      return *this;
133    }
134
135
136    virtual ~VectorMap() {
137      if (attached()) {
138        detach();
[822]139      }
[946]140    }
141
142    const Graph* getGraph() const {
143      return graph;
[822]144    }
[937]145
146    /// The subcript operator.
147
[946]148    /// The subscript operator. The map can be subscripted by the
149    /// actual items of the graph.
150     
[987]151    Reference operator[](const Key& key) {
[980]152      return container[graph->id(key)];
[822]153    }
154               
[937]155    /// The const subcript operator.
156
[946]157    /// The const subscript operator. The map can be subscripted by the
158    /// actual items of the graph.
159     
[987]160    ConstReference operator[](const Key& key) const {
[980]161      return container[graph->id(key)];
[822]162    }
163
[937]164
[946]165    /// The setter function of the map.
166
167    /// It the same as operator[](key) = value expression.
168    ///
169     
[987]170    void set(const Key& key, const Value& value) {
[946]171      (*this)[key] = value;
[822]172    }
[946]173
[937]174    /// Adds a new key to the map.
[822]175               
[946]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     
[987]179    void add(const Key& key) {
[980]180      int id = graph->id(key);
[822]181      if (id >= (int)container.size()) {
182        container.resize(id + 1);
183      }
184    }
[937]185
186    /// Erases a key from the map.
[822]187               
[946]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.     
[987]190    void erase(const Key&) {}
[822]191
[946]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.
[937]196
[946]197    void build() {
[980]198      container.resize(graph->maxId(_Item()) + 1);
[946]199    }
[937]200
[946]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.     
[822]205    void clear() {
206      container.clear();
207    }
208
209  private:
210               
211    Container container;
[946]212    const Graph *graph;
[822]213
[946]214  };
215
216
217  template <typename _Base>
218  class VectorMappableGraphExtender : public _Base {
[844]219  public:
[946]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;
[1039]227    typedef typename Parent::NodeNotifier NodeObserverRegistry;
[946]228
229    typedef typename Parent::Edge Edge;
230    typedef typename Parent::EdgeIt EdgeIt;
231    typedef typename Parent::EdgeIdMap EdgeIdMap;
[1039]232    typedef typename Parent::EdgeNotifier EdgeObserverRegistry;
[946]233
234   
235
236    template <typename _Value>
[980]237    class NodeMap : public VectorMap<Graph, Node, _Value> {
[946]238    public:
239      typedef VectorMappableGraphExtender<_Base> Graph;
240
241      typedef typename Graph::Node Node;
242
[980]243      typedef VectorMap<Graph, Node, _Value> Parent;
[946]244
[979]245      //typedef typename Parent::Graph Graph;
[946]246      typedef typename Parent::Value Value;
247
248      NodeMap(const Graph& g)
[980]249        : Parent(g) {}
[946]250      NodeMap(const Graph& g, const Value& v)
[980]251        : Parent(g, v) {}
[946]252
253    };
254
255    template <typename _Value>
[980]256    class EdgeMap : public VectorMap<Graph, Edge, _Value> {
[946]257    public:
258      typedef VectorMappableGraphExtender<_Base> Graph;
259
260      typedef typename Graph::Edge Edge;
261
[980]262      typedef VectorMap<Graph, Edge, _Value> Parent;
[946]263
[979]264      //typedef typename Parent::Graph Graph;
[946]265      typedef typename Parent::Value Value;
266
267      EdgeMap(const Graph& g)
[980]268        : Parent(g) {}
[946]269      EdgeMap(const Graph& g, const Value& v)
[980]270        : Parent(g, v) {}
[946]271
272    };
273   
[822]274  };
275 
276  /// @}
277 
278}
279
280#endif
Note: See TracBrowser for help on using the repository browser.