predicate-requirements0.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include <boost/parameter.hpp>
  2. BOOST_PARAMETER_NAME((_graph, graphs) graph)
  3. BOOST_PARAMETER_NAME((_visitor, graphs) visitor)
  4. BOOST_PARAMETER_NAME((_root_vertex, graphs) in(root_vertex))
  5. BOOST_PARAMETER_NAME((_index_map, graphs) in(index_map))
  6. BOOST_PARAMETER_NAME((_color_map, graphs) in_out(color_map))
  7. #include <boost/graph/graph_traits.hpp>
  8. #include <boost/mpl/bool.hpp>
  9. #include <boost/mpl/if.hpp>
  10. #include <boost/type_traits/is_convertible.hpp>
  11. #include <boost/type_traits/is_integral.hpp>
  12. #include <boost/type_traits/is_same.hpp>
  13. struct vertex_descriptor_predicate
  14. {
  15. template <typename T, typename Args>
  16. struct apply
  17. : boost::mpl::if_<
  18. boost::is_convertible<
  19. T
  20. , typename boost::graph_traits<
  21. typename boost::parameter::value_type<
  22. Args
  23. , graphs::graph
  24. >::type
  25. >::vertex_descriptor
  26. >
  27. , boost::mpl::true_
  28. , boost::mpl::false_
  29. >
  30. {
  31. };
  32. };
  33. #include <boost/mpl/eval_if.hpp>
  34. struct graph_predicate
  35. {
  36. template <typename T, typename Args>
  37. struct apply
  38. : boost::mpl::eval_if<
  39. boost::is_convertible<
  40. typename boost::graph_traits<T>::traversal_category
  41. , boost::incidence_graph_tag
  42. >
  43. , boost::mpl::if_<
  44. boost::is_convertible<
  45. typename boost::graph_traits<T>::traversal_category
  46. , boost::vertex_list_graph_tag
  47. >
  48. , boost::mpl::true_
  49. , boost::mpl::false_
  50. >
  51. , boost::mpl::false_
  52. >
  53. {
  54. };
  55. };
  56. #include <boost/property_map/property_map.hpp>
  57. #include <boost/type_traits/is_same.hpp>
  58. struct color_map_predicate
  59. {
  60. template <typename T, typename Args>
  61. struct apply
  62. : boost::mpl::if_<
  63. boost::is_same<
  64. typename boost::property_traits<T>::key_type
  65. , typename boost::graph_traits<
  66. typename boost::parameter::value_type<
  67. Args
  68. , graphs::graph
  69. >::type
  70. >::vertex_descriptor
  71. >
  72. , boost::mpl::true_
  73. , boost::mpl::false_
  74. >
  75. {
  76. };
  77. };
  78. #include <boost/type_traits/is_integral.hpp>
  79. struct index_map_predicate
  80. {
  81. template <typename T, typename Args>
  82. struct apply
  83. : boost::mpl::eval_if<
  84. boost::is_integral<
  85. typename boost::property_traits<T>::value_type
  86. >
  87. , boost::mpl::if_<
  88. boost::is_same<
  89. typename boost::property_traits<T>::key_type
  90. , typename boost::graph_traits<
  91. typename boost::parameter::value_type<
  92. Args
  93. , graphs::graph
  94. >::type
  95. >::vertex_descriptor
  96. >
  97. , boost::mpl::true_
  98. , boost::mpl::false_
  99. >
  100. , boost::mpl::false_
  101. >
  102. {
  103. };
  104. };
  105. #include <boost/graph/properties.hpp>
  106. #include <vector>
  107. template <typename Size, typename IndexMap>
  108. boost::iterator_property_map<
  109. std::vector<boost::default_color_type>::iterator
  110. , IndexMap
  111. , boost::default_color_type
  112. , boost::default_color_type&
  113. >&
  114. default_color_map(Size num_vertices, IndexMap const& index_map)
  115. {
  116. static std::vector<boost::default_color_type> colors(num_vertices);
  117. static boost::iterator_property_map<
  118. std::vector<boost::default_color_type>::iterator
  119. , IndexMap
  120. , boost::default_color_type
  121. , boost::default_color_type&
  122. > m(colors.begin(), index_map);
  123. return m;
  124. }
  125. #include <boost/graph/depth_first_search.hpp>
  126. BOOST_PARAMETER_FUNCTION((void), depth_first_search, graphs,
  127. (required
  128. (graph, *(graph_predicate))
  129. )
  130. (optional
  131. (visitor
  132. , * // not easily checkable
  133. , boost::dfs_visitor<>()
  134. )
  135. (root_vertex
  136. , *(vertex_descriptor_predicate)
  137. , *vertices(graph).first
  138. )
  139. (index_map
  140. , *(index_map_predicate)
  141. , get(boost::vertex_index, graph)
  142. )
  143. (color_map
  144. , *(color_map_predicate)
  145. , default_color_map(num_vertices(graph), index_map)
  146. )
  147. )
  148. )
  149. {
  150. }
  151. #include <boost/core/lightweight_test.hpp>
  152. #include <boost/graph/adjacency_list.hpp>
  153. #include <utility>
  154. int main()
  155. {
  156. typedef boost::adjacency_list<
  157. boost::vecS
  158. , boost::vecS
  159. , boost::directedS
  160. > G;
  161. enum {u, v, w, x, y, z, N};
  162. typedef std::pair<std::size_t,std::size_t> E;
  163. E edges[] = {
  164. E(u, v), E(u, x), E(x, v), E(y, x),
  165. E(v, y), E(w, y), E(w, z), E(z, z)
  166. };
  167. G g(edges, edges + sizeof(edges) / sizeof(E), N);
  168. ::depth_first_search(g);
  169. ::depth_first_search(g, _root_vertex = static_cast<std::size_t>(x));
  170. return boost::report_errors();
  171. }