[Lemon-commits] [lemon_svn] alpar: r1401 - hugo/trunk/src/lemon

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:45:07 CET 2006


Author: alpar
Date: Sat Nov 20 11:19:06 2004
New Revision: 1401

Modified:
   hugo/trunk/src/lemon/list_graph.h
   hugo/trunk/src/lemon/smart_graph.h

Log:
- snapshot-rollback functionarity added to ListGraph
- The iterface of the snapshot-rollback functionarity in SmartGraph has
  changed to be compatible with ListGraph::SnapShot.

Modified: hugo/trunk/src/lemon/list_graph.h
==============================================================================
--- hugo/trunk/src/lemon/list_graph.h	(original)
+++ hugo/trunk/src/lemon/list_graph.h	Sat Nov 20 11:19:06 2004
@@ -31,6 +31,7 @@
 
 #include <lemon/default_map.h>
 
+#include <list>
 
 namespace lemon {
 
@@ -386,6 +387,119 @@
       }
       erase(b);
     }
+
+
+    ///Class to make a snapshot of the graph and to restrore to it later.
+
+    ///Class to make a snapshot of the graph and to restrore to it later.
+    ///
+    ///The newly added nodes and edges can be removed using the
+    ///restore() function.
+    ///
+    ///\warning Edge and node deletions cannot be restored.
+    ///\warning SnapShots cannot be nested.
+    ///\ingroup graphs
+    class SnapShot : public AlterationObserverRegistry<Node>::ObserverBase,
+		     public AlterationObserverRegistry<Edge>::ObserverBase
+    {
+      protected:
+      
+      ListGraph *g;
+      std::list<Node> added_nodes;
+      std::list<Edge> added_edges;
+      
+      bool active;
+      virtual void add(const Node& n) {
+	added_nodes.push_back(n);
+      };
+      ///\bug Exception...
+      ///
+      virtual void erase(const Node&) 
+      {
+	exit(1);
+      }
+      virtual void add(const Edge& n) {
+	added_edges.push_back(n);
+      };
+      ///\bug Exception...
+      ///
+      virtual void erase(const Edge&) 
+      {
+	exit(1);
+      }
+
+      void regist(ListGraph &_g) {
+	g=&_g;
+	AlterationObserverRegistry<Node>::ObserverBase::
+	  attach(g->node_observers);
+	AlterationObserverRegistry<Edge>::ObserverBase::
+	  attach(g->edge_observers);
+      }
+            
+      void deregist() {
+	AlterationObserverRegistry<Node>::ObserverBase::
+	  detach();
+	AlterationObserverRegistry<Edge>::ObserverBase::
+	  detach();
+	g=0;
+      }
+            
+    public:
+      ///Default constructur.
+      
+      ///Default constructur.
+      ///To actually make a snapshot you must call save().
+      ///
+      SnapShot() : g(0) {}
+      ///Constructor that immediately makes a snapshot.
+      
+      ///This constructor immediately makes a snapshot of the graph.
+      ///\param _g The graph we make a snapshot of.
+      SnapShot(ListGraph &_g) {
+	regist(_g);
+      }
+      ///\bug Is it necessary?
+      ///
+      ~SnapShot() 
+      {
+	if(g) deregist();
+      }
+      
+      ///Make a snapshot.
+
+      ///Make a snapshot of the graph.
+      ///
+      ///This function can be called more than once. In case of a repeated
+      ///call, the previous snapshot gets lost.
+      ///\param _g The graph we make the snapshot of.
+      void save(ListGraph &_g) 
+      {
+	if(g!=&_g) {
+	  if(g) deregist();
+	  regist(_g);
+	}
+	added_nodes.clear();
+	added_edges.clear();
+      }
+      
+    ///Undo the changes until the last snapshot.
+
+    ///Undo the changes until last snapshot created by save().
+    ///
+    ///\todo This function might be called undo().
+      void restore() {
+	deregist();
+	while(!added_edges.empty()) {
+	  g->erase(added_edges.front());
+	  added_edges.pop_front();
+	}
+ 	while(!added_nodes.empty()) {
+	  g->erase(added_nodes.front());
+	  added_nodes.pop_front();
+	}
+      }
+    };
+    
   };
   
   /// @}  

