lemon/bits/alteration_notifier.h
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 09 Oct 2008 10:09:44 +0200
changeset 313 64f8f7cc6168
parent 236 da953e387d31
child 314 2cc60866a0c9
permissions -rw-r--r--
Fix several doxygen warnings
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@57
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@57
     4
 *
alpar@107
     5
 * Copyright (C) 2003-2008
deba@57
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57
     8
 *
deba@57
     9
 * Permission to use, modify and distribute this software is granted
deba@57
    10
 * provided that this copyright notice appears in all copies. For
deba@57
    11
 * precise terms see the accompanying LICENSE file.
deba@57
    12
 *
deba@57
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@57
    14
 * express or implied, and with no claim as to its suitability for any
deba@57
    15
 * purpose.
deba@57
    16
 *
deba@57
    17
 */
deba@57
    18
deba@57
    19
#ifndef LEMON_BITS_ALTERATION_NOTIFIER_H
deba@57
    20
#define LEMON_BITS_ALTERATION_NOTIFIER_H
deba@57
    21
deba@57
    22
#include <vector>
deba@57
    23
#include <list>
deba@57
    24
deba@220
    25
#include <lemon/core.h>
deba@57
    26
deba@57
    27
///\ingroup graphbits
deba@57
    28
///\file
deba@57
    29
///\brief Observer notifier for graph alteration observers.
deba@57
    30
deba@57
    31
