lemon/bits/alteration_notifier.h
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 13 Jul 2008 20:09:47 +0100
changeset 210 81cfc04531e8
parent 157 2ccc1afc2c52
child 220 a5d8c039f218
permissions -rw-r--r--
Remove long lines (from all but one file)
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@57
    25
#include <lemon/bits/utility.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
deba@57
    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
alpar@209
    82
  /// exception. Actullay, it can be throw only
deba@57
    83
  /// \ref AlterationObserver::ImmediateDetach ImmediateDetach
deba@57
    84
  /// exception which detach the observer from the notifier.
deba@57
    85
  ///
deba@57
    86
  /// There are some place when the alteration observing is not completly
deba@57
    87
  /// reliable. If we want to carry out the node degree in the graph
alpar@209
    88
  /// as in the \ref InDegMap and we use the reverseEdge that cause
deba@57
    89
  /// unreliable functionality. Because the alteration observing signals
deba@57
    90
  /// only erasing and adding but not the reversing it will stores bad
deba@57
    91
  /// degrees. The sub graph adaptors cannot signal the alterations because
deba@57
    92
  /// just a setting in the filter map can modify the graph and this cannot
deba@57
    93
  /// be watched in any way.
deba@57
    94
  ///
deba@57
    95
  /// \param _Container The container which is observed.
deba@57
    96
  /// \param _Item The item type which is obserbved.
deba@57
    97
deba@57
    98
  template <typename _Container, typename _Item>
