# HG changeset patch # User deba # Date 1157368153 0 # Node ID dd887831e9c1c953a60087eef1174351ee117a04 # Parent de2b77e3868c713f2478cbf942323061d75cb995 Snapshot for SmartUGraph an SmartBpUGraph diff -r de2b77e3868c -r dd887831e9c1 lemon/smart_graph.h --- a/lemon/smart_graph.h Mon Sep 04 11:08:32 2006 +0000 +++ b/lemon/smart_graph.h Mon Sep 04 11:09:13 2006 +0000 @@ -43,20 +43,17 @@ ///Base of SmartGraph /// class SmartGraphBase { + protected: - friend class SmatGraph; - - protected: struct NodeT { - int first_in,first_out; - NodeT() : first_in(-1), first_out(-1) {} + int first_in, first_out; + NodeT() {} }; struct EdgeT { int target, source, next_in, next_out; - //FIXME: is this necessary? - EdgeT() : next_in(-1), next_out(-1) {} + EdgeT() {} }; std::vector nodes; @@ -81,71 +78,44 @@ typedef True NodeNumTag; typedef True EdgeNumTag; - ///Number of nodes. int nodeNum() const { return nodes.size(); } - ///Number of edges. int edgeNum() const { return edges.size(); } - /// Maximum node ID. - - /// Maximum node ID. - ///\sa id(Node) int maxNodeId() const { return nodes.size()-1; } - /// Maximum edge ID. - - /// Maximum edge ID. - ///\sa id(Edge) int maxEdgeId() const { return edges.size()-1; } Node addNode() { - Node n; n.n=nodes.size(); - nodes.push_back(NodeT()); //FIXME: Hmmm... - return n; + int n = nodes.size(); + nodes.push_back(NodeT()); + nodes[n].first_in = -1; + nodes[n].first_out = -1; + return Node(n); } Edge addEdge(Node u, Node v) { - Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm... - edges[e.n].source=u.n; edges[e.n].target=v.n; - edges[e.n].next_out=nodes[u.n].first_out; - edges[e.n].next_in=nodes[v.n].first_in; - nodes[u.n].first_out=nodes[v.n].first_in=e.n; + int n = edges.size(); + edges.push_back(EdgeT()); + edges[n].source = u.id; + edges[n].target = v.id; + edges[n].next_out = nodes[u.id].first_out; + edges[n].next_in = nodes[v.id].first_in; + nodes[u.id].first_out = nodes[v.id].first_in = n; - return e; + return Edge(n); } + void clear() { + edges.clear(); + nodes.clear(); + } - Node source(Edge e) const { return edges[e.n].source; } - Node target(Edge e) const { return edges[e.n].target; } + Node source(Edge e) const { return Node(edges[e.id].source); } + Node target(Edge e) const { return Node(edges[e.id].target); } - /// Node ID. - - /// The ID of a valid Node is a nonnegative integer not greater than - /// \ref maxNodeId(). The range of the ID's is not surely continuous - /// and the greatest node ID can be actually less then \ref maxNodeId(). - /// - /// The ID of the \ref INVALID node is -1. - ///\return The ID of the node \c v. - static int id(Node v) { return v.n; } - /// Edge ID. - - /// The ID of a valid Edge is a nonnegative integer not greater than - /// \ref maxEdgeId(). The range of the ID's is not surely continuous - /// and the greatest edge ID can be actually less then \ref maxEdgeId(). - /// - /// The ID of the \ref INVALID edge is -1. - ///\return The ID of the edge \c e. - static int id(Edge e) { return e.n; } + static int id(Node v) { return v.id; } + static int id(Edge e) { return e.id; } - /// \brief Returns the node from its \c id. - /// - /// Returns the node from its \c id. If there is not node - /// with the given id the effect of the function is undefinied. static Node nodeFromId(int id) { return Node(id);} - - /// \brief Returns the edge from its \c id. - /// - /// Returns the edge from its \c id. If there is not edge - /// with the given id the effect of the function is undefinied. static Edge edgeFromId(int id) { return Edge(id);} class Node { @@ -153,14 +123,14 @@ friend class SmartGraph; protected: - int n; - Node(int nn) {n=nn;} + int id; + explicit Node(int _id) : id(_id) {} public: Node() {} - Node (Invalid) { n=-1; } - bool operator==(const Node i) const {return n==i.n;} - bool operator!=(const Node i) const {return n!=i.n;} - bool operator<(const Node i) const {return nrestoreSnapshot(*this); @@ -407,23 +384,146 @@ /// class SmartUGraph : public ExtendedSmartUGraphBase { private: + ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead. ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead. /// SmartUGraph(const SmartUGraph &) : ExtendedSmartUGraphBase() {}; + ///\brief Assignment of SmartUGraph to another one is \e not allowed. ///Use UGraphCopy() instead. ///Assignment of SmartUGraph to another one is \e not allowed. ///Use UGraphCopy() instead. void operator=(const SmartUGraph &) {} + public: + + typedef ExtendedSmartUGraphBase Parent; + /// Constructor /// Constructor. /// SmartUGraph() {} + + ///Add a new node to the graph. + + /// \return the new node. + /// + Node addNode() { return Parent::addNode(); } + + ///Add a new undirected edge to the graph. + + ///Add a new undirected edge to the graph with node \c s + ///and \c t. + ///\return the new undirected edge. + UEdge addEdge(const Node& s, const Node& t) { + return Parent::addEdge(s, t); + } + + ///Clear the graph. + + ///Erase all the nodes and edges from the graph. + /// + void clear() { + Parent::clear(); + } + + public: + + class Snapshot; + + protected: + + + void restoreSnapshot(const Snapshot &s) + { + while(s.edge_num dir; + dir.push_back(Parent::direct(edge, true)); + dir.push_back(Parent::direct(edge, false)); + Parent::getNotifier(Edge()).erase(dir); + nodes[edges.back().source].first_out=edges.back().next_out; + nodes[edges.back().target].first_in=edges.back().next_in; + edges.pop_back(); + } + while(s.node_numnodes.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(SmartUGraph &_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(). + /// + ///\note After you restored a state, you cannot restore + ///a later state, in other word you cannot add again the edges deleted + ///by restore(). + void restore() + { + g->restoreSnapshot(*this); + } + }; }; @@ -462,10 +562,10 @@ protected: int id; - Node(int _id) : id(_id) {} + explicit Node(int _id) : id(_id) {} public: Node() {} - Node(Invalid) { id = -1; } + Node(Invalid) : id(-1) {} bool operator==(const Node i) const {return id==i.id;} bool operator!=(const Node i) const {return id!=i.id;} bool operator<(const Node i) const {return id dir; + dir.push_back(Parent::direct(edge, true)); + dir.push_back(Parent::direct(edge, false)); + Parent::getNotifier(Edge()).erase(dir); + aNodes[edges.back().aNode >> 1].first=edges.back().next_out; + bNodes[edges.back().bNode >> 1].first=edges.back().next_in; + edges.pop_back(); + } + while(s.anode_numaNodes.size(); + bnode_num=g->bNodes.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(SmartBpUGraph &_g) + { + g=&_g; + anode_num=g->aNodes.size(); + bnode_num=g->bNodes.size(); + edge_num=g->edges.size(); + } + + ///Undo the changes until a snapshot. + + ///Undo the changes until a snapshot created 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(). + void restore() + { + g->restoreSnapshot(*this); + } + }; + }; /// @}