# HG changeset patch
# User Hoyt Koepke <hoytak@stat.washington.edu>
# Date 1316920262 25200
# Node ID f1452d03a7cef42acdc234e4f3b98a4288fa54fb
# Parent  98961d3556a70c3a9967e35774e4af9357aa7a1a
Added an alternate version of build() for StaticDigraphs that allows the number of arcs to be given explicitly, avoiding a possibly expensive call to std::distance().

diff -r 98961d3556a7 -r f1452d03a7ce lemon/static_graph.h
--- a/lemon/static_graph.h	Thu Aug 04 22:03:49 2011 +0200
+++ b/lemon/static_graph.h	Sat Sep 24 20:11:02 2011 -0700
@@ -199,11 +199,11 @@
     }
 
     template <typename ArcListIterator>
-    void build(int n, ArcListIterator first, ArcListIterator last) {
+    void build(int n_nodes, int n_arcs, ArcListIterator first, ArcListIterator last) {
       built = true;
 
-      node_num = n;
-      arc_num = std::distance(first, last);
+      node_num = n_nodes;
+      arc_num = n_arcs;
 
       node_first_out = new int[node_num + 1];
       node_first_in = new int[node_num];
@@ -239,6 +239,11 @@
       node_first_out[node_num] = arc_num;
     }
 
+    template <typename ArcListIterator>
+    void build(int n_nodes, ArcListIterator first, ArcListIterator last) {
+      build(n_nodes, std::distance(first, last), first, last);
+    }
+
   protected:
 
     void fastFirstOut(Arc& e, const Node& n) const {
@@ -373,12 +378,14 @@
 
     /// \brief Build the digraph from an arc list.
     ///
-    /// This function builds the digraph from the given arc list.
-    /// It can be called more than once, but in such case, the whole
-    /// structure and all maps will be cleared and rebuilt.
+    /// This function builds the digraph from the given arc list.  It
+    /// can be called more than once, but in such case, the whole
+    /// structure and all maps will be cleared and rebuilt.  The
+    /// number of arcs is determined by calling
+    /// <tt>std::distance(begin, end)</tt>.
     ///
     /// The list of the arcs must be given in the range <tt>[begin, end)</tt>
-    /// specified by STL compatible itartors whose \c value_type must be
+    /// specified by STL compatible iterators whose \c value_type must be
     /// <tt>std::pair<int,int></tt>.
     /// Each arc must be specified by a pair of integer indices
     /// from the range <tt>[0..n-1]</tt>. <i>The pairs must be in a
@@ -409,6 +416,48 @@
       notifier(Arc()).build();
     }
 
+    /// \brief Build the digraph from an arc list.  
+    ///
+    /// This version requires the number of arcs to be specified
+    /// explicitly, but avoids a possibly expensive call to
+    /// <tt>std::distance()</tt>.  
+    ///
+    /// This function builds the digraph from the given arc list.  It
+    /// can be called more than once, but in such case, the whole
+    /// structure and all maps will be cleared and rebuilt.
+    ///
+    /// The list of the arcs must be given in the range <tt>[begin, end)</tt>
+    /// specified by STL compatible itartors whose \c value_type must be
+    /// <tt>std::pair<int,int></tt>.
+    /// Each arc must be specified by a pair of integer indices
+    /// from the range <tt>[0..n-1]</tt>. <i>The pairs must be in a
+    /// non-decreasing order with respect to their first values.</i>
+    /// If the k-th pair in the list is <tt>(i,j)</tt>, then
+    /// <tt>arc(k-1)</tt> will connect <tt>node(i)</tt> to <tt>node(j)</tt>.
+    ///
+    /// \param n The number of nodes.
+    /// \param begin An iterator pointing to the beginning of the arc list.
+    /// \param end An iterator pointing to the end of the arc list.
+    /// 
+    /// For example, a simple digraph can be constructed like this.
+    /// \code
+    ///   std::vector<std::pair<int,int> > arcs;
+    ///   arcs.push_back(std::make_pair(0,1));
+    ///   arcs.push_back(std::make_pair(0,2));
+    ///   arcs.push_back(std::make_pair(1,3));
+    ///   arcs.push_back(std::make_pair(1,2));
+    ///   arcs.push_back(std::make_pair(3,0));
+    ///   StaticDigraph gr;
+    ///   gr.build(4, arcs.begin(), arcs.end());
+    /// \endcode
+    template <typename ArcListIterator>
+    void build(int n_nodes, int n_arcs, ArcListIterator begin, ArcListIterator end) {
+      if (built) Parent::clear();
+      StaticDigraphBase::build(n_nodes, n_arcs, begin, end);
+      notifier(Node()).build();
+      notifier(Arc()).build();
+    }
+
     /// \brief Clear the digraph.
     ///
     /// This function erases all nodes and arcs from the digraph.
