iteration_macros.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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>
  40. bgl_range_9 = vertices(g); 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,
  44. true) : false;
  45. ++bgl_range_9.first)
  46. */
  47. #define BGL_FORALL_VERTICES_T(VNAME, GNAME, GraphType) \
  48. for (std::pair< \
  49. typename boost::graph_traits< GraphType >::vertex_iterator, \
  50. typename boost::graph_traits< GraphType >::vertex_iterator > \
  51. BGL_RANGE(__LINE__) \
  52. = vertices(GNAME); \
  53. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); \
  54. BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  55. for (typename boost::graph_traits< GraphType >::vertex_descriptor \
  56. VNAME; \
  57. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) \
  58. ? (VNAME = *BGL_FIRST(__LINE__), true) \
  59. : false; \
  60. ++BGL_FIRST(__LINE__))
  61. #define BGL_FORALL_VERTICES(VNAME, GNAME, GraphType) \
  62. for (std::pair< boost::graph_traits< GraphType >::vertex_iterator, \
  63. boost::graph_traits< GraphType >::vertex_iterator > \
  64. BGL_RANGE(__LINE__) \
  65. = vertices(GNAME); \
  66. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); \
  67. BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  68. for (boost::graph_traits< GraphType >::vertex_descriptor VNAME; \
  69. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) \
  70. ? (VNAME = *BGL_FIRST(__LINE__), true) \
  71. : false; \
  72. ++BGL_FIRST(__LINE__))
  73. #define BGL_FORALL_EDGES_T(ENAME, GNAME, GraphType) \
  74. for (std::pair< typename boost::graph_traits< GraphType >::edge_iterator, \
  75. typename boost::graph_traits< GraphType >::edge_iterator > \
  76. BGL_RANGE(__LINE__) \
  77. = edges(GNAME); \
  78. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); \
  79. BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  80. for (typename boost::graph_traits< GraphType >::edge_descriptor ENAME; \
  81. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) \
  82. ? (ENAME = *BGL_FIRST(__LINE__), true) \
  83. : false; \
  84. ++BGL_FIRST(__LINE__))
  85. #define BGL_FORALL_EDGES(ENAME, GNAME, GraphType) \
  86. for (std::pair< boost::graph_traits< GraphType >::edge_iterator, \
  87. boost::graph_traits< GraphType >::edge_iterator > \
  88. BGL_RANGE(__LINE__) \
  89. = edges(GNAME); \
  90. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); \
  91. BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  92. for (boost::graph_traits< GraphType >::edge_descriptor ENAME; \
  93. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) \
  94. ? (ENAME = *BGL_FIRST(__LINE__), true) \
  95. : false; \
  96. ++BGL_FIRST(__LINE__))
  97. #define BGL_FORALL_ADJ_T(UNAME, VNAME, GNAME, GraphType) \
  98. for (std::pair< \
  99. typename boost::graph_traits< GraphType >::adjacency_iterator, \
  100. typename boost::graph_traits< GraphType >::adjacency_iterator > \
  101. BGL_RANGE(__LINE__) \
  102. = adjacent_vertices(UNAME, GNAME); \
  103. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); \
  104. BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  105. for (typename boost::graph_traits< GraphType >::vertex_descriptor \
  106. VNAME; \
  107. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) \
  108. ? (VNAME = *BGL_FIRST(__LINE__), true) \
  109. : false; \
  110. ++BGL_FIRST(__LINE__))
  111. #define BGL_FORALL_ADJ(UNAME, VNAME, GNAME, GraphType) \
  112. for (std::pair< boost::graph_traits< GraphType >::adjacency_iterator, \
  113. boost::graph_traits< GraphType >::adjacency_iterator > \
  114. BGL_RANGE(__LINE__) \
  115. = adjacent_vertices(UNAME, GNAME); \
  116. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); \
  117. BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  118. for (boost::graph_traits< GraphType >::vertex_descriptor VNAME; \
  119. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) \
  120. ? (VNAME = *BGL_FIRST(__LINE__), true) \
  121. : false; \
  122. ++BGL_FIRST(__LINE__))
  123. #define BGL_FORALL_OUTEDGES_T(UNAME, ENAME, GNAME, GraphType) \
  124. for (std::pair< \
  125. typename boost::graph_traits< GraphType >::out_edge_iterator, \
  126. typename boost::graph_traits< GraphType >::out_edge_iterator > \
  127. BGL_RANGE(__LINE__) \
  128. = out_edges(UNAME, GNAME); \
  129. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); \
  130. BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  131. for (typename boost::graph_traits< GraphType >::edge_descriptor ENAME; \
  132. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) \
  133. ? (ENAME = *BGL_FIRST(__LINE__), true) \
  134. : false; \
  135. ++BGL_FIRST(__LINE__))
  136. #define BGL_FORALL_OUTEDGES(UNAME, ENAME, GNAME, GraphType) \
  137. for (std::pair< boost::graph_traits< GraphType >::out_edge_iterator, \
  138. boost::graph_traits< GraphType >::out_edge_iterator > \
  139. BGL_RANGE(__LINE__) \
  140. = out_edges(UNAME, GNAME); \
  141. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); \
  142. BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  143. for (boost::graph_traits< GraphType >::edge_descriptor ENAME; \
  144. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) \
  145. ? (ENAME = *BGL_FIRST(__LINE__), true) \
  146. : false; \
  147. ++BGL_FIRST(__LINE__))
  148. #define BGL_FORALL_INEDGES_T(UNAME, ENAME, GNAME, GraphType) \
  149. for (std::pair< \
  150. typename boost::graph_traits< GraphType >::in_edge_iterator, \
  151. typename boost::graph_traits< GraphType >::in_edge_iterator > \
  152. BGL_RANGE(__LINE__) \
  153. = in_edges(UNAME, GNAME); \
  154. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); \
  155. BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  156. for (typename boost::graph_traits< GraphType >::edge_descriptor ENAME; \
  157. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) \
  158. ? (ENAME = *BGL_FIRST(__LINE__), true) \
  159. : false; \
  160. ++BGL_FIRST(__LINE__))
  161. #define BGL_FORALL_INEDGES(UNAME, ENAME, GNAME, GraphType) \
  162. for (std::pair< boost::graph_traits< GraphType >::in_edge_iterator, \
  163. boost::graph_traits< GraphType >::in_edge_iterator > \
  164. BGL_RANGE(__LINE__) \
  165. = in_edges(UNAME, GNAME); \
  166. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__); \
  167. BGL_FIRST(__LINE__) = BGL_LAST(__LINE__)) \
  168. for (boost::graph_traits< GraphType >::edge_descriptor ENAME; \
  169. BGL_FIRST(__LINE__) != BGL_LAST(__LINE__) \
  170. ? (ENAME = *BGL_FIRST(__LINE__), true) \
  171. : false; \
  172. ++BGL_FIRST(__LINE__))
  173. #endif // BOOST_GRAPH_ITERATION_MACROS_HPP