Modified: hugo/trunk/src/lemon/smart_graph.h
==============================================================================
--- hugo/trunk/src/lemon/smart_graph.h	(original)
+++ hugo/trunk/src/lemon/smart_graph.h	Sat Nov 20 11:19:06 2004
@@ -260,50 +260,11 @@
       return _findEdge(u,v,prev);
     }
     
-    ///Internal data structure to store snapshots
-    
-    ///\ingroup graphs
-    ///\sa makeSnapShot()
-    ///\sa rollBack()
-    struct SnapShot 
-    {
-      unsigned int node_num;
-      unsigned int edge_num;
-    };
-    
-    ///Make a snapshot of the graph.
+    class SnapShot;
+    friend class SnapShot;
 
-    ///Make a snapshot of the graph.
-    ///
-    ///The newly added nodes and edges can be removed using the
-    ///rollBack() function.
-    ///
-    ///\return An stucture SnapShot describing the pesent state of the
-    ///graph.
-    ///\note After you rolled back to a state, you cannot roll "back" to
-    ///a later state, in other word you cannot add again the edges deleted
-    ///by rollBack().
-    ///\todo This function might be called saveState() or getState().
-    SnapShot makeSnapShot() 
-    {
-      SnapShot s;
-      s.node_num=nodes.size();
-      s.edge_num=edges.size();
-      return s;
-    }
-    
-    ///Undo the changes until a snapshot.
-
-    ///Undo the changes until a snapshot created by makeSnapShot().
-    ///
-    ///\param s an internal stucture given back by makeSnapShot()
-    ///\note After you rolled back to a state, you cannot "roll forward" to
-    ///a later state, in other word you cannot add again the edges deleted
-    ///by rollBack().
-    ///
-    ///\todo This function might be called undo().
-    
-    void rollBack(const SnapShot &s)
+  protected:
+    void restoreSnapShot(const SnapShot &s)
     {
       while(s.edge_num>edges.size()) {
 	edge_observers.erase(Edge(edges.size()-1));
@@ -316,7 +277,73 @@
 	node_observers.erase(Node(nodes.size()-1));
 	nodes.pop_back();
       }
-    }
+    }    
+
+  public:
+    ///Class to make a snapshot of the graph and to restrore to it later.
+
+    ///Class to make a snapshot of the graph and to restrore to it later.
+    ///
+    ///The newly added nodes and edges can be removed using the
+    ///restore() function.
+    ///\note After you restore a state, you cannot restore
+    ///a later state, in other word you cannot add again the edges deleted
+    ///by restore() using another SnapShot instance.
+    ///
+    ///\ingroup graphs
+    class SnapShot 
+    {
+      SmartGraph *g;
+    protected:
+      friend class SmartGraph;
+      unsigned int node_num;
+      unsigned int edge_num;
+    public:
+      ///Default constructur.
+      
+      ///Default constructur.
+      ///To actually make a snapshot you must call save().
+      ///
+      SnapShot() : g(0) {}
+      ///Constructor that immediately makes a snapshot
+      
+      ///This constructor immediately makes a snapshot of the graph.
+      ///\param _g The graph we make a snapshot of.
+      SnapShot(SmartGraph &_g) :g(&_g) {
+	node_num=g->nodes.size();
+	edge_num=g->edges.size();
+      }
+
+      ///Make a snapshot.
+
+      ///Make a snapshot of the graph.
+      ///
+      ///This function can be called more than once. In case of a repeated
+      ///call, the previous snapshot gets lost.
+      ///\param _g The graph we make the snapshot of.
+      void save(SmartGraph &_g) 
+      {
+	g=&_g;
+	node_num=g->nodes.size();
+	edge_num=g->edges.size();
+      }
+
+      ///Undo the changes until a snapshot.
+      
+      ///Undo the changes until a snapshot created by save().
+      ///
+      ///\param s an internal stucture given back by save()
+      ///\note After you restored a state, you cannot restore
+      ///a later state, in other word you cannot add again the edges deleted
+      ///by restore().
+      ///
+      ///\todo This function might be called undo().
+      
+      void restore()
+      {
+	g->restoreSnapShot(*this);
+      }
+    };
   };
   
   /// @}  



More information about the Lemon-commits mailing list