depth_first_search.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/depth_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. G
  21. */
  22. typedef mpl::vector<mpl::vector<A_B,A,B>,
  23. mpl::vector<B_C,B,C>,
  24. mpl::vector<C_D,C,D>,
  25. mpl::vector<C_E,C,E>,
  26. mpl::vector<C_F,C,F>,
  27. mpl::vector<B_F,B,F> >
  28. some_incidence_list;
  29. typedef mpl_graph::incidence_list_graph<some_incidence_list> some_incidence_list_graph;
  30. /*
  31. adjacency list test graph:
  32. A -> B -> C -\--> D
  33. \ |--> E
  34. \ \--> F
  35. \-----/
  36. G
  37. */
  38. typedef mpl::vector<
  39. mpl::pair<A, mpl::vector<mpl::pair<A_B, B> > >,
  40. mpl::pair<B, mpl::vector<mpl::pair<B_C, C>,
  41. mpl::pair<B_F, F> > >,
  42. mpl::pair<C, mpl::vector<mpl::pair<C_D, D>,
  43. mpl::pair<C_E, E>,
  44. mpl::pair<C_F, F> > >,
  45. mpl::pair<G, mpl::vector<> > >
  46. some_adjacency_list;
  47. typedef mpl_graph::adjacency_list_graph<some_adjacency_list> some_adjacency_list_graph;
  48. struct preordering_visitor : mpl_graph::dfs_default_visitor_operations {
  49. template<typename Node, typename Graph, typename State>
  50. struct discover_vertex :
  51. mpl::push_back<State, Node>
  52. {};
  53. };
  54. struct postordering_visitor : mpl_graph::dfs_default_visitor_operations {
  55. template<typename Node, typename Graph, typename State>
  56. struct finish_vertex :
  57. mpl::push_back<State, Node>
  58. {};
  59. };
  60. // adjacency list tests
  61. // preordering, default start node (first)
  62. typedef mpl::first<mpl_graph::
  63. depth_first_search_all<some_adjacency_list_graph,
  64. preordering_visitor,
  65. mpl::vector<> >::type>::type
  66. preorder_adj;
  67. BOOST_MPL_ASSERT(( mpl::equal<preorder_adj::type, mpl::vector<A,B,C,D,E,F,G> > ));
  68. // postordering, default start node
  69. typedef mpl::first<mpl_graph::
  70. depth_first_search_all<some_adjacency_list_graph,
  71. postordering_visitor,
  72. mpl::vector<> >::type>::type
  73. postorder_adj;
  74. BOOST_MPL_ASSERT(( mpl::equal<postorder_adj::type, mpl::vector<D,E,F,C,B,A,G> > ));
  75. // preordering all starting at C
  76. typedef mpl::first<mpl_graph::
  77. depth_first_search_all<some_adjacency_list_graph,
  78. preordering_visitor,
  79. mpl::vector<>,
  80. C>::type>::type
  81. preorder_adj_all_from_c;
  82. BOOST_MPL_ASSERT(( mpl::equal<preorder_adj_all_from_c::type, mpl::vector<C,D,E,F,A,B,G> > ));
  83. // preordering just those starting at C
  84. typedef mpl::first<mpl_graph::
  85. depth_first_search<some_adjacency_list_graph,
  86. preordering_visitor,
  87. mpl::vector<>,
  88. C>::type>::type
  89. preorder_adj_from_c;
  90. BOOST_MPL_ASSERT(( mpl::equal<preorder_adj_from_c::type, mpl::vector<C,D,E,F> > ));
  91. // incidence list tests
  92. // preordering, default start node (first)
  93. typedef mpl::first<mpl_graph::
  94. depth_first_search_all<some_incidence_list_graph,
  95. preordering_visitor,
  96. mpl::vector<> >::type>::type
  97. preorder_inc;
  98. BOOST_MPL_ASSERT(( mpl::equal<preorder_inc::type, mpl::vector<A,B,C,D,E,F> > ));
  99. // postordering, default start node
  100. typedef mpl::first<mpl_graph::
  101. depth_first_search_all<some_incidence_list_graph,
  102. postordering_visitor,
  103. mpl::vector<> >::type>::type
  104. postorder_inc;
  105. BOOST_MPL_ASSERT(( mpl::equal<postorder_inc::type, mpl::vector<D,E,F,C,B,A> > ));
  106. // preordering starting at C
  107. typedef mpl::first<mpl_graph::
  108. depth_first_search_all<some_incidence_list_graph,
  109. preordering_visitor,
  110. mpl::vector<>,
  111. C>::type>::type
  112. preorder_inc_all_from_c;
  113. BOOST_MPL_ASSERT(( mpl::equal<preorder_inc_all_from_c::type, mpl::vector<C,D,E,F,A,B> > ));
  114. // preordering starting at B
  115. typedef mpl::first<mpl_graph::
  116. depth_first_search<some_incidence_list_graph,
  117. preordering_visitor,
  118. mpl::vector<>,
  119. B>::type>::type
  120. preorder_inc_from_b;
  121. BOOST_MPL_ASSERT(( mpl::equal<preorder_inc_from_b::type, mpl::vector<B,C,D,E,F> > ));
  122. int main() {
  123. return 0;
  124. }