mirror of
https://github.com/boostorg/graph.git
synced 2026-02-01 08:32:11 +00:00
232 lines
6.5 KiB
HTML
232 lines
6.5 KiB
HTML
<HTML>
|
|
<!--
|
|
-- Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 2000
|
|
--
|
|
-- Permission to use, copy, modify, distribute and sell this software
|
|
-- and its documentation for any purpose is hereby granted without fee,
|
|
-- provided that the above copyright notice appears in all copies and
|
|
-- that both that copyright notice and this permission notice appear
|
|
-- in supporting documentation. We make no
|
|
-- representations about the suitability of this software for any
|
|
-- purpose. It is provided "as is" without express or implied warranty.
|
|
-->
|
|
<Head>
|
|
<Title>Boost Graph Library: EventVisitor</Title>
|
|
<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
|
|
ALINK="#ff0000">
|
|
<IMG SRC="../../../c++boost.gif"
|
|
ALT="C++ Boost">
|
|
|
|
<BR Clear>
|
|
|
|
<H1>EventVisitor Concept</H1>
|
|
|
|
This concept defines the interface for single-event visitors. The
|
|
visitor has a single apply member function (<tt>operator()</tt>).
|
|
This function is invoked within the graph algorithm at the event-point
|
|
specified by the <tt>event_filter</tt> typedef within the
|
|
EventVisitor. The following is the list of event tags that can be
|
|
invoked in BGL algorithms. Each tag corresponds to a member function
|
|
of the visitor for an algorithm. For example, the <a
|
|
href="./BFSVisitor.html">BFSVisitor</a> of <a
|
|
href="./breadth_first_search.html"><tt>breadth_first_search()</tt></a>
|
|
has a <tt>cycle_edge()</tt> member function. The corresponding tag is
|
|
<tt>on_cycle_edge</tt>. The first argument is the event visitor's
|
|
<tt>operator()</tt> must be either an edge or vertex descriptor
|
|
depending on the event tag.
|
|
|
|
<pre>
|
|
namespace boost {
|
|
struct on_initialize_vertex { };
|
|
struct on_start_vertex { };
|
|
struct on_discover_vertex { };
|
|
struct on_examine_edge { };
|
|
struct on_tree_edge { };
|
|
struct on_cycle_edge { };
|
|
struct on_finish_vertex { };
|
|
struct on_forward_or_cross_edge { };
|
|
struct on_back_edge { };
|
|
struct on_edge_relaxed { };
|
|
struct on_edge_not_relaxed { };
|
|
struct on_edge_minimized { };
|
|
struct on_edge_not_minimized { };
|
|
} // namespace boost
|
|
</pre>
|
|
|
|
<h3>Creating an EventVisitorList</h3>
|
|
|
|
Often times one would like to use multiple single-event vistors in a
|
|
call to a particular algorithm. In the following example we will show
|
|
how to combine event visitors into a list using <tt>std::pair</tt> and
|
|
the algorithm's visitor adaptor class.
|
|
|
|
<p>
|
|
Suppose we would like to print out the parenthesis
|
|
structure of the discover/finish times of vertices in a <a
|
|
href="./graph_theory_review.html#sec:dfs-algorithm">depth-first
|
|
search</a>. We can use the BGL algorithm <a
|
|
href="./depth_first_search.html"><tt>depth_first_search()</tt></a> and
|
|
two event visitors to accomplish this. The complete source code for
|
|
the following example is in <a href="../examples/dfs_parenthesis.cpp">
|
|
<tt>examples/dfs_parenthesis.cpp</tt></a>. First we define the two
|
|
event visitors. We use <tt>on_discover_vertex</tt> and
|
|
<tt>on_finish_vertex</tt> as the event points, selected from the list
|
|
of event points specified in <a
|
|
href="./DFSVisitor.html">DFSVisitor</a>.
|
|
|
|
<pre>
|
|
struct open_paren : public base_visitor<open_paren> {
|
|
typedef on_discover_vertex event_filter;
|
|
template <class Vertex, class Graph>
|
|
void operator()(Vertex v, Graph& G) {
|
|
std::cout << "(" << v;
|
|
}
|
|
};
|
|
struct close_paren : public base_visitor<close_paren> {
|
|
typedef on_finish_vertex event_filter;
|
|
template <class Vertex, class Graph>
|
|
void operator()(Vertex v, Graph& G) {
|
|
std::cout << v << ")";
|
|
}
|
|
};
|
|
</pre>
|
|
|
|
Next we create two event visitor objects and make an EventVisitorList
|
|
out of them using a <tt>std::pair</tt> which created by
|
|
<tt>std::make_pair</tt>.
|
|
|
|
<pre>
|
|
std::make_pair(open_paren(), close_paren())
|
|
</pre>
|
|
|
|
Next we want to pass this list into <tt>depth_first_search()</tt>, but
|
|
<tt>depth_first_search()</tt> is expecting a <a
|
|
href="./DFSVisitor.html">DFSVisitor</a>, not a EventVisitorList. We
|
|
therefore use the <a
|
|
href="./dfs_visitor.html"><tt>dfs_visitor</tt></a> adaptor which turns
|
|
an EventVisitor list into a DFSVisitor. Like all of the visitor
|
|
adaptors, <tt>dfs_visitor</tt> has a creation function called
|
|
<tt>make_dfs_visitor()</tt>.
|
|
|
|
<pre>
|
|
make_dfs_visitor(std::make_pair(open_paren(), close_paren()))
|
|
</pre>
|
|
|
|
Now we can pass the resulting visitor object into
|
|
<tt>depth_first_search()</tt> as follows.
|
|
|
|
<pre>
|
|
// graph object G is created ...
|
|
|
|
std::vector<default_color_type> color(num_vertices(G));
|
|
|
|
depth_first_search(G, make_dfs_visitor(std::make_pair(open_paren(), close_paren())),
|
|
color.begin());
|
|
</pre>
|
|
|
|
<h3>Refinement of</h3>
|
|
|
|
none
|
|
<p>
|
|
|
|
<h3>Notation</h3>
|
|
|
|
<Table>
|
|
<TR>
|
|
<TD><tt>G</tt></TD>
|
|
<TD>A type that is a model of <a href="./Graph.html">Graph</a>.</TD>
|
|
</TR>
|
|
|
|
<TR>
|
|
<TD><tt>g</tt></TD>
|
|
<TD>An object of type <tt>G</tt>.</TD>
|
|
</TR>
|
|
|
|
<TR>
|
|
<TD><tt>V</tt></TD>
|
|
<TD>A type that is a model of EventVisitor.</TD>
|
|
</TR>
|
|
|
|
<TR>
|
|
<TD><tt>vis</tt></TD>
|
|
<TD>An object of type <tt>V</tt>.</TD>
|
|
</TR>
|
|
|
|
<TR>
|
|
<TD><tt>x</tt></TD>
|
|
<TD>An object of type
|
|
<tt>boost::graph_traits<G>::vertex_descriptor</tt>
|
|
or <tt>boost::graph_traits<G>::edge_descriptor</tt>.</TD>
|
|
</TR>
|
|
|
|
</table>
|
|
|
|
<h3>Associated Types</h3>
|
|
|
|
<Table border>
|
|
|
|
<TR>
|
|
<TD>Event Filter </TD>
|
|
<TD><TT>V::event_filter</TT></TD>
|
|
<TD>
|
|
A tag struct to specify on which event the visitor should be invoked.
|
|
</TD>
|
|
</TR>
|
|
|
|
</table>
|
|
|
|
<h3>Valid Expressions</h3>
|
|
|
|
<Table border>
|
|
|
|
<tr>
|
|
<th>Name</th><th>Expression</th><th>Return Type</th><th>Description</th>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>Apply Visitor</td>
|
|
<td><TT>vis(x, g)</TT></TD>
|
|
<TD><TT>void</TT></TD>
|
|
<TD>
|
|
Invokes the visitor operation on object <tt>x</tt>, which is
|
|
either a vertex or edge descriptor of the graph.
|
|
</TD>
|
|
</TR>
|
|
|
|
|
|
</table>
|
|
|
|
|
|
<h3>Models</h3>
|
|
|
|
<ul>
|
|
<li><a
|
|
href="./predecessor_recorder.html"><tt>predecessor_recorder</tt></a>
|
|
<li><a href="./distance_recorder.html"><tt>distance_recorder</tt></a>
|
|
<li><a href="./time_stamper.html"><tt>time_stamper</tt></a>
|
|
<li><a href="./property_writer.html"><tt>property_writer</tt></a>
|
|
<li><a href="./null_visitor.html"><tt>null_visitor</tt></a>
|
|
</ul>
|
|
|
|
<h3>See Also</h3>
|
|
|
|
<a href="./visitor_concepts.html">Visitor concepts</a>
|
|
|
|
|
|
<br>
|
|
<HR>
|
|
<TABLE>
|
|
<TR valign=top>
|
|
<TD nowrap>Copyright © 2000</TD><TD>
|
|
<A HREF=http://www.boost.org/people/jeremy_siek.htm>Jeremy Siek</A>,
|
|
Univ.of Notre Dame (<A
|
|
HREF="mailto:jsiek@lsc.nd.edu">jsiek@lsc.nd.edu</A>)<br>
|
|
<A HREF=http://www.lsc.nd.edu/~llee1>Lie-Quan Lee</A>, Univ.of Notre Dame (<A HREF="mailto:llee1@lsc.nd.edu">llee1@lsc.nd.edu</A>)<br>
|
|
<A HREF=http://www.lsc.nd.edu/~lums>Andrew Lumsdaine</A>,
|
|
Univ.of Notre Dame (<A
|
|
HREF="mailto:lums@lsc.nd.edu">lums@lsc.nd.edu</A>)
|
|
</TD></TR></TABLE>
|
|
|
|
</BODY>
|
|
</HTML>
|