vertex_and_edge_range.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2004 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_GRAPH_VERTEX_AND_EDGE_RANGE_HPP
  8. #define BOOST_GRAPH_VERTEX_AND_EDGE_RANGE_HPP
  9. #include <boost/graph/graph_traits.hpp>
  10. #include <iterator>
  11. namespace boost
  12. {
  13. namespace graph
  14. {
  15. template < typename Graph, typename VertexIterator, typename EdgeIterator >
  16. class vertex_and_edge_range
  17. {
  18. typedef graph_traits< Graph > traits_type;
  19. public:
  20. typedef typename traits_type::directed_category directed_category;
  21. typedef
  22. typename traits_type::edge_parallel_category edge_parallel_category;
  23. struct traversal_category : public virtual vertex_list_graph_tag,
  24. public virtual edge_list_graph_tag
  25. {
  26. };
  27. typedef std::size_t vertices_size_type;
  28. typedef VertexIterator vertex_iterator;
  29. typedef typename std::iterator_traits< VertexIterator >::value_type
  30. vertex_descriptor;
  31. typedef EdgeIterator edge_iterator;
  32. typedef typename std::iterator_traits< EdgeIterator >::value_type
  33. edge_descriptor;
  34. typedef std::size_t edges_size_type;
  35. typedef void adjacency_iterator;
  36. typedef void out_edge_iterator;
  37. typedef void in_edge_iterator;
  38. typedef void degree_size_type;
  39. static vertex_descriptor null_vertex()
  40. {
  41. return traits_type::null_vertex();
  42. }
  43. vertex_and_edge_range(const Graph& g, VertexIterator first_v,
  44. VertexIterator last_v, vertices_size_type n, EdgeIterator first_e,
  45. EdgeIterator last_e, edges_size_type m)
  46. : g(&g)
  47. , first_vertex(first_v)
  48. , last_vertex(last_v)
  49. , m_num_vertices(n)
  50. , first_edge(first_e)
  51. , last_edge(last_e)
  52. , m_num_edges(m)
  53. {
  54. }
  55. vertex_and_edge_range(const Graph& g, VertexIterator first_v,
  56. VertexIterator last_v, EdgeIterator first_e, EdgeIterator last_e)
  57. : g(&g)
  58. , first_vertex(first_v)
  59. , last_vertex(last_v)
  60. , first_edge(first_e)
  61. , last_edge(last_e)
  62. {
  63. m_num_vertices = std::distance(first_v, last_v);
  64. m_num_edges = std::distance(first_e, last_e);
  65. }
  66. const Graph* g;
  67. vertex_iterator first_vertex;
  68. vertex_iterator last_vertex;
  69. vertices_size_type m_num_vertices;
  70. edge_iterator first_edge;
  71. edge_iterator last_edge;
  72. edges_size_type m_num_edges;
  73. };
  74. template < typename Graph, typename VertexIterator, typename EdgeIterator >
  75. inline std::pair< VertexIterator, VertexIterator > vertices(
  76. const vertex_and_edge_range< Graph, VertexIterator, EdgeIterator >& g)
  77. {
  78. return std::make_pair(g.first_vertex, g.last_vertex);
  79. }
  80. template < typename Graph, typename VertexIterator, typename EdgeIterator >
  81. inline typename vertex_and_edge_range< Graph, VertexIterator,
  82. EdgeIterator >::vertices_size_type
  83. num_vertices(
  84. const vertex_and_edge_range< Graph, VertexIterator, EdgeIterator >& g)
  85. {
  86. return g.m_num_vertices;
  87. }
  88. template < typename Graph, typename VertexIterator, typename EdgeIterator >
  89. inline std::pair< EdgeIterator, EdgeIterator > edges(
  90. const vertex_and_edge_range< Graph, VertexIterator, EdgeIterator >& g)
  91. {
  92. return std::make_pair(g.first_edge, g.last_edge);
  93. }
  94. template < typename Graph, typename VertexIterator, typename EdgeIterator >
  95. inline typename vertex_and_edge_range< Graph, VertexIterator,
  96. EdgeIterator >::edges_size_type
  97. num_edges(
  98. const vertex_and_edge_range< Graph, VertexIterator, EdgeIterator >& g)
  99. {
  100. return g.m_num_edges;
  101. }
  102. template < typename Graph, typename VertexIterator, typename EdgeIterator >
  103. inline typename vertex_and_edge_range< Graph, VertexIterator,
  104. EdgeIterator >::vertex_descriptor
  105. source(typename vertex_and_edge_range< Graph, VertexIterator,
  106. EdgeIterator >::edge_descriptor e,
  107. const vertex_and_edge_range< Graph, VertexIterator, EdgeIterator >& g)
  108. {
  109. return source(e, *g.g);
  110. }
  111. template < typename Graph, typename VertexIterator, typename EdgeIterator >
  112. inline typename vertex_and_edge_range< Graph, VertexIterator,
  113. EdgeIterator >::vertex_descriptor
  114. target(typename vertex_and_edge_range< Graph, VertexIterator,
  115. EdgeIterator >::edge_descriptor e,
  116. const vertex_and_edge_range< Graph, VertexIterator, EdgeIterator >& g)
  117. {
  118. return target(e, *g.g);
  119. }
  120. template < typename Graph, typename VertexIterator, typename EdgeIterator >
  121. inline vertex_and_edge_range< Graph, VertexIterator, EdgeIterator >
  122. make_vertex_and_edge_range(const Graph& g, VertexIterator first_v,
  123. VertexIterator last_v, EdgeIterator first_e, EdgeIterator last_e)
  124. {
  125. typedef vertex_and_edge_range< Graph, VertexIterator, EdgeIterator >
  126. result_type;
  127. return result_type(g, first_v, last_v, first_e, last_e);
  128. }
  129. } // end namespace graph
  130. using graph::make_vertex_and_edge_range;
  131. using graph::vertex_and_edge_range;
  132. } // end namespace boost
  133. #endif // BOOST_GRAPH_VERTEX_AND_EDGE_RANGE_HPP