CMake improvements.
- documentation generation with Doxygen
- installation support
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
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_ALTERATION_NOTIFIER_H
20 #define LEMON_BITS_ALTERATION_NOTIFIER_H
25 #include <lemon/bits/utility.h>
29 ///\brief Observer notifier for graph alteration observers.
33 /// \ingroup graphbits
35 /// \brief Notifier class to notify observes about alterations in
38 /// The simple graph's can be refered as two containers, one node container
39 /// and one edge container. But they are not standard containers they
40 /// does not store values directly they are just key continars for more
41 /// value containers which are the node and edge maps.
43 /// The graph's node and edge sets can be changed as we add or erase
44 /// nodes and edges in the graph. Lemon would like to handle easily
45 /// that the node and edge maps should contain values for all nodes or
46 /// edges. If we want to check on every indicing if the map contains
47 /// the current indicing key that cause a drawback in the performance
48 /// in the library. We use another solution we notify all maps about
49 /// an alteration in the graph, which cause only drawback on the
50 /// alteration of the graph.
52 /// This class provides an interface to the container. The \e first() and \e
53 /// next() member functions make possible to iterate on the keys of the
54 /// container. The \e id() function returns an integer id for each key.
55 /// The \e maxId() function gives back an upper bound of the ids.
57 /// For the proper functonality of this class, we should notify it
58 /// about each alteration in the container. The alterations have four type
59 /// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and
60 /// \e erase() signals that only one or few items added or erased to or
61 /// from the graph. If all items are erased from the graph or from an empty
62 /// graph a new graph is builded then it can be signaled with the
63 /// clear() and build() members. Important rule that if we erase items
64 /// from graph we should first signal the alteration and after that erase
65 /// them from the container, on the other way on item addition we should
66 /// first extend the container and just after that signal the alteration.
68 /// The alteration can be observed with a class inherited from the
69 /// \e ObserverBase nested class. The signals can be handled with
70 /// overriding the virtual functions defined in the base class. The
71 /// observer base can be attached to the notifier with the
72 /// \e attach() member and can be detached with detach() function. The
73 /// alteration handlers should not call any function which signals
74 /// an other alteration in the same notifier and should not
75 /// detach any observer from the notifier.
77 /// Alteration observers try to be exception safe. If an \e add() or
78 /// a \e clear() function throws an exception then the remaining
79 /// observeres will not be notified and the fulfilled additions will
80 /// be rolled back by calling the \e erase() or \e clear()
81 /// functions. Thence the \e erase() and \e clear() should not throw
82 /// exception. Actullay, it can be throw only
83 /// \ref AlterationObserver::ImmediateDetach ImmediateDetach
84 /// exception which detach the observer from the notifier.
86 /// There are some place when the alteration observing is not completly
87 /// reliable. If we want to carry out the node degree in the graph
88 /// as in the \ref InDegMap and we use the reverseEdge that cause
89 /// unreliable functionality. Because the alteration observing signals
90 /// only erasing and adding but not the reversing it will stores bad
91 /// degrees. The sub graph adaptors cannot signal the alterations because
92 /// just a setting in the filter map can modify the graph and this cannot
93 /// be watched in any way.
95 /// \param _Container The container which is observed.
96 /// \param _Item The item type which is obserbved.
98 template <typename _Container, typename _Item>
99 class AlterationNotifier {
102 typedef True Notifier;
104 typedef _Container Container;
107 /// \brief Exception which can be called from \e clear() and
110 /// From the \e clear() and \e erase() function only this
111 /// exception is allowed to throw. The exception immediatly
112 /// detaches the current observer from the notifier. Because the
113 /// \e clear() and \e erase() should not throw other exceptions
114 /// it can be used to invalidate the observer.
115 struct ImmediateDetach {};
117 /// \brief ObserverBase is the base class for the observers.
119 /// ObserverBase is the abstract base class for the observers.
120 /// It will be notified about an item was inserted into or
121 /// erased from the graph.
123 /// The observer interface contains some pure virtual functions
124 /// to override. The add() and erase() functions are
125 /// to notify the oberver when one item is added or
128 /// The build() and clear() members are to notify the observer
129 /// about the container is built from an empty container or
130 /// is cleared to an empty container.
134 typedef AlterationNotifier Notifier;
136 friend class AlterationNotifier;
138 /// \brief Default constructor.
140 /// Default constructor for ObserverBase.
142 ObserverBase() : _notifier(0) {}
144 /// \brief Constructor which attach the observer into notifier.
146 /// Constructor which attach the observer into notifier.
147 ObserverBase(AlterationNotifier& nf) {
151 /// \brief Constructor which attach the obserever to the same notifier.
153 /// Constructor which attach the obserever to the same notifier as
154 /// the other observer is attached to.
155 ObserverBase(const ObserverBase& copy) {
156 if (copy.attached()) {
157 attach(*copy.notifier());
161 /// \brief Destructor
162 virtual ~ObserverBase() {
168 /// \brief Attaches the observer into an AlterationNotifier.
170 /// This member attaches the observer into an AlterationNotifier.
172 void attach(AlterationNotifier& nf) {
176 /// \brief Detaches the observer into an AlterationNotifier.
178 /// This member detaches the observer from an AlterationNotifier.
181 _notifier->detach(*this);
184 /// \brief Gives back a pointer to the notifier which the map
187 /// This function gives back a pointer to the notifier which the map
190 Notifier* notifier() const { return const_cast<Notifier*>(_notifier); }
192 /// Gives back true when the observer is attached into a notifier.
193 bool attached() const { return _notifier != 0; }
197 ObserverBase& operator=(const ObserverBase& copy);
202 typename std::list<ObserverBase*>::iterator _index;
204 /// \brief The member function to notificate the observer about an
205 /// item is added to the container.
207 /// The add() member function notificates the observer about an item
208 /// is added to the container. It have to be overrided in the
210 virtual void add(const Item&) = 0;
212 /// \brief The member function to notificate the observer about
213 /// more item is added to the container.
215 /// The add() member function notificates the observer about more item
216 /// is added to the container. It have to be overrided in the
218 virtual void add(const std::vector<Item>& items) = 0;
220 /// \brief The member function to notificate the observer about an
221 /// item is erased from the container.
223 /// The erase() member function notificates the observer about an
224 /// item is erased from the container. It have to be overrided in
226 virtual void erase(const Item&) = 0;
228 /// \brief The member function to notificate the observer about
229 /// more item is erased from the container.
231 /// The erase() member function notificates the observer about more item
232 /// is erased from the container. It have to be overrided in the
234 virtual void erase(const std::vector<Item>& items) = 0;
236 /// \brief The member function to notificate the observer about the
237 /// container is built.
239 /// The build() member function notificates the observer about the
240 /// container is built from an empty container. It have to be
241 /// overrided in the subclasses.
243 virtual void build() = 0;
245 /// \brief The member function to notificate the observer about all
246 /// items are erased from the container.
248 /// The clear() member function notificates the observer about all
249 /// items are erased from the container. It have to be overrided in
251 virtual void clear() = 0;
257 const Container* container;
259 typedef std::list<ObserverBase*> Observers;
260 Observers _observers;
265 /// \brief Default constructor.
267 /// The default constructor of the AlterationNotifier.
268 /// It creates an empty notifier.
272 /// \brief Constructor.
274 /// Constructor with the observed container parameter.
275 AlterationNotifier(const Container& _container)
276 : container(&_container) {}
278 /// \brief Copy Constructor of the AlterationNotifier.
280 /// Copy constructor of the AlterationNotifier.
281 /// It creates only an empty notifier because the copiable
282 /// notifier's observers have to be registered still into that notifier.
283 AlterationNotifier(const AlterationNotifier& _notifier)
284 : container(_notifier.container) {}
286 /// \brief Destructor.
288 /// Destructor of the AlterationNotifier.
290 ~AlterationNotifier() {
291 typename Observers::iterator it;
292 for (it = _observers.begin(); it != _observers.end(); ++it) {
293 (*it)->_notifier = 0;
297 /// \brief Sets the container.
299 /// Sets the container.
300 void setContainer(const Container& _container) {
301 container = &_container;
306 AlterationNotifier& operator=(const AlterationNotifier&);
312 /// \brief First item in the container.
314 /// Returns the first item in the container. It is
315 /// for start the iteration on the container.
316 void first(Item& item) const {
317 container->first(item);
320 /// \brief Next item in the container.
322 /// Returns the next item in the container. It is
323 /// for iterate on the container.
324 void next(Item& item) const {
325 container->next(item);
328 /// \brief Returns the id of the item.
330 /// Returns the id of the item provided by the container.
331 int id(const Item& item) const {
332 return container->id(item);
335 /// \brief Returns the maximum id of the container.
337 /// Returns the maximum id of the container.
339 return container->maxId(Item());
344 void attach(ObserverBase& observer) {
345 observer._index = _observers.insert(_observers.begin(), &observer);
346 observer._notifier = this;
349 void detach(ObserverBase& observer) {
350 _observers.erase(observer._index);
351 observer._index = _observers.end();
352 observer._notifier = 0;
357 /// \brief Notifies all the registed observers about an item added to
360 /// It notifies all the registed observers about an item added to
363 void add(const Item& item) {
364 typename Observers::reverse_iterator it;
366 for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
370 typename Observers::iterator jt;
371 for (jt = it.base(); jt != _observers.end(); ++jt) {
378 /// \brief Notifies all the registed observers about more item added to
381 /// It notifies all the registed observers about more item added to
384 void add(const std::vector<Item>& items) {
385 typename Observers::reverse_iterator it;
387 for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
391 typename Observers::iterator jt;
392 for (jt = it.base(); jt != _observers.end(); ++jt) {
399 /// \brief Notifies all the registed observers about an item erased from
402 /// It notifies all the registed observers about an item erased from
405 void erase(const Item& item) throw() {
406 typename Observers::iterator it = _observers.begin();
407 while (it != _observers.end()) {
411 } catch (const ImmediateDetach&) {
412 it = _observers.erase(it);
413 (*it)->_index = _observers.end();
414 (*it)->_notifier = 0;
419 /// \brief Notifies all the registed observers about more item erased
420 /// from the container.
422 /// It notifies all the registed observers about more item erased from
425 void erase(const std::vector<Item>& items) {
426 typename Observers::iterator it = _observers.begin();
427 while (it != _observers.end()) {
431 } catch (const ImmediateDetach&) {
432 it = _observers.erase(it);
433 (*it)->_index = _observers.end();
434 (*it)->_notifier = 0;
439 /// \brief Notifies all the registed observers about the container is
442 /// Notifies all the registed observers about the container is built
443 /// from an empty container.
445 typename Observers::reverse_iterator it;
447 for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
451 typename Observers::iterator jt;
452 for (jt = it.base(); jt != _observers.end(); ++jt) {
459 /// \brief Notifies all the registed observers about all items are
462 /// Notifies all the registed observers about all items are erased
463 /// from the container.
465 typename Observers::iterator it = _observers.begin();
466 while (it != _observers.end()) {
470 } catch (const ImmediateDetach&) {
471 it = _observers.erase(it);
472 (*it)->_index = _observers.end();
473 (*it)->_notifier = 0;