COIN-OR::LEMON - Graph Library

Ticket #427: StaticDigraph.patch

File StaticDigraph.patch, 4.3 KB (added by Hoyt Koepke, 13 years ago)

patch for static_graph

  • lemon/static_graph.h

    # 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 b  
    199199    }
    200200
    201201    template <typename ArcListIterator>
    202     void build(int n, ArcListIterator first, ArcListIterator last) {
     202    void build(int n_nodes, int n_arcs, ArcListIterator first, ArcListIterator last) {
    203203      built = true;
    204204
    205       node_num = n;
    206       arc_num = std::distance(first, last);
     205      node_num = n_nodes;
     206      arc_num = n_arcs;
    207207
    208208      node_first_out = new int[node_num + 1];
    209209      node_first_in = new int[node_num];
     
    239239      node_first_out[node_num] = arc_num;
    240240    }
    241241
     242    template <typename ArcListIterator>
     243    void build(int n_nodes, ArcListIterator first, ArcListIterator last) {
     244      build(n_nodes, std::distance(first, last), first, last);
     245    }
     246
    242247  protected:
    243248
    244249    void fastFirstOut(Arc& e, const Node& n) const {
     
    373378
    374379    /// \brief Build the digraph from an arc list.
    375380    ///
    376     /// This function builds the digraph from the given arc list.
    377     /// It can be called more than once, but in such case, the whole
    378     /// structure and all maps will be cleared and rebuilt.
     381    /// This function builds the digraph from the given arc list.  It
     382    /// can be called more than once, but in such case, the whole
     383    /// structure and all maps will be cleared and rebuilt.  The
     384    /// number of arcs is determined by calling
     385    /// <tt>std::distance(begin, end)</tt>.
    379386    ///
    380387    /// The list of the arcs must be given in the range <tt>[begin, end)</tt>
    381     /// specified by STL compatible itartors whose \c value_type must be
     388    /// specified by STL compatible iterators whose \c value_type must be
    382389    /// <tt>std::pair<int,int></tt>.
    383390    /// Each arc must be specified by a pair of integer indices
    384391    /// from the range <tt>[0..n-1]</tt>. <i>The pairs must be in a
     
    409416      notifier(Arc()).build();
    410417    }
    411418
     419    /// \brief Build the digraph from an arc list. 
     420    ///
     421    /// This version requires the number of arcs to be specified
     422    /// explicitly, but avoids a possibly expensive call to
     423    /// <tt>std::distance()</tt>. 
     424    ///
     425    /// This function builds the digraph from the given arc list.  It
     426    /// can be called more than once, but in such case, the whole
     427    /// structure and all maps will be cleared and rebuilt.
     428    ///
     429    /// The list of the arcs must be given in the range <tt>[begin, end)</tt>
     430    /// specified by STL compatible itartors whose \c value_type must be
     431    /// <tt>std::pair<int,int></tt>.
     432    /// Each arc must be specified by a pair of integer indices
     433    /// from the range <tt>[0..n-1]</tt>. <i>The pairs must be in a
     434    /// non-decreasing order with respect to their first values.</i>
     435    /// If the k-th pair in the list is <tt>(i,j)</tt>, then
     436    /// <tt>arc(k-1)</tt> will connect <tt>node(i)</tt> to <tt>node(j)</tt>.
     437    ///
     438    /// \param n The number of nodes.
     439    /// \param begin An iterator pointing to the beginning of the arc list.
     440    /// \param end An iterator pointing to the end of the arc list.
     441    ///
     442    /// For example, a simple digraph can be constructed like this.
     443    /// \code
     444    ///   std::vector<std::pair<int,int> > arcs;
     445    ///   arcs.push_back(std::make_pair(0,1));
     446    ///   arcs.push_back(std::make_pair(0,2));
     447    ///   arcs.push_back(std::make_pair(1,3));
     448    ///   arcs.push_back(std::make_pair(1,2));
     449    ///   arcs.push_back(std::make_pair(3,0));
     450    ///   StaticDigraph gr;
     451    ///   gr.build(4, arcs.begin(), arcs.end());
     452    /// \endcode
     453    template <typename ArcListIterator>
     454    void build(int n_nodes, int n_arcs, ArcListIterator begin, ArcListIterator end) {
     455      if (built) Parent::clear();
     456      StaticDigraphBase::build(n_nodes, n_arcs, begin, end);
     457      notifier(Node()).build();
     458      notifier(Arc()).build();
     459    }
     460
    412461    /// \brief Clear the digraph.
    413462    ///
    414463    /// This function erases all nodes and arcs from the digraph.