Update INSTALL to use CMAKE. Also update AUTHORS and LICENSE
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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/core.h>
26 #include <lemon/bits/lock.h>
30 //\brief Observer notifier for graph alteration observers.
36 // \brief Notifier class to notify observes about alterations in
39 // The simple graphs can be refered as two containers: a node container
40 // and an edge container. But they do not store values directly, they
41 // are just key continars for more value containers, which are the
42 // node and edge maps.
44 // The node and edge sets of the graphs can be changed as we add or erase
45 // nodes and edges in the graph. LEMON would like to handle easily
46 // that the node and edge maps should contain values for all nodes or
47 // edges. If we want to check on every indicing if the map contains
48 // the current indicing key that cause a drawback in the performance
49 // in the library. We use another solution: we notify all maps about
50 // an alteration in the graph, which cause only drawback on the
51 // alteration of the graph.
53 // This class provides an interface to a node or edge container.
54 // The first() and next() member functions make possible
55 // to iterate on the keys of the container.
56 // The id() function returns an integer id for each key.
57 // The maxId() function gives back an upper bound of the ids.
59 // For the proper functonality of this class, we should notify it
60 // about each alteration in the container. The alterations have four type:
61 // add(), erase(), build() and clear(). The add() and
62 // erase() signal that only one or few items added or erased to or
63 // from the graph. If all items are erased from the graph or if a new graph
64 // is built from an empty graph, then it can be signaled with the
65 // clear() and build() members. Important rule that if we erase items
66 // from graphs we should first signal the alteration and after that erase
67 // them from the container, on the other way on item addition we should
68 // first extend the container and just after that signal the alteration.
70 // The alteration can be observed with a class inherited from the
71 // ObserverBase nested class. The signals can be handled with
72 // overriding the virtual functions defined in the base class. The
73 // observer base can be attached to the notifier with the
74 // attach() member and can be detached with detach() function. The
75 // alteration handlers should not call any function which signals
76 // an other alteration in the same notifier and should not
77 // detach any observer from the notifier.
79 // Alteration observers try to be exception safe. If an add() or
80 // a clear() function throws an exception then the remaining
81 // observeres will not be notified and the fulfilled additions will
82 // be rolled back by calling the erase() or clear() functions.
83 // Hence erase() and clear() should not throw exception.
84 // Actullay, they can throw only \ref ImmediateDetach exception,
85 // which detach the observer from the notifier.
87 // There are some cases, when the alteration observing is not completly
88 // reliable. If we want to carry out the node degree in the graph
89 // as in the \ref InDegMap and we use the reverseArc(), then it cause
90 // unreliable functionality. Because the alteration observing signals
91 // only erasing and adding but not the reversing, it will stores bad
92 // degrees. Apart form that the subgraph adaptors cannot even signal
93 // the alterations because just a setting in the filter map can modify
94 // the graph and this cannot be watched in any way.
96 // \param _Container The container which is observed.
97 // \param _Item The item type which is obserbved.
99 template <typename _Container, typename _Item>
100 class AlterationNotifier {
103 typedef True Notifier;
105 typedef _Container Container;
108 // \brief Exception which can be called from clear() and
111 // From the clear() and erase() function only this
112 // exception is allowed to throw. The exception immediatly
113 // detaches the current observer from the notifier. Because the
114 // clear() and erase() should not throw other exceptions
115 // it can be used to invalidate the observer.
116 struct ImmediateDetach {};
118 // \brief ObserverBase is the base class for the observers.
120 // ObserverBase is the abstract base class for the observers.
121 // It will be notified about an item was inserted into or
122 // erased from the graph.
124 // The observer interface contains some pure virtual functions
125 // to override. The add() and erase() functions are
126 // to notify the oberver when one item is added or erased.
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.
133 typedef AlterationNotifier Notifier;
135 friend class AlterationNotifier;
137 // \brief Default constructor.
139 // Default constructor for ObserverBase.
140 ObserverBase() : _notifier(0) {}
142 // \brief Constructor which attach the observer into notifier.
144 // Constructor which attach the observer into notifier.
145 ObserverBase(AlterationNotifier& nf) {
149 // \brief Constructor which attach the obserever to the same notifier.
151 // Constructor which attach the obserever to the same notifier as
152 // the other observer is attached to.
153 ObserverBase(const ObserverBase& copy) {
154 if (copy.attached()) {
155 attach(*copy.notifier());
160 virtual ~ObserverBase() {
166 // \brief Attaches the observer into an AlterationNotifier.
168 // This member attaches the observer into an AlterationNotifier.
169 void attach(AlterationNotifier& nf) {
173 // \brief Detaches the observer into an AlterationNotifier.
175 // This member detaches the observer from an AlterationNotifier.
177 _notifier->detach(*this);
180 // \brief Gives back a pointer to the notifier which the map
183 // This function gives back a pointer to the notifier which the map
185 Notifier* notifier() const { return const_cast<Notifier*>(_notifier); }
187 // Gives back true when the observer is attached into a notifier.
188 bool attached() const { return _notifier != 0; }
192 ObserverBase& operator=(const ObserverBase& copy);
197 typename std::list<ObserverBase*>::iterator _index;
199 // \brief The member function to notificate the observer about an
200 // item is added to the container.
202 // The add() member function notificates the observer about an item
203 // is added to the container. It have to be overrided in the
205 virtual void add(const Item&) = 0;
207 // \brief The member function to notificate the observer about
208 // more item is added to the container.
210 // The add() member function notificates the observer about more item
211 // is added to the container. It have to be overrided in the
213 virtual void add(const std::vector<Item>& items) = 0;
215 // \brief The member function to notificate the observer about an
216 // item is erased from the container.
218 // The erase() member function notificates the observer about an
219 // item is erased from the container. It have to be overrided in
221 virtual void erase(const Item&) = 0;
223 // \brief The member function to notificate the observer about
224 // more item is erased from the container.
226 // The erase() member function notificates the observer about more item
227 // is erased from the container. It have to be overrided in the
229 virtual void erase(const std::vector<Item>& items) = 0;
231 // \brief The member function to notificate the observer about the
232 // container is built.
234 // The build() member function notificates the observer about the
235 // container is built from an empty container. It have to be
236 // overrided in the subclasses.
237 virtual void build() = 0;
239 // \brief The member function to notificate the observer about all
240 // items are erased from the container.
242 // The clear() member function notificates the observer about all
243 // items are erased from the container. It have to be overrided in
245 virtual void clear() = 0;
251 const Container* container;
253 typedef std::list<ObserverBase*> Observers;
254 Observers _observers;
255 lemon::bits::Lock _lock;
259 // \brief Default constructor.
261 // The default constructor of the AlterationNotifier.
262 // It creates an empty notifier.
266 // \brief Constructor.
268 // Constructor with the observed container parameter.
269 AlterationNotifier(const Container& _container)
270 : container(&_container) {}
272 // \brief Copy Constructor of the AlterationNotifier.
274 // Copy constructor of the AlterationNotifier.
275 // It creates only an empty notifier because the copiable
276 // notifier's observers have to be registered still into that notifier.
277 AlterationNotifier(const AlterationNotifier& _notifier)
278 : container(_notifier.container) {}
280 // \brief Destructor.
282 // Destructor of the AlterationNotifier.
283 ~AlterationNotifier() {
284 typename Observers::iterator it;
285 for (it = _observers.begin(); it != _observers.end(); ++it) {
286 (*it)->_notifier = 0;
290 // \brief Sets the container.
292 // Sets the container.
293 void setContainer(const Container& _container) {
294 container = &_container;
299 AlterationNotifier& operator=(const AlterationNotifier&);
303 // \brief First item in the container.
305 // Returns the first item in the container. It is
306 // for start the iteration on the container.
307 void first(Item& item) const {
308 container->first(item);
311 // \brief Next item in the container.
313 // Returns the next item in the container. It is
314 // for iterate on the container.
315 void next(Item& item) const {
316 container->next(item);
319 // \brief Returns the id of the item.
321 // Returns the id of the item provided by the container.
322 int id(const Item& item) const {
323 return container->id(item);
326 // \brief Returns the maximum id of the container.
328 // Returns the maximum id of the container.
330 return container->maxId(Item());
335 void attach(ObserverBase& observer) {
337 observer._index = _observers.insert(_observers.begin(), &observer);
338 observer._notifier = this;
342 void detach(ObserverBase& observer) {
344 _observers.erase(observer._index);
345 observer._index = _observers.end();
346 observer._notifier = 0;
352 // \brief Notifies all the registed observers about an item added to
355 // It notifies all the registed observers about an item added to
357 void add(const Item& item) {
358 typename Observers::reverse_iterator it;
360 for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
364 typename Observers::iterator jt;
365 for (jt = it.base(); jt != _observers.end(); ++jt) {
372 // \brief Notifies all the registed observers about more item added to
375 // It notifies all the registed observers about more item added to
377 void add(const std::vector<Item>& items) {
378 typename Observers::reverse_iterator it;
380 for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
384 typename Observers::iterator jt;
385 for (jt = it.base(); jt != _observers.end(); ++jt) {
392 // \brief Notifies all the registed observers about an item erased from
395 // It notifies all the registed observers about an item erased from
397 void erase(const Item& item) throw() {
398 typename Observers::iterator it = _observers.begin();
399 while (it != _observers.end()) {
403 } catch (const ImmediateDetach&) {
404 (*it)->_index = _observers.end();
405 (*it)->_notifier = 0;
406 it = _observers.erase(it);
411 // \brief Notifies all the registed observers about more item erased
412 // from the container.
414 // It notifies all the registed observers about more item erased from
416 void erase(const std::vector<Item>& items) {
417 typename Observers::iterator it = _observers.begin();
418 while (it != _observers.end()) {
422 } catch (const ImmediateDetach&) {
423 (*it)->_index = _observers.end();
424 (*it)->_notifier = 0;
425 it = _observers.erase(it);
430 // \brief Notifies all the registed observers about the container is
433 // Notifies all the registed observers about the container is built
434 // from an empty container.
436 typename Observers::reverse_iterator it;
438 for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
442 typename Observers::iterator jt;
443 for (jt = it.base(); jt != _observers.end(); ++jt) {
450 // \brief Notifies all the registed observers about all items are
453 // Notifies all the registed observers about all items are erased
454 // from the container.
456 typename Observers::iterator it = _observers.begin();
457 while (it != _observers.end()) {
461 } catch (const ImmediateDetach&) {
462 (*it)->_index = _observers.end();
463 (*it)->_notifier = 0;
464 it = _observers.erase(it);