# HG changeset patch # User alpar # Date 1100945946 0 # Node ID 5bd6c7671c9edc8c850a76b4a80e052fc93bf4b9 # Parent 072bddac076e4de531ecec3bfa602ab4fac5d1d7 - snapshot-rollback functionarity added to ListGraph - The iterface of the snapshot-rollback functionarity in SmartGraph has changed to be compatible with ListGraph::SnapShot. diff -r 072bddac076e -r 5bd6c7671c9e src/lemon/list_graph.h --- a/src/lemon/list_graph.h Fri Nov 19 18:17:25 2004 +0000 +++ b/src/lemon/list_graph.h Sat Nov 20 10:19:06 2004 +0000 @@ -31,6 +31,7 @@ #include +#include 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::ObserverBase, + public AlterationObserverRegistry::ObserverBase + { + protected: + + ListGraph *g; + std::list added_nodes; + std::list 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::ObserverBase:: + attach(g->node_observers); + AlterationObserverRegistry::ObserverBase:: + attach(g->edge_observers); + } + + void deregist() { + AlterationObserverRegistry::ObserverBase:: + detach(); + AlterationObserverRegistry::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(); + } + } + }; + }; /// @} diff -r 072bddac076e -r 5bd6c7671c9e src/lemon/smart_graph.h --- a/src/lemon/smart_graph.h Fri Nov 19 18:17:25 2004 +0000 +++ b/src/lemon/smart_graph.h Sat Nov 20 10:19:06 2004 +0000 @@ -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); + } + }; }; /// @}