namespace lemon {
deba@57
    32
deba@57
    33
  /// \ingroup graphbits
deba@57
    34
  ///
alpar@209
    35
  /// \brief Notifier class to notify observes about alterations in
deba@57
    36
  /// a container.
deba@57
    37
  ///
deba@57
    38
  /// The simple graph's can be refered as two containers, one node container
deba@57
    39
  /// and one edge container. But they are not standard containers they
deba@57
    40
  /// does not store values directly they are just key continars for more
deba@57
    41
  /// value containers which are the node and edge maps.
deba@57
    42
  ///
deba@57
    43
  /// The graph's node and edge sets can be changed as we add or erase
ladanyi@236
    44
  /// nodes and edges in the graph. LEMON would like to handle easily
deba@57
    45
  /// that the node and edge maps should contain values for all nodes or
deba@57
    46
  /// edges. If we want to check on every indicing if the map contains
deba@57
    47
  /// the current indicing key that cause a drawback in the performance
deba@57
    48
  /// in the library. We use another solution we notify all maps about
deba@57
    49
  /// an alteration in the graph, which cause only drawback on the
deba@57
    50
  /// alteration of the graph.
deba@57
    51
  ///
alpar@209
    52
  /// This class provides an interface to the container. The \e first() and \e
deba@57
    53
  /// next() member functions make possible to iterate on the keys of the
deba@57
    54
  /// container. The \e id() function returns an integer id for each key.
deba@57
    55
  /// The \e maxId() function gives back an upper bound of the ids.
deba@57
    56
  ///
deba@57
    57
  /// For the proper functonality of this class, we should notify it
deba@57
    58
  /// about each alteration in the container. The alterations have four type
deba@57
    59
  /// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and
deba@57
    60
  /// \e erase() signals that only one or few items added or erased to or
deba@57
    61
  /// from the graph. If all items are erased from the graph or from an empty
deba@57
    62
  /// graph a new graph is builded then it can be signaled with the
alpar@209
    63
  /// clear() and build() members. Important rule that if we erase items
deba@57
    64
  /// from graph we should first signal the alteration and after that erase
deba@57
    65
  /// them from the container, on the other way on item addition we should
deba@57
    66
  /// first extend the container and just after that signal the alteration.
deba@57
    67
  ///
deba@57
    68
  /// The alteration can be observed with a class inherited from the
deba@57
    69
  /// \e ObserverBase nested class. The signals can be handled with
deba@57
    70
  /// overriding the virtual functions defined in the base class.  The
alpar@209
    71
  /// observer base can be attached to the notifier with the
deba@57
    72
  /// \e attach() member and can be detached with detach() function. The
deba@57
    73
  /// alteration handlers should not call any function which signals
deba@57
    74
  /// an other alteration in the same notifier and should not
deba@57
    75
  /// detach any observer from the notifier.
deba@57
    76
  ///
deba@57
    77
  /// Alteration observers try to be exception safe. If an \e add() or
deba@57
    78
  /// a \e clear() function throws an exception then the remaining
deba@57
    79
  /// observeres will not be notified and the fulfilled additions will
deba@57
    80
  /// be rolled back by calling the \e erase() or \e clear()
deba@57
    81
  /// functions. Thence the \e erase() and \e clear() should not throw
kpeter@313
    82
  /// exception. Actullay, it can be throw only \ref ImmediateDetach 
deba@57
    83
  /// exception which detach the observer from the notifier.
deba@57
    84
  ///
deba@57
    85
  /// There are some place when the alteration observing is not completly
deba@57
    86
  /// reliable. If we want to carry out the node degree in the graph
alpar@209
    87
  /// as in the \ref InDegMap and we use the reverseEdge that cause
deba@57
    88
  /// unreliable functionality. Because the alteration observing signals
deba@57
    89
  /// only erasing and adding but not the reversing it will stores bad
deba@57
    90
  /// degrees. The sub graph adaptors cannot signal the alterations because
deba@57
    91
  /// just a setting in the filter map can modify the graph and this cannot
deba@57
    92
  /// be watched in any way.
deba@57
    93
  ///
deba@57
    94
  /// \param _Container The container which is observed.
deba@57
    95
  /// \param _Item The item type which is obserbved.
deba@57
    96
deba@57
    97
  template <typename _Container, typename _Item>
deba@57
    98
  class AlterationNotifier {
deba@57
    99
  public:
deba@57
   100
deba@57
   101
    typedef True Notifier;
deba@57
   102
deba@57
   103
    typedef _Container Container;
deba@57
   104
    typedef _Item Item;
deba@57
   105
alpar@209
   106
    /// \brief Exception which can be called from \e clear() and
deba@57
   107
    /// \e erase().
deba@57
   108
    ///
deba@57
   109
    /// From the \e clear() and \e erase() function only this
deba@57
   110
    /// exception is allowed to throw. The exception immediatly
deba@57
   111
    /// detaches the current observer from the notifier. Because the
deba@57
   112
    /// \e clear() and \e erase() should not throw other exceptions
deba@57
   113
    /// it can be used to invalidate the observer.
deba@57
   114
    struct ImmediateDetach {};
deba@57
   115
deba@57
   116
    /// \brief ObserverBase is the base class for the observers.
deba@57
   117
    ///
deba@57
   118
    /// ObserverBase is the abstract base class for the observers.
deba@57
   119
    /// It will be notified about an item was inserted into or
deba@57
   120
    /// erased from the graph.
deba@57
   121
    ///
deba@57
   122
    /// The observer interface contains some pure virtual functions
deba@57
   123
    /// to override. The add() and erase() functions are
deba@57
   124
    /// to notify the oberver when one item is added or
deba@57
   125
    /// erased.
deba@57
   126
    ///
deba@57
   127
    /// The build() and clear() members are to notify the observer
deba@57
   128
    /// about the container is built from an empty container or
alpar@209
   129
    /// is cleared to an empty container.
deba@57
   130
deba@57
   131
    class ObserverBase {
deba@57
   132
    protected:
deba@57
   133
      typedef AlterationNotifier Notifier;
deba@57
   134
deba@57
   135
      friend class AlterationNotifier;
deba@57
   136
deba@57
   137
      /// \brief Default constructor.
deba@57
   138
      ///
deba@57
   139
      /// Default constructor for ObserverBase.
alpar@209
   140
      ///
deba@57
   141
      ObserverBase() : _notifier(0) {}
deba@57
   142
deba@57
   143
      /// \brief Constructor which attach the observer into notifier.
deba@57
   144
      ///
deba@57
   145
      /// Constructor which attach the observer into notifier.
deba@57
   146
      ObserverBase(AlterationNotifier& nf) {
deba@57
   147
        attach(nf);
deba@57
   148
      }
deba@57
   149
deba@57
   150
      /// \brief Constructor which attach the obserever to the same notifier.
deba@57
   151
      ///
deba@57
   152
      /// Constructor which attach the obserever to the same notifier as
alpar@209
   153
      /// the other observer is attached to.
deba@57
   154
      ObserverBase(const ObserverBase& copy) {
alpar@209
   155
        if (copy.attached()) {
deba@57
   156
          attach(*copy.notifier());
alpar@209
   157
        }
deba@57
   158
      }
alpar@209
   159
deba@57
   160
      /// \brief Destructor
deba@57
   161
      virtual ~ObserverBase() {
deba@57
   162
        if (attached()) {
deba@57
   163
          detach();
deba@57
   164
        }
deba@57
   165
      }
deba@57
   166
deba@57
   167
      /// \brief Attaches the observer into an AlterationNotifier.
deba@57
   168
      ///
deba@57
   169
      /// This member attaches the observer into an AlterationNotifier.
deba@57
   170
      ///
deba@57
   171
      void attach(AlterationNotifier& nf) {
alpar@209
   172
        nf.attach(*this);
deba@57
   173
      }
alpar@209
   174
deba@57
   175
      /// \brief Detaches the observer into an AlterationNotifier.
deba@57
   176
      ///
deba@57
   177
      /// This member detaches the observer from an AlterationNotifier.
deba@57
   178
      ///
deba@57
   179
      void detach() {
deba@57
   180
        _notifier->detach(*this);
deba@57
   181
      }
alpar@209
   182
alpar@209
   183
      /// \brief Gives back a pointer to the notifier which the map
deba@57
   184
      /// attached into.
deba@57
   185
      ///
deba@57
   186
      /// This function gives back a pointer to the notifier which the map
deba@57
   187
      /// attached into.
deba@57
   188
      ///
deba@57
   189
      Notifier* notifier() const { return const_cast<Notifier*>(_notifier); }
alpar@209
   190
deba@57
   191
      /// Gives back true when the observer is attached into a notifier.
deba@57
   192
      bool attached() const { return _notifier != 0; }
deba@57
   193
deba@57
   194
    private:
deba@57
   195
deba@57
   196
      ObserverBase& operator=(const ObserverBase& copy);
deba@57
   197
deba@57
   198
    protected:
alpar@209
   199
deba@57
   200
      Notifier* _notifier;
deba@57
   201
      typename std::list<ObserverBase*>::iterator _index;
deba@57
   202
deba@57
   203
      /// \brief The member function to notificate the observer about an
deba@57
   204
      /// item is added to the container.
deba@57
   205
      ///
deba@57
   206
      /// The add() member function notificates the observer about an item
deba@57
   207
      /// is added to the container. It have to be overrided in the
deba@57
   208
      /// subclasses.
deba@57
   209
      virtual void add(const Item&) = 0;
deba@57
   210
alpar@209
   211
      /// \brief The member function to notificate the observer about
deba@57
   212
      /// more item is added to the container.
deba@57
   213
      ///
deba@57
   214
      /// The add() member function notificates the observer about more item
deba@57
   215
      /// is added to the container. It have to be overrided in the
deba@57
   216
      /// subclasses.
deba@57
   217
      virtual void add(const std::vector<Item>& items) = 0;
deba@57
   218
deba@57
   219
      /// \brief The member function to notificate the observer about an
deba@57
   220
      /// item is erased from the container.
deba@57
   221
      ///
deba@57
   222
      /// The erase() member function notificates the observer about an
deba@57
   223
      /// item is erased from the container. It have to be overrided in
alpar@209
   224
      /// the subclasses.
deba@57
   225
      virtual void erase(const Item&) = 0;
deba@57
   226
alpar@209
   227
      /// \brief The member function to notificate the observer about
deba@57
   228
      /// more item is erased from the container.
deba@57
   229
      ///
deba@57
   230
      /// The erase() member function notificates the observer about more item
deba@57
   231
      /// is erased from the container. It have to be overrided in the
deba@57
   232
      /// subclasses.
deba@57
   233
      virtual void erase(const std::vector<Item>& items) = 0;
deba@57
   234
deba@57
   235
      /// \brief The member function to notificate the observer about the
deba@57
   236
      /// container is built.
deba@57
   237
      ///
deba@57
   238
      /// The build() member function notificates the observer about the
deba@57
   239
      /// container is built from an empty container. It have to be
deba@57
   240
      /// overrided in the subclasses.
deba@57
   241
deba@57
   242
      virtual void build() = 0;
deba@57
   243
deba@57
   244
      /// \brief The member function to notificate the observer about all
deba@57
   245
      /// items are erased from the container.
deba@57
   246
      ///
deba@57
   247
      /// The clear() member function notificates the observer about all
deba@57
   248
      /// items are erased from the container. It have to be overrided in
alpar@209
   249
      /// the subclasses.
deba@57
   250
      virtual void clear() = 0;
deba@57
   251
deba@57
   252
    };
alpar@209
   253
deba@57
   254
  protected:
deba@57
   255
deba@57
   256
    const Container* container;
deba@57
   257
alpar@209
   258
    typedef std::list<ObserverBase*> Observers;
deba@57
   259
    Observers _observers;
deba@57
   260
alpar@209
   261
deba@57
   262
  public:
deba@57
   263
deba@57
   264
    /// \brief Default constructor.
deba@57
   265
    ///
alpar@209
   266
    /// The default constructor of the AlterationNotifier.
deba@57
   267
    /// It creates an empty notifier.
alpar@209
   268
    AlterationNotifier()
deba@57
   269
      : container(0) {}
deba@57
   270
deba@57
   271
    /// \brief Constructor.
deba@57
   272
    ///
deba@57
   273
    /// Constructor with the observed container parameter.
alpar@209
   274
    AlterationNotifier(const Container& _container)
deba@57
   275
      : container(&_container) {}
deba@57
   276
alpar@209
   277
    /// \brief Copy Constructor of the AlterationNotifier.
deba@57
   278
    ///
alpar@209
   279
    /// Copy constructor of the AlterationNotifier.
deba@57
   280
    /// It creates only an empty notifier because the copiable
deba@57
   281
    /// notifier's observers have to be registered still into that notifier.
alpar@209
   282
    AlterationNotifier(const AlterationNotifier& _notifier)
deba@57
   283
      : container(_notifier.container) {}
deba@57
   284
deba@57
   285
    /// \brief Destructor.
alpar@209
   286
    ///
deba@57
   287
    /// Destructor of the AlterationNotifier.
deba@57
   288
    ///
deba@57
   289
    ~AlterationNotifier() {
deba@57
   290
      typename Observers::iterator it;
deba@57
   291
      for (it = _observers.begin(); it != _observers.end(); ++it) {
alpar@209
   292
        (*it)->_notifier = 0;
deba@57
   293
      }
deba@57
   294
    }
deba@57
   295
deba@57
   296
    /// \brief Sets the container.
deba@57
   297
    ///
deba@57
   298
    /// Sets the container.
deba@57
   299
    void setContainer(const Container& _container) {
deba@57
   300
      container = &_container;
deba@57
   301
    }
deba@57
   302
deba@57
   303
  protected:
deba@57
   304
deba@57
   305
    AlterationNotifier& operator=(const AlterationNotifier&);
deba@57
   306
deba@57
   307
  public:
deba@57
   308
deba@57
   309
deba@57
   310
deba@57
   311
    /// \brief First item in the container.
deba@57
   312
    ///
deba@57
   313
    /// Returns the first item in the container. It is
deba@57
   314
    /// for start the iteration on the container.
deba@57
   315
    void first(Item& item) const {
deba@57
   316
      container->first(item);
deba@57
   317
    }
deba@57
   318
deba@57
   319
    /// \brief Next item in the container.
deba@57
   320
    ///
deba@57
   321
    /// Returns the next item in the container. It is
deba@57
   322
    /// for iterate on the container.
deba@57
   323
    void next(Item& item) const {
deba@57
   324
      container->next(item);
deba@57
   325
    }
deba@57
   326
deba@57
   327
    /// \brief Returns the id of the item.
deba@57
   328
    ///
deba@57
   329
    /// Returns the id of the item provided by the container.
deba@57
   330
    int id(const Item& item) const {
deba@57
   331
      return container->id(item);
deba@57
   332
    }
deba@57
   333
deba@57
   334
    /// \brief Returns the maximum id of the container.
deba@57
   335
    ///
deba@57
   336
    /// Returns the maximum id of the container.
deba@57
   337
    int maxId() const {
deba@57
   338
      return container->maxId(Item());
deba@57
   339
    }
alpar@209
   340
deba@57
   341
  protected:
deba@57
   342
deba@57
   343
    void attach(ObserverBase& observer) {
deba@57
   344
      observer._index = _observers.insert(_observers.begin(), &observer);
deba@57
   345
      observer._notifier = this;
alpar@209
   346
    }
deba@57
   347
deba@57
   348
    void detach(ObserverBase& observer) {
deba@57
   349
      _observers.erase(observer._index);
deba@57
   350
      observer._index = _observers.end();
deba@57
   351
      observer._notifier = 0;
deba@57
   352
    }
deba@57
   353
deba@57
   354
  public:
alpar@209
   355
alpar@209
   356
    /// \brief Notifies all the registed observers about an item added to
deba@57
   357
    /// the container.
deba@57
   358
    ///
alpar@209
   359
    /// It notifies all the registed observers about an item added to
deba@57
   360
    /// the container.
alpar@209
   361
    ///
deba@57
   362
    void add(const Item& item) {
deba@57
   363
      typename Observers::reverse_iterator it;
deba@57
   364
      try {
deba@57
   365
        for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
deba@57
   366
          (*it)->add(item);
deba@57
   367
        }
deba@57
   368
      } catch (...) {
deba@57
   369
        typename Observers::iterator jt;
deba@57
   370
        for (jt = it.base(); jt != _observers.end(); ++jt) {
deba@57
   371
          (*jt)->erase(item);
deba@57
   372
        }
deba@57
   373
        throw;
deba@57
   374
      }
alpar@209
   375
    }
deba@57
   376
alpar@209
   377
    /// \brief Notifies all the registed observers about more item added to
deba@57
   378
    /// the container.
deba@57
   379
    ///
alpar@209
   380
    /// It notifies all the registed observers about more item added to
deba@57
   381
    /// the container.
alpar@209
   382
    ///
deba@57
   383
    void add(const std::vector<Item>& items) {
deba@57
   384
      typename Observers::reverse_iterator it;
deba@57
   385
      try {
deba@57
   386
        for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
deba@57
   387
          (*it)->add(items);
deba@57
   388
        }
deba@57
   389
      } catch (...) {
deba@57
   390
        typename Observers::iterator jt;
deba@57
   391
        for (jt = it.base(); jt != _observers.end(); ++jt) {
deba@57
   392
          (*jt)->erase(items);
deba@57
   393
        }
deba@57
   394
        throw;
deba@57
   395
      }
alpar@209
   396
    }