deba@57
    99
  class AlterationNotifier {
deba@57
   100
  public:
deba@57
   101
deba@57
   102
    typedef True Notifier;
deba@57
   103
deba@57
   104
    typedef _Container Container;
deba@57
   105
    typedef _Item Item;
deba@57
   106
alpar@209
   107
    /// \brief Exception which can be called from \e clear() and
deba@57
   108
    /// \e erase().
deba@57
   109
    ///
deba@57
   110
    /// From the \e clear() and \e erase() function only this
deba@57
   111
    /// exception is allowed to throw. The exception immediatly
deba@57
   112
    /// detaches the current observer from the notifier. Because the
deba@57
   113
    /// \e clear() and \e erase() should not throw other exceptions
deba@57
   114
    /// it can be used to invalidate the observer.
deba@57
   115
    struct ImmediateDetach {};
deba@57
   116
deba@57
   117
    /// \brief ObserverBase is the base class for the observers.
deba@57
   118
    ///
deba@57
   119
    /// ObserverBase is the abstract base class for the observers.
deba@57
   120
    /// It will be notified about an item was inserted into or
deba@57
   121
    /// erased from the graph.
deba@57
   122
    ///
deba@57
   123
    /// The observer interface contains some pure virtual functions
deba@57
   124
    /// to override. The add() and erase() functions are
deba@57
   125
    /// to notify the oberver when one item is added or
deba@57
   126
    /// erased.
deba@57
   127
    ///
deba@57
   128
    /// The build() and clear() members are to notify the observer
deba@57
   129
    /// about the container is built from an empty container or
alpar@209
   130
    /// is cleared to an empty container.
deba@57
   131
deba@57
   132
    class ObserverBase {
deba@57
   133
    protected:
deba@57
   134
      typedef AlterationNotifier Notifier;
deba@57
   135
deba@57
   136
      friend class AlterationNotifier;
deba@57
   137
deba@57
   138
      /// \brief Default constructor.
deba@57
   139
      ///
deba@57
   140
      /// Default constructor for ObserverBase.
alpar@209
   141
      ///
deba@57
   142
      ObserverBase() : _notifier(0) {}
deba@57
   143
deba@57
   144
      /// \brief Constructor which attach the observer into notifier.
deba@57
   145
      ///
deba@57
   146
      /// Constructor which attach the observer into notifier.
deba@57
   147
      ObserverBase(AlterationNotifier& nf) {
deba@57
   148
        attach(nf);
deba@57
   149
      }
deba@57
   150
deba@57
   151
      /// \brief Constructor which attach the obserever to the same notifier.
deba@57
   152
      ///
deba@57
   153
      /// Constructor which attach the obserever to the same notifier as
alpar@209
   154
      /// the other observer is attached to.
deba@57
   155
      ObserverBase(const ObserverBase& copy) {
alpar@209
   156
        if (copy.attached()) {
deba@57
   157
          attach(*copy.notifier());
alpar@209
   158
        }
deba@57
   159
      }
alpar@209
   160
deba@57
   161
      /// \brief Destructor
deba@57
   162
      virtual ~ObserverBase() {
deba@57
   163
        if (attached()) {
deba@57
   164
          detach();
deba@57
   165
        }
deba@57
   166
      }
deba@57
   167
deba@57
   168
      /// \brief Attaches the observer into an AlterationNotifier.
deba@57
   169
      ///
deba@57
   170
      /// This member attaches the observer into an AlterationNotifier.
deba@57
   171
      ///
deba@57
   172
      void attach(AlterationNotifier& nf) {
alpar@209
   173
        nf.attach(*this);
deba@57
   174
      }
alpar@209
   175
deba@57
   176
      /// \brief Detaches the observer into an AlterationNotifier.
deba@57
   177
      ///
deba@57
   178
      /// This member detaches the observer from an AlterationNotifier.
deba@57
   179
      ///
deba@57
   180
      void detach() {
deba@57
   181
        _notifier->detach(*this);
deba@57
   182
      }
alpar@209
   183
alpar@209
   184
      /// \brief Gives back a pointer to the notifier which the map
deba@57
   185
      /// attached into.
deba@57
   186
      ///
deba@57
   187
      /// This function gives back a pointer to the notifier which the map
deba@57
   188
      /// attached into.
deba@57
   189
      ///
deba@57
   190
      Notifier* notifier() const { return const_cast<Notifier*>(_notifier); }
alpar@209
   191
deba@57
   192
      /// Gives back true when the observer is attached into a notifier.
deba@57
   193
      bool attached() const { return _notifier != 0; }
deba@57
   194
deba@57
   195
    private:
deba@57
   196
deba@57
   197
      ObserverBase& operator=(const ObserverBase& copy);
deba@57
   198
deba@57
   199
    protected:
alpar@209
   200
deba@57
   201
      Notifier* _notifier;
deba@57
   202
      typename std::list<ObserverBase*>::iterator _index;
deba@57
   203
deba@57
   204
      /// \brief The member function to notificate the observer about an
deba@57
   205
      /// item is added to the container.
deba@57
   206
      ///
deba@57
   207
      /// The add() member function notificates the observer about an item
deba@57
   208
      /// is added to the container. It have to be overrided in the
deba@57
   209
      /// subclasses.
deba@57
   210
      virtual void add(const Item&) = 0;
deba@57
   211
alpar@209
   212
      /// \brief The member function to notificate the observer about
deba@57
   213
      /// more item is added to the container.
deba@57
   214
      ///
deba@57
   215
      /// The add() member function notificates the observer about more item
deba@57
   216
      /// is added to the container. It have to be overrided in the
deba@57
   217
      /// subclasses.
deba@57
   218
      virtual void add(const std::vector<Item>& items) = 0;
deba@57
   219
deba@57
   220
      /// \brief The member function to notificate the observer about an
deba@57
   221
      /// item is erased from the container.
deba@57
   222
      ///
deba@57
   223
      /// The erase() member function notificates the observer about an
deba@57
   224
      /// item is erased from the container. It have to be overrided in
alpar@209
   225
      /// the subclasses.
deba@57
   226
      virtual void erase(const Item&) = 0;
deba@57
   227
alpar@209
   228
      /// \brief The member function to notificate the observer about
deba@57
   229
      /// more item is erased from the container.
deba@57
   230
      ///
deba@57
   231
      /// The erase() member function notificates the observer about more item
deba@57
   232
      /// is erased from the container. It have to be overrided in the
deba@57
   233
      /// subclasses.
deba@57
   234
      virtual void erase(const std::vector<Item>& items) = 0;
deba@57
   235
deba@57
   236
      /// \brief The member function to notificate the observer about the
deba@57
   237
      /// container is built.
deba@57
   238
      ///
deba@57
   239
      /// The build() member function notificates the observer about the
deba@57
   240
      /// container is built from an empty container. It have to be
deba@57
   241
      /// overrided in the subclasses.
deba@57
   242
deba@57
   243
      virtual void build() = 0;
deba@57
   244
deba@57
   245
      /// \brief The member function to notificate the observer about all
deba@57
   246
      /// items are erased from the container.
deba@57
   247
      ///
deba@57
   248
      /// The clear() member function notificates the observer about all
deba@57
   249
      /// items are erased from the container. It have to be overrided in
alpar@209
   250
      /// the subclasses.
deba@57
   251
      virtual void clear() = 0;
deba@57
   252
deba@57
   253
    };
alpar@209
   254
deba@57
   255
  protected:
deba@57
   256
deba@57
   257
    const Container* container;
deba@57
   258
alpar@209
   259
    typedef std::list<ObserverBase*> Observers;
deba@57
   260
    Observers _observers;
deba@57
   261
alpar@209
   262
deba@57
   263
  public:
deba@57
   264
deba@57
   265
    /// \brief Default constructor.
deba@57
   266
    ///
alpar@209
   267
    /// The default constructor of the AlterationNotifier.
deba@57
   268
    /// It creates an empty notifier.
alpar@209
   269
    AlterationNotifier()
deba@57
   270
      : container(0) {}
deba@57
   271
deba@57
   272
    /// \brief Constructor.
deba@57
   273
    ///
deba@57
   274
    /// Constructor with the observed container parameter.
alpar@209
   275
    AlterationNotifier(const Container& _container)
deba@57
   276
      : container(&_container) {}
deba@57
   277
alpar@209
   278
    /// \brief Copy Constructor of the AlterationNotifier.
deba@57
   279
    ///
alpar@209
   280
    /// Copy constructor of the AlterationNotifier.
deba@57
   281
    /// It creates only an empty notifier because the copiable
deba@57
   282
    /// notifier's observers have to be registered still into that notifier.
alpar@209
   283
    AlterationNotifier(const AlterationNotifier& _notifier)
deba@57
   284
      : container(_notifier.container) {}
deba@57
   285
deba@57
   286
    /// \brief Destructor.
alpar@209
   287
    ///
deba@57
   288
    /// Destructor of the AlterationNotifier.
deba@57
   289
    ///
deba@57
   290
    ~AlterationNotifier() {
deba@57
   291
      typename Observers::iterator it;
deba@57
   292
      for (it = _observers.begin(); it != _observers.end(); ++it) {
alpar@209
   293
        (*it)->_notifier = 0;
deba@57
   294
      }
deba@57
   295
    }
deba@57
   296
deba@57
   297
    /// \brief Sets the container.
deba@57
   298
    ///
deba@57
   299
    /// Sets the container.
deba@57
   300
    void setContainer(const Container& _container) {
deba@57
   301
      container = &_container;
deba@57
   302
    }
deba@57
   303
deba@57
   304
  protected:
deba@57
   305
deba@57
   306
    AlterationNotifier& operator=(const AlterationNotifier&);
deba@57
   307
deba@57
   308
  public:
deba@57
   309
deba@57
   310
deba@57
   311
deba@57
   312
    /// \brief First item in the container.
deba@57
   313
    ///
deba@57
   314
    /// Returns the first item in the container. It is
deba@57
   315
    /// for start the iteration on the container.
deba@57
   316
    void first(Item& item) const {
deba@57
   317
      container->first(item);
deba@57
   318
    }
deba@57
   319
deba@57
   320
    /// \brief Next item in the container.
deba@57
   321
    ///
deba@57
   322
    /// Returns the next item in the container. It is
deba@57
   323
    /// for iterate on the container.
deba@57
   324
    void next(Item& item) const {
deba@57
   325
      container->next(item);
deba@57
   326
    }
deba@57
   327
deba@57
   328
    /// \brief Returns the id of the item.
deba@57
   329
    ///
deba@57
   330
    /// Returns the id of the item provided by the container.
deba@57
   331
    int id(const Item& item) const {
deba@57
   332
      return container->id(item);
deba@57
   333
    }
deba@57
   334
deba@57
   335
    /// \brief Returns the maximum id of the container.
deba@57
   336
    ///
deba@57
   337
    /// Returns the maximum id of the container.
deba@57
   338
    int maxId() const {
deba@57
   339
      return container->maxId(Item());
deba@57
   340
    }
alpar@209
   341
deba@57
   342
  protected:
deba@57
   343
deba@57
   344
    void attach(ObserverBase& observer) {
deba@57
   345
      observer._index = _observers.insert(_observers.begin(), &observer);
deba@57
   346
      observer._notifier = this;
alpar@209
   347
    }
deba@57
   348
deba@57
   349
    void detach(ObserverBase& observer) {
deba@57
   350
      _observers.erase(observer._index);
deba@57
   351
      observer._index = _observers.end();
deba@57
   352
      observer._notifier = 0;
deba@57
   353
    }
deba@57
   354
deba@57
   355
  public:
alpar@209
   356
alpar@209
   357
    /// \brief Notifies all the registed observers about an item added to
deba@57
   358
    /// the container.
deba@57
   359
    ///
alpar@209
   360
    /// It notifies all the registed observers about an item added to
deba@57
   361
    /// the container.
alpar@209
   362
    ///
deba@57
   363
    void add(const Item& item) {
deba@57
   364
      typename Observers::reverse_iterator it;
deba@57
   365
      try {
deba@57
   366
        for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
deba@57
   367
          (*it)->add(item);
deba@57
   368
        }
deba@57
   369
      } catch (...) {
deba@57
   370
        typename Observers::iterator jt;
deba@57
   371
        for (jt = it.base(); jt != _observers.end(); ++jt) {
deba@57
   372
          (*jt)->erase(item);
deba@57
   373
        }
deba@57
   374
        throw;
deba@57
   375
      }
alpar@209
   376
    }
