# HG changeset patch
# User gyorokp
# Date 1270898683 -7200
# Node ID ece0de9a63f86df029e092cdcf0b8949606992bc
# Parent  873b8d0104e73ab66cacb90ff7e6e2de6122eb81
CommonFacesIt, CommonNodesIt fixes (#363)

diff -r 873b8d0104e7 -r ece0de9a63f8 lemon/planar_graph.h
--- a/lemon/planar_graph.h	Sat Apr 10 11:23:05 2010 +0200
+++ b/lemon/planar_graph.h	Sat Apr 10 13:24:43 2010 +0200
@@ -1284,8 +1284,8 @@
   class CommonFacesIt : public Face {
       const Graph* _graph;
       const Node _n1, _n2;
-      Arc _i2;
-      std::set<Face> _f1;
+      std::set<Face> _f1, _f2;
+      typename std::set<Face>::const_iterator _fi2;
     public:
 
       CommonFacesIt() {}
@@ -1293,27 +1293,31 @@
       CommonFacesIt(Invalid i) : Face(i) { }
 
       explicit CommonFacesIt(const Graph& graph, Node n1, Node n2) : _graph(
-        &graph), _n1(n1), _n2(n2), _i2(), _f1(){
+        &graph), _n1(n1), _n2(n2), _f1(), _f2(){
         Arc _i1;
         _graph->firstOut(_i1,_n1);
         while (_i1 != INVALID) {
           _f1.insert(_graph->leftFace(_i1));
           _graph->nextOut(_i1);
         }
+        Arc _i2;
         _graph->firstOut(_i2,_n2);
-        _graph->firstOut(_i2,_n2);
-        while (_i2 != INVALID && _f1.count(_graph->leftFace(_i2)) == 0)
+        while (_i2 != INVALID) {
+          _f2.insert(_graph->leftFace(_i2));
           _graph->nextOut(_i2);
-        static_cast<Face&>(*this) = (_i2 != INVALID)?_graph->leftFace(_i2):
-          INVALID;
+        }
+
+        _fi2 = _f2.begin();
+        while (_fi2 != _f2.end() && _f1.count(*_fi2) == 0)
+          ++_fi2;
+        static_cast<Face&>(*this) = (_fi2 != _f2.end())?*_fi2:INVALID;
       }
 
       CommonFacesIt& operator++() {
-        _graph->nextOut(_i2);
-        while (_i2 != INVALID && _f1.count(_graph->leftFace(_i2)) == 0)
-          _graph->nextOut(_i2);
-        static_cast<Face&>(*this) = (_i2 != INVALID)?_graph->leftFace(_i2):
-          INVALID;
+        ++_fi2;
+        while (_fi2 != _f2.end() && _f1.count(*_fi2) == 0)
+          ++_fi2;
+        static_cast<Face&>(*this) = (_fi2 != _f2.end())?*_fi2:INVALID;
         return *this;
       }
 
@@ -1325,8 +1329,8 @@
   class CommonNodesIt : public Node {
       const Graph* _graph;
       const Face _f1, _f2;
-      Arc _i2;
-      std::set<Node> _ns;
+      std::set<Node> _ns1,_ns2;
+      typename std::set<Node>::const_iterator _ni2;
     public:
 
       CommonNodesIt() {}
@@ -1334,27 +1338,31 @@
       CommonNodesIt(Invalid i) : Face(i) { }
 
       explicit CommonNodesIt(const Graph& graph, Face f1, Face f2) : _graph(
-        &graph), _f1(f1), _f2(f2), _i2(), _ns(){
+        &graph), _f1(f1), _f2(f2), _ns1(), _ns2(){
         Arc _i1;
         _graph->firstCwF(_i1,_f1);
         while (_i1 != INVALID) {
-          _ns.insert(_graph->source(_i1));
+          _ns1.insert(_graph->source(_i1));
           _graph->nextCwF(_i1);
         }
+        Arc _i2;
+        _graph->firstCwF(_i2,_f2);
+        while (_i2 != INVALID) {
+          _ns2.insert(_graph->source(_i2));
+          _graph->nextCwF(_i2);
+        }
 
-        _graph->firstCwF(_i2,_f2);
-        while (_i2 != INVALID && _ns.count(_graph->source(_i2)) == 0)
-          _graph->nextCwF(_i2);
-        static_cast<Node&>(*this) = (_i2 != INVALID)?_graph->source(_i2):
-          INVALID;
+        _ni2 = _ns2.begin();
+        while (_ni2 != _ns2.end() && _ns1.count(*_ni2) == 0)
+          ++_ni2;
+        static_cast<Node&>(*this) = (_ni2 != _ns2.end())?*_ni2:INVALID;
       }
 
       CommonNodesIt& operator++() {
-        _graph->nextCwF(_i2);
-        while (_i2 != INVALID && _ns.count(_graph->source(_i2)) == 0)
-          _graph->nextCwF(_i2);
-        static_cast<Node&>(*this) = (_i2 != INVALID)?_graph->source(_i2):
-          INVALID;
+        ++_ni2;
+        while (_ni2 != _ns2.end() && _ns1.count(*_ni2) == 0)
+          ++_ni2;
+        static_cast<Node&>(*this) = (_ni2 != _ns2.end())?*_ni2:INVALID;
         return *this;
       }
 
diff -r 873b8d0104e7 -r ece0de9a63f8 test/planar_graph_test.cc
--- a/test/planar_graph_test.cc	Sat Apr 10 11:23:05 2010 +0200
+++ b/test/planar_graph_test.cc	Sat Apr 10 13:24:43 2010 +0200
@@ -228,6 +228,15 @@
     checkGraphBoundaryArcList(G, G.leftFace(G.direct(a2,false)), 3);
     checkGraphBoundaryArcList(G, G.leftFace(G.direct(a3,true)), 4);
 
+    checkGraphCommonFaceList(G, n0, n2, 2);
+    checkGraphCommonFaceList(G, n0, n3, 2);
+    checkGraphCommonFaceList(G, n2, n3, 1);
+
+    checkGraphCommonNodeList(G, G.leftFace(G.direct(a3,true)), G.leftFace(G.
+            direct(a3,false)), 2);
+    checkGraphCommonNodeList(G, G.leftFace(G.direct(a1,false)), G.leftFace(G.
+            direct(a2,true)), 1);
+
 }
 
 template <class Graph>
diff -r 873b8d0104e7 -r ece0de9a63f8 test/planar_graph_test.h
--- a/test/planar_graph_test.h	Sat Apr 10 11:23:05 2010 +0200
+++ b/test/planar_graph_test.h	Sat Apr 10 13:24:43 2010 +0200
@@ -141,17 +141,12 @@
   void checkGraphBoundaryArcList(const Graph &G, typename Graph::Face n, int
     cnt)
   {
-//    std::cout << G.id(n) << ' ';
-//    int alma = countBoundaryArcs(G,n);
-//    std::cout << alma << ':';
     typename Graph::CwBoundaryArcIt e(G,n);
     for(int i=0;i<cnt;i++) {
-//      std::cout << ',' << G.id(e);
       check(e!=INVALID,"Wrong CwBoundaryArc list linking.");
       check(n==G.w1(e) || n==G.w2(e),"Wrong CwBoundaryArc list linking.");
       ++e;
     }
-//    std::cout << std::endl;
     check(e==INVALID,"Wrong BoundaryArc list linking.");
     check(countBoundaryArcs(G,n)==cnt,"Wrong IncEdge number.");
   }
@@ -186,6 +181,30 @@
     check(2 * cnt == i, "Wrong iterator.");
   }
 
+  template<class Graph>
+  void checkGraphCommonNodeList(const Graph &G, typename Graph::Face f1,
+      typename Graph::Face f2, int cnt)
+  {
+    typename Graph::CommonNodesIt e(G,f1,f2);
+    for(int i=0;i<cnt;i++) {
+      check(e!=INVALID,"Wrong CommonNodeIt.");
+      ++e;
+    }
+    check(e==INVALID,"Wrong CommonNodeIt.");
+  }
+
+  template<class Graph>
+  void checkGraphCommonFaceList(const Graph &G, typename Graph::Node n1,
+      typename Graph::Node n2, int cnt)
+  {
+    typename Graph::CommonFacesIt e(G,n1,n2);
+    for(int i=0;i<cnt;i++) {
+      check(e!=INVALID,"Wrong CommonNodeIt.");
+      ++e;
+    }
+    check(e==INVALID,"Wrong CommonNodeIt.");
+  }
+
   template <typename Graph>
   void checkArcDirections(const Graph& G) {
     for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
