# HG changeset patch # User alpar # Date 1100022532 0 # Node ID 6a6f3ac07b20e47c90d3d6c7d1b7f0e2e915b4f8 # Parent c0fdb1ad8e8d903c4ccfee1034aec3d61420e656 - Add makeSnapshot()/rollBack() functionality - Remove an unnecessary #include diff -r c0fdb1ad8e8d -r 6a6f3ac07b20 src/lemon/smart_graph.h --- a/src/lemon/smart_graph.h Tue Nov 09 09:12:35 2004 +0000 +++ b/src/lemon/smart_graph.h Tue Nov 09 17:48:52 2004 +0000 @@ -25,7 +25,6 @@ #include -#include #include #include @@ -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 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(const SmartGraph& graph) {