src/lemon/graph_utils.h
changeset 977 48962802d168
parent 967 6563019430ba
child 986 e997802b855c
     1.1 --- a/src/lemon/graph_utils.h	Wed Nov 10 19:59:14 2004 +0000
     1.2 +++ b/src/lemon/graph_utils.h	Wed Nov 10 20:14:32 2004 +0000
     1.3 @@ -20,6 +20,7 @@
     1.4  #include <iterator>
     1.5  
     1.6  #include <lemon/invalid.h>
     1.7 +#include <lemon/utility.h>
     1.8  
     1.9  ///\ingroup gutils
    1.10  ///\file
    1.11 @@ -35,7 +36,6 @@
    1.12  /// \addtogroup gutils
    1.13  /// @{
    1.14  
    1.15 -  // counters in the graph
    1.16    /// \brief Function to count the items in the graph.
    1.17    ///
    1.18    /// This function counts the items in the graph.
    1.19 @@ -43,23 +43,53 @@
    1.20    /// it iterates on all of the items.
    1.21  
    1.22    template <typename Graph, typename ItemIt>
    1.23 -  inline int countItems(const Graph& _g) {
    1.24 +  inline int countItems(const Graph& g) {
    1.25      int num = 0;
    1.26 -    for (ItemIt it(_g); it != INVALID; ++it) {
    1.27 +    for (ItemIt it(g); it != INVALID; ++it) {
    1.28        ++num;
    1.29      }
    1.30      return num;
    1.31    }
    1.32  
    1.33 +  // Node counting:
    1.34 +
    1.35 +  template <typename Graph>
    1.36 +  inline
    1.37 +  typename enable_if<typename Graph::NodeNumTag, int>::type
    1.38 +  _countNodes(const Graph &g) {
    1.39 +    return g.nodeNum();
    1.40 +  }
    1.41 +
    1.42 +  template <typename Graph>
    1.43 +  inline int _countNodes(Wrap<Graph> w) {
    1.44 +    return countItems<Graph, typename Graph::NodeIt>(w.value);
    1.45 +  }
    1.46 +
    1.47    /// \brief Function to count the nodes in the graph.
    1.48    ///
    1.49    /// This function counts the nodes in the graph.
    1.50    /// The complexity of the function is O(n) but for some
    1.51    /// graph structure it is specialized to run in O(1).
    1.52 +  ///
    1.53 +  /// \todo refer how to specialize it
    1.54  
    1.55    template <typename Graph>
    1.56 -  inline int countNodes(const Graph& _g) {
    1.57 -    return countItems<Graph, typename Graph::NodeIt>(_g);
    1.58 +  inline int countNodes(const Graph& g) {
    1.59 +    return _countNodes<Graph>(g);
    1.60 +  }
    1.61 +
    1.62 +  // Edge counting:
    1.63 +
    1.64 +  template <typename Graph>
    1.65 +  inline
    1.66 +  typename enable_if<typename Graph::EdgeNumTag, int>::type
    1.67 +  _countEdges(const Graph &g) {
    1.68 +    return g.edgeNum();
    1.69 +  }
    1.70 +
    1.71 +  template <typename Graph>
    1.72 +  inline int _countEdges(Wrap<Graph> w) {
    1.73 +    return countItems<Graph, typename Graph::EdgeIt>(w.value);
    1.74    }
    1.75  
    1.76    /// \brief Function to count the edges in the graph.
    1.77 @@ -67,9 +97,10 @@
    1.78    /// This function counts the edges in the graph.
    1.79    /// The complexity of the function is O(e) but for some
    1.80    /// graph structure it is specialized to run in O(1).
    1.81 +
    1.82    template <typename Graph>
    1.83 -  inline int countEdges(const Graph& _g) {
    1.84 -    return countItems<Graph, typename Graph::EdgeIt>(_g);
    1.85 +  inline int countEdges(const Graph& g) {
    1.86 +    return _countEdges<Graph>(g);
    1.87    }
    1.88  
    1.89    /// \brief Function to count the symmetric edges in the graph.
    1.90 @@ -82,6 +113,7 @@
    1.91      return countItems<Graph, typename Graph::SymEdgeIt>(_g);
    1.92    }
    1.93  
    1.94 +
    1.95    template <typename Graph, typename DegIt>
    1.96    inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
    1.97      int num = 0;