COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/bits/vector_map.h @ 1719:674182524bd9

Last change on this file since 1719:674182524bd9 was 1719:674182524bd9, checked in by Balazs Dezso, 14 years ago

Traits moved to own file
Tag for reference maps
Possibility to handle proper the return type
of the operator[]() const -- value or reference

File size: 6.1 KB
Line 
1/* -*- C++ -*-
2 * lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
3 *
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, 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/utility.h>
24#include <lemon/bits/map_iterator.h>
25#include <lemon/bits/alteration_notifier.h>
26#include <lemon/concept_check.h>
27#include <lemon/concept/maps.h>
28
29/// \ingroup graphmapfactory
30///
31///\file
32///\brief Vector based graph maps.
33
34namespace lemon {
35
36  /// \ingroup graphmapfactory
37  ///
38  /// \brief Graph map based on the std::vector storage.
39  ///
40  /// The VectorMap template class is graph map structure what
41  /// automatically updates the map when a key is added to or erased from
42  /// the map. This map factory uses the allocators to implement
43  /// the container functionality. This map factory
44  /// uses the std::vector to implement the container function.
45  ///
46  /// \param Registry The AlterationNotifier that will notify this map.
47  /// \param Item The item type of the graph items.
48  /// \param Value The value type of the map.
49  ///
50  /// \author Balazs Dezso
51       
52  template <
53    typename _Graph,
54    typename _Item,   
55    typename _Value
56    >
57  class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
58  private:
59               
60    /// The container type of the map.
61    typedef std::vector<_Value> Container;     
62
63  public:
64
65    /// The graph type of the map.
66    typedef _Graph Graph;
67    /// The reference map tag.
68    typedef True ReferenceMapTag;
69
70    /// The key type of the map.
71    typedef _Item Key;
72    /// The value type of the map.
73    typedef _Value Value;
74    /// The const reference type of the map.
75    typedef typename Container::const_reference ConstReference;
76    /// The reference type of the map.
77    typedef typename Container::reference Reference;
78
79    typedef const Value ConstValue;
80    typedef Value* Pointer;
81    typedef const Value* ConstPointer;
82
83    typedef AlterationNotifier<_Item> Registry;
84
85    /// The map type.
86    typedef VectorMap Map;
87    /// The base class of the map.
88    typedef typename Registry::ObserverBase Parent;
89
90    /// The reference type of the map;
91    typedef typename Container::reference Reference;
92    /// The pointer type of the map;
93    typedef typename Container::pointer Pointer;
94
95    /// The const value type of the map.
96    typedef const Value ConstValue;
97    /// The const reference type of the map;
98    typedef typename Container::const_reference ConstReference;
99    /// The pointer type of the map;
100    typedef typename Container::const_pointer ConstPointer;
101
102    typedef True FullTypeTag;
103
104    /// \brief Constructor to attach the new map into the registry.
105    ///
106    /// It constructs a map and attachs it into the registry.
107    /// It adds all the items of the graph to the map.
108    VectorMap(const Graph& _g) : graph(&_g) {
109      attach(_g.getNotifier(_Item()));
110      build();
111    }
112
113    /// \brief Constructor uses given value to initialize the map.
114    ///
115    /// It constructs a map uses a given value to initialize the map.
116    /// It adds all the items of the graph to the map.
117    VectorMap(const Graph& _g, const Value& _v) : graph(&_g) {
118      attach(_g.getNotifier(_Item()));
119      container.resize(graph->maxId(_Item()) + 1, _v);
120    }
121
122    /// \brief Copy constructor
123    ///
124    /// Copy constructor.
125    VectorMap(const VectorMap& _copy)
126      : Parent(), graph(_copy.getGraph()) {
127      if (_copy.attached()) {
128        attach(*_copy.getRegistry());
129        container = _copy.container;
130      }
131    }
132
133    /// \brief Destrcutor
134    ///
135    /// Destructor.
136    virtual ~VectorMap() {
137      if (attached()) {
138        detach();
139      }
140    }
141
142
143  private:
144
145    VectorMap& operator=(const VectorMap&);
146
147  protected:
148
149    using Parent::attach;
150    using Parent::detach;
151    using Parent::attached;
152
153    const Graph* getGraph() const {
154      return graph;
155    }
156
157  public:
158
159    /// \brief The subcript operator.
160    ///
161    /// The subscript operator. The map can be subscripted by the
162    /// actual items of the graph.     
163    Reference operator[](const Key& key) {
164      return container[graph->id(key)];
165    }
166               
167    /// \brief The const subcript operator.
168    ///
169    /// The const subscript operator. The map can be subscripted by the
170    /// actual items of the graph.
171    ConstReference operator[](const Key& key) const {
172      return container[graph->id(key)];
173    }
174
175
176    /// \brief The setter function of the map.
177    ///
178    /// It the same as operator[](key) = value expression.
179    void set(const Key& key, const Value& value) {
180      (*this)[key] = value;
181    }
182
183  protected:
184
185    /// \brief Adds a new key to the map.
186    ///         
187    /// It adds a new key to the map. It called by the observer registry
188    /// and it overrides the add() member function of the observer base.     
189    virtual void add(const Key& key) {
190      int id = graph->id(key);
191      if (id >= (int)container.size()) {
192        container.resize(id + 1);
193      }
194    }
195
196    /// \brief Erase a key from the map.
197    ///
198    /// Erase a key from the map. It called by the observer registry
199    /// and it overrides the erase() member function of the observer base.     
200    virtual void erase(const Key&) {}
201
202    /// \brief Buildes the map.
203    ///
204    /// It buildes the map. It called by the observer registry
205    /// and it overrides the build() member function of the observer base.
206    virtual void build() {
207      container.resize(graph->maxId(_Item()) + 1);
208    }
209
210    /// \brief Clear the map.
211    ///
212    /// It erase all items from the map. It called by the observer registry
213    /// and it overrides the clear() member function of the observer base.     
214    virtual void clear() {
215      container.clear();
216    }
217   
218  private:
219               
220    Container container;
221    const Graph *graph;
222
223  };
224
225}
226
227#endif
Note: See TracBrowser for help on using the repository browser.