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