Modified a bit.
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 Combinatorial Optimization Research Group, 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/alteration_notifier.h>
27 ///\brief Vector based graph maps.
31 /// \addtogroup graphmaps
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.
40 /// \param Registry The AlterationNotifier that will notify this map.
41 /// \param IdMap The IdMap type of the graph items.
42 /// \param Value The value type of the map.
44 /// \author Balazs Dezso
47 template <typename _Graph,
50 class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
53 /// The graph type of the map.
55 /// The key type of the map.
57 /// The id map type of the map.
58 typedef AlterationNotifier<_Item> Registry;
59 /// The value type of the map.
63 typedef VectorMap Map;
64 /// The base class of the map.
65 typedef typename Registry::ObserverBase Parent;
69 /// The container type of the map.
70 typedef std::vector<Value> Container;
74 /// The reference type of the map;
75 typedef typename Container::reference Reference;
76 /// The pointer type of the map;
77 typedef typename Container::pointer Pointer;
79 /// The const value type of the map.
80 typedef const Value ConstValue;
81 /// The const reference type of the map;
82 typedef typename Container::const_reference ConstReference;
83 /// The pointer type of the map;
84 typedef typename Container::const_pointer ConstPointer;
86 /// Constructor to attach the new map into the registry.
88 /// It construates a map and attachs it into the registry.
89 /// It adds all the items of the graph to the map.
91 VectorMap(const Graph& _g) : graph(&_g) {
92 attach(_g.getNotifier(_Item()));
96 /// Constructor uses given value to initialize the map.
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.
101 VectorMap(const Graph& _g, const Value& _v) : graph(&_g) {
102 attach(_g.getNotifier(_Item()));
103 container.resize(graph->maxId(_Item()) + 1, _v);
106 VectorMap(const VectorMap& _copy) : graph(_copy.getGraph()) {
107 if (_copy.attached()) {
108 attach(*_copy.getRegistry());
109 container = _copy.container;
113 using Parent::attach;
114 using Parent::detach;
115 using Parent::attached;
117 /** Assign operator to copy a map of the same map type.
119 VectorMap& operator=(const VectorMap& copy) {
120 if (© == this) return *this;
122 if (graph != copy.graph) {
126 if (copy.attached()) {
127 attach(*copy.getRegistry());
130 container = copy.container;
136 virtual ~VectorMap() {
142 const Graph* getGraph() const {
146 /// The subcript operator.
148 /// The subscript operator. The map can be subscripted by the
149 /// actual items of the graph.
151 Reference operator[](const Key& key) {
152 return container[graph->id(key)];
155 /// The const subcript operator.
157 /// The const subscript operator. The map can be subscripted by the
158 /// actual items of the graph.
160 ConstReference operator[](const Key& key) const {
161 return container[graph->id(key)];
165 /// The setter function of the map.
167 /// It the same as operator[](key) = value expression.
170 void set(const Key& key, const Value& value) {
171 (*this)[key] = value;
174 /// Adds a new key to the map.
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.
179 void add(const Key& key) {
180 int id = graph->id(key);
181 if (id >= (int)container.size()) {
182 container.resize(id + 1);
186 /// Erases a key from the map.
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.
190 void erase(const Key&) {}
194 /// It buildes the map. It called by the observer registry
195 /// and it overrides the build() member function of the observer base.
198 container.resize(graph->maxId(_Item()) + 1);
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.
217 template <typename _Base>
218 class VectorMappableGraphExtender : public _Base {
221 typedef VectorMappableGraphExtender<_Base> Graph;
222 typedef _Base Parent;
224 typedef typename Parent::Node Node;
225 typedef typename Parent::NodeIt NodeIt;
226 typedef typename Parent::NodeIdMap NodeIdMap;
227 typedef typename Parent::NodeNotifier NodeObserverRegistry;
229 typedef typename Parent::Edge Edge;
230 typedef typename Parent::EdgeIt EdgeIt;
231 typedef typename Parent::EdgeIdMap EdgeIdMap;
232 typedef typename Parent::EdgeNotifier EdgeObserverRegistry;
236 template <typename _Value>
237 class NodeMap : public VectorMap<Graph, Node, _Value> {
239 typedef VectorMappableGraphExtender<_Base> Graph;
241 typedef typename Graph::Node Node;
243 typedef VectorMap<Graph, Node, _Value> Parent;
245 //typedef typename Parent::Graph Graph;
246 typedef typename Parent::Value Value;
248 NodeMap(const Graph& g)
250 NodeMap(const Graph& g, const Value& v)
255 template <typename _Value>
256 class EdgeMap : public VectorMap<Graph, Edge, _Value> {
258 typedef VectorMappableGraphExtender<_Base> Graph;
260 typedef typename Graph::Edge Edge;
262 typedef VectorMap<Graph, Edge, _Value> Parent;
264 //typedef typename Parent::Graph Graph;
265 typedef typename Parent::Value Value;
267 EdgeMap(const Graph& g)
269 EdgeMap(const Graph& g, const Value& v)