- Add makeSnapshot()/rollBack() functionality
authoralpar
Tue, 09 Nov 2004 17:48:52 +0000
changeset 9736a6f3ac07b20
parent 972 c0fdb1ad8e8d
child 974 785062a83f8e
- Add makeSnapshot()/rollBack() functionality
- Remove an unnecessary #include
src/lemon/smart_graph.h
     1.1 --- a/src/lemon/smart_graph.h	Tue Nov 09 09:12:35 2004 +0000
     1.2 +++ b/src/lemon/smart_graph.h	Tue Nov 09 17:48:52 2004 +0000
     1.3 @@ -25,7 +25,6 @@
     1.4  
     1.5  #include <lemon/invalid.h>
     1.6  
     1.7 -#include <lemon/erasable_graph_extender.h>
     1.8  #include <lemon/clearable_graph_extender.h>
     1.9  #include <lemon/extendable_graph_extender.h>
    1.10  
    1.11 @@ -45,12 +44,16 @@
    1.12    /// \addtogroup graphs
    1.13    /// @{
    1.14  
    1.15 +  class SmartGraph;
    1.16    ///Base of SmartGraph
    1.17  
    1.18    ///Base of SmartGraph
    1.19    ///
    1.20    class SmartGraphBase {
    1.21  
    1.22 +    friend class SmatGraph;
    1.23 +
    1.24 +  protected:
    1.25      struct NodeT 
    1.26      {
    1.27        int first_in,first_out;      
    1.28 @@ -143,9 +146,12 @@
    1.29  
    1.30      class Node {
    1.31        friend class SmartGraphBase;
    1.32 +      friend class SmartGraph;
    1.33  
    1.34      protected:
    1.35        int n;
    1.36 +      ///\todo It should be removed (or at least define a setToId() instead).
    1.37 +      ///
    1.38        Node(int nn) {n=nn;}
    1.39      public:
    1.40        Node() {}
    1.41 @@ -158,9 +164,12 @@
    1.42  
    1.43      class Edge {
    1.44        friend class SmartGraphBase;
    1.45 +      friend class SmartGraph;
    1.46  
    1.47      protected:
    1.48        int n;
    1.49 +      ///\todo It should be removed (or at least define a setToId() instead).
    1.50 +      ///
    1.51        Edge(int nn) {n=nn;}
    1.52      public:
    1.53        Edge() { }
    1.54 @@ -209,6 +218,7 @@
    1.55        prev.n=e;
    1.56        return prev;
    1.57      }
    1.58 +
    1.59    };
    1.60  
    1.61    typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase;
    1.62 @@ -240,7 +250,7 @@
    1.63    class SmartGraph :public ClearableSmartGraphBase {
    1.64    public:
    1.65      /// Finds an edge between two nodes.
    1.66 -
    1.67 +    
    1.68      /// Finds an edge from node \c u to node \c v.
    1.69      ///
    1.70      /// If \c prev is \ref INVALID (this is the default value), then
    1.71 @@ -259,7 +269,64 @@
    1.72      {
    1.73        return _findEdge(u,v,prev);
    1.74      }
    1.75 -};
    1.76 +    
    1.77 +    ///Internal data structure to store snapshots
    1.78 +    
    1.79 +    ///\ingroup graphs
    1.80 +    ///\sa makeSnapShot()
    1.81 +    ///\sa rollBack()
    1.82 +    struct SnapShot 
    1.83 +    {
    1.84 +      unsigned int node_num;
    1.85 +      unsigned int edge_num;
    1.86 +    };
    1.87 +    
    1.88 +    ///Make a snapshot of the graph.
    1.89 +
    1.90 +    ///Make a snapshot of the graph.
    1.91 +    ///
    1.92 +    ///The newly added nodes and edges can be removed using the
    1.93 +    ///rollBack() function.
    1.94 +    ///
    1.95 +    ///\return An stucture SnapShot describing the pesent state of the
    1.96 +    ///graph.
    1.97 +    ///\note After you rolled back to a state, you cannot roll "back" to
    1.98 +    ///a later state, in other word you cannot add again the edges deleted
    1.99 +    ///by rollBack().
   1.100 +    SnapShot makeSnapShot() 
   1.101 +    {
   1.102 +      SnapShot s;
   1.103 +      s.node_num=nodes.size();
   1.104 +      s.edge_num=edges.size();
   1.105 +      return s;
   1.106 +    }
   1.107 +    
   1.108 +    ///Undo the changes until a snapshot.
   1.109 +
   1.110 +    ///Undo the changes until a snapshot created by makeSnapShot().
   1.111 +    ///
   1.112 +    ///\param s an internal stucture given back by makeSnapShot()
   1.113 +    ///\note After you rolled back to a state, you cannot "roll forward" to
   1.114 +    ///a later state, in other word you cannot add again the edges deleted
   1.115 +    ///by rollBack().
   1.116 +    ///
   1.117 +    ///\todo This function might be called undo().
   1.118 +    
   1.119 +    void rollBack(const SnapShot &s)
   1.120 +    {
   1.121 +      while(s.edge_num>edges.size()) {
   1.122 +	edge_observers.erase(Edge(edges.size()-1));
   1.123 +	nodes[edges.back().head].first_in=edges.back().next_in;
   1.124 +	nodes[edges.back().tail].first_out=edges.back().next_out;
   1.125 +	edges.pop_back();
   1.126 +      }
   1.127 +      //nodes.resize(s.nodes_num);
   1.128 +      while(s.node_num>nodes.size()) {
   1.129 +	node_observers.erase(Node(nodes.size()-1));
   1.130 +	nodes.pop_back();
   1.131 +      }
   1.132 +    }
   1.133 +  };
   1.134    
   1.135    template <>
   1.136    int countNodes<SmartGraph>(const SmartGraph& graph) {