COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/bits/vector_map.h @ 1374:bcfa3980b432

Last change on this file since 1374:bcfa3980b432 was 1374:bcfa3980b432, checked in by Balazs Dezso, 15 years ago

Call the default constructor of the ObserverBase?.

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 *
[1164]4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[906]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
[1267]23#include <lemon/utility.h>
[1307]24#include <lemon/bits/map_iterator.h>
25#include <lemon/bits/alteration_notifier.h>
[822]26
27///\ingroup graphmaps
28///\file
29///\brief Vector based graph maps.
30
[921]31namespace lemon {
[822]32 
33  /// \addtogroup graphmaps
34  /// @{
35 
[946]36  /// The VectorMap template class is graph map structure what
37  /// automatically updates the map when a key is added to or erased from
38  /// the map. This map factory uses the allocators to implement
39  /// the container functionality. This map factory
40  /// uses the std::vector to implement the container function.
41  ///
[1039]42  /// \param Registry The AlterationNotifier that will notify this map.
[946]43  /// \param IdMap The IdMap type of the graph items.
44  /// \param Value The value type of the map.
45  ///
46  /// \author Balazs Dezso
47       
48
[1267]49  template <
50    typename _Graph,
51    typename _Item,   
52    typename _Value
53    >
[1039]54  class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
[822]55  public:
56               
[946]57    /// The graph type of the map.
58    typedef _Graph Graph;
59    /// The key type of the map.
[987]60    typedef _Item Key;
[946]61    /// The id map type of the map.
[1039]62    typedef AlterationNotifier<_Item> Registry;
[946]63    /// The value type of the map.
64    typedef _Value Value;
[822]65
66    /// The map type.
67    typedef VectorMap Map;
[946]68    /// The base class of the map.
69    typedef typename Registry::ObserverBase Parent;
[822]70
71  private:
72               
73    /// The container type of the map.
74    typedef std::vector<Value> Container;       
75
76  public:
77
78    /// The reference type of the map;
[987]79    typedef typename Container::reference Reference;
[822]80    /// The pointer type of the map;
[987]81    typedef typename Container::pointer Pointer;
[822]82
83    /// The const value type of the map.
[987]84    typedef const Value ConstValue;
[822]85    /// The const reference type of the map;
[987]86    typedef typename Container::const_reference ConstReference;
[822]87    /// The pointer type of the map;
[987]88    typedef typename Container::const_pointer ConstPointer;
[822]89
[1267]90    typedef True FullTypeTag;
91
[946]92    /// Constructor to attach the new map into the registry.
[937]93
[946]94    /// It construates a map and attachs it into the registry.
95    /// It adds all the items of the graph to the map.
96     
[980]97    VectorMap(const Graph& _g) : graph(&_g) {
[1039]98      attach(_g.getNotifier(_Item()));
[946]99      build();
100    }
[822]101
[937]102    /// Constructor uses given value to initialize the map.
103
[946]104    /// It construates a map uses a given value to initialize the map.
105    /// It adds all the items of the graph to the map.
106     
[980]107    VectorMap(const Graph& _g, const Value& _v) : graph(&_g) {
[1039]108      attach(_g.getNotifier(_Item()));
[980]109      container.resize(graph->maxId(_Item()) + 1, _v);
[946]110    }
[822]111
[1374]112    VectorMap(const VectorMap& _copy)
113      : Parent(), graph(_copy.getGraph()) {
[980]114      if (_copy.attached()) {
115        attach(*_copy.getRegistry());
116        container = _copy.container;
[822]117      }
118    }
119
[978]120    using Parent::attach;
121    using Parent::detach;
122    using Parent::attached;
[937]123
[946]124    /** Assign operator to copy a map of the same map type.
[822]125     */
[946]126    VectorMap& operator=(const VectorMap& copy) {
127      if (&copy == this) return *this;
128     
129      if (graph != copy.graph) {
130        if (attached()) {
131          detach();
132        }
133        if (copy.attached()) {
134          attach(*copy.getRegistry());
135        }
[897]136      }
[946]137      container = copy.container;
138
139      return *this;
140    }
141
142
143    virtual ~VectorMap() {
144      if (attached()) {
145        detach();
[822]146      }
[946]147    }
148
149    const Graph* getGraph() const {
150      return graph;
[822]151    }
[937]152
153    /// The subcript operator.
154
[946]155    /// The subscript operator. The map can be subscripted by the
156    /// actual items of the graph.
157     
[987]158    Reference operator[](const Key& key) {
[980]159      return container[graph->id(key)];
[822]160    }
161               
[937]162    /// The const subcript operator.
163
[946]164    /// The const subscript operator. The map can be subscripted by the
165    /// actual items of the graph.
166     
[987]167    ConstReference operator[](const Key& key) const {
[980]168      return container[graph->id(key)];
[822]169    }
170
[937]171
[946]172    /// The setter function of the map.
173
174    /// It the same as operator[](key) = value expression.
175    ///
176     
[987]177    void set(const Key& key, const Value& value) {
[946]178      (*this)[key] = value;
[822]179    }
[946]180
[937]181    /// Adds a new key to the map.
[822]182               
[946]183    /// It adds a new key to the map. It called by the observer registry
184    /// and it overrides the add() member function of the observer base.
185     
[987]186    void add(const Key& key) {
[980]187      int id = graph->id(key);
[822]188      if (id >= (int)container.size()) {
189        container.resize(id + 1);
190      }
191    }
[937]192
193    /// Erases a key from the map.
[822]194               
[946]195    /// Erase a key from the map. It called by the observer registry
196    /// and it overrides the erase() member function of the observer base.     
[987]197    void erase(const Key&) {}
[822]198
[946]199    /// Buildes the map.
200               
201    /// It buildes the map. It called by the observer registry
202    /// and it overrides the build() member function of the observer base.
[937]203
[946]204    void build() {
[980]205      container.resize(graph->maxId(_Item()) + 1);
[946]206    }
[937]207
[946]208    /// Clear the map.
209
210    /// It erase all items from the map. It called by the observer registry
211    /// and it overrides the clear() member function of the observer base.     
[822]212    void clear() {
213      container.clear();
214    }
[1267]215   
[822]216  private:
217               
218    Container container;
[946]219    const Graph *graph;
[822]220
[946]221  };
222
223
224  template <typename _Base>
225  class VectorMappableGraphExtender : public _Base {
[844]226  public:
[946]227
228    typedef VectorMappableGraphExtender<_Base> Graph;
229    typedef _Base Parent;
230
231    typedef typename Parent::Node Node;
232    typedef typename Parent::NodeIt NodeIt;
233    typedef typename Parent::NodeIdMap NodeIdMap;
[1039]234    typedef typename Parent::NodeNotifier NodeObserverRegistry;
[946]235
236    typedef typename Parent::Edge Edge;
237    typedef typename Parent::EdgeIt EdgeIt;
238    typedef typename Parent::EdgeIdMap EdgeIdMap;
[1039]239    typedef typename Parent::EdgeNotifier EdgeObserverRegistry;
[946]240
241   
242    template <typename _Value>
[1267]243    class NodeMap :
244      public IterableMapExtender<VectorMap<Graph, Node, _Value> > {
[946]245    public:
246      typedef VectorMappableGraphExtender<_Base> Graph;
247
248      typedef typename Graph::Node Node;
249
[1267]250      typedef IterableMapExtender<VectorMap<Graph, Node, _Value> > Parent;
[946]251
[979]252      //typedef typename Parent::Graph Graph;
[946]253      typedef typename Parent::Value Value;
254
255      NodeMap(const Graph& g)
[980]256        : Parent(g) {}
[946]257      NodeMap(const Graph& g, const Value& v)
[980]258        : Parent(g, v) {}
[946]259
260    };
261
262    template <typename _Value>
[1267]263    class EdgeMap
264      : public IterableMapExtender<VectorMap<Graph, Edge, _Value> > {
[946]265    public:
266      typedef VectorMappableGraphExtender<_Base> Graph;
267
268      typedef typename Graph::Edge Edge;
269
[1267]270      typedef IterableMapExtender<VectorMap<Graph, Edge, _Value> > Parent;
[946]271
[979]272      //typedef typename Parent::Graph Graph;
[946]273      typedef typename Parent::Value Value;
274
275      EdgeMap(const Graph& g)
[980]276        : Parent(g) {}
[946]277      EdgeMap(const Graph& g, const Value& v)
[980]278        : Parent(g, v) {}
[946]279
280    };
281   
[822]282  };
283 
284  /// @}
285 
286}
287
288#endif
Note: See TracBrowser for help on using the repository browser.