# HG changeset patch
# User gyorokp
# Date 1269765634 -7200
# Node ID 7ac85249925613d89547bccc26e977084be69e59
# Parent  4e27249fd00f82efa5a0d0c2380427e346fc3e4a
Fixed a few bugs in erase(Edge) (#363)

diff -r 4e27249fd00f -r 7ac852499256 lemon/planar_graph.h
--- a/lemon/planar_graph.h	Sat Mar 27 21:34:23 2010 +0100
+++ b/lemon/planar_graph.h	Sun Mar 28 10:40:34 2010 +0200
@@ -623,18 +623,20 @@
         Arc arc(faces[fr].first_arc);
         wall_paint(arc,fl,arc);
         erase(Face(fr));
-      } else if ((arcs[n].next_out > -1 || arcs[n].prev_out > -1) &&
+      } else {
+        comp_split = true;
+        if ((arcs[n].next_out > -1 || arcs[n].prev_out > -1) &&
                  (arcs[n | 1].next_out > -1 || arcs[n | 1].prev_out > -1)) {
-        comp_split = true;
-        Arc arc(n);
-        Arc ed = arc;
-        ed.id ^= 1;
-        turnRightF(arc);
-        Face f = addFace();
-        wall_paint(arc,f.id,ed);
-        faces[f.id].first_arc = arc.id;
-        turnRightF(ed);
-        faces[fr].first_arc = ed.id;
+          Arc arc(n);
+          Arc ed = arc;
+          ed.id ^= 1;
+          turnRightF(arc);
+          Face f = addFace();
+          wall_paint(arc,f.id,ed);
+          faces[f.id].first_arc = arc.id;
+          turnRightF(ed);
+          faces[fr].first_arc = ed.id;
+        }
       }
 
       if (arcs[n].next_out != -1) {
@@ -665,9 +667,13 @@
 
       if (comp_split) component_relabel(Node(arcs[n | 1].target),
         addComponent());
-      if (nodes[arcs[n].target].first_out == -1)
+      if (nodes[arcs[n].target].first_out == -1 && nodes[arcs[n | 1].target].
+              first_out == -1) {
         nodes[arcs[n].target].outer_face = addFace().id;
-      if (nodes[arcs[n | 1].target].first_out == -1)
+        nodes[arcs[n | 1].target].outer_face = fr;
+      } else if (nodes[arcs[n].target].first_out == -1)
+        nodes[arcs[n].target].outer_face = addFace().id;
+      else if (nodes[arcs[n | 1].target].first_out == -1)
         nodes[arcs[n | 1].target].outer_face = addFace().id;
 
     }
@@ -1083,6 +1089,60 @@
 
   };
 
+  // Face counting:
+
+  namespace _core_bits {
+
+    template <typename Graph, typename Enable = void>
+    struct CountFacesSelector {
+      static int count(const Graph &g) {
+        return countItems<Graph, typename Graph::Face>(g);
+      }
+    };
+
+    template <typename Graph>
+    struct CountFacesSelector<
+      Graph, typename
+      enable_if<typename Graph::FaceNumTag, void>::type>
+    {
+      static int count(const Graph &g) {
+        return g.faceNum();
+      }
+    };
+  }
+
+  /// \brief Function to count the faces in the graph.
+  ///
+  /// This function counts the faces in the graph.
+  /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
+  /// graph structures it is specialized to run in <em>O</em>(1).
+  ///
+  /// \note If the graph contains a \c faceNum() member function and a
+  /// \c FaceNumTag tag then this function calls directly the member
+  /// function to query the cardinality of the face set.
+  template <typename Graph>
+  inline int countFaces(const Graph& g) {
+    return _core_bits::CountFacesSelector<Graph>::count(g);
+  }
+
+  template <typename Graph, typename DegIt>
+  inline int countFaceDegree(const Graph& _g, const typename Graph::Face& _n) {
+    int num = 0;
+    for (DegIt it(_g, _n); it != INVALID; ++it) {
+      ++num;
+    }
+    return num;
+  }
+  /// \brief Function to count the number of the boundary arcs of face \c f.
+  ///
+  /// This function counts the number of the boundary arcs of face \c f
+  /// in the undirected graph \c g.
+  template <typename Graph>
+  inline int countBoundaryArcs(const Graph& g,  const typename Graph::Face& f) {
+    return countFaceDegree<Graph, typename Graph::CwBoundaryArcIt>(g, f);
+  }
+
+
   template <typename GR, typename Enable = void>
   struct FaceNotifierIndicator {
     typedef InvalidType Type;
@@ -1474,17 +1534,6 @@
 
     typedef Parent::OutArcIt IncEdgeIt;
 
-    /// \brief Add a new node to the graph.
-    ///
-    /// This function adds a new node to the graph. A new outer face is created
-    /// for the node.
-    /// \return The new node.
-    Node addNode() {
-      Node n = Parent::addNode();
-      notifier(Face()).add(outerFace(n));
-      return n;
-    }
-
   protected:
     void edge_add_notify(Edge edge) {
       notifier(Edge()).add(edge);
@@ -1503,6 +1552,18 @@
     }
 
   public:
+    /// \brief Add a new node to the graph.
+    ///
+    /// This function adds a new node to the graph. A new outer face is created
+    /// for the node.
+    /// \return The new node.
+    Node addNode() {
+      Node n = PlanarGraphBase::addNode();
+      notifier(Node()).add(n);
+      notifier(Face()).add(outerFace(n));
+      return n;
+    }
+
     /// \brief Add a new edge to the graph.
     ///
     /// This function adds a new edge to the graph between nodes