deba@57
   377
alpar@209
   378
    /// \brief Notifies all the registed observers about more item added to
deba@57
   379
    /// the container.
deba@57
   380
    ///
alpar@209
   381
    /// It notifies all the registed observers about more item added to
deba@57
   382
    /// the container.
alpar@209
   383
    ///
deba@57
   384
    void add(const std::vector<Item>& items) {
deba@57
   385
      typename Observers::reverse_iterator it;
deba@57
   386
      try {
deba@57
   387
        for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
deba@57
   388
          (*it)->add(items);
deba@57
   389
        }
deba@57
   390
      } catch (...) {
deba@57
   391
        typename Observers::iterator jt;
deba@57
   392
        for (jt = it.base(); jt != _observers.end(); ++jt) {
deba@57
   393
          (*jt)->erase(items);
deba@57
   394
        }
deba@57
   395
        throw;
deba@57
   396
      }
alpar@209
   397
    }
deba@57
   398
alpar@209
   399
    /// \brief Notifies all the registed observers about an item erased from
deba@57
   400
    /// the container.
alpar@209
   401
    ///
alpar@209
   402
    /// It notifies all the registed observers about an item erased from
deba@57
   403
    /// the container.
alpar@209
   404
    ///
deba@57
   405
    void erase(const Item& item) throw() {
deba@57
   406
      typename Observers::iterator it = _observers.begin();
deba@57
   407
      while (it != _observers.end()) {
deba@57
   408
        try {
deba@57
   409
          (*it)->erase(item);
deba@57
   410
          ++it;
deba@57
   411
        } catch (const ImmediateDetach&) {
deba@57
   412
          it = _observers.erase(it);
deba@57
   413
          (*it)->_index = _observers.end();
deba@57
   414
          (*it)->_notifier = 0;
deba@57
   415
        }
deba@57
   416
      }
deba@57
   417
    }
