COIN-OR::LEMON - Graph Library

Changeset 1011:5bd6c7671c9e in lemon-0.x for src/lemon/list_graph.h


Ignore:
Timestamp:
11/20/04 11:19:06 (16 years ago)
Author:
Alpar Juttner
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1401
Message:
  • snapshot-rollback functionarity added to ListGraph?
  • The iterface of the snapshot-rollback functionarity in SmartGraph? has changed to be compatible with ListGraph::SnapShot?.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/lemon/list_graph.h

    r1010 r1011  
    3232#include <lemon/default_map.h>
    3333
     34#include <list>
    3435
    3536namespace lemon {
     
    387388      erase(b);
    388389    }
     390
     391
     392    ///Class to make a snapshot of the graph and to restrore to it later.
     393
     394    ///Class to make a snapshot of the graph and to restrore to it later.
     395    ///
     396    ///The newly added nodes and edges can be removed using the
     397    ///restore() function.
     398    ///
     399    ///\warning Edge and node deletions cannot be restored.
     400    ///\warning SnapShots cannot be nested.
     401    ///\ingroup graphs
     402    class SnapShot : public AlterationObserverRegistry<Node>::ObserverBase,
     403                     public AlterationObserverRegistry<Edge>::ObserverBase
     404    {
     405      protected:
     406     
     407      ListGraph *g;
     408      std::list<Node> added_nodes;
     409      std::list<Edge> added_edges;
     410     
     411      bool active;
     412      virtual void add(const Node& n) {
     413        added_nodes.push_back(n);
     414      };
     415      ///\bug Exception...
     416      ///
     417      virtual void erase(const Node&)
     418      {
     419        exit(1);
     420      }
     421      virtual void add(const Edge& n) {
     422        added_edges.push_back(n);
     423      };
     424      ///\bug Exception...
     425      ///
     426      virtual void erase(const Edge&)
     427      {
     428        exit(1);
     429      }
     430
     431      void regist(ListGraph &_g) {
     432        g=&_g;
     433        AlterationObserverRegistry<Node>::ObserverBase::
     434          attach(g->node_observers);
     435        AlterationObserverRegistry<Edge>::ObserverBase::
     436          attach(g->edge_observers);
     437      }
     438           
     439      void deregist() {
     440        AlterationObserverRegistry<Node>::ObserverBase::
     441          detach();
     442        AlterationObserverRegistry<Edge>::ObserverBase::
     443          detach();
     444        g=0;
     445      }
     446           
     447    public:
     448      ///Default constructur.
     449     
     450      ///Default constructur.
     451      ///To actually make a snapshot you must call save().
     452      ///
     453      SnapShot() : g(0) {}
     454      ///Constructor that immediately makes a snapshot.
     455     
     456      ///This constructor immediately makes a snapshot of the graph.
     457      ///\param _g The graph we make a snapshot of.
     458      SnapShot(ListGraph &_g) {
     459        regist(_g);
     460      }
     461      ///\bug Is it necessary?
     462      ///
     463      ~SnapShot()
     464      {
     465        if(g) deregist();
     466      }
     467     
     468      ///Make a snapshot.
     469
     470      ///Make a snapshot of the graph.
     471      ///
     472      ///This function can be called more than once. In case of a repeated
     473      ///call, the previous snapshot gets lost.
     474      ///\param _g The graph we make the snapshot of.
     475      void save(ListGraph &_g)
     476      {
     477        if(g!=&_g) {
     478          if(g) deregist();
     479          regist(_g);
     480        }
     481        added_nodes.clear();
     482        added_edges.clear();
     483      }
     484     
     485    ///Undo the changes until the last snapshot.
     486
     487    ///Undo the changes until last snapshot created by save().
     488    ///
     489    ///\todo This function might be called undo().
     490      void restore() {
     491        deregist();
     492        while(!added_edges.empty()) {
     493          g->erase(added_edges.front());
     494          added_edges.pop_front();
     495        }
     496        while(!added_nodes.empty()) {
     497          g->erase(added_nodes.front());
     498          added_nodes.pop_front();
     499        }
     500      }
     501    };
     502   
    389503  };
    390504 
Note: See TracChangeset for help on using the changeset viewer.