1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
4 Copyright Doug Gregor 2004. Use, modification and
5 distribution is subject to the Boost Software License, Version
6 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
9 For more information, see http://www.boost.org
12 <title>Bundled Properties
</title>
15 <body BGCOLOR=
"#ffffff" LINK=
"#0000ee" TEXT=
"#000000" VLINK=
"#551a8b"
17 <IMG SRC=
"../../../boost.png"
18 ALT=
"C++ Boost" width=
"277" height=
"86"/>
19 <h1>Bundled Properties
</h1>
21 <p>Class templates
<code><a
22 href=
"adjacency_list.html">adjacency_list
</a></code> and
23 <code><a href=
"adjacency_matrix.html">adjacency_matrix
</a></code> support
24 the introduction of named properties via
<a
25 href=
"using_adjacency_list.html#sec:adjacency-list-properties">internal
26 properties
</a>. However, this method is cumbersome in many uses,
27 where it would be more intuitive to just specify a structure or
28 class that contains internal properties for edges or
29 vertices. Bundled properties allow one to use
30 <code>adjacency_list
</code> and
<code>adjacency_matrix
</code> in this
31 manner, providing a simple
32 way to introduce and access any number of internal properties
33 for vertices and edges.
</p>
35 <p>One can introduce bundled properties into an
36 either graph type by providing a user-defined class
37 type for the
<code>VertexProperties
</code> or
38 <code>EdgeProperties
</code> template arguments. The user-defined
39 class may alternatively be placed at the end of a
40 <code>property
</code> list, replacing the (implicit)
41 <code>boost::no_property
</code> argument.
</p>
43 <h2>Example: Route planning
</h2>
44 <p>Consider the implementation of a simple route planner that
45 should find the shortest directions from one city to another
46 via a set of highways. The vertices of the graph are cities,
47 and we may wish to store several bits of information about the
48 city within each vertex:
</p>
54 vector
<int
> zipcodes;
59 The edges in the graph represent highways, which also have several interesting
74 <p>With bundled properties, we can directly use the
<code>City
</code> and
75 <code>Highway
</code> structures to define the graph:
</p>
77 typedef boost::adjacency_list
<
78 boost::listS, boost::vecS, boost::bidirectionalS,
83 <p>Without bundled properties, translating this example directly
84 into an instantiation of
<code>adjacency_list
</code> would
85 involve several custom properties and would result in a type
88 typedef boost::adjacency_list
<
89 boost::listS, boost::vecS, boost::bidirectionalS,
91 boost::property
<boost::vertex_name_t, std::string,
92 boost::property
<population_t, int,
93 boost::property
<zipcodes_t, std::vector
<int
> > > >,
95 boost::property
<boost::edge_name_t, std::string,
96 boost::property
<boost::edge_weight_t, double,
97 boost::property
<edge_speed_limit_t, int,
98 boost::property
<edge_lanes_t, int,
99 boost::property
<edge_divided, bool
> > > > > >
104 Bundling vertex and edge properties greatly simplifies the declaration of
108 In addition to vertex and edge bundles, we can also bundle properties of the
109 graph itself. Suppopse we extend the application to include a portfolio of
110 route-planning maps for different countries. In addition to the
<code>City
</code>
111 and
<code>Highway
</code> bundles above, we can declare a graph bundle,
112 <code>Country
</Code>.
118 bool use_right; // Drive on the left or right
119 bool use_metric; // mph or km/h
123 <p>The graph would now be declared as:
</p>
127 typedef boost::adjacency_list
<
128 boost::listS, boost::vecS, boost::bidirectionalS,
129 City, Highway, Country
>
134 <h2>Accessing bundled properties
</h2>
135 <p>To access a bundled property for a particular edge or vertex,
136 subscript your graph with the descriptor of the edge or vertex
137 whose bundled property you wish to access. For instance:
</p>
139 Map map; // load the map
140 Map::vertex_descriptor v = *vertices(map).first;
141 map[v].name =
"Troy";
142 map[v].population =
49170;
143 map[v].zipcodes.push_back(
12180);
144 Map::edge_descriptor e = *out_edges(v, map).first;
145 map[e].name =
"I-87";
147 map[e].speed_limit =
65;
149 map[e].divided = true;
153 The graph bundle, since it does not correspond to a vertex or edge descripor
154 is accessed using the graph_bundle object as a key.
158 map[graph_bundle].name =
"United States";
159 map[graph_bundle].use_right = true;
160 map[graph_bundle].use_metric = false;
164 <h2>Properties maps from bundled properties
</h2>
165 <p>Often one needs to create a property map from an internal
166 property for use in a generic algorithm. For instance, using the
167 graph without bundled properties we might invoke
<a
168 href=
"dijkstra_shortest_paths.html">Dijkstra's shortest
169 paths
</a> algorithm like this:
</p>
171 vector
<double
> distances(num_vertices(map));
172 dijkstra_shortest_paths(map, from,
173 weight_map(get(edge_length, map))
174 .distance_map(make_iterator_property_map(distances.begin(),
175 get(vertex_index, map))));
178 <p>With bundled properties, we can just pass a
<em>member pointer
</em>
179 as the property for
<code>get
</code>. The equivalent example using bundled
182 vector
<double
> distances(num_vertices(map));
183 dijkstra_shortest_paths(map, from,
184 weight_map(get(
<font color=
"#ff0000">&Highway::miles
</font>, map))
185 .distance_map(make_iterator_property_map(distances.begin(),
186 get(vertex_index, map))));
189 <p>The type of the returned property map is
<code>property_map
<Map, double Highway::*
>::type
</code>
190 or
<code>property_map
<Map, double Highway::*
>::const_type
</code>, depending on whether the graph
191 <code>map
</code> is non-constant or constant.
193 <p> You may also access the entire vertex or edge bundle as a property map
194 using the
<code>vertex_bundle
</code> or
<code>edge_bundle
</code> properties,
195 respectively. For instance, the property map returned by
<code>get(vertex_bundle, map)
</code> is
196 an
<a href=
"../../property_map/doc/LvaluePropertyMap.html">Lvalue Property Map
</a> providing access to the
197 <code>City
</code> values stored in each vertex.
199 <h2>Property maps for a graph bundle
</h2>
200 There is currently no support for creating property maps from the bundled
201 properties of a graph.
203 <h2>Getting the type of bundled properties
</h2>
205 <p>To get the type of the vertex or edge bundle for a given graph
206 type
<tt>Graph
</tt>, you can use the trait
207 classes
<tt>vertex_bundle_type
</tt>
208 and
<tt>edge_bundle_type
</tt>. The
209 type
<tt>vertex_bundle_type
<Graph
>::type
</tt> will be the
210 type bundled with vertices (or
<tt>no_vertex_bundle
</tt> if the
211 graph supports bundles but no vertex bundle
212 exists). Likewise,
<tt>edge_bundle_type
<Graph
>::type
</tt>
213 will be the type bundled with edges (or
<tt>no_edge_bundle
</tt> if
214 no edge bundle exists).
</p>
216 <h2>Compatibility
</h2> <p>Bundled properties will only work
217 properly on compilers that support class template partial
221 Copyright
© 2004 <a href=
"http://www.boost.org/people/doug_gregor.html">Doug Gregor
</a>.
222 <address><a href=
"mailto:gregod@cs.rpi.edu"></a></address>
223 <!-- Created: Fri May 7 09:59:21 EDT 2004 -->
225 Last modified: Fri May
7 10:
56:
01 EDT
2004