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

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


Author: alpar
Date: Tue Nov  9 18:48:52 2004
New Revision: 1361

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

Log:
- Add makeSnapshot()/rollBack() functionality
- Remove an unnecessary #include

Modified: hugo/trunk/src/lemon/smart_graph.h
==============================================================================
--- hugo/trunk/src/lemon/smart_graph.h	(original)
+++ hugo/trunk/src/lemon/smart_graph.h	Tue Nov  9 18:48:52 2004
@@ -25,7 +25,6 @@
 
 #include <lemon/invalid.h>
 
-#include <lemon/erasable_graph_extender.h>
 #include <lemon/clearable_graph_extender.h>
 #include <lemon/extendable_graph_extender.h>
 
@@ -45,12 +44,16 @@
   /// \addtogroup graphs
   /// @{
 
+  class SmartGraph;
   ///Base of SmartGraph
 
   ///Base of SmartGraph
   ///
   class SmartGraphBase {
 
+    friend class SmatGraph;
+
+  protected:
     struct NodeT 
     {
       int first_in,first_out;      
@@ -143,9 +146,12 @@
 
     class Node {
       friend class SmartGraphBase;
+      friend class SmartGraph;
 
     protected:
       int n;
+      ///\todo It should be removed (or at least define a setToId() instead).
+      ///
       Node(int nn) {n=nn;}
     public:
       Node() {}
@@ -158,9 +164,12 @@
 
     class Edge {
       friend class SmartGraphBase;
+      friend class SmartGraph;
 
     protected:
       int n;
+      ///\todo It should be removed (or at least define a setToId() instead).
+      ///
       Edge(int nn) {n=nn;}
     public:
       Edge() { }
@@ -209,6 +218,7 @@
       prev.n=e;
       return prev;
     }
+
   };
 
   typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase;
@@ -240,7 +250,7 @@
   class SmartGraph :public ClearableSmartGraphBase {
   public:
     /// Finds an edge between two nodes.
-
+    
     /// Finds an edge from node \c u to node \c v.
     ///
     /// If \c prev is \ref INVALID (this is the default value), then
@@ -259,7 +269,64 @@
     {
       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.
+
+    ///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().
+    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)
+    {
+      while(s.edge_num>edges.size()) {
+	edge_observers.erase(Edge(edges.size()-1));
+	nodes[edges.back().head].first_in=edges.back().next_in;
+	nodes[edges.back().tail].first_out=edges.back().next_out;
+	edges.pop_back();
+      }
+      //nodes.resize(s.nodes_num);
+      while(s.node_num>nodes.size()) {
+	node_observers.erase(Node(nodes.size()-1));
+	nodes.pop_back();
+      }
+    }
+  };
   
   template <>
   int countNodes<SmartGraph>(const SmartGraph& graph) {



More information about the Lemon-commits mailing list