3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_BITS_DEBUG_MAP_H
20 #define LEMON_BITS_DEBUG_MAP_H
25 #include <lemon/bits/traits.h>
26 #include <lemon/bits/utility.h>
27 #include <lemon/error.h>
29 #include <lemon/bits/alteration_notifier.h>
31 #include <lemon/concept_check.h>
32 #include <lemon/concepts/maps.h>
37 ///\brief Vector based graph maps for debugging.
40 /// \ingroup graphbits
42 /// \brief Graph map based on the std::vector storage.
44 /// The DebugMap template class is graph map structure what
45 /// automatically updates the map when a key is added to or erased from
46 /// the map. This map also checks some programming failures by example
47 /// multiple addition of items, erasing of not existing item or
48 /// not erased items at the destruction of the map. It helps the
49 /// programmer to avoid segmentation faults and memory leaks.
51 /// \param Notifier The AlterationNotifier that will notify this map.
52 /// \param Item The item type of the graph items.
53 /// \param Value The value type of the map.
55 /// \author Balazs Dezso
56 template <typename _Graph, typename _Item, typename _Value>
58 : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
61 /// The container type of the map.
62 typedef std::vector<_Value> Container;
64 /// The container type of the debug flags.
65 typedef std::vector<bool> Flag;
69 static const bool strictCheck = false;
73 virtual ~MapError() {}
74 virtual const char* what() const throw() {
75 return "lemon::DebugMap::MapError";
79 /// The graph type of the map.
81 /// The item type of the map.
83 /// The reference map tag.
84 typedef True ReferenceMapTag;
86 /// The key type of the map.
88 /// The value type of the map.
91 /// The notifier type.
92 typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
96 /// The base class of the map.
97 typedef typename Notifier::ObserverBase Parent;
99 /// The reference type of the map;
100 typedef typename Container::reference Reference;
101 /// The const reference type of the map;
102 typedef typename Container::const_reference ConstReference;
105 /// \brief Constructor to attach the new map into the notifier.
107 /// It constructs a map and attachs it into the notifier.
108 /// It adds all the items of the graph to the map.
109 DebugMap(const Graph& graph) {
110 Parent::attach(graph.getNotifier(Item()));
111 container.resize(Parent::getNotifier()->maxId() + 1);
112 flag.resize(Parent::getNotifier()->maxId() + 1, false);
113 const typename Parent::Notifier* notifier = Parent::getNotifier();
115 for (notifier->first(it); it != INVALID; notifier->next(it)) {
116 flag[Parent::getNotifier()->id(it)] = true;
120 /// \brief Constructor uses given value to initialize the map.
122 /// It constructs a map uses a given value to initialize the map.
123 /// It adds all the items of the graph to the map.
124 DebugMap(const Graph& graph, const Value& value) {
125 Parent::attach(graph.getNotifier(Item()));
126 container.resize(Parent::getNotifier()->maxId() + 1, value);
127 flag.resize(Parent::getNotifier()->maxId() + 1, false);
128 const typename Parent::Notifier* notifier = Parent::getNotifier();
130 for (notifier->first(it); it != INVALID; notifier->next(it)) {
131 flag[Parent::getNotifier()->id(it)] = true;
135 /// \brief Copy constructor
137 /// Copy constructor.
138 DebugMap(const DebugMap& _copy) : Parent() {
139 if (_copy.attached()) {
140 Parent::attach(*_copy.getNotifier());
141 container = _copy.container;
143 flag.resize(Parent::getNotifier()->maxId() + 1, false);
144 const typename Parent::Notifier* notifier = Parent::getNotifier();
146 for (notifier->first(it); it != INVALID; notifier->next(it)) {
147 flag[Parent::getNotifier()->id(it)] = true;
148 LEMON_ASSERT(_copy.flag[Parent::getNotifier()->id(it)], MapError());
152 /// \brief Destructor
156 const typename Parent::Notifier* notifier = Parent::getNotifier();
159 for (notifier->first(it); it != INVALID; notifier->next(it)) {
160 LEMON_ASSERT(flag[Parent::getNotifier()->id(it)], MapError());
161 flag[Parent::getNotifier()->id(it)] = false;
164 for (int i = 0; i < (int)flag.size(); ++i) {
165 LEMON_ASSERT(!flag[i], MapError());
169 /// \brief Assign operator.
171 /// This operator assigns for each item in the map the
172 /// value mapped to the same item in the copied map.
173 /// The parameter map should be indiced with the same
174 /// itemset because this assign operator does not change
175 /// the container of the map.
176 DebugMap& operator=(const DebugMap& cmap) {
177 return operator=<DebugMap>(cmap);
181 /// \brief Template assign operator.
183 /// The given parameter should be conform to the ReadMap
184 /// concecpt and could be indiced by the current item set of
185 /// the NodeMap. In this case the value for each item
186 /// is assigned by the value of the given ReadMap.
187 template <typename CMap>
188 DebugMap& operator=(const CMap& cmap) {
189 checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
190 const typename Parent::Notifier* notifier = Parent::getNotifier();
192 for (notifier->first(it); it != INVALID; notifier->next(it)) {
200 /// \brief The subcript operator.
202 /// The subscript operator. The map can be subscripted by the
203 /// actual items of the graph.
204 Reference operator[](const Key& key) {
205 LEMON_ASSERT(flag[Parent::getNotifier()->id(key)], MapError());
206 return container[Parent::getNotifier()->id(key)];
209 /// \brief The const subcript operator.
211 /// The const subscript operator. The map can be subscripted by the
212 /// actual items of the graph.
213 ConstReference operator[](const Key& key) const {
214 LEMON_ASSERT(flag[Parent::getNotifier()->id(key)], MapError());
215 return container[Parent::getNotifier()->id(key)];
219 /// \brief The setter function of the map.
221 /// It the same as operator[](key) = value expression.
222 void set(const Key& key, const Value& value) {
223 (*this)[key] = value;
228 /// \brief Adds a new key to the map.
230 /// It adds a new key to the map. It called by the observer notifier
231 /// and it overrides the add() member function of the observer base.
232 virtual void add(const Key& key) {
233 int id = Parent::getNotifier()->id(key);
234 if (id >= (int)container.size()) {
235 container.resize(id + 1);
236 flag.resize(id + 1, false);
238 LEMON_ASSERT(!flag[Parent::getNotifier()->id(key)], MapError());
239 flag[Parent::getNotifier()->id(key)] = true;
241 std::vector<bool> fl(flag.size(), false);
242 const typename Parent::Notifier* notifier = Parent::getNotifier();
244 for (notifier->first(it); it != INVALID; notifier->next(it)) {
245 int id = Parent::getNotifier()->id(it);
248 LEMON_ASSERT(fl == flag, MapError());
252 /// \brief Adds more new keys to the map.
254 /// It adds more new keys to the map. It called by the observer notifier
255 /// and it overrides the add() member function of the observer base.
256 virtual void add(const std::vector<Key>& keys) {
257 int max = container.size() - 1;
258 for (int i = 0; i < (int)keys.size(); ++i) {
259 int id = Parent::getNotifier()->id(keys[i]);
264 container.resize(max + 1);
265 flag.resize(max + 1, false);
266 for (int i = 0; i < (int)keys.size(); ++i) {
267 LEMON_ASSERT(!flag[Parent::getNotifier()->id(keys[i])], MapError());
268 flag[Parent::getNotifier()->id(keys[i])] = true;
271 std::vector<bool> fl(flag.size(), false);
272 const typename Parent::Notifier* notifier = Parent::getNotifier();
274 for (notifier->first(it); it != INVALID; notifier->next(it)) {
275 int id = Parent::getNotifier()->id(it);
278 LEMON_ASSERT(fl == flag, MapError());
282 /// \brief Erase a key from the map.
284 /// Erase a key from the map. It called by the observer notifier
285 /// and it overrides the erase() member function of the observer base.
286 virtual void erase(const Key& key) {
288 std::vector<bool> fl(flag.size(), false);
289 const typename Parent::Notifier* notifier = Parent::getNotifier();
291 for (notifier->first(it); it != INVALID; notifier->next(it)) {
292 int id = Parent::getNotifier()->id(it);
295 LEMON_ASSERT(fl == flag, MapError());
297 container[Parent::getNotifier()->id(key)] = Value();
298 LEMON_ASSERT(flag[Parent::getNotifier()->id(key)], MapError());
299 flag[Parent::getNotifier()->id(key)] = false;
302 /// \brief Erase more keys from the map.
304 /// Erase more keys from the map. It called by the observer notifier
305 /// and it overrides the erase() member function of the observer base.
306 virtual void erase(const std::vector<Key>& keys) {
308 std::vector<bool> fl(flag.size(), false);
309 const typename Parent::Notifier* notifier = Parent::getNotifier();
311 for (notifier->first(it); it != INVALID; notifier->next(it)) {
312 int id = Parent::getNotifier()->id(it);
315 LEMON_ASSERT(fl == flag, MapError());
317 for (int i = 0; i < (int)keys.size(); ++i) {
318 container[Parent::getNotifier()->id(keys[i])] = Value();
319 LEMON_ASSERT(flag[Parent::getNotifier()->id(keys[i])], MapError());
320 flag[Parent::getNotifier()->id(keys[i])] = false;
324 /// \brief Buildes the map.
326 /// It buildes the map. It called by the observer notifier
327 /// and it overrides the build() member function of the observer base.
328 virtual void build() {
330 for (int i = 0; i < (int)flag.size(); ++i) {
331 LEMON_ASSERT(flag[i], MapError());
334 int size = Parent::getNotifier()->maxId() + 1;
335 container.reserve(size);
336 container.resize(size);
338 flag.resize(size, false);
339 const typename Parent::Notifier* notifier = Parent::getNotifier();
341 for (notifier->first(it); it != INVALID; notifier->next(it)) {
342 int id = Parent::getNotifier()->id(it);
343 LEMON_ASSERT(!flag[id], MapError());
348 /// \brief Clear the map.
350 /// It erase all items from the map. It called by the observer notifier
351 /// and it overrides the clear() member function of the observer base.
352 virtual void clear() {
353 const typename Parent::Notifier* notifier = Parent::getNotifier();
355 for (notifier->first(it); it != INVALID; notifier->next(it)) {
356 int id = Parent::getNotifier()->id(it);
357 LEMON_ASSERT(flag[id], MapError());
361 for (int i = 0; i < (int)flag.size(); ++i) {
362 LEMON_ASSERT(!flag[i], MapError());