deba@57
   397
alpar@209
   398
    /// \brief Notifies all the registed observers about an item erased from
deba@57
   399
    /// the container.
alpar@209
   400
    ///
alpar@209
   401
    /// It notifies all the registed observers about an item erased from
deba@57
   402
    /// the container.
alpar@209
   403
    ///
deba@57
   404
    void erase(const Item& item) throw() {
deba@57
   405
      typename Observers::iterator it = _observers.begin();
deba@57
   406
      while (it != _observers.end()) {
deba@57
   407
        try {
deba@57
   408
          (*it)->erase(item);
deba@57
   409
          ++it;
deba@57
   410
        } catch (const ImmediateDetach&) {
deba@57
   411
          (*it)->_index = _observers.end();
deba@57
   412
          (*it)->_notifier = 0;
deba@230
   413
          it = _observers.erase(it);
deba@57
   414
        }
deba@57
   415
      }
deba@57
   416
    }
deba@57
   417
alpar@209
   418
    /// \brief Notifies all the registed observers about more item erased
deba@57
   419
    /// from the container.
alpar@209
   420
    ///
alpar@209
   421
    /// It notifies all the registed observers about more item erased from
deba@57
   422
    /// the container.
alpar@209
   423
    ///
deba@57
   424
    void erase(const std::vector<Item>& items) {
deba@57
   425
      typename Observers::iterator it = _observers.begin();
deba@57
   426
      while (it != _observers.end()) {
deba@57
   427
        try {
deba@57
   428
          (*it)->erase(items);
deba@57
   429
          ++it;
deba@57
   430
        } catch (const ImmediateDetach&) {
deba@57
   431
          (*it)->_index = _observers.end();
deba@57
   432
          (*it)->_notifier = 0;
deba@230
   433
          it = _observers.erase(it);
deba@57
   434
        }
deba@57
   435
      }
deba@57
   436
    }
