1.1 --- a/lemon/bits/alteration_notifier.h Thu Oct 09 10:09:44 2008 +0200
1.2 +++ b/lemon/bits/alteration_notifier.h Thu Oct 09 13:27:35 2008 +0200
1.3 @@ -24,75 +24,75 @@
1.4
1.5 #include <lemon/core.h>
1.6
1.7 -///\ingroup graphbits
1.8 -///\file
1.9 -///\brief Observer notifier for graph alteration observers.
1.10 +//\ingroup graphbits
1.11 +//\file
1.12 +//\brief Observer notifier for graph alteration observers.
1.13
1.14 namespace lemon {
1.15
1.16 - /// \ingroup graphbits
1.17 - ///
1.18 - /// \brief Notifier class to notify observes about alterations in
1.19 - /// a container.
1.20 - ///
1.21 - /// The simple graph's can be refered as two containers, one node container
1.22 - /// and one edge container. But they are not standard containers they
1.23 - /// does not store values directly they are just key continars for more
1.24 - /// value containers which are the node and edge maps.
1.25 - ///
1.26 - /// The graph's node and edge sets can be changed as we add or erase
1.27 - /// nodes and edges in the graph. LEMON would like to handle easily
1.28 - /// that the node and edge maps should contain values for all nodes or
1.29 - /// edges. If we want to check on every indicing if the map contains
1.30 - /// the current indicing key that cause a drawback in the performance
1.31 - /// in the library. We use another solution we notify all maps about
1.32 - /// an alteration in the graph, which cause only drawback on the
1.33 - /// alteration of the graph.
1.34 - ///
1.35 - /// This class provides an interface to the container. The \e first() and \e
1.36 - /// next() member functions make possible to iterate on the keys of the
1.37 - /// container. The \e id() function returns an integer id for each key.
1.38 - /// The \e maxId() function gives back an upper bound of the ids.
1.39 - ///
1.40 - /// For the proper functonality of this class, we should notify it
1.41 - /// about each alteration in the container. The alterations have four type
1.42 - /// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and
1.43 - /// \e erase() signals that only one or few items added or erased to or
1.44 - /// from the graph. If all items are erased from the graph or from an empty
1.45 - /// graph a new graph is builded then it can be signaled with the
1.46 - /// clear() and build() members. Important rule that if we erase items
1.47 - /// from graph we should first signal the alteration and after that erase
1.48 - /// them from the container, on the other way on item addition we should
1.49 - /// first extend the container and just after that signal the alteration.
1.50 - ///
1.51 - /// The alteration can be observed with a class inherited from the
1.52 - /// \e ObserverBase nested class. The signals can be handled with
1.53 - /// overriding the virtual functions defined in the base class. The
1.54 - /// observer base can be attached to the notifier with the
1.55 - /// \e attach() member and can be detached with detach() function. The
1.56 - /// alteration handlers should not call any function which signals
1.57 - /// an other alteration in the same notifier and should not
1.58 - /// detach any observer from the notifier.
1.59 - ///
1.60 - /// Alteration observers try to be exception safe. If an \e add() or
1.61 - /// a \e clear() function throws an exception then the remaining
1.62 - /// observeres will not be notified and the fulfilled additions will
1.63 - /// be rolled back by calling the \e erase() or \e clear()
1.64 - /// functions. Thence the \e erase() and \e clear() should not throw
1.65 - /// exception. Actullay, it can be throw only \ref ImmediateDetach
1.66 - /// exception which detach the observer from the notifier.
1.67 - ///
1.68 - /// There are some place when the alteration observing is not completly
1.69 - /// reliable. If we want to carry out the node degree in the graph
1.70 - /// as in the \ref InDegMap and we use the reverseEdge that cause
1.71 - /// unreliable functionality. Because the alteration observing signals
1.72 - /// only erasing and adding but not the reversing it will stores bad
1.73 - /// degrees. The sub graph adaptors cannot signal the alterations because
1.74 - /// just a setting in the filter map can modify the graph and this cannot
1.75 - /// be watched in any way.
1.76 - ///
1.77 - /// \param _Container The container which is observed.
1.78 - /// \param _Item The item type which is obserbved.
1.79 + // \ingroup graphbits
1.80 + //
1.81 + // \brief Notifier class to notify observes about alterations in
1.82 + // a container.
1.83 + //
1.84 + // The simple graph's can be refered as two containers, one node container
1.85 + // and one edge container. But they are not standard containers they
1.86 + // does not store values directly they are just key continars for more
1.87 + // value containers which are the node and edge maps.
1.88 + //
1.89 + // The graph's node and edge sets can be changed as we add or erase
1.90 + // nodes and edges in the graph. LEMON would like to handle easily
1.91 + // that the node and edge maps should contain values for all nodes or
1.92 + // edges. If we want to check on every indicing if the map contains
1.93 + // the current indicing key that cause a drawback in the performance
1.94 + // in the library. We use another solution we notify all maps about
1.95 + // an alteration in the graph, which cause only drawback on the
1.96 + // alteration of the graph.
1.97 + //
1.98 + // This class provides an interface to the container. The \e first() and \e
1.99 + // next() member functions make possible to iterate on the keys of the
1.100 + // container. The \e id() function returns an integer id for each key.
1.101 + // The \e maxId() function gives back an upper bound of the ids.
1.102 + //
1.103 + // For the proper functonality of this class, we should notify it
1.104 + // about each alteration in the container. The alterations have four type
1.105 + // as \e add(), \e erase(), \e build() and \e clear(). The \e add() and
1.106 + // \e erase() signals that only one or few items added or erased to or
1.107 + // from the graph. If all items are erased from the graph or from an empty
1.108 + // graph a new graph is builded then it can be signaled with the
1.109 + // clear() and build() members. Important rule that if we erase items
1.110 + // from graph we should first signal the alteration and after that erase
1.111 + // them from the container, on the other way on item addition we should
1.112 + // first extend the container and just after that signal the alteration.
1.113 + //
1.114 + // The alteration can be observed with a class inherited from the
1.115 + // \e ObserverBase nested class. The signals can be handled with
1.116 + // overriding the virtual functions defined in the base class. The
1.117 + // observer base can be attached to the notifier with the
1.118 + // \e attach() member and can be detached with detach() function. The
1.119 + // alteration handlers should not call any function which signals
1.120 + // an other alteration in the same notifier and should not
1.121 + // detach any observer from the notifier.
1.122 + //
1.123 + // Alteration observers try to be exception safe. If an \e add() or
1.124 + // a \e clear() function throws an exception then the remaining
1.125 + // observeres will not be notified and the fulfilled additions will
1.126 + // be rolled back by calling the \e erase() or \e clear()
1.127 + // functions. Thence the \e erase() and \e clear() should not throw
1.128 + // exception. Actullay, it can be throw only \ref ImmediateDetach
1.129 + // exception which detach the observer from the notifier.
1.130 + //
1.131 + // There are some place when the alteration observing is not completly
1.132 + // reliable. If we want to carry out the node degree in the graph
1.133 + // as in the \ref InDegMap and we use the reverseEdge that cause
1.134 + // unreliable functionality. Because the alteration observing signals
1.135 + // only erasing and adding but not the reversing it will stores bad
1.136 + // degrees. The sub graph adaptors cannot signal the alterations because
1.137 + // just a setting in the filter map can modify the graph and this cannot
1.138 + // be watched in any way.
1.139 + //
1.140 + // \param _Container The container which is observed.
1.141 + // \param _Item The item type which is obserbved.
1.142
1.143 template <typename _Container, typename _Item>
1.144 class AlterationNotifier {
1.145 @@ -103,92 +103,87 @@
1.146 typedef _Container Container;
1.147 typedef _Item Item;
1.148
1.149 - /// \brief Exception which can be called from \e clear() and
1.150 - /// \e erase().
1.151 - ///
1.152 - /// From the \e clear() and \e erase() function only this
1.153 - /// exception is allowed to throw. The exception immediatly
1.154 - /// detaches the current observer from the notifier. Because the
1.155 - /// \e clear() and \e erase() should not throw other exceptions
1.156 - /// it can be used to invalidate the observer.
1.157 + // \brief Exception which can be called from \e clear() and
1.158 + // \e erase().
1.159 + //
1.160 + // From the \e clear() and \e erase() function only this
1.161 + // exception is allowed to throw. The exception immediatly
1.162 + // detaches the current observer from the notifier. Because the
1.163 + // \e clear() and \e erase() should not throw other exceptions
1.164 + // it can be used to invalidate the observer.
1.165 struct ImmediateDetach {};
1.166
1.167 - /// \brief ObserverBase is the base class for the observers.
1.168 - ///
1.169 - /// ObserverBase is the abstract base class for the observers.
1.170 - /// It will be notified about an item was inserted into or
1.171 - /// erased from the graph.
1.172 - ///
1.173 - /// The observer interface contains some pure virtual functions
1.174 - /// to override. The add() and erase() functions are
1.175 - /// to notify the oberver when one item is added or
1.176 - /// erased.
1.177 - ///
1.178 - /// The build() and clear() members are to notify the observer
1.179 - /// about the container is built from an empty container or
1.180 - /// is cleared to an empty container.
1.181 -
1.182 + // \brief ObserverBase is the base class for the observers.
1.183 + //
1.184 + // ObserverBase is the abstract base class for the observers.
1.185 + // It will be notified about an item was inserted into or
1.186 + // erased from the graph.
1.187 + //
1.188 + // The observer interface contains some pure virtual functions
1.189 + // to override. The add() and erase() functions are
1.190 + // to notify the oberver when one item is added or
1.191 + // erased.
1.192 + //
1.193 + // The build() and clear() members are to notify the observer
1.194 + // about the container is built from an empty container or
1.195 + // is cleared to an empty container.
1.196 class ObserverBase {
1.197 protected:
1.198 typedef AlterationNotifier Notifier;
1.199
1.200 friend class AlterationNotifier;
1.201
1.202 - /// \brief Default constructor.
1.203 - ///
1.204 - /// Default constructor for ObserverBase.
1.205 - ///
1.206 + // \brief Default constructor.
1.207 + //
1.208 + // Default constructor for ObserverBase.
1.209 ObserverBase() : _notifier(0) {}
1.210
1.211 - /// \brief Constructor which attach the observer into notifier.
1.212 - ///
1.213 - /// Constructor which attach the observer into notifier.
1.214 + // \brief Constructor which attach the observer into notifier.
1.215 + //
1.216 + // Constructor which attach the observer into notifier.
1.217 ObserverBase(AlterationNotifier& nf) {
1.218 attach(nf);
1.219 }
1.220
1.221 - /// \brief Constructor which attach the obserever to the same notifier.
1.222 - ///
1.223 - /// Constructor which attach the obserever to the same notifier as
1.224 - /// the other observer is attached to.
1.225 + // \brief Constructor which attach the obserever to the same notifier.
1.226 + //
1.227 + // Constructor which attach the obserever to the same notifier as
1.228 + // the other observer is attached to.
1.229 ObserverBase(const ObserverBase& copy) {
1.230 if (copy.attached()) {
1.231 attach(*copy.notifier());
1.232 }
1.233 }
1.234
1.235 - /// \brief Destructor
1.236 + // \brief Destructor
1.237 virtual ~ObserverBase() {
1.238 if (attached()) {
1.239 detach();
1.240 }
1.241 }
1.242
1.243 - /// \brief Attaches the observer into an AlterationNotifier.
1.244 - ///
1.245 - /// This member attaches the observer into an AlterationNotifier.
1.246 - ///
1.247 + // \brief Attaches the observer into an AlterationNotifier.
1.248 + //
1.249 + // This member attaches the observer into an AlterationNotifier.
1.250 void attach(AlterationNotifier& nf) {
1.251 nf.attach(*this);
1.252 }
1.253
1.254 - /// \brief Detaches the observer into an AlterationNotifier.
1.255 - ///
1.256 - /// This member detaches the observer from an AlterationNotifier.
1.257 - ///
1.258 + // \brief Detaches the observer into an AlterationNotifier.
1.259 + //
1.260 + // This member detaches the observer from an AlterationNotifier.
1.261 void detach() {
1.262 _notifier->detach(*this);
1.263 }
1.264
1.265 - /// \brief Gives back a pointer to the notifier which the map
1.266 - /// attached into.
1.267 - ///
1.268 - /// This function gives back a pointer to the notifier which the map
1.269 - /// attached into.
1.270 - ///
1.271 + // \brief Gives back a pointer to the notifier which the map
1.272 + // attached into.
1.273 + //
1.274 + // This function gives back a pointer to the notifier which the map
1.275 + // attached into.
1.276 Notifier* notifier() const { return const_cast<Notifier*>(_notifier); }
1.277
1.278 - /// Gives back true when the observer is attached into a notifier.
1.279 + // Gives back true when the observer is attached into a notifier.
1.280 bool attached() const { return _notifier != 0; }
1.281
1.282 private:
1.283 @@ -200,53 +195,52 @@
1.284 Notifier* _notifier;
1.285 typename std::list<ObserverBase*>::iterator _index;
1.286
1.287 - /// \brief The member function to notificate the observer about an
1.288 - /// item is added to the container.
1.289 - ///
1.290 - /// The add() member function notificates the observer about an item
1.291 - /// is added to the container. It have to be overrided in the
1.292 - /// subclasses.
1.293 + // \brief The member function to notificate the observer about an
1.294 + // item is added to the container.
1.295 + //
1.296 + // The add() member function notificates the observer about an item
1.297 + // is added to the container. It have to be overrided in the
1.298 + // subclasses.
1.299 virtual void add(const Item&) = 0;
1.300
1.301 - /// \brief The member function to notificate the observer about
1.302 - /// more item is added to the container.
1.303 - ///
1.304 - /// The add() member function notificates the observer about more item
1.305 - /// is added to the container. It have to be overrided in the
1.306 - /// subclasses.
1.307 + // \brief The member function to notificate the observer about
1.308 + // more item is added to the container.
1.309 + //
1.310 + // The add() member function notificates the observer about more item
1.311 + // is added to the container. It have to be overrided in the
1.312 + // subclasses.
1.313 virtual void add(const std::vector<Item>& items) = 0;
1.314
1.315 - /// \brief The member function to notificate the observer about an
1.316 - /// item is erased from the container.
1.317 - ///
1.318 - /// The erase() member function notificates the observer about an
1.319 - /// item is erased from the container. It have to be overrided in
1.320 - /// the subclasses.
1.321 + // \brief The member function to notificate the observer about an
1.322 + // item is erased from the container.
1.323 + //
1.324 + // The erase() member function notificates the observer about an
1.325 + // item is erased from the container. It have to be overrided in
1.326 + // the subclasses.
1.327 virtual void erase(const Item&) = 0;
1.328
1.329 - /// \brief The member function to notificate the observer about
1.330 - /// more item is erased from the container.
1.331 - ///
1.332 - /// The erase() member function notificates the observer about more item
1.333 - /// is erased from the container. It have to be overrided in the
1.334 - /// subclasses.
1.335 + // \brief The member function to notificate the observer about
1.336 + // more item is erased from the container.
1.337 + //
1.338 + // The erase() member function notificates the observer about more item
1.339 + // is erased from the container. It have to be overrided in the
1.340 + // subclasses.
1.341 virtual void erase(const std::vector<Item>& items) = 0;
1.342
1.343 - /// \brief The member function to notificate the observer about the
1.344 - /// container is built.
1.345 - ///
1.346 - /// The build() member function notificates the observer about the
1.347 - /// container is built from an empty container. It have to be
1.348 - /// overrided in the subclasses.
1.349 -
1.350 + // \brief The member function to notificate the observer about the
1.351 + // container is built.
1.352 + //
1.353 + // The build() member function notificates the observer about the
1.354 + // container is built from an empty container. It have to be
1.355 + // overrided in the subclasses.
1.356 virtual void build() = 0;
1.357
1.358 - /// \brief The member function to notificate the observer about all
1.359 - /// items are erased from the container.
1.360 - ///
1.361 - /// The clear() member function notificates the observer about all
1.362 - /// items are erased from the container. It have to be overrided in
1.363 - /// the subclasses.
1.364 + // \brief The member function to notificate the observer about all
1.365 + // items are erased from the container.
1.366 + //
1.367 + // The clear() member function notificates the observer about all
1.368 + // items are erased from the container. It have to be overrided in
1.369 + // the subclasses.
1.370 virtual void clear() = 0;
1.371
1.372 };
1.373 @@ -261,31 +255,30 @@
1.374
1.375 public:
1.376
1.377 - /// \brief Default constructor.
1.378 - ///
1.379 - /// The default constructor of the AlterationNotifier.
1.380 - /// It creates an empty notifier.
1.381 + // \brief Default constructor.
1.382 + //
1.383 + // The default constructor of the AlterationNotifier.
1.384 + // It creates an empty notifier.
1.385 AlterationNotifier()
1.386 : container(0) {}
1.387
1.388 - /// \brief Constructor.
1.389 - ///
1.390 - /// Constructor with the observed container parameter.
1.391 + // \brief Constructor.
1.392 + //
1.393 + // Constructor with the observed container parameter.
1.394 AlterationNotifier(const Container& _container)
1.395 : container(&_container) {}
1.396
1.397 - /// \brief Copy Constructor of the AlterationNotifier.
1.398 - ///
1.399 - /// Copy constructor of the AlterationNotifier.
1.400 - /// It creates only an empty notifier because the copiable
1.401 - /// notifier's observers have to be registered still into that notifier.
1.402 + // \brief Copy Constructor of the AlterationNotifier.
1.403 + //
1.404 + // Copy constructor of the AlterationNotifier.
1.405 + // It creates only an empty notifier because the copiable
1.406 + // notifier's observers have to be registered still into that notifier.
1.407 AlterationNotifier(const AlterationNotifier& _notifier)
1.408 : container(_notifier.container) {}
1.409
1.410 - /// \brief Destructor.
1.411 - ///
1.412 - /// Destructor of the AlterationNotifier.
1.413 - ///
1.414 + // \brief Destructor.
1.415 + //
1.416 + // Destructor of the AlterationNotifier.
1.417 ~AlterationNotifier() {
1.418 typename Observers::iterator it;
1.419 for (it = _observers.begin(); it != _observers.end(); ++it) {
1.420 @@ -293,9 +286,9 @@
1.421 }
1.422 }
1.423
1.424 - /// \brief Sets the container.
1.425 - ///
1.426 - /// Sets the container.
1.427 + // \brief Sets the container.
1.428 + //
1.429 + // Sets the container.
1.430 void setContainer(const Container& _container) {
1.431 container = &_container;
1.432 }
1.433 @@ -306,34 +299,32 @@
1.434
1.435 public:
1.436
1.437 -
1.438 -
1.439 - /// \brief First item in the container.
1.440 - ///
1.441 - /// Returns the first item in the container. It is
1.442 - /// for start the iteration on the container.
1.443 + // \brief First item in the container.
1.444 + //
1.445 + // Returns the first item in the container. It is
1.446 + // for start the iteration on the container.
1.447 void first(Item& item) const {
1.448 container->first(item);
1.449 }
1.450
1.451 - /// \brief Next item in the container.
1.452 - ///
1.453 - /// Returns the next item in the container. It is
1.454 - /// for iterate on the container.
1.455 + // \brief Next item in the container.
1.456 + //
1.457 + // Returns the next item in the container. It is
1.458 + // for iterate on the container.
1.459 void next(Item& item) const {
1.460 container->next(item);
1.461 }
1.462
1.463 - /// \brief Returns the id of the item.
1.464 - ///
1.465 - /// Returns the id of the item provided by the container.
1.466 + // \brief Returns the id of the item.
1.467 + //
1.468 + // Returns the id of the item provided by the container.
1.469 int id(const Item& item) const {
1.470 return container->id(item);
1.471 }
1.472
1.473 - /// \brief Returns the maximum id of the container.
1.474 - ///
1.475 - /// Returns the maximum id of the container.
1.476 + // \brief Returns the maximum id of the container.
1.477 + //
1.478 + // Returns the maximum id of the container.
1.479 int maxId() const {
1.480 return container->maxId(Item());
1.481 }
1.482 @@ -353,12 +344,11 @@
1.483
1.484 public:
1.485
1.486 - /// \brief Notifies all the registed observers about an item added to
1.487 - /// the container.
1.488 - ///
1.489 - /// It notifies all the registed observers about an item added to
1.490 - /// the container.
1.491 - ///
1.492 + // \brief Notifies all the registed observers about an item added to
1.493 + // the container.
1.494 + //
1.495 + // It notifies all the registed observers about an item added to
1.496 + // the container.
1.497 void add(const Item& item) {
1.498 typename Observers::reverse_iterator it;
1.499 try {
1.500 @@ -374,12 +364,11 @@
1.501 }
1.502 }
1.503
1.504 - /// \brief Notifies all the registed observers about more item added to
1.505 - /// the container.
1.506 - ///
1.507 - /// It notifies all the registed observers about more item added to
1.508 - /// the container.
1.509 - ///
1.510 + // \brief Notifies all the registed observers about more item added to
1.511 + // the container.
1.512 + //
1.513 + // It notifies all the registed observers about more item added to
1.514 + // the container.
1.515 void add(const std::vector<Item>& items) {
1.516 typename Observers::reverse_iterator it;
1.517 try {
1.518 @@ -395,12 +384,11 @@
1.519 }
1.520 }
1.521
1.522 - /// \brief Notifies all the registed observers about an item erased from
1.523 - /// the container.
1.524 - ///
1.525 - /// It notifies all the registed observers about an item erased from
1.526 - /// the container.
1.527 - ///
1.528 + // \brief Notifies all the registed observers about an item erased from
1.529 + // the container.
1.530 + //
1.531 + // It notifies all the registed observers about an item erased from
1.532 + // the container.
1.533 void erase(const Item& item) throw() {
1.534 typename Observers::iterator it = _observers.begin();
1.535 while (it != _observers.end()) {
1.536 @@ -415,12 +403,11 @@
1.537 }
1.538 }
1.539
1.540 - /// \brief Notifies all the registed observers about more item erased
1.541 - /// from the container.
1.542 - ///
1.543 - /// It notifies all the registed observers about more item erased from
1.544 - /// the container.
1.545 - ///
1.546 + // \brief Notifies all the registed observers about more item erased
1.547 + // from the container.
1.548 + //
1.549 + // It notifies all the registed observers about more item erased from
1.550 + // the container.
1.551 void erase(const std::vector<Item>& items) {
1.552 typename Observers::iterator it = _observers.begin();
1.553 while (it != _observers.end()) {
1.554 @@ -435,11 +422,11 @@
1.555 }
1.556 }
1.557
1.558 - /// \brief Notifies all the registed observers about the container is
1.559 - /// built.
1.560 - ///
1.561 - /// Notifies all the registed observers about the container is built
1.562 - /// from an empty container.
1.563 + // \brief Notifies all the registed observers about the container is
1.564 + // built.
1.565 + //
1.566 + // Notifies all the registed observers about the container is built
1.567 + // from an empty container.
1.568 void build() {
1.569 typename Observers::reverse_iterator it;
1.570 try {
1.571 @@ -455,11 +442,11 @@
1.572 }
1.573 }
1.574
1.575 - /// \brief Notifies all the registed observers about all items are
1.576 - /// erased.
1.577 - ///
1.578 - /// Notifies all the registed observers about all items are erased
1.579 - /// from the container.
1.580 + // \brief Notifies all the registed observers about all items are
1.581 + // erased.
1.582 + //
1.583 + // Notifies all the registed observers about all items are erased
1.584 + // from the container.
1.585 void clear() {
1.586 typename Observers::iterator it = _observers.begin();
1.587 while (it != _observers.end()) {