breadth_first_search.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2008-2010 Gordon Woodhull
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include <boost/msm/mpl_graph/breadth_first_search.hpp>
  5. #include <boost/msm/mpl_graph/adjacency_list_graph.hpp>
  6. #include <boost/msm/mpl_graph/incidence_list_graph.hpp>
  7. #include <iostream>
  8. namespace mpl_graph = boost::msm::mpl_graph;
  9. namespace mpl = boost::mpl;
  10. // vertices
  11. struct A{}; struct B{}; struct C{}; struct D{}; struct E{}; struct F{}; struct G{};
  12. // edges
  13. struct A_B{}; struct B_C{}; struct C_D{}; struct C_E{}; struct C_F{}; struct B_F{};
  14. /*
  15. incidence list test graph:
  16. A -> B -> C -\--> D
  17. \ |--> E
  18. \ \--> F
  19. \-----/
  20. */
  21. typedef mpl::vector<mpl::vector<A_B,A,B>,
  22. mpl::vector<B_C,B,C>,
  23. mpl::vector<C_D,C,D>,
  24. mpl::vector<C_E,C,E>,
  25. mpl::vector<C_F,C,F>,
  26. mpl::vector<B_F,B,F> >
  27. some_incidence_list;
  28. typedef mpl_graph::incidence_list_graph<some_incidence_list> some_incidence_list_graph;
  29. /*
  30. adjacency list test graph:
  31. A -> B -> C -\--> D
  32. \ |--> E
  33. \ \--> F
  34. \-----/
  35. G
  36. */
  37. typedef mpl::vector<
  38. mpl::pair<A, mpl::vector<mpl::pair<A_B, B> > >,
  39. mpl::pair<B, mpl::vector<mpl::pair<B_C, C>,
  40. mpl::pair<B_F, F> > >,
  41. mpl::pair<C, mpl::vector<mpl::pair<C_D, D>,
  42. mpl::pair<C_E, E>,
  43. mpl::pair<C_F, F> > >,
  44. mpl::pair<G, mpl::vector<> > >
  45. some_adjacency_list;
  46. typedef mpl_graph::adjacency_list_graph<some_adjacency_list> some_adjacency_list_graph;
  47. struct preordering_visitor : mpl_graph::bfs_default_visitor_operations {
  48. template<typename Vertex, typename Graph, typename State>
  49. struct discover_vertex :
  50. mpl::push_back<State, Vertex>
  51. {};
  52. };
  53. struct postordering_visitor : mpl_graph::bfs_default_visitor_operations {
  54. template<typename Vertex, typename Graph, typename State>
  55. struct finish_vertex :
  56. mpl::push_back<State, Vertex>
  57. {};
  58. };
  59. struct examine_edge_visitor : mpl_graph::bfs_default_visitor_operations {
  60. template<typename Edge, typename Graph, typename State>
  61. struct examine_edge :
  62. mpl::push_back<State, Edge>
  63. {};
  64. };
  65. struct tree_edge_visitor : mpl_graph::bfs_default_visitor_operations {
  66. template<typename Edge, typename Graph, typename State>
  67. struct tree_edge :
  68. mpl::push_back<State, Edge>
  69. {};
  70. };
  71. // adjacency list tests
  72. // preordering, start from A
  73. typedef mpl::first<mpl_graph::
  74. breadth_first_search<some_adjacency_list_graph,
  75. preordering_visitor,
  76. mpl::vector<>,
  77. A>::type>::type
  78. preorder_adj_a;
  79. BOOST_MPL_ASSERT(( mpl::equal<preorder_adj_a::type, mpl::vector<A,B,C,F,D,E> > ));
  80. // examine edges, start from A
  81. typedef mpl::first<mpl_graph::
  82. breadth_first_search<some_adjacency_list_graph,
  83. examine_edge_visitor,
  84. mpl::vector<>,
  85. A>::type>::type
  86. ex_edges_adj_a;
  87. BOOST_MPL_ASSERT(( mpl::equal<ex_edges_adj_a::type, mpl::vector<A_B,B_C,B_F,C_D,C_E,C_F> > ));
  88. // tree edges, start from A
  89. typedef mpl::first<mpl_graph::
  90. breadth_first_search<some_adjacency_list_graph,
  91. tree_edge_visitor,
  92. mpl::vector<>,
  93. A>::type>::type
  94. tree_edges_adj_a;
  95. BOOST_MPL_ASSERT(( mpl::equal<tree_edges_adj_a::type, mpl::vector<A_B,B_C,B_F,C_D,C_E> > ));
  96. // preordering, search all, default start node (first)
  97. typedef mpl::first<mpl_graph::
  98. breadth_first_search_all<some_adjacency_list_graph,
  99. preordering_visitor,
  100. mpl::vector<> >::type>::type
  101. preorder_adj;
  102. BOOST_MPL_ASSERT(( mpl::equal<preorder_adj::type, mpl::vector<A,B,C,F,D,E,G> > ));
  103. // postordering, starting at A (same as preordering because BFS fully processes one vertex b4 moving to next)
  104. typedef mpl::first<mpl_graph::
  105. breadth_first_search<some_adjacency_list_graph,
  106. postordering_visitor,
  107. mpl::vector<>,
  108. A>::type>::type
  109. postorder_adj_a;
  110. BOOST_MPL_ASSERT(( mpl::equal<postorder_adj_a::type, mpl::vector<A,B,C,F,D,E> > ));
  111. // postordering, default start node (same as preordering because BFS fully processes one vertex b4 moving to next)
  112. typedef mpl::first<mpl_graph::
  113. breadth_first_search_all<some_adjacency_list_graph,
  114. postordering_visitor,
  115. mpl::vector<> >::type>::type
  116. postorder_adj;
  117. BOOST_MPL_ASSERT(( mpl::equal<postorder_adj::type, mpl::vector<A,B,C,F,D,E,G> > ));
  118. // preordering starting at C
  119. typedef mpl::first<mpl_graph::
  120. breadth_first_search<some_adjacency_list_graph,
  121. preordering_visitor,
  122. mpl::vector<>,
  123. C>::type>::type
  124. preorder_adj_from_c;
  125. BOOST_MPL_ASSERT(( mpl::equal<preorder_adj_from_c::type, mpl::vector<C,D,E,F> > ));
  126. // preordering, search all, starting at C
  127. typedef mpl::first<mpl_graph::
  128. breadth_first_search_all<some_adjacency_list_graph,
  129. preordering_visitor,
  130. mpl::vector<>,
  131. C>::type>::type
  132. preorder_adj_from_c_all;
  133. BOOST_MPL_ASSERT(( mpl::equal<preorder_adj_from_c_all::type, mpl::vector<C,D,E,F,A,B,G> > ));
  134. // incidence list tests
  135. // preordering, start from A
  136. typedef mpl::first<mpl_graph::
  137. breadth_first_search<some_incidence_list_graph,
  138. preordering_visitor,
  139. mpl::vector<>,
  140. A>::type>::type
  141. preorder_inc_a;
  142. BOOST_MPL_ASSERT(( mpl::equal<preorder_inc_a::type, mpl::vector<A,B,C,F,D,E> > ));
  143. // preordering, start from C
  144. typedef mpl::first<mpl_graph::
  145. breadth_first_search<some_incidence_list_graph,
  146. preordering_visitor,
  147. mpl::vector<>,
  148. C>::type>::type
  149. preorder_inc_c;
  150. BOOST_MPL_ASSERT(( mpl::equal<preorder_inc_c::type, mpl::vector<C,D,E,F> > ));
  151. // preordering, default start node (first)
  152. typedef mpl::first<mpl_graph::
  153. breadth_first_search_all<some_incidence_list_graph,
  154. preordering_visitor,
  155. mpl::vector<> >::type>::type
  156. preorder_inc;
  157. BOOST_MPL_ASSERT(( mpl::equal<preorder_inc::type, mpl::vector<A,B,C,F,D,E> > ));
  158. // postordering, default start node
  159. typedef mpl::first<mpl_graph::
  160. breadth_first_search_all<some_incidence_list_graph,
  161. postordering_visitor,
  162. mpl::vector<> >::type>::type
  163. postorder_inc;
  164. BOOST_MPL_ASSERT(( mpl::equal<postorder_inc::type, mpl::vector<A,B,C,F,D,E> > ));
  165. // preordering, search all, starting at C
  166. typedef mpl::first<mpl_graph::
  167. breadth_first_search_all<some_incidence_list_graph,
  168. preordering_visitor,
  169. mpl::vector<>,
  170. C>::type>::type
  171. preorder_inc_from_c;
  172. BOOST_MPL_ASSERT(( mpl::equal<preorder_inc_from_c::type, mpl::vector<C,D,E,F,A,B> > ));
  173. int main() {
  174. return 0;
  175. }