Bug fix in ListBpUGraph
authordeba
Mon, 04 Sep 2006 11:08:32 +0000
changeset 2189de2b77e3868c
parent 2188 984870a2dde4
child 2190 dd887831e9c1
Bug fix in ListBpUGraph

Snapshot improvments
lemon/list_graph.h
     1.1 --- a/lemon/list_graph.h	Mon Sep 04 11:05:21 2006 +0000
     1.2 +++ b/lemon/list_graph.h	Mon Sep 04 11:08:32 2006 +0000
     1.3 @@ -502,18 +502,9 @@
     1.4      /// The newly added nodes and edges can be removed using the
     1.5      /// restore() function.
     1.6      ///
     1.7 -    /// \warning Edge and node deletions cannot be restored.
     1.8 +    /// \warning Edge and node deletions cannot be restored. This
     1.9 +    /// events invalidate the snapshot. 
    1.10      class Snapshot {
    1.11 -    public:
    1.12 -      
    1.13 -      class UnsupportedOperation : public LogicError {
    1.14 -      public:
    1.15 -	virtual const char* what() const throw() {
    1.16 -	  return "lemon::ListGraph::Snapshot::UnsupportedOperation";
    1.17 -	}
    1.18 -      };
    1.19 -            
    1.20 -
    1.21      protected:
    1.22  
    1.23        typedef Parent::NodeNotifier NodeNotifier;
    1.24 @@ -543,7 +534,7 @@
    1.25          }
    1.26          virtual void erase(const std::vector<Node>& nodes) {
    1.27            for (int i = 0; i < (int)nodes.size(); ++i) {
    1.28 -            if (!snapshot.eraseNode(nodes[i])) break;
    1.29 +            snapshot.eraseNode(nodes[i]);
    1.30            }
    1.31          }
    1.32          virtual void build() {
    1.33 @@ -561,7 +552,7 @@
    1.34            NodeNotifier* notifier = getNotifier();
    1.35            Node node;
    1.36            for (notifier->first(node); node != INVALID; notifier->next(node)) {
    1.37 -            if (!snapshot.eraseNode(node)) break;
    1.38 +            snapshot.eraseNode(node);
    1.39            }
    1.40          }
    1.41  
    1.42 @@ -593,7 +584,7 @@
    1.43          }
    1.44          virtual void erase(const std::vector<Edge>& edges) {
    1.45            for (int i = 0; i < (int)edges.size(); ++i) {
    1.46 -            if (!snapshot.eraseEdge(edges[i])) break;
    1.47 +            snapshot.eraseEdge(edges[i]);
    1.48            }
    1.49          }
    1.50          virtual void build() {
    1.51 @@ -611,7 +602,7 @@
    1.52            EdgeNotifier* notifier = getNotifier();
    1.53            Edge edge;
    1.54            for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
    1.55 -            if (!snapshot.eraseEdge(edge)) break;
    1.56 +            snapshot.eraseEdge(edge);
    1.57            }
    1.58          }
    1.59  
    1.60 @@ -630,30 +621,30 @@
    1.61        void addNode(const Node& node) {
    1.62          added_nodes.push_front(node);        
    1.63        }
    1.64 -      bool eraseNode(const Node& node) {
    1.65 +      void eraseNode(const Node& node) {
    1.66          std::list<Node>::iterator it = 
    1.67            std::find(added_nodes.begin(), added_nodes.end(), node);
    1.68          if (it == added_nodes.end()) {
    1.69            clear();
    1.70 -          return false;
    1.71 +          edge_observer_proxy.detach();
    1.72 +          throw NodeNotifier::ImmediateDetach();
    1.73          } else {
    1.74            added_nodes.erase(it);
    1.75 -          return true;
    1.76          }
    1.77        }
    1.78  
    1.79        void addEdge(const Edge& edge) {
    1.80          added_edges.push_front(edge);        
    1.81        }
    1.82 -      bool eraseEdge(const Edge& edge) {
    1.83 +      void eraseEdge(const Edge& edge) {
    1.84          std::list<Edge>::iterator it = 
    1.85            std::find(added_edges.begin(), added_edges.end(), edge);
    1.86          if (it == added_edges.end()) {
    1.87            clear();
    1.88 -          return false;
    1.89 +          node_observer_proxy.detach(); 
    1.90 +          throw EdgeNotifier::ImmediateDetach();
    1.91          } else {
    1.92            added_edges.erase(it);
    1.93 -          return true;
    1.94          }        
    1.95        }
    1.96  
    1.97 @@ -668,8 +659,11 @@
    1.98  	edge_observer_proxy.detach();
    1.99        }
   1.100  
   1.101 +      bool attached() const {
   1.102 +        return node_observer_proxy.attached();
   1.103 +      }
   1.104 +
   1.105        void clear() {
   1.106 -        detach();
   1.107          added_nodes.clear();
   1.108          added_edges.clear();        
   1.109        }
   1.110 @@ -702,7 +696,10 @@
   1.111        /// call, the previous snapshot gets lost.
   1.112        /// \param _graph The graph we make the snapshot of.
   1.113        void save(ListGraph &_graph) {
   1.114 -        clear();
   1.115 +        if (attached()) {
   1.116 +          detach();
   1.117 +          clear();
   1.118 +        }
   1.119          attach(_graph);
   1.120        }
   1.121        
   1.122 @@ -711,21 +708,22 @@
   1.123        /// Undo the changes until the last snapshot created by save().
   1.124        void restore() {
   1.125  	detach();
   1.126 -	while(!added_edges.empty()) {
   1.127 -	  graph->erase(added_edges.front());
   1.128 -	  added_edges.pop_front();
   1.129 +	for(std::list<Edge>::iterator it = added_edges.begin(); 
   1.130 +            it != added_edges.end(); ++it) {
   1.131 +	  graph->erase(*it);
   1.132  	}
   1.133 - 	while(!added_nodes.empty()) {
   1.134 -	  graph->erase(added_nodes.front());
   1.135 -	  added_nodes.pop_front();
   1.136 +	for(std::list<Node>::iterator it = added_nodes.begin(); 
   1.137 +            it != added_nodes.end(); ++it) {
   1.138 +	  graph->erase(*it);
   1.139  	}
   1.140 +        clear();
   1.141        }
   1.142  
   1.143        /// \brief Gives back true when the snapshot is valid.
   1.144        ///
   1.145        /// Gives back true when the snapshot is valid.
   1.146        bool valid() const {
   1.147 -        return node_observer_proxy.attached();
   1.148 +        return attached();
   1.149        }
   1.150      };
   1.151      
   1.152 @@ -859,6 +857,241 @@
   1.153        }
   1.154        erase(b);
   1.155      }
   1.156 +
   1.157 +
   1.158 +    /// \brief Class to make a snapshot of the graph and restore
   1.159 +    /// to it later.
   1.160 +    ///
   1.161 +    /// Class to make a snapshot of the graph and to restore it
   1.162 +    /// later.
   1.163 +    ///
   1.164 +    /// The newly added nodes and undirected edges can be removed
   1.165 +    /// using the restore() function.
   1.166 +    ///
   1.167 +    /// \warning Edge and node deletions cannot be restored. This
   1.168 +    /// events invalidate the snapshot. 
   1.169 +    class Snapshot {
   1.170 +    protected:
   1.171 +
   1.172 +      typedef Parent::NodeNotifier NodeNotifier;
   1.173 +
   1.174 +      class NodeObserverProxy : public NodeNotifier::ObserverBase {
   1.175 +      public:
   1.176 +
   1.177 +        NodeObserverProxy(Snapshot& _snapshot)
   1.178 +          : snapshot(_snapshot) {}
   1.179 +
   1.180 +        using NodeNotifier::ObserverBase::attach;
   1.181 +        using NodeNotifier::ObserverBase::detach;
   1.182 +        using NodeNotifier::ObserverBase::attached;
   1.183 +        
   1.184 +      protected:
   1.185 +        
   1.186 +        virtual void add(const Node& node) {
   1.187 +          snapshot.addNode(node);
   1.188 +        }
   1.189 +        virtual void add(const std::vector<Node>& nodes) {
   1.190 +          for (int i = nodes.size() - 1; i >= 0; ++i) {
   1.191 +            snapshot.addNode(nodes[i]);
   1.192 +          }
   1.193 +        }
   1.194 +        virtual void erase(const Node& node) {
   1.195 +          snapshot.eraseNode(node);
   1.196 +        }
   1.197 +        virtual void erase(const std::vector<Node>& nodes) {
   1.198 +          for (int i = 0; i < (int)nodes.size(); ++i) {
   1.199 +            snapshot.eraseNode(nodes[i]);
   1.200 +          }
   1.201 +        }
   1.202 +        virtual void build() {
   1.203 +          NodeNotifier* notifier = getNotifier();
   1.204 +          Node node;
   1.205 +          std::vector<Node> nodes;
   1.206 +          for (notifier->first(node); node != INVALID; notifier->next(node)) {
   1.207 +            nodes.push_back(node);
   1.208 +          }
   1.209 +          for (int i = nodes.size() - 1; i >= 0; --i) {
   1.210 +            snapshot.addNode(nodes[i]);
   1.211 +          }
   1.212 +        }
   1.213 +        virtual void clear() {
   1.214 +          NodeNotifier* notifier = getNotifier();
   1.215 +          Node node;
   1.216 +          for (notifier->first(node); node != INVALID; notifier->next(node)) {
   1.217 +            snapshot.eraseNode(node);
   1.218 +          }
   1.219 +        }
   1.220 +
   1.221 +        Snapshot& snapshot;
   1.222 +      };
   1.223 +
   1.224 +      class UEdgeObserverProxy : public UEdgeNotifier::ObserverBase {
   1.225 +      public:
   1.226 +
   1.227 +        UEdgeObserverProxy(Snapshot& _snapshot)
   1.228 +          : snapshot(_snapshot) {}
   1.229 +
   1.230 +        using UEdgeNotifier::ObserverBase::attach;
   1.231 +        using UEdgeNotifier::ObserverBase::detach;
   1.232 +        using UEdgeNotifier::ObserverBase::attached;
   1.233 +        
   1.234 +      protected:
   1.235 +
   1.236 +        virtual void add(const UEdge& edge) {
   1.237 +          snapshot.addUEdge(edge);
   1.238 +        }
   1.239 +        virtual void add(const std::vector<UEdge>& edges) {
   1.240 +          for (int i = edges.size() - 1; i >= 0; ++i) {
   1.241 +            snapshot.addUEdge(edges[i]);
   1.242 +          }
   1.243 +        }
   1.244 +        virtual void erase(const UEdge& edge) {
   1.245 +          snapshot.eraseUEdge(edge);
   1.246 +        }
   1.247 +        virtual void erase(const std::vector<UEdge>& edges) {
   1.248 +          for (int i = 0; i < (int)edges.size(); ++i) {
   1.249 +            snapshot.eraseUEdge(edges[i]);
   1.250 +          }
   1.251 +        }
   1.252 +        virtual void build() {
   1.253 +          UEdgeNotifier* notifier = getNotifier();
   1.254 +          UEdge edge;
   1.255 +          std::vector<UEdge> edges;
   1.256 +          for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
   1.257 +            edges.push_back(edge);
   1.258 +          }
   1.259 +          for (int i = edges.size() - 1; i >= 0; --i) {
   1.260 +            snapshot.addUEdge(edges[i]);
   1.261 +          }
   1.262 +        }
   1.263 +        virtual void clear() {
   1.264 +          UEdgeNotifier* notifier = getNotifier();
   1.265 +          UEdge edge;
   1.266 +          for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
   1.267 +            snapshot.eraseUEdge(edge);
   1.268 +          }
   1.269 +        }
   1.270 +
   1.271 +        Snapshot& snapshot;
   1.272 +      };
   1.273 +      
   1.274 +      ListUGraph *graph;
   1.275 +
   1.276 +      NodeObserverProxy node_observer_proxy;
   1.277 +      UEdgeObserverProxy edge_observer_proxy;
   1.278 +
   1.279 +      std::list<Node> added_nodes;
   1.280 +      std::list<UEdge> added_edges;
   1.281 +
   1.282 +
   1.283 +      void addNode(const Node& node) {
   1.284 +        added_nodes.push_front(node);        
   1.285 +      }
   1.286 +      void eraseNode(const Node& node) {
   1.287 +        std::list<Node>::iterator it = 
   1.288 +          std::find(added_nodes.begin(), added_nodes.end(), node);
   1.289 +        if (it == added_nodes.end()) {
   1.290 +          clear();
   1.291 +          edge_observer_proxy.detach();
   1.292 +          throw NodeNotifier::ImmediateDetach();
   1.293 +        } else {
   1.294 +          added_nodes.erase(it);
   1.295 +        }
   1.296 +      }
   1.297 +
   1.298 +      void addUEdge(const UEdge& edge) {
   1.299 +        added_edges.push_front(edge);        
   1.300 +      }
   1.301 +      void eraseUEdge(const UEdge& edge) {
   1.302 +        std::list<UEdge>::iterator it = 
   1.303 +          std::find(added_edges.begin(), added_edges.end(), edge);
   1.304 +        if (it == added_edges.end()) {
   1.305 +          clear();
   1.306 +          node_observer_proxy.detach();
   1.307 +          throw UEdgeNotifier::ImmediateDetach();
   1.308 +        } else {
   1.309 +          added_edges.erase(it);
   1.310 +        }        
   1.311 +      }
   1.312 +
   1.313 +      void attach(ListUGraph &_graph) {
   1.314 +	graph = &_graph;
   1.315 +	node_observer_proxy.attach(graph->getNotifier(Node()));
   1.316 +        edge_observer_proxy.attach(graph->getNotifier(UEdge()));
   1.317 +      }
   1.318 +            
   1.319 +      void detach() {
   1.320 +	node_observer_proxy.detach();
   1.321 +	edge_observer_proxy.detach();
   1.322 +      }
   1.323 +
   1.324 +      bool attached() const {
   1.325 +        return node_observer_proxy.attached();
   1.326 +      }
   1.327 +
   1.328 +      void clear() {
   1.329 +        added_nodes.clear();
   1.330 +        added_edges.clear();        
   1.331 +      }
   1.332 +
   1.333 +    public:
   1.334 +
   1.335 +      /// \brief Default constructor.
   1.336 +      ///
   1.337 +      /// Default constructor.
   1.338 +      /// To actually make a snapshot you must call save().
   1.339 +      Snapshot() 
   1.340 +        : graph(0), node_observer_proxy(*this), 
   1.341 +          edge_observer_proxy(*this) {}
   1.342 +      
   1.343 +      /// \brief Constructor that immediately makes a snapshot.
   1.344 +      ///      
   1.345 +      /// This constructor immediately makes a snapshot of the graph.
   1.346 +      /// \param _graph The graph we make a snapshot of.
   1.347 +      Snapshot(ListUGraph &_graph) 
   1.348 +        : node_observer_proxy(*this), 
   1.349 +          edge_observer_proxy(*this) {
   1.350 +	attach(_graph);
   1.351 +      }
   1.352 +      
   1.353 +      /// \brief Make a snapshot.
   1.354 +      ///
   1.355 +      /// Make a snapshot of the graph.
   1.356 +      ///
   1.357 +      /// This function can be called more than once. In case of a repeated
   1.358 +      /// call, the previous snapshot gets lost.
   1.359 +      /// \param _graph The graph we make the snapshot of.
   1.360 +      void save(ListUGraph &_graph) {
   1.361 +        if (attached()) {
   1.362 +          detach();
   1.363 +          clear();
   1.364 +        }
   1.365 +        attach(_graph);
   1.366 +      }
   1.367 +      
   1.368 +      /// \brief Undo the changes until the last snapshot.
   1.369 +      // 
   1.370 +      /// Undo the changes until the last snapshot created by save().
   1.371 +      void restore() {
   1.372 +	detach();
   1.373 +	for(std::list<UEdge>::iterator it = added_edges.begin(); 
   1.374 +            it != added_edges.end(); ++it) {
   1.375 +	  graph->erase(*it);
   1.376 +	}
   1.377 +	for(std::list<Node>::iterator it = added_nodes.begin(); 
   1.378 +            it != added_nodes.end(); ++it) {
   1.379 +	  graph->erase(*it);
   1.380 +	}
   1.381 +        clear();
   1.382 +      }
   1.383 +
   1.384 +      /// \brief Gives back true when the snapshot is valid.
   1.385 +      ///
   1.386 +      /// Gives back true when the snapshot is valid.
   1.387 +      bool valid() const {
   1.388 +        return attached();
   1.389 +      }
   1.390 +    };
   1.391    };
   1.392  
   1.393  
   1.394 @@ -1105,6 +1338,7 @@
   1.395        } else {
   1.396          bNodes[bNodeId].next = -1;
   1.397        }
   1.398 +      bNodes[bNodeId].prev = -1;
   1.399        first_bnode = bNodeId;
   1.400        bNodes[bNodeId].first_edge = -1;
   1.401        return Node((bNodeId << 1) + 1);
   1.402 @@ -1148,7 +1382,8 @@
   1.403          if (aNodes[aNodeId].prev != -1) {
   1.404            aNodes[aNodes[aNodeId].prev >> 1].next = aNodes[aNodeId].next;
   1.405          } else {
   1.406 -          first_anode = aNodes[aNodeId].next >> 1;
   1.407 +          first_anode = 
   1.408 +            aNodes[aNodeId].next != -1 ? aNodes[aNodeId].next >> 1 : -1;
   1.409          }
   1.410          if (aNodes[aNodeId].next != -1) {
   1.411            aNodes[aNodes[aNodeId].next >> 1].prev = aNodes[aNodeId].prev;
   1.412 @@ -1160,7 +1395,8 @@
   1.413          if (bNodes[bNodeId].prev != -1) {
   1.414            bNodes[bNodes[bNodeId].prev >> 1].next = bNodes[bNodeId].next;
   1.415          } else {
   1.416 -          first_bnode = bNodes[bNodeId].next >> 1;
   1.417 +          first_bnode = 
   1.418 +            bNodes[bNodeId].next != -1 ? bNodes[bNodeId].next >> 1 : -1;
   1.419          }
   1.420          if (bNodes[bNodeId].next != -1) {
   1.421            bNodes[bNodes[bNodeId].next >> 1].prev = bNodes[bNodeId].prev;
   1.422 @@ -1396,6 +1632,239 @@
   1.423        erase(b);
   1.424      }
   1.425  
   1.426 +    /// \brief Class to make a snapshot of the graph and restore
   1.427 +    /// to it later.
   1.428 +    ///
   1.429 +    /// Class to make a snapshot of the graph and to restore it
   1.430 +    /// later.
   1.431 +    ///
   1.432 +    /// The newly added nodes and undirected edges can be removed
   1.433 +    /// using the restore() function.
   1.434 +    ///
   1.435 +    /// \warning Edge and node deletions cannot be restored. This
   1.436 +    /// events invalidate the snapshot. 
   1.437 +    class Snapshot {
   1.438 +    protected:
   1.439 +
   1.440 +      typedef Parent::NodeNotifier NodeNotifier;
   1.441 +
   1.442 +      class NodeObserverProxy : public NodeNotifier::ObserverBase {
   1.443 +      public:
   1.444 +
   1.445 +        NodeObserverProxy(Snapshot& _snapshot)
   1.446 +          : snapshot(_snapshot) {}
   1.447 +
   1.448 +        using NodeNotifier::ObserverBase::attach;
   1.449 +        using NodeNotifier::ObserverBase::detach;
   1.450 +        using NodeNotifier::ObserverBase::attached;
   1.451 +        
   1.452 +      protected:
   1.453 +        
   1.454 +        virtual void add(const Node& node) {
   1.455 +          snapshot.addNode(node);
   1.456 +        }
   1.457 +        virtual void add(const std::vector<Node>& nodes) {
   1.458 +          for (int i = nodes.size() - 1; i >= 0; ++i) {
   1.459 +            snapshot.addNode(nodes[i]);
   1.460 +          }
   1.461 +        }
   1.462 +        virtual void erase(const Node& node) {
   1.463 +          snapshot.eraseNode(node);
   1.464 +        }
   1.465 +        virtual void erase(const std::vector<Node>& nodes) {
   1.466 +          for (int i = 0; i < (int)nodes.size(); ++i) {
   1.467 +            snapshot.eraseNode(nodes[i]);
   1.468 +          }
   1.469 +        }
   1.470 +        virtual void build() {
   1.471 +          NodeNotifier* notifier = getNotifier();
   1.472 +          Node node;
   1.473 +          std::vector<Node> nodes;
   1.474 +          for (notifier->first(node); node != INVALID; notifier->next(node)) {
   1.475 +            nodes.push_back(node);
   1.476 +          }
   1.477 +          for (int i = nodes.size() - 1; i >= 0; --i) {
   1.478 +            snapshot.addNode(nodes[i]);
   1.479 +          }
   1.480 +        }
   1.481 +        virtual void clear() {
   1.482 +          NodeNotifier* notifier = getNotifier();
   1.483 +          Node node;
   1.484 +          for (notifier->first(node); node != INVALID; notifier->next(node)) {
   1.485 +            snapshot.eraseNode(node);
   1.486 +          }
   1.487 +        }
   1.488 +
   1.489 +        Snapshot& snapshot;
   1.490 +      };
   1.491 +
   1.492 +      class UEdgeObserverProxy : public UEdgeNotifier::ObserverBase {
   1.493 +      public:
   1.494 +
   1.495 +        UEdgeObserverProxy(Snapshot& _snapshot)
   1.496 +          : snapshot(_snapshot) {}
   1.497 +
   1.498 +        using UEdgeNotifier::ObserverBase::attach;
   1.499 +        using UEdgeNotifier::ObserverBase::detach;
   1.500 +        using UEdgeNotifier::ObserverBase::attached;
   1.501 +        
   1.502 +      protected:
   1.503 +
   1.504 +        virtual void add(const UEdge& edge) {
   1.505 +          snapshot.addUEdge(edge);
   1.506 +        }
   1.507 +        virtual void add(const std::vector<UEdge>& edges) {
   1.508 +          for (int i = edges.size() - 1; i >= 0; ++i) {
   1.509 +            snapshot.addUEdge(edges[i]);
   1.510 +          }
   1.511 +        }
   1.512 +        virtual void erase(const UEdge& edge) {
   1.513 +          snapshot.eraseUEdge(edge);
   1.514 +        }
   1.515 +        virtual void erase(const std::vector<UEdge>& edges) {
   1.516 +          for (int i = 0; i < (int)edges.size(); ++i) {
   1.517 +            snapshot.eraseUEdge(edges[i]);
   1.518 +          }
   1.519 +        }
   1.520 +        virtual void build() {
   1.521 +          UEdgeNotifier* notifier = getNotifier();
   1.522 +          UEdge edge;
   1.523 +          std::vector<UEdge> edges;
   1.524 +          for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
   1.525 +            edges.push_back(edge);
   1.526 +          }
   1.527 +          for (int i = edges.size() - 1; i >= 0; --i) {
   1.528 +            snapshot.addUEdge(edges[i]);
   1.529 +          }
   1.530 +        }
   1.531 +        virtual void clear() {
   1.532 +          UEdgeNotifier* notifier = getNotifier();
   1.533 +          UEdge edge;
   1.534 +          for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
   1.535 +            snapshot.eraseUEdge(edge);
   1.536 +          }
   1.537 +        }
   1.538 +
   1.539 +        Snapshot& snapshot;
   1.540 +      };
   1.541 +      
   1.542 +      ListBpUGraph *graph;
   1.543 +
   1.544 +      NodeObserverProxy node_observer_proxy;
   1.545 +      UEdgeObserverProxy edge_observer_proxy;
   1.546 +
   1.547 +      std::list<Node> added_nodes;
   1.548 +      std::list<UEdge> added_edges;
   1.549 +
   1.550 +
   1.551 +      void addNode(const Node& node) {
   1.552 +        added_nodes.push_front(node);        
   1.553 +      }
   1.554 +      void eraseNode(const Node& node) {
   1.555 +        std::list<Node>::iterator it = 
   1.556 +          std::find(added_nodes.begin(), added_nodes.end(), node);
   1.557 +        if (it == added_nodes.end()) {
   1.558 +          clear();
   1.559 +          edge_observer_proxy.detach();
   1.560 +          throw NodeNotifier::ImmediateDetach();
   1.561 +        } else {
   1.562 +          added_nodes.erase(it);
   1.563 +        }
   1.564 +      }
   1.565 +
   1.566 +      void addUEdge(const UEdge& edge) {
   1.567 +        added_edges.push_front(edge);        
   1.568 +      }
   1.569 +      void eraseUEdge(const UEdge& edge) {
   1.570 +        std::list<UEdge>::iterator it = 
   1.571 +          std::find(added_edges.begin(), added_edges.end(), edge);
   1.572 +        if (it == added_edges.end()) {
   1.573 +          clear();
   1.574 +          node_observer_proxy.detach();
   1.575 +          throw UEdgeNotifier::ImmediateDetach();
   1.576 +        } else {
   1.577 +          added_edges.erase(it);
   1.578 +        }        
   1.579 +      }
   1.580 +
   1.581 +      void attach(ListBpUGraph &_graph) {
   1.582 +	graph = &_graph;
   1.583 +	node_observer_proxy.attach(graph->getNotifier(Node()));
   1.584 +        edge_observer_proxy.attach(graph->getNotifier(UEdge()));
   1.585 +      }
   1.586 +            
   1.587 +      void detach() {
   1.588 +	node_observer_proxy.detach();
   1.589 +	edge_observer_proxy.detach();
   1.590 +      }
   1.591 +
   1.592 +      bool attached() const {
   1.593 +        return node_observer_proxy.attached();
   1.594 +      }
   1.595 +
   1.596 +      void clear() {
   1.597 +        added_nodes.clear();
   1.598 +        added_edges.clear();        
   1.599 +      }
   1.600 +
   1.601 +    public:
   1.602 +
   1.603 +      /// \brief Default constructor.
   1.604 +      ///
   1.605 +      /// Default constructor.
   1.606 +      /// To actually make a snapshot you must call save().
   1.607 +      Snapshot() 
   1.608 +        : graph(0), node_observer_proxy(*this), 
   1.609 +          edge_observer_proxy(*this) {}
   1.610 +      
   1.611 +      /// \brief Constructor that immediately makes a snapshot.
   1.612 +      ///      
   1.613 +      /// This constructor immediately makes a snapshot of the graph.
   1.614 +      /// \param _graph The graph we make a snapshot of.
   1.615 +      Snapshot(ListBpUGraph &_graph) 
   1.616 +        : node_observer_proxy(*this), 
   1.617 +          edge_observer_proxy(*this) {
   1.618 +	attach(_graph);
   1.619 +      }
   1.620 +      
   1.621 +      /// \brief Make a snapshot.
   1.622 +      ///
   1.623 +      /// Make a snapshot of the graph.
   1.624 +      ///
   1.625 +      /// This function can be called more than once. In case of a repeated
   1.626 +      /// call, the previous snapshot gets lost.
   1.627 +      /// \param _graph The graph we make the snapshot of.
   1.628 +      void save(ListBpUGraph &_graph) {
   1.629 +        if (attached()) {
   1.630 +          detach();
   1.631 +          clear();
   1.632 +        }
   1.633 +        attach(_graph);
   1.634 +      }
   1.635 +      
   1.636 +      /// \brief Undo the changes until the last snapshot.
   1.637 +      // 
   1.638 +      /// Undo the changes until the last snapshot created by save().
   1.639 +      void restore() {
   1.640 +	detach();
   1.641 +	for(std::list<UEdge>::iterator it = added_edges.begin(); 
   1.642 +            it != added_edges.end(); ++it) {
   1.643 +	  graph->erase(*it);
   1.644 +	}
   1.645 +	for(std::list<Node>::iterator it = added_nodes.begin(); 
   1.646 +            it != added_nodes.end(); ++it) {
   1.647 +	  graph->erase(*it);
   1.648 +	}
   1.649 +        clear();
   1.650 +      }
   1.651 +
   1.652 +      /// \brief Gives back true when the snapshot is valid.
   1.653 +      ///
   1.654 +      /// Gives back true when the snapshot is valid.
   1.655 +      bool valid() const {
   1.656 +        return attached();
   1.657 +      }
   1.658 +    };
   1.659    };
   1.660  
   1.661