adj_list_edge_iterator.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. //=======================================================================
  3. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. //
  11. #ifndef BOOST_GRAPH_DETAIL_ADJ_LIST_EDGE_ITERATOR_HPP
  12. #define BOOST_GRAPH_DETAIL_ADJ_LIST_EDGE_ITERATOR_HPP
  13. #include <iterator>
  14. #include <utility>
  15. #include <boost/detail/workaround.hpp>
  16. #if BOOST_WORKAROUND( __IBMCPP__, <= 600 )
  17. # define BOOST_GRAPH_NO_OPTIONAL
  18. #endif
  19. #ifdef BOOST_GRAPH_NO_OPTIONAL
  20. # define BOOST_GRAPH_MEMBER .
  21. #else
  22. # define BOOST_GRAPH_MEMBER ->
  23. # include <boost/optional.hpp>
  24. #endif // ndef BOOST_GRAPH_NO_OPTIONAL
  25. namespace boost {
  26. namespace detail {
  27. template <class VertexIterator, class OutEdgeIterator, class Graph>
  28. class adj_list_edge_iterator {
  29. typedef adj_list_edge_iterator self;
  30. public:
  31. typedef std::forward_iterator_tag iterator_category;
  32. typedef typename OutEdgeIterator::value_type value_type;
  33. typedef typename OutEdgeIterator::reference reference;
  34. typedef typename OutEdgeIterator::pointer pointer;
  35. typedef typename OutEdgeIterator::difference_type difference_type;
  36. typedef difference_type distance_type;
  37. inline adj_list_edge_iterator() {}
  38. inline adj_list_edge_iterator(const self& x)
  39. : vBegin(x.vBegin), vCurr(x.vCurr), vEnd(x.vEnd),
  40. edges(x.edges), m_g(x.m_g) { }
  41. template <class G>
  42. inline adj_list_edge_iterator(VertexIterator b,
  43. VertexIterator c,
  44. VertexIterator e,
  45. const G& g)
  46. : vBegin(b), vCurr(c), vEnd(e), m_g(&g) {
  47. if ( vCurr != vEnd ) {
  48. while ( vCurr != vEnd && out_degree(*vCurr, *m_g) == 0 )
  49. ++vCurr;
  50. if ( vCurr != vEnd )
  51. edges = out_edges(*vCurr, *m_g);
  52. }
  53. }
  54. /*Note:
  55. In the directed graph cases, it is fine.
  56. For undirected graphs, one edge go through twice.
  57. */
  58. inline self& operator++() {
  59. ++edges BOOST_GRAPH_MEMBER first;
  60. if (edges BOOST_GRAPH_MEMBER first == edges BOOST_GRAPH_MEMBER second)
  61. {
  62. ++vCurr;
  63. while ( vCurr != vEnd && out_degree(*vCurr, *m_g) == 0 )
  64. ++vCurr;
  65. if ( vCurr != vEnd )
  66. edges = out_edges(*vCurr, *m_g);
  67. }
  68. return *this;
  69. }
  70. inline self operator++(int) {
  71. self tmp = *this;
  72. ++(*this);
  73. return tmp;
  74. }
  75. inline value_type operator*() const
  76. { return *edges BOOST_GRAPH_MEMBER first; }
  77. inline bool operator==(const self& x) const {
  78. return vCurr == x.vCurr
  79. && (vCurr == vEnd
  80. || edges BOOST_GRAPH_MEMBER first == x.edges BOOST_GRAPH_MEMBER first);
  81. }
  82. inline bool operator!=(const self& x) const {
  83. return vCurr != x.vCurr
  84. || (vCurr != vEnd
  85. && edges BOOST_GRAPH_MEMBER first != x.edges BOOST_GRAPH_MEMBER first);
  86. }
  87. protected:
  88. VertexIterator vBegin;
  89. VertexIterator vCurr;
  90. VertexIterator vEnd;
  91. #ifdef BOOST_GRAPH_NO_OPTIONAL
  92. std::pair<OutEdgeIterator, OutEdgeIterator> edges;
  93. #else
  94. boost::optional<std::pair<OutEdgeIterator, OutEdgeIterator> >
  95. edges;
  96. #endif // ndef BOOST_GRAPH_NO_OPTIONAL
  97. const Graph* m_g;
  98. };
  99. } // namespace detail
  100. }
  101. #undef BOOST_GRAPH_MEMBER
  102. #endif // BOOST_GRAPH_DETAIL_ADJ_LIST_EDGE_ITERATOR_HPP