deba@57
   418
alpar@209
   419
    /// \brief Notifies all the registed observers about more item erased
deba@57
   420
    /// from the container.
alpar@209
   421
    ///
alpar@209
   422
    /// It notifies all the registed observers about more item erased from
deba@57
   423
    /// the container.
alpar@209
   424
    ///
deba@57
   425
    void erase(const std::vector<Item>& items) {
deba@57
   426
      typename Observers::iterator it = _observers.begin();
deba@57
   427
      while (it != _observers.end()) {
deba@57
   428
        try {
deba@57
   429
          (*it)->erase(items);
deba@57
   430
          ++it;
deba@57
   431
        } catch (const ImmediateDetach&) {
deba@57
   432
          it = _observers.erase(it);
deba@57
   433
          (*it)->_index = _observers.end();
deba@57
   434
          (*it)->_notifier = 0;
deba@57
   435
        }
deba@57
   436
      }
deba@57
   437
    }
deba@57
   438
alpar@209
   439
    /// \brief Notifies all the registed observers about the container is
deba@57
   440
    /// built.
alpar@209
   441
    ///
deba@57
   442
    /// Notifies all the registed observers about the container is built
deba@57
   443
    /// from an empty container.
deba@57
   444
    void build() {
deba@57
   445
      typename Observers::reverse_iterator it;
deba@57
   446
      try {
deba@57
   447
        for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
deba@57
   448
          (*it)->build();
deba@57
   449
        }
deba@57
   450
      } catch (...) {
deba@57
   451
        typename Observers::iterator jt;
deba@57
   452
        for (jt = it.base(); jt != _observers.end(); ++jt) {
deba@57
   453
          (*jt)->clear();
deba@57
   454
        }
deba@57
   455
        throw;
deba@57
   456
      }
deba@57
   457
    }
deba@57
   458
alpar@209
   459
    /// \brief Notifies all the registed observers about all items are
deba@57
   460
    /// erased.
deba@57
   461
    ///
deba@57
   462
    /// Notifies all the registed observers about all items are erased
deba@57
   463
    /// from the container.
deba@57
   464
    void clear() {
deba@57
   465
      typename Observers::iterator it = _observers.begin();
deba@57
   466
      while (it != _observers.end()) {
deba@57
   467
        try {
deba@57
   468
          (*it)->clear();
deba@57
   469
          ++it;
deba@57
   470
        } catch (const ImmediateDetach&) {
deba@57
   471
          it = _observers.erase(it);
deba@57
   472
          (*it)->_index = _observers.end();
deba@57
   473
          (*it)->_notifier = 0;
deba@57
   474
        }
deba@57
   475
      }
deba@57
   476
    }
deba@57
   477
  };
deba@57
   478
deba@57
   479
}
deba@57
   480
deba@57
   481
#endif