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