deba@57: /* -*- C++ -*- deba@57: * deba@57: * This file is a part of LEMON, a generic C++ optimization library deba@57: * deba@57: * Copyright (C) 2003-2007 deba@57: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@57: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@57: * deba@57: * Permission to use, modify and distribute this software is granted deba@57: * provided that this copyright notice appears in all copies. For deba@57: * precise terms see the accompanying LICENSE file. deba@57: * deba@57: * This software is provided "AS IS" with no warranty of any kind, deba@57: * express or implied, and with no claim as to its suitability for any deba@57: * purpose. deba@57: * deba@57: */ deba@57: deba@57: #ifndef LEMON_BITS_ALTERATION_NOTIFIER_H deba@57: #define LEMON_BITS_ALTERATION_NOTIFIER_H deba@57: deba@57: #include deba@57: #include deba@57: deba@57: #include deba@57: deba@57: ///\ingroup graphbits deba@57: ///\file deba@57: ///\brief Observer notifier for graph alteration observers. deba@57: deba@57: namespace lemon { deba@57: deba@57: /// \ingroup graphbits deba@57: /// deba@57: /// \brief Notifier class to notify observes about alterations in deba@57: /// a container. deba@57: /// deba@57: /// The simple graph's can be refered as two containers, one node container deba@57: /// and one edge container. But they are not standard containers they deba@57: /// does not store values directly they are just key continars for more deba@57: /// value containers which are the node and edge maps. deba@57: /// deba@57: /// The graph's node and edge sets can be changed as we add or erase deba@57: /// nodes and edges in the graph. Lemon would like to handle easily deba@57: /// that the node and edge maps should contain values for all nodes or deba@57: /// edges. If we want to check on every indicing if the map contains deba@57: /// the current indicing key that cause a drawback in the performance deba@57: /// in the library. We use another solution we notify all maps about deba@57: /// an alteration in the graph, which cause only drawback on the deba@57: /// alteration of the graph. deba@57: /// deba@57: /// This class provides an interface to the container. The \e first() and \e deba@57: /// next() member functions make possible to iterate on the keys of the deba@57: /// container. The \e id() function returns an integer id for each key. deba@57: /// The \e maxId() function gives back an upper bound of the ids. deba@57: /// deba@57: /// For the proper functonality of this class, we should notify it deba@57: /// about each alteration in the container. The alterations have four type deba@57: /// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and deba@57: /// \e erase() signals that only one or few items added or erased to or deba@57: /// from the graph. If all items are erased from the graph or from an empty deba@57: /// graph a new graph is builded then it can be signaled with the deba@57: /// clear() and build() members. Important rule that if we erase items deba@57: /// from graph we should first signal the alteration and after that erase deba@57: /// them from the container, on the other way on item addition we should deba@57: /// first extend the container and just after that signal the alteration. deba@57: /// deba@57: /// The alteration can be observed with a class inherited from the deba@57: /// \e ObserverBase nested class. The signals can be handled with deba@57: /// overriding the virtual functions defined in the base class. The deba@57: /// observer base can be attached to the notifier with the deba@57: /// \e attach() member and can be detached with detach() function. The deba@57: /// alteration handlers should not call any function which signals deba@57: /// an other alteration in the same notifier and should not deba@57: /// detach any observer from the notifier. deba@57: /// deba@57: /// Alteration observers try to be exception safe. If an \e add() or deba@57: /// a \e clear() function throws an exception then the remaining deba@57: /// observeres will not be notified and the fulfilled additions will deba@57: /// be rolled back by calling the \e erase() or \e clear() deba@57: /// functions. Thence the \e erase() and \e clear() should not throw deba@57: /// exception. Actullay, it can be throw only deba@57: /// \ref AlterationObserver::ImmediateDetach ImmediateDetach deba@57: /// exception which detach the observer from the notifier. deba@57: /// deba@57: /// There are some place when the alteration observing is not completly deba@57: /// reliable. If we want to carry out the node degree in the graph deba@57: /// as in the \ref InDegMap and we use the reverseEdge that cause deba@57: /// unreliable functionality. Because the alteration observing signals deba@57: /// only erasing and adding but not the reversing it will stores bad deba@57: /// degrees. The sub graph adaptors cannot signal the alterations because deba@57: /// just a setting in the filter map can modify the graph and this cannot deba@57: /// be watched in any way. deba@57: /// deba@57: /// \param _Container The container which is observed. deba@57: /// \param _Item The item type which is obserbved. deba@57: /// deba@57: /// \author Balazs Dezso deba@57: deba@57: template deba@57: class AlterationNotifier { deba@57: public: deba@57: deba@57: typedef True Notifier; deba@57: deba@57: typedef _Container Container; deba@57: typedef _Item Item; deba@57: deba@57: /// \brief Exception which can be called from \e clear() and deba@57: /// \e erase(). deba@57: /// deba@57: /// From the \e clear() and \e erase() function only this deba@57: /// exception is allowed to throw. The exception immediatly deba@57: /// detaches the current observer from the notifier. Because the deba@57: /// \e clear() and \e erase() should not throw other exceptions deba@57: /// it can be used to invalidate the observer. deba@57: struct ImmediateDetach {}; deba@57: deba@57: /// \brief ObserverBase is the base class for the observers. deba@57: /// deba@57: /// ObserverBase is the abstract base class for the observers. deba@57: /// It will be notified about an item was inserted into or deba@57: /// erased from the graph. deba@57: /// deba@57: /// The observer interface contains some pure virtual functions deba@57: /// to override. The add() and erase() functions are deba@57: /// to notify the oberver when one item is added or deba@57: /// erased. deba@57: /// deba@57: /// The build() and clear() members are to notify the observer deba@57: /// about the container is built from an empty container or deba@57: /// is cleared to an empty container. deba@57: /// deba@57: /// \author Balazs Dezso deba@57: deba@57: class ObserverBase { deba@57: protected: deba@57: typedef AlterationNotifier Notifier; deba@57: deba@57: friend class AlterationNotifier; deba@57: deba@57: /// \brief Default constructor. deba@57: /// deba@57: /// Default constructor for ObserverBase. deba@57: /// deba@57: ObserverBase() : _notifier(0) {} deba@57: deba@57: /// \brief Constructor which attach the observer into notifier. deba@57: /// deba@57: /// Constructor which attach the observer into notifier. deba@57: ObserverBase(AlterationNotifier& nf) { deba@57: attach(nf); deba@57: } deba@57: deba@57: /// \brief Constructor which attach the obserever to the same notifier. deba@57: /// deba@57: /// Constructor which attach the obserever to the same notifier as deba@57: /// the other observer is attached to. deba@57: ObserverBase(const ObserverBase& copy) { deba@57: if (copy.attached()) { deba@57: attach(*copy.notifier()); deba@57: } deba@57: } deba@57: deba@57: /// \brief Destructor deba@57: virtual ~ObserverBase() { deba@57: if (attached()) { deba@57: detach(); deba@57: } deba@57: } deba@57: deba@57: /// \brief Attaches the observer into an AlterationNotifier. deba@57: /// deba@57: /// This member attaches the observer into an AlterationNotifier. deba@57: /// deba@57: void attach(AlterationNotifier& nf) { deba@57: nf.attach(*this); deba@57: } deba@57: deba@57: /// \brief Detaches the observer into an AlterationNotifier. deba@57: /// deba@57: /// This member detaches the observer from an AlterationNotifier. deba@57: /// deba@57: void detach() { deba@57: _notifier->detach(*this); deba@57: } deba@57: deba@57: /// \brief Gives back a pointer to the notifier which the map deba@57: /// attached into. deba@57: /// deba@57: /// This function gives back a pointer to the notifier which the map deba@57: /// attached into. deba@57: /// deba@57: Notifier* notifier() const { return const_cast(_notifier); } deba@57: deba@57: /// Gives back true when the observer is attached into a notifier. deba@57: bool attached() const { return _notifier != 0; } deba@57: deba@57: private: deba@57: deba@57: ObserverBase& operator=(const ObserverBase& copy); deba@57: deba@57: protected: deba@57: deba@57: Notifier* _notifier; deba@57: typename std::list::iterator _index; deba@57: deba@57: /// \brief The member function to notificate the observer about an deba@57: /// item is added to the container. deba@57: /// deba@57: /// The add() member function notificates the observer about an item deba@57: /// is added to the container. It have to be overrided in the deba@57: /// subclasses. deba@57: virtual void add(const Item&) = 0; deba@57: deba@57: /// \brief The member function to notificate the observer about deba@57: /// more item is added to the container. deba@57: /// deba@57: /// The add() member function notificates the observer about more item deba@57: /// is added to the container. It have to be overrided in the deba@57: /// subclasses. deba@57: virtual void add(const std::vector& items) = 0; deba@57: deba@57: /// \brief The member function to notificate the observer about an deba@57: /// item is erased from the container. deba@57: /// deba@57: /// The erase() member function notificates the observer about an deba@57: /// item is erased from the container. It have to be overrided in deba@57: /// the subclasses. deba@57: virtual void erase(const Item&) = 0; deba@57: deba@57: /// \brief The member function to notificate the observer about deba@57: /// more item is erased from the container. deba@57: /// deba@57: /// The erase() member function notificates the observer about more item deba@57: /// is erased from the container. It have to be overrided in the deba@57: /// subclasses. deba@57: virtual void erase(const std::vector& items) = 0; deba@57: deba@57: /// \brief The member function to notificate the observer about the deba@57: /// container is built. deba@57: /// deba@57: /// The build() member function notificates the observer about the deba@57: /// container is built from an empty container. It have to be deba@57: /// overrided in the subclasses. deba@57: deba@57: virtual void build() = 0; deba@57: deba@57: /// \brief The member function to notificate the observer about all deba@57: /// items are erased from the container. deba@57: /// deba@57: /// The clear() member function notificates the observer about all deba@57: /// items are erased from the container. It have to be overrided in deba@57: /// the subclasses. deba@57: virtual void clear() = 0; deba@57: deba@57: }; deba@57: deba@57: protected: deba@57: deba@57: const Container* container; deba@57: deba@57: typedef std::list Observers; deba@57: Observers _observers; deba@57: deba@57: deba@57: public: deba@57: deba@57: /// \brief Default constructor. deba@57: /// deba@57: /// The default constructor of the AlterationNotifier. deba@57: /// It creates an empty notifier. deba@57: AlterationNotifier() deba@57: : container(0) {} deba@57: deba@57: /// \brief Constructor. deba@57: /// deba@57: /// Constructor with the observed container parameter. deba@57: AlterationNotifier(const Container& _container) deba@57: : container(&_container) {} deba@57: deba@57: /// \brief Copy Constructor of the AlterationNotifier. deba@57: /// deba@57: /// Copy constructor of the AlterationNotifier. deba@57: /// It creates only an empty notifier because the copiable deba@57: /// notifier's observers have to be registered still into that notifier. deba@57: AlterationNotifier(const AlterationNotifier& _notifier) deba@57: : container(_notifier.container) {} deba@57: deba@57: /// \brief Destructor. deba@57: /// deba@57: /// Destructor of the AlterationNotifier. deba@57: /// deba@57: ~AlterationNotifier() { deba@57: typename Observers::iterator it; deba@57: for (it = _observers.begin(); it != _observers.end(); ++it) { deba@57: (*it)->_notifier = 0; deba@57: } deba@57: } deba@57: deba@57: /// \brief Sets the container. deba@57: /// deba@57: /// Sets the container. deba@57: void setContainer(const Container& _container) { deba@57: container = &_container; deba@57: } deba@57: deba@57: protected: deba@57: deba@57: AlterationNotifier& operator=(const AlterationNotifier&); deba@57: deba@57: public: deba@57: deba@57: deba@57: deba@57: /// \brief First item in the container. deba@57: /// deba@57: /// Returns the first item in the container. It is deba@57: /// for start the iteration on the container. deba@57: void first(Item& item) const { deba@57: container->first(item); deba@57: } deba@57: deba@57: /// \brief Next item in the container. deba@57: /// deba@57: /// Returns the next item in the container. It is deba@57: /// for iterate on the container. deba@57: void next(Item& item) const { deba@57: container->next(item); deba@57: } deba@57: deba@57: /// \brief Returns the id of the item. deba@57: /// deba@57: /// Returns the id of the item provided by the container. deba@57: int id(const Item& item) const { deba@57: return container->id(item); deba@57: } deba@57: deba@57: /// \brief Returns the maximum id of the container. deba@57: /// deba@57: /// Returns the maximum id of the container. deba@57: int maxId() const { deba@57: return container->maxId(Item()); deba@57: } deba@57: deba@57: protected: deba@57: deba@57: void attach(ObserverBase& observer) { deba@57: observer._index = _observers.insert(_observers.begin(), &observer); deba@57: observer._notifier = this; deba@57: } deba@57: deba@57: void detach(ObserverBase& observer) { deba@57: _observers.erase(observer._index); deba@57: observer._index = _observers.end(); deba@57: observer._notifier = 0; deba@57: } deba@57: deba@57: public: deba@57: deba@57: /// \brief Notifies all the registed observers about an item added to deba@57: /// the container. deba@57: /// deba@57: /// It notifies all the registed observers about an item added to deba@57: /// the container. deba@57: /// deba@57: void add(const Item& item) { deba@57: typename Observers::reverse_iterator it; deba@57: try { deba@57: for (it = _observers.rbegin(); it != _observers.rend(); ++it) { deba@57: (*it)->add(item); deba@57: } deba@57: } catch (...) { deba@57: typename Observers::iterator jt; deba@57: for (jt = it.base(); jt != _observers.end(); ++jt) { deba@57: (*jt)->erase(item); deba@57: } deba@57: throw; deba@57: } deba@57: } deba@57: deba@57: /// \brief Notifies all the registed observers about more item added to deba@57: /// the container. deba@57: /// deba@57: /// It notifies all the registed observers about more item added to deba@57: /// the container. deba@57: /// deba@57: void add(const std::vector& items) { deba@57: typename Observers::reverse_iterator it; deba@57: try { deba@57: for (it = _observers.rbegin(); it != _observers.rend(); ++it) { deba@57: (*it)->add(items); deba@57: } deba@57: } catch (...) { deba@57: typename Observers::iterator jt; deba@57: for (jt = it.base(); jt != _observers.end(); ++jt) { deba@57: (*jt)->erase(items); deba@57: } deba@57: throw; deba@57: } deba@57: } deba@57: deba@57: /// \brief Notifies all the registed observers about an item erased from deba@57: /// the container. deba@57: /// deba@57: /// It notifies all the registed observers about an item erased from deba@57: /// the container. deba@57: /// deba@57: void erase(const Item& item) throw() { deba@57: typename Observers::iterator it = _observers.begin(); deba@57: while (it != _observers.end()) { deba@57: try { deba@57: (*it)->erase(item); deba@57: ++it; deba@57: } catch (const ImmediateDetach&) { deba@57: it = _observers.erase(it); deba@57: (*it)->_index = _observers.end(); deba@57: (*it)->_notifier = 0; deba@57: } deba@57: } deba@57: } deba@57: deba@57: /// \brief Notifies all the registed observers about more item erased deba@57: /// from the container. deba@57: /// deba@57: /// It notifies all the registed observers about more item erased from deba@57: /// the container. deba@57: /// deba@57: void erase(const std::vector& items) { deba@57: typename Observers::iterator it = _observers.begin(); deba@57: while (it != _observers.end()) { deba@57: try { deba@57: (*it)->erase(items); deba@57: ++it; deba@57: } catch (const ImmediateDetach&) { deba@57: it = _observers.erase(it); deba@57: (*it)->_index = _observers.end(); deba@57: (*it)->_notifier = 0; deba@57: } deba@57: } deba@57: } deba@57: deba@57: /// \brief Notifies all the registed observers about the container is deba@57: /// built. deba@57: /// deba@57: /// Notifies all the registed observers about the container is built deba@57: /// from an empty container. deba@57: void build() { deba@57: typename Observers::reverse_iterator it; deba@57: try { deba@57: for (it = _observers.rbegin(); it != _observers.rend(); ++it) { deba@57: (*it)->build(); deba@57: } deba@57: } catch (...) { deba@57: typename Observers::iterator jt; deba@57: for (jt = it.base(); jt != _observers.end(); ++jt) { deba@57: (*jt)->clear(); deba@57: } deba@57: throw; deba@57: } deba@57: } deba@57: deba@57: /// \brief Notifies all the registed observers about all items are deba@57: /// erased. deba@57: /// deba@57: /// Notifies all the registed observers about all items are erased deba@57: /// from the container. deba@57: void clear() { deba@57: typename Observers::iterator it = _observers.begin(); deba@57: while (it != _observers.end()) { deba@57: try { deba@57: (*it)->clear(); deba@57: ++it; deba@57: } catch (const ImmediateDetach&) { deba@57: it = _observers.erase(it); deba@57: (*it)->_index = _observers.end(); deba@57: (*it)->_notifier = 0; deba@57: } deba@57: } deba@57: } deba@57: }; deba@57: deba@57: } deba@57: deba@57: #endif