2 * src/lemon/vector_map.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
17 #ifndef LEMON_VECTOR_MAP_H
18 #define LEMON_VECTOR_MAP_H
23 #include <lemon/utility.h>
24 #include <lemon/bits/map_iterator.h>
25 #include <lemon/bits/alteration_notifier.h>
29 ///\brief Vector based graph maps.
33 /// \addtogroup graphmaps
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.
42 /// \param Registry The AlterationNotifier that will notify this map.
43 /// \param IdMap The IdMap type of the graph items.
44 /// \param Value The value type of the map.
46 /// \author Balazs Dezso
54 class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
57 /// The graph type of the map.
59 /// The key type of the map.
61 /// The id map type of the map.
62 typedef AlterationNotifier<_Item> Registry;
63 /// The value type of the map.
67 typedef VectorMap Map;
68 /// The base class of the map.
69 typedef typename Registry::ObserverBase Parent;
73 /// The container type of the map.
74 typedef std::vector<Value> Container;
78 /// The reference type of the map;
79 typedef typename Container::reference Reference;
80 /// The pointer type of the map;
81 typedef typename Container::pointer Pointer;
83 /// The const value type of the map.
84 typedef const Value ConstValue;
85 /// The const reference type of the map;
86 typedef typename Container::const_reference ConstReference;
87 /// The pointer type of the map;
88 typedef typename Container::const_pointer ConstPointer;
90 typedef True FullTypeTag;
92 /// Constructor to attach the new map into the registry.
94 /// It construates a map and attachs it into the registry.
95 /// It adds all the items of the graph to the map.
97 VectorMap(const Graph& _g) : graph(&_g) {
98 attach(_g.getNotifier(_Item()));
102 /// Constructor uses given value to initialize the map.
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.
107 VectorMap(const Graph& _g, const Value& _v) : graph(&_g) {
108 attach(_g.getNotifier(_Item()));
109 container.resize(graph->maxId(_Item()) + 1, _v);
112 VectorMap(const VectorMap& _copy)
113 : Parent(), graph(_copy.getGraph()) {
114 if (_copy.attached()) {
115 attach(*_copy.getRegistry());
116 container = _copy.container;
120 using Parent::attach;
121 using Parent::detach;
122 using Parent::attached;
124 /** Assign operator to copy a map of the same map type.
126 VectorMap& operator=(const VectorMap& copy) {
127 if (© == this) return *this;
129 if (graph != copy.graph) {
133 if (copy.attached()) {
134 attach(*copy.getRegistry());
137 container = copy.container;
143 virtual ~VectorMap() {
149 const Graph* getGraph() const {
153 /// The subcript operator.
155 /// The subscript operator. The map can be subscripted by the
156 /// actual items of the graph.
158 Reference operator[](const Key& key) {
159 return container[graph->id(key)];
162 /// The const subcript operator.
164 /// The const subscript operator. The map can be subscripted by the
165 /// actual items of the graph.
167 ConstReference operator[](const Key& key) const {
168 return container[graph->id(key)];
172 /// The setter function of the map.
174 /// It the same as operator[](key) = value expression.
177 void set(const Key& key, const Value& value) {
178 (*this)[key] = value;
181 /// Adds a new key to the map.
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.
186 void add(const Key& key) {
187 int id = graph->id(key);
188 if (id >= (int)container.size()) {
189 container.resize(id + 1);
193 /// Erases a key from the map.
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.
197 void erase(const Key&) {}
201 /// It buildes the map. It called by the observer registry
202 /// and it overrides the build() member function of the observer base.
205 container.resize(graph->maxId(_Item()) + 1);
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.
224 template <typename _Base>
225 class VectorMappableGraphExtender : public _Base {
228 typedef VectorMappableGraphExtender<_Base> Graph;
229 typedef _Base Parent;
231 typedef typename Parent::Node Node;
232 typedef typename Parent::NodeIt NodeIt;
233 typedef typename Parent::NodeIdMap NodeIdMap;
234 typedef typename Parent::NodeNotifier NodeObserverRegistry;
236 typedef typename Parent::Edge Edge;
237 typedef typename Parent::EdgeIt EdgeIt;
238 typedef typename Parent::EdgeIdMap EdgeIdMap;
239 typedef typename Parent::EdgeNotifier EdgeObserverRegistry;
242 template <typename _Value>
244 public IterableMapExtender<VectorMap<Graph, Node, _Value> > {
246 typedef VectorMappableGraphExtender<_Base> Graph;
248 typedef typename Graph::Node Node;
250 typedef IterableMapExtender<VectorMap<Graph, Node, _Value> > Parent;
252 //typedef typename Parent::Graph Graph;
253 typedef typename Parent::Value Value;
255 NodeMap(const Graph& g)
257 NodeMap(const Graph& g, const Value& v)
262 template <typename _Value>
264 : public IterableMapExtender<VectorMap<Graph, Edge, _Value> > {
266 typedef VectorMappableGraphExtender<_Base> Graph;
268 typedef typename Graph::Edge Edge;
270 typedef IterableMapExtender<VectorMap<Graph, Edge, _Value> > Parent;
272 //typedef typename Parent::Graph Graph;
273 typedef typename Parent::Value Value;
275 EdgeMap(const Graph& g)
277 EdgeMap(const Graph& g, const Value& v)