EventVisitorList.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <HTML>
  2. <!--
  3. Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 2000
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <Head>
  9. <Title>Boost Graph Library: EventVisitorList</Title>
  10. <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
  11. ALINK="#ff0000">
  12. <IMG SRC="../../../boost.png"
  13. ALT="C++ Boost" width="277" height="86">
  14. <BR Clear>
  15. <H1>EventVisitorList Concept</H1>
  16. An EventVisitorList is either an <a
  17. href="./EventVisitor.html">EventVisitor</a>, or a list of
  18. EventVisitors combined using <tt>std::pair</tt>. Each graph algorithm
  19. defines visitor adaptors that convert an EventVisitorList into the
  20. particular kind of visitor needed by the algorithm.
  21. In the following example we will show how to combine event visitors
  22. into a list using <tt>std::pair</tt> and how to use an algorithm's
  23. visitor adaptor class.
  24. <p>
  25. Suppose we would like to print out the parenthesis
  26. structure of the discover/finish times of vertices in a <a
  27. href="./graph_theory_review.html#sec:dfs-algorithm">depth-first
  28. search</a>. We can use the BGL algorithm <a
  29. href="./depth_first_search.html"><tt>depth_first_search()</tt></a> and
  30. two event visitors to accomplish this. The complete source code for
  31. the following example is in <a href="../example/dfs_parenthesis.cpp">
  32. <tt>examples/dfs_parenthesis.cpp</tt></a>. First we define the two
  33. event visitors. We use <tt>on_discover_vertex</tt> and
  34. <tt>on_finish_vertex</tt> as the event points, selected from the list
  35. of event points specified in <a
  36. href="./DFSVisitor.html">DFSVisitor</a>.
  37. <pre>
  38. struct open_paren : public base_visitor&lt;open_paren&gt; {
  39. typedef on_discover_vertex event_filter;
  40. template &lt;class Vertex, class Graph&gt;
  41. void operator()(Vertex v, Graph& G) {
  42. std::cout &lt;&lt; "(" &lt;&lt; v;
  43. }
  44. };
  45. struct close_paren : public base_visitor&lt;close_paren&gt; {
  46. typedef on_finish_vertex event_filter;
  47. template &lt;class Vertex, class Graph&gt;
  48. void operator()(Vertex v, Graph& G) {
  49. std::cout &lt;&lt; v &lt;&lt; ")";
  50. }
  51. };
  52. </pre>
  53. Next we create two event visitor objects and make an EventVisitorList
  54. out of them using a <tt>std::pair</tt> which created by
  55. <tt>std::make_pair</tt>.
  56. <pre>
  57. std::make_pair(open_paren(), close_paren())
  58. </pre>
  59. Next we want to pass this list into <tt>depth_first_search()</tt>, but
  60. <tt>depth_first_search()</tt> is expecting a <a
  61. href="./DFSVisitor.html">DFSVisitor</a>, not a EventVisitorList. We
  62. therefore use the <a
  63. href="./dfs_visitor.html"><tt>dfs_visitor</tt></a> adaptor which turns
  64. an EventVisitor list into a DFSVisitor. Like all of the visitor
  65. adaptors, <tt>dfs_visitor</tt> has a creation function called
  66. <tt>make_dfs_visitor()</tt>.
  67. <pre>
  68. make_dfs_visitor(std::make_pair(open_paren(), close_paren()))
  69. </pre>
  70. Now we can pass the resulting visitor object into
  71. <tt>depth_first_search()</tt> as follows.
  72. <pre>
  73. // graph object G is created ...
  74. std::vector&lt;default_color_type&gt; color(num_vertices(G));
  75. depth_first_search(G, make_dfs_visitor(std::make_pair(open_paren(), close_paren())),
  76. color.begin());
  77. </pre>
  78. For creating a list of more than two event visitors, you can nest calls to
  79. <tt>std::make_pair</tt> in the following way:
  80. <pre>
  81. std::make_pair(<i>visitor1</i>,
  82. std::make_pair(<i>visitor2</i>,
  83. ...
  84. std::make_pair(<i>visitorN-1</i>, <i>visitorN</i>)...));
  85. </pre>
  86. <h3>See Also</h3>
  87. <a href="./EventVisitor.html">EventVisitor</a>,
  88. <a href="./visitor_concepts.html">Visitor concepts</a>
  89. <br>
  90. <HR>
  91. <TABLE>
  92. <TR valign=top>
  93. <TD nowrap>Copyright &copy; 2000-2001</TD><TD>
  94. <A HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</A>,
  95. Indiana University (<A
  96. HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br>
  97. <A HREF="http://www.boost.org/people/liequan_lee.htm">Lie-Quan Lee</A>, Indiana University (<A HREF="mailto:llee@cs.indiana.edu">llee@cs.indiana.edu</A>)<br>
  98. <A HREF="https://homes.cs.washington.edu/~al75">Andrew Lumsdaine</A>,
  99. Indiana University (<A
  100. HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>)
  101. </TD></TR></TABLE>
  102. </BODY>
  103. </HTML>