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