1.1 --- a/src/lemon/smart_graph.h Fri Nov 19 18:17:25 2004 +0000
1.2 +++ b/src/lemon/smart_graph.h Sat Nov 20 10:19:06 2004 +0000
1.3 @@ -260,50 +260,11 @@
1.4 return _findEdge(u,v,prev);
1.5 }
1.6
1.7 - ///Internal data structure to store snapshots
1.8 -
1.9 - ///\ingroup graphs
1.10 - ///\sa makeSnapShot()
1.11 - ///\sa rollBack()
1.12 - struct SnapShot
1.13 - {
1.14 - unsigned int node_num;
1.15 - unsigned int edge_num;
1.16 - };
1.17 -
1.18 - ///Make a snapshot of the graph.
1.19 + class SnapShot;
1.20 + friend class SnapShot;
1.21
1.22 - ///Make a snapshot of the graph.
1.23 - ///
1.24 - ///The newly added nodes and edges can be removed using the
1.25 - ///rollBack() function.
1.26 - ///
1.27 - ///\return An stucture SnapShot describing the pesent state of the
1.28 - ///graph.
1.29 - ///\note After you rolled back to a state, you cannot roll "back" to
1.30 - ///a later state, in other word you cannot add again the edges deleted
1.31 - ///by rollBack().
1.32 - ///\todo This function might be called saveState() or getState().
1.33 - SnapShot makeSnapShot()
1.34 - {
1.35 - SnapShot s;
1.36 - s.node_num=nodes.size();
1.37 - s.edge_num=edges.size();
1.38 - return s;
1.39 - }
1.40 -
1.41 - ///Undo the changes until a snapshot.
1.42 -
1.43 - ///Undo the changes until a snapshot created by makeSnapShot().
1.44 - ///
1.45 - ///\param s an internal stucture given back by makeSnapShot()
1.46 - ///\note After you rolled back to a state, you cannot "roll forward" to
1.47 - ///a later state, in other word you cannot add again the edges deleted
1.48 - ///by rollBack().
1.49 - ///
1.50 - ///\todo This function might be called undo().
1.51 -
1.52 - void rollBack(const SnapShot &s)
1.53 + protected:
1.54 + void restoreSnapShot(const SnapShot &s)
1.55 {
1.56 while(s.edge_num>edges.size()) {
1.57 edge_observers.erase(Edge(edges.size()-1));
1.58 @@ -316,7 +277,73 @@
1.59 node_observers.erase(Node(nodes.size()-1));
1.60 nodes.pop_back();
1.61 }
1.62 - }
1.63 + }
1.64 +
1.65 + public:
1.66 + ///Class to make a snapshot of the graph and to restrore to it later.
1.67 +
1.68 + ///Class to make a snapshot of the graph and to restrore to it later.
1.69 + ///
1.70 + ///The newly added nodes and edges can be removed using the
1.71 + ///restore() function.
1.72 + ///\note After you restore a state, you cannot restore
1.73 + ///a later state, in other word you cannot add again the edges deleted
1.74 + ///by restore() using another SnapShot instance.
1.75 + ///
1.76 + ///\ingroup graphs
1.77 + class SnapShot
1.78 + {
1.79 + SmartGraph *g;
1.80 + protected:
1.81 + friend class SmartGraph;
1.82 + unsigned int node_num;
1.83 + unsigned int edge_num;
1.84 + public:
1.85 + ///Default constructur.
1.86 +
1.87 + ///Default constructur.
1.88 + ///To actually make a snapshot you must call save().
1.89 + ///
1.90 + SnapShot() : g(0) {}
1.91 + ///Constructor that immediately makes a snapshot
1.92 +
1.93 + ///This constructor immediately makes a snapshot of the graph.
1.94 + ///\param _g The graph we make a snapshot of.
1.95 + SnapShot(SmartGraph &_g) :g(&_g) {
1.96 + node_num=g->nodes.size();
1.97 + edge_num=g->edges.size();
1.98 + }
1.99 +
1.100 + ///Make a snapshot.
1.101 +
1.102 + ///Make a snapshot of the graph.
1.103 + ///
1.104 + ///This function can be called more than once. In case of a repeated
1.105 + ///call, the previous snapshot gets lost.
1.106 + ///\param _g The graph we make the snapshot of.
1.107 + void save(SmartGraph &_g)
1.108 + {
1.109 + g=&_g;
1.110 + node_num=g->nodes.size();
1.111 + edge_num=g->edges.size();
1.112 + }
1.113 +
1.114 + ///Undo the changes until a snapshot.
1.115 +
1.116 + ///Undo the changes until a snapshot created by save().
1.117 + ///
1.118 + ///\param s an internal stucture given back by save()
1.119 + ///\note After you restored a state, you cannot restore
1.120 + ///a later state, in other word you cannot add again the edges deleted
1.121 + ///by restore().
1.122 + ///
1.123 + ///\todo This function might be called undo().
1.124 +
1.125 + void restore()
1.126 + {
1.127 + g->restoreSnapShot(*this);
1.128 + }
1.129 + };
1.130 };
1.131
1.132 /// @}