deba@57
   437
alpar@209
   438
    /// \brief Notifies all the registed observers about the container is
deba@57
   439
    /// built.
alpar@209
   440
    ///
deba@57
   441
    /// Notifies all the registed observers about the container is built
deba@57
   442
    /// from an empty container.
deba@57
   443
    void build() {
deba@57
   444
      typename Observers::reverse_iterator it;
deba@57
   445
      try {
deba@57
   446
        for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
deba@57
   447
          (*it)->build();
deba@57
   448
        }
deba@57
   449
      } catch (...) {
deba@57
   450
        typename Observers::iterator jt;
deba@57
   451
        for (jt = it.base(); jt != _observers.end(); ++jt) {
deba@57
   452
          (*jt)->clear();
deba@57
   453
        }
deba@57
   454
        throw;
deba@57
   455
      }
deba@57
   456
    }
deba@57
   457
alpar@209
   458
    /// \brief Notifies all the registed observers about all items are
deba@57
   459
    /// erased.
deba@57
   460
    ///
deba@57
   461
    /// Notifies all the registed observers about all items are erased
deba@57
   462
    /// from the container.
deba@57
   463
    void clear() {
deba@57
   464
      typename Observers::iterator it = _observers.begin();
deba@57
   465
      while (it != _observers.end()) {
deba@57
   466
        try {
deba@57
   467
          (*it)->clear();
deba@57
   468
          ++it;
deba@57
   469
        } catch (const ImmediateDetach&) {
deba@57
   470
          (*it)->_index = _observers.end();
deba@57
   471
          (*it)->_notifier = 0;
deba@230
   472
          it = _observers.erase(it);
deba@57
   473
        }
deba@57
   474
      }
deba@57
   475
    }
deba@57
   476
  };
deba@57
   477
deba@57
   478
}
deba@57
   479
deba@57
   480
#endif