iteration_macros.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //=======================================================================
  2. // Copyright 2001 Indiana University
  3. // Author: Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #ifndef BOOST_GRAPH_ITERATION_MACROS_HPP
  10. #define BOOST_GRAPH_ITERATION_MACROS_HPP
  11. #include <utility>
  12. #define BGL_CAT(x,y) x ## y
  13. #define BGL_RANGE(linenum) BGL_CAT(bgl_range_,linenum)
  14. #define BGL_FIRST(linenum) (BGL_RANGE(linenum).first)
  15. #define BGL_LAST(linenum) (BGL_RANGE(linenum).second)
  16. /*
  17. BGL_FORALL_VERTICES_T(v, g, graph_t) // This is on line 9
  18. expands to the following, but all on the same line
  19. for (typename boost::graph_traits<graph_t>::vertex_iterator
  20. bgl_first_9 = vertices(g).first, bgl_last_9 = vertices(g).second;
  21. bgl_first_9 != bgl_last_9; bgl_first_9 = bgl_last_9)
  22. for (typename boost::graph_traits<graph_t>::vertex_descriptor v;
  23. bgl_first_9 != bgl_last_9 ? (v = *bgl_first_9, true) : false;
  24. ++bgl_first_9)
  25. The purpose of having two for-loops is just to provide a place to
  26. declare both the iterator and value variables. There is really only
  27. one loop. The stopping condition gets executed two more times than it
  28. usually would be, oh well. The reason for the bgl_first_9 = bgl_last_9
  29. in the outer for-loop is in case the user puts a break statement
  30. in the inner for-loop.
  31. The other macros work in a similar fashion.
  32. Use the _T versions when the graph type is a template parameter or
  33. dependent on a template parameter. Otherwise use the non _T versions.
  34. -----------------------
  35. 6/9/09 THK
  36. The above contains two calls to the vertices function. I modified these
  37. macros to expand to
  38. for (std::pair<typename boost::graph_traits<graph_t>::vertex_iterator,
  39. typename boost::graph_traits<graph_t>::vertex_iterator> bgl_range_9 = vertices(g);
  40. bgl_range_9.first != bgl_range_9.second;
  41. bgl_range_9.first = bgl_range_9.second)
  42. for (typename boost::graph_traits<graph_t>::vertex_descriptor v;
  43. bgl_range_9.first != bgl_range_9.second ? (v = *bgl_range_9.first, true) : false;
  44. ++bgl_range_9.first)
  45. */
  46. #define BGL_FORALL_VERTICES_T(VNAME, GNAME, GraphType) \
  47. for (std::pair<typename boost::graph_traits<GraphType>::vertex_iterator, \
  48. typename boost::graph_traits<GraphType>::vertex_iterator> BGL_RANGE(__LINE__) = vertices(GNAME); \
  49. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  50. for (typename boost::graph_traits<GraphType>::vertex_descriptor VNAME; \
  51. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (VNAME = *BGL_FIRST(__LINE__), true):false; \
  52. ++BGL_FIRST(__LINE__))
  53. #define BGL_FORALL_VERTICES(VNAME, GNAME, GraphType) \
  54. for (std::pair<boost::graph_traits<GraphType>::vertex_iterator, \
  55. boost::graph_traits<GraphType>::vertex_iterator> BGL_RANGE(__LINE__) = vertices(GNAME); \
  56. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  57. for (boost::graph_traits<GraphType>::vertex_descriptor VNAME; \
  58. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (VNAME = *BGL_FIRST(__LINE__), true):false; \
  59. ++BGL_FIRST(__LINE__))
  60. #define BGL_FORALL_EDGES_T(ENAME, GNAME, GraphType) \
  61. for (std::pair<typename boost::graph_traits<GraphType>::edge_iterator, \
  62. typename boost::graph_traits<GraphType>::edge_iterator> BGL_RANGE(__LINE__) = edges(GNAME); \
  63. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  64. for (typename boost::graph_traits<GraphType>::edge_descriptor ENAME; \
  65. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true):false; \
  66. ++BGL_FIRST(__LINE__))
  67. #define BGL_FORALL_EDGES(ENAME, GNAME, GraphType) \
  68. for (std::pair<boost::graph_traits<GraphType>::edge_iterator, \
  69. boost::graph_traits<GraphType>::edge_iterator> BGL_RANGE(__LINE__) = edges(GNAME); \
  70. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  71. for (boost::graph_traits<GraphType>::edge_descriptor ENAME; \
  72. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true):false; \
  73. ++BGL_FIRST(__LINE__))
  74. #define BGL_FORALL_ADJ_T(UNAME, VNAME, GNAME, GraphType) \
  75. for (std::pair<typename boost::graph_traits<GraphType>::adjacency_iterator, \
  76. typename boost::graph_traits<GraphType>::adjacency_iterator> BGL_RANGE(__LINE__) = adjacent_vertices(UNAME, GNAME); \
  77. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  78. for (typename boost::graph_traits<GraphType>::vertex_descriptor VNAME; \
  79. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (VNAME = *BGL_FIRST(__LINE__), true) : false; \
  80. ++BGL_FIRST(__LINE__))
  81. #define BGL_FORALL_ADJ(UNAME, VNAME, GNAME, GraphType) \
  82. for (std::pair<boost::graph_traits<GraphType>::adjacency_iterator, \
  83. boost::graph_traits<GraphType>::adjacency_iterator> BGL_RANGE(__LINE__) = adjacent_vertices(UNAME, GNAME); \
  84. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  85. for (boost::graph_traits<GraphType>::vertex_descriptor VNAME; \
  86. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (VNAME = *BGL_FIRST(__LINE__), true) : false; \
  87. ++BGL_FIRST(__LINE__))
  88. #define BGL_FORALL_OUTEDGES_T(UNAME, ENAME, GNAME, GraphType) \
  89. for (std::pair<typename boost::graph_traits<GraphType>::out_edge_iterator, \
  90. typename boost::graph_traits<GraphType>::out_edge_iterator> BGL_RANGE(__LINE__) = out_edges(UNAME, GNAME); \
  91. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  92. for (typename boost::graph_traits<GraphType>::edge_descriptor ENAME; \
  93. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true) : false; \
  94. ++BGL_FIRST(__LINE__))
  95. #define BGL_FORALL_OUTEDGES(UNAME, ENAME, GNAME, GraphType) \
  96. for (std::pair<boost::graph_traits<GraphType>::out_edge_iterator, \
  97. boost::graph_traits<GraphType>::out_edge_iterator> BGL_RANGE(__LINE__) = out_edges(UNAME, GNAME); \
  98. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  99. for (boost::graph_traits<GraphType>::edge_descriptor ENAME; \
  100. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true) : false; \
  101. ++BGL_FIRST(__LINE__))
  102. #define BGL_FORALL_INEDGES_T(UNAME, ENAME, GNAME, GraphType) \
  103. for (std::pair<typename boost::graph_traits<GraphType>::in_edge_iterator, \
  104. typename boost::graph_traits<GraphType>::in_edge_iterator> BGL_RANGE(__LINE__) = in_edges(UNAME, GNAME); \
  105. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  106. for (typename boost::graph_traits<GraphType>::edge_descriptor ENAME; \
  107. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true) : false; \
  108. ++BGL_FIRST(__LINE__))
  109. #define BGL_FORALL_INEDGES(UNAME, ENAME, GNAME, GraphType) \
  110. for (std::pair<boost::graph_traits<GraphType>::in_edge_iterator, \
  111. boost::graph_traits<GraphType>::in_edge_iterator> BGL_RANGE(__LINE__) = in_edges(UNAME, GNAME); \
  112. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  113. for (boost::graph_traits<GraphType>::edge_descriptor ENAME; \
  114. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) ? (ENAME = *BGL_FIRST(__LINE__), true) : false; \
  115. ++BGL_FIRST(__LINE__))
  116. #endif // BOOST_GRAPH_ITERATION_MACROS_HPP