Chris@16
|
1 // Copyright (C) 2007 Douglas Gregor
|
Chris@16
|
2
|
Chris@16
|
3 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 // Provides support for named vertices in graphs, allowing one to more
|
Chris@16
|
8 // easily associate unique external names (URLs, city names, employee
|
Chris@16
|
9 // ID numbers, etc.) with the vertices of a graph.
|
Chris@16
|
10 #ifndef BOOST_GRAPH_NAMED_GRAPH_HPP
|
Chris@16
|
11 #define BOOST_GRAPH_NAMED_GRAPH_HPP
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/config.hpp>
|
Chris@16
|
14 #include <boost/static_assert.hpp>
|
Chris@16
|
15 #include <boost/functional/hash.hpp>
|
Chris@16
|
16 #include <boost/graph/graph_traits.hpp>
|
Chris@16
|
17 #include <boost/graph/properties.hpp>
|
Chris@16
|
18 #include <boost/multi_index/hashed_index.hpp>
|
Chris@16
|
19 #include <boost/multi_index/member.hpp>
|
Chris@16
|
20 #include <boost/multi_index_container.hpp>
|
Chris@16
|
21 #include <boost/optional.hpp>
|
Chris@16
|
22 #include <boost/pending/property.hpp> // for boost::lookup_one_property
|
Chris@16
|
23 #include <boost/pending/container_traits.hpp>
|
Chris@16
|
24 #include <boost/throw_exception.hpp>
|
Chris@16
|
25 #include <boost/tuple/tuple.hpp> // for boost::make_tuple
|
Chris@16
|
26 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
27 #include <boost/type_traits/is_base_of.hpp>
|
Chris@16
|
28 #include <boost/type_traits/remove_cv.hpp>
|
Chris@16
|
29 #include <boost/type_traits/remove_reference.hpp>
|
Chris@16
|
30 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
31 #include <functional> // for std::equal_to
|
Chris@16
|
32 #include <stdexcept> // for std::runtime_error
|
Chris@16
|
33 #include <utility> // for std::pair
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost { namespace graph {
|
Chris@16
|
36
|
Chris@16
|
37 /*******************************************************************
|
Chris@16
|
38 * User-customized traits *
|
Chris@16
|
39 *******************************************************************/
|
Chris@16
|
40
|
Chris@16
|
41 /**
|
Chris@16
|
42 * @brief Trait used to extract the internal vertex name from a vertex
|
Chris@16
|
43 * property.
|
Chris@16
|
44 *
|
Chris@16
|
45 * To enable the use of internal vertex names in a graph type,
|
Chris@16
|
46 * specialize the @c internal_vertex_name trait for your graph
|
Chris@16
|
47 * property (e.g., @c a City class, which stores information about the
|
Chris@16
|
48 * vertices in a road map).
|
Chris@16
|
49 */
|
Chris@16
|
50 template<typename VertexProperty>
|
Chris@16
|
51 struct internal_vertex_name
|
Chris@16
|
52 {
|
Chris@16
|
53 /**
|
Chris@16
|
54 * The @c type field provides a function object that extracts a key
|
Chris@16
|
55 * from the @c VertexProperty. The function object type must have a
|
Chris@16
|
56 * nested @c result_type that provides the type of the key. For
|
Chris@16
|
57 * more information, see the @c KeyExtractor concept in the
|
Chris@16
|
58 * Boost.MultiIndex documentation: @c type must either be @c void
|
Chris@16
|
59 * (if @c VertexProperty does not have an internal vertex name) or
|
Chris@16
|
60 * a model of @c KeyExtractor.
|
Chris@16
|
61 */
|
Chris@16
|
62 typedef void type;
|
Chris@16
|
63 };
|
Chris@16
|
64
|
Chris@16
|
65 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
Chris@16
|
66 /**
|
Chris@16
|
67 * Extract the internal vertex name from a @c property structure by
|
Chris@16
|
68 * looking at its base.
|
Chris@16
|
69 */
|
Chris@16
|
70 template<typename Tag, typename T, typename Base>
|
Chris@16
|
71 struct internal_vertex_name<property<Tag, T, Base> >
|
Chris@16
|
72 : internal_vertex_name<Base> { };
|
Chris@16
|
73 #endif
|
Chris@16
|
74
|
Chris@16
|
75 /**
|
Chris@16
|
76 * Construct an instance of @c VertexProperty directly from its
|
Chris@16
|
77 * name. This function object should be used within the @c
|
Chris@16
|
78 * internal_vertex_constructor trait.
|
Chris@16
|
79 */
|
Chris@16
|
80 template<typename VertexProperty>
|
Chris@16
|
81 struct vertex_from_name
|
Chris@16
|
82 {
|
Chris@16
|
83 private:
|
Chris@16
|
84 typedef typename internal_vertex_name<VertexProperty>::type extract_name_type;
|
Chris@16
|
85
|
Chris@16
|
86 typedef typename remove_cv<
|
Chris@16
|
87 typename remove_reference<
|
Chris@16
|
88 typename extract_name_type::result_type>::type>::type
|
Chris@16
|
89 vertex_name_type;
|
Chris@16
|
90
|
Chris@16
|
91 public:
|
Chris@16
|
92 typedef vertex_name_type argument_type;
|
Chris@16
|
93 typedef VertexProperty result_type;
|
Chris@16
|
94
|
Chris@16
|
95 VertexProperty operator()(const vertex_name_type& name)
|
Chris@16
|
96 {
|
Chris@16
|
97 return VertexProperty(name);
|
Chris@16
|
98 }
|
Chris@16
|
99 };
|
Chris@16
|
100
|
Chris@16
|
101 /**
|
Chris@16
|
102 * Throw an exception whenever one tries to construct a @c
|
Chris@16
|
103 * VertexProperty instance from its name.
|
Chris@16
|
104 */
|
Chris@16
|
105 template<typename VertexProperty>
|
Chris@16
|
106 struct cannot_add_vertex
|
Chris@16
|
107 {
|
Chris@16
|
108 private:
|
Chris@16
|
109 typedef typename internal_vertex_name<VertexProperty>::type extract_name_type;
|
Chris@16
|
110
|
Chris@16
|
111 typedef typename remove_cv<
|
Chris@16
|
112 typename remove_reference<
|
Chris@16
|
113 typename extract_name_type::result_type>::type>::type
|
Chris@16
|
114 vertex_name_type;
|
Chris@16
|
115
|
Chris@16
|
116 public:
|
Chris@16
|
117 typedef vertex_name_type argument_type;
|
Chris@16
|
118 typedef VertexProperty result_type;
|
Chris@16
|
119
|
Chris@16
|
120 VertexProperty operator()(const vertex_name_type&)
|
Chris@16
|
121 {
|
Chris@16
|
122 boost::throw_exception(std::runtime_error("add_vertex: "
|
Chris@16
|
123 "unable to create a vertex from its name"));
|
Chris@16
|
124 }
|
Chris@16
|
125 };
|
Chris@16
|
126
|
Chris@16
|
127 /**
|
Chris@16
|
128 * @brief Trait used to construct an instance of a @c VertexProperty,
|
Chris@16
|
129 * which is a class type that stores the properties associated with a
|
Chris@16
|
130 * vertex in a graph, from just the name of that vertex property. This
|
Chris@16
|
131 * operation is used when an operation is required to map from a
|
Chris@16
|
132 * vertex name to a vertex descriptor (e.g., to add an edge outgoing
|
Chris@16
|
133 * from that vertex), but no vertex by the name exists. The function
|
Chris@16
|
134 * object provided by this trait will be used to add new vertices
|
Chris@16
|
135 * based only on their names. Since this cannot be done for arbitrary
|
Chris@16
|
136 * types, the default behavior is to throw an exception when this
|
Chris@16
|
137 * routine is called, which requires that all named vertices be added
|
Chris@16
|
138 * before adding any edges by name.
|
Chris@16
|
139 */
|
Chris@16
|
140 template<typename VertexProperty>
|
Chris@16
|
141 struct internal_vertex_constructor
|
Chris@16
|
142 {
|
Chris@16
|
143 /**
|
Chris@16
|
144 * The @c type field provides a function object that constructs a
|
Chris@16
|
145 * new instance of @c VertexProperty from the name of the vertex (as
|
Chris@16
|
146 * determined by @c internal_vertex_name). The function object shall
|
Chris@16
|
147 * accept a vertex name and return a @c VertexProperty. Predefined
|
Chris@16
|
148 * options include:
|
Chris@16
|
149 *
|
Chris@16
|
150 * @c vertex_from_name<VertexProperty>: construct an instance of
|
Chris@16
|
151 * @c VertexProperty directly from the name.
|
Chris@16
|
152 *
|
Chris@16
|
153 * @c cannot_add_vertex<VertexProperty>: the default value, which
|
Chris@16
|
154 * throws an @c std::runtime_error if one attempts to add a vertex
|
Chris@16
|
155 * given just the name.
|
Chris@16
|
156 */
|
Chris@16
|
157 typedef cannot_add_vertex<VertexProperty> type;
|
Chris@16
|
158 };
|
Chris@16
|
159
|
Chris@16
|
160 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
Chris@16
|
161 /**
|
Chris@16
|
162 * Extract the internal vertex constructor from a @c property structure by
|
Chris@16
|
163 * looking at its base.
|
Chris@16
|
164 */
|
Chris@16
|
165 template<typename Tag, typename T, typename Base>
|
Chris@16
|
166 struct internal_vertex_constructor<property<Tag, T, Base> >
|
Chris@16
|
167 : internal_vertex_constructor<Base> { };
|
Chris@16
|
168 #endif
|
Chris@16
|
169
|
Chris@16
|
170 /*******************************************************************
|
Chris@16
|
171 * Named graph mixin *
|
Chris@16
|
172 *******************************************************************/
|
Chris@16
|
173
|
Chris@16
|
174 /**
|
Chris@16
|
175 * named_graph is a mixin that provides names for the vertices of a
|
Chris@16
|
176 * graph, including a mapping from names to vertices. Graph types that
|
Chris@16
|
177 * may or may not be have vertex names (depending on the properties
|
Chris@16
|
178 * supplied by the user) should use maybe_named_graph.
|
Chris@16
|
179 *
|
Chris@16
|
180 * Template parameters:
|
Chris@16
|
181 *
|
Chris@16
|
182 * Graph: the graph type that derives from named_graph
|
Chris@16
|
183 *
|
Chris@16
|
184 * Vertex: the type of a vertex descriptor in Graph. Note: we cannot
|
Chris@16
|
185 * use graph_traits here, because the Graph is not yet defined.
|
Chris@16
|
186 *
|
Chris@16
|
187 * VertexProperty: the type stored with each vertex in the Graph.
|
Chris@16
|
188 */
|
Chris@16
|
189 template<typename Graph, typename Vertex, typename VertexProperty>
|
Chris@16
|
190 class named_graph
|
Chris@16
|
191 {
|
Chris@16
|
192 public:
|
Chris@16
|
193 /// The type of the function object that extracts names from vertex
|
Chris@16
|
194 /// properties.
|
Chris@16
|
195 typedef typename internal_vertex_name<VertexProperty>::type extract_name_type;
|
Chris@16
|
196 /// The type of the "bundled" property, from which the name can be
|
Chris@16
|
197 /// extracted.
|
Chris@16
|
198 typedef typename lookup_one_property<VertexProperty, vertex_bundle_t>::type
|
Chris@16
|
199 bundled_vertex_property_type;
|
Chris@16
|
200
|
Chris@16
|
201 /// The type of the function object that generates vertex properties
|
Chris@16
|
202 /// from names, for the implicit addition of vertices.
|
Chris@16
|
203 typedef typename internal_vertex_constructor<VertexProperty>::type
|
Chris@16
|
204 vertex_constructor_type;
|
Chris@16
|
205
|
Chris@16
|
206 /// The type used to name vertices in the graph
|
Chris@16
|
207 typedef typename remove_cv<
|
Chris@16
|
208 typename remove_reference<
|
Chris@16
|
209 typename extract_name_type::result_type>::type>::type
|
Chris@16
|
210 vertex_name_type;
|
Chris@16
|
211
|
Chris@16
|
212 /// The type of vertex descriptors in the graph
|
Chris@16
|
213 typedef Vertex vertex_descriptor;
|
Chris@16
|
214
|
Chris@16
|
215 private:
|
Chris@16
|
216 /// Key extractor for use with the multi_index_container
|
Chris@16
|
217 struct extract_name_from_vertex
|
Chris@16
|
218 {
|
Chris@16
|
219 typedef vertex_name_type result_type;
|
Chris@16
|
220
|
Chris@16
|
221 extract_name_from_vertex(Graph& graph, const extract_name_type& extract)
|
Chris@16
|
222 : graph(graph), extract(extract) { }
|
Chris@16
|
223
|
Chris@16
|
224 const result_type& operator()(Vertex vertex) const
|
Chris@16
|
225 {
|
Chris@16
|
226 return extract(graph[vertex]);
|
Chris@16
|
227 }
|
Chris@16
|
228
|
Chris@16
|
229 Graph& graph;
|
Chris@16
|
230 extract_name_type extract;
|
Chris@16
|
231 };
|
Chris@16
|
232
|
Chris@16
|
233 public:
|
Chris@16
|
234 /// The type that maps names to vertices
|
Chris@16
|
235 typedef multi_index::multi_index_container<
|
Chris@16
|
236 Vertex,
|
Chris@16
|
237 multi_index::indexed_by<
|
Chris@16
|
238 multi_index::hashed_unique<multi_index::tag<vertex_name_t>,
|
Chris@16
|
239 extract_name_from_vertex> >
|
Chris@16
|
240 > named_vertices_type;
|
Chris@16
|
241
|
Chris@16
|
242 /// The set of vertices, indexed by name
|
Chris@16
|
243 typedef typename named_vertices_type::template index<vertex_name_t>::type
|
Chris@16
|
244 vertices_by_name_type;
|
Chris@16
|
245
|
Chris@16
|
246 /// Construct an instance of the named graph mixin, using the given
|
Chris@16
|
247 /// function object to extract a name from the bundled property
|
Chris@16
|
248 /// associated with a vertex.
|
Chris@16
|
249 named_graph(const extract_name_type& extract = extract_name_type(),
|
Chris@16
|
250 const vertex_constructor_type& vertex_constructor
|
Chris@16
|
251 = vertex_constructor_type());
|
Chris@16
|
252
|
Chris@16
|
253 /// Notify the named_graph that we have added the given vertex. The
|
Chris@16
|
254 /// name of the vertex will be added to the mapping.
|
Chris@16
|
255 void added_vertex(Vertex vertex);
|
Chris@16
|
256
|
Chris@16
|
257 /// Notify the named_graph that we are removing the given
|
Chris@16
|
258 /// vertex. The name of the vertex will be removed from the mapping.
|
Chris@16
|
259 template <typename VertexIterStability>
|
Chris@16
|
260 void removing_vertex(Vertex vertex, VertexIterStability);
|
Chris@16
|
261
|
Chris@16
|
262 /// Notify the named_graph that we are clearing the graph.
|
Chris@16
|
263 /// This will clear out all of the name->vertex mappings
|
Chris@16
|
264 void clearing_graph();
|
Chris@16
|
265
|
Chris@16
|
266 /// Retrieve the derived instance
|
Chris@16
|
267 Graph& derived() { return static_cast<Graph&>(*this); }
|
Chris@16
|
268 const Graph& derived() const { return static_cast<const Graph&>(*this); }
|
Chris@16
|
269
|
Chris@16
|
270 /// Extract the name from a vertex property instance
|
Chris@16
|
271 typename extract_name_type::result_type
|
Chris@16
|
272 extract_name(const bundled_vertex_property_type& property);
|
Chris@16
|
273
|
Chris@16
|
274 /// Search for a vertex that has the given property (based on its
|
Chris@16
|
275 /// name)
|
Chris@16
|
276 optional<vertex_descriptor>
|
Chris@16
|
277 vertex_by_property(const bundled_vertex_property_type& property);
|
Chris@16
|
278
|
Chris@16
|
279 /// Mapping from names to vertices
|
Chris@16
|
280 named_vertices_type named_vertices;
|
Chris@16
|
281
|
Chris@16
|
282 /// Constructs a vertex from the name of that vertex
|
Chris@16
|
283 vertex_constructor_type vertex_constructor;
|
Chris@16
|
284 };
|
Chris@16
|
285
|
Chris@16
|
286 /// Helper macro containing the template parameters of named_graph
|
Chris@16
|
287 #define BGL_NAMED_GRAPH_PARAMS \
|
Chris@16
|
288 typename Graph, typename Vertex, typename VertexProperty
|
Chris@16
|
289 /// Helper macro containing the named_graph<...> instantiation
|
Chris@16
|
290 #define BGL_NAMED_GRAPH \
|
Chris@16
|
291 named_graph<Graph, Vertex, VertexProperty>
|
Chris@16
|
292
|
Chris@16
|
293 template<BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
294 BGL_NAMED_GRAPH::named_graph(const extract_name_type& extract,
|
Chris@16
|
295 const vertex_constructor_type& vertex_constructor)
|
Chris@16
|
296 : named_vertices(
|
Chris@16
|
297 typename named_vertices_type::ctor_args_list(
|
Chris@16
|
298 boost::make_tuple(
|
Chris@16
|
299 boost::make_tuple(
|
Chris@16
|
300 0, // initial number of buckets
|
Chris@16
|
301 extract_name_from_vertex(derived(), extract),
|
Chris@16
|
302 boost::hash<vertex_name_type>(),
|
Chris@16
|
303 std::equal_to<vertex_name_type>())))),
|
Chris@16
|
304 vertex_constructor(vertex_constructor)
|
Chris@16
|
305 {
|
Chris@16
|
306 }
|
Chris@16
|
307
|
Chris@16
|
308 template<BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
309 inline void BGL_NAMED_GRAPH::added_vertex(Vertex vertex)
|
Chris@16
|
310 {
|
Chris@16
|
311 named_vertices.insert(vertex);
|
Chris@16
|
312 }
|
Chris@16
|
313
|
Chris@16
|
314 template<BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
315 template<typename VertexIterStability>
|
Chris@16
|
316 inline void BGL_NAMED_GRAPH::removing_vertex(Vertex vertex, VertexIterStability)
|
Chris@16
|
317 {
|
Chris@16
|
318 BOOST_STATIC_ASSERT_MSG ((boost::is_base_of<boost::graph_detail::stable_tag, VertexIterStability>::value), "Named graphs cannot use vecS as vertex container and remove vertices; the lack of vertex descriptor stability (which iterator stability is a proxy for) means that the name -> vertex mapping would need to be completely rebuilt after each deletion. See https://svn.boost.org/trac/boost/ticket/7863 for more information and a test case.");
|
Chris@16
|
319 typedef typename BGL_NAMED_GRAPH::vertex_name_type vertex_name_type;
|
Chris@16
|
320 const vertex_name_type& vertex_name = extract_name(derived()[vertex]);
|
Chris@16
|
321 named_vertices.erase(vertex_name);
|
Chris@16
|
322 }
|
Chris@16
|
323
|
Chris@16
|
324 template<BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
325 inline void BGL_NAMED_GRAPH::clearing_graph()
|
Chris@16
|
326 {
|
Chris@16
|
327 named_vertices.clear();
|
Chris@16
|
328 }
|
Chris@16
|
329
|
Chris@16
|
330 template<BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
331 typename BGL_NAMED_GRAPH::extract_name_type::result_type
|
Chris@16
|
332 BGL_NAMED_GRAPH::extract_name(const bundled_vertex_property_type& property)
|
Chris@16
|
333 {
|
Chris@16
|
334 return named_vertices.key_extractor().extract(property);
|
Chris@16
|
335 }
|
Chris@16
|
336
|
Chris@16
|
337 template<BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
338 optional<typename BGL_NAMED_GRAPH::vertex_descriptor>
|
Chris@16
|
339 BGL_NAMED_GRAPH::
|
Chris@16
|
340 vertex_by_property(const bundled_vertex_property_type& property)
|
Chris@16
|
341 {
|
Chris@16
|
342 return find_vertex(extract_name(property), *this);
|
Chris@16
|
343 }
|
Chris@16
|
344
|
Chris@16
|
345 /// Retrieve the vertex associated with the given name
|
Chris@16
|
346 template<BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
347 optional<Vertex>
|
Chris@16
|
348 find_vertex(typename BGL_NAMED_GRAPH::vertex_name_type const& name,
|
Chris@16
|
349 const BGL_NAMED_GRAPH& g)
|
Chris@16
|
350 {
|
Chris@16
|
351 typedef typename BGL_NAMED_GRAPH::vertices_by_name_type
|
Chris@16
|
352 vertices_by_name_type;
|
Chris@16
|
353
|
Chris@16
|
354 // Retrieve the set of vertices indexed by name
|
Chris@16
|
355 vertices_by_name_type const& vertices_by_name
|
Chris@16
|
356 = g.named_vertices.template get<vertex_name_t>();
|
Chris@16
|
357
|
Chris@16
|
358 /// Look for a vertex with the given name
|
Chris@16
|
359 typename vertices_by_name_type::const_iterator iter
|
Chris@16
|
360 = vertices_by_name.find(name);
|
Chris@16
|
361
|
Chris@16
|
362 if (iter == vertices_by_name.end())
|
Chris@16
|
363 return optional<Vertex>(); // vertex not found
|
Chris@16
|
364 else
|
Chris@16
|
365 return *iter;
|
Chris@16
|
366 }
|
Chris@16
|
367
|
Chris@16
|
368 /// Retrieve the vertex associated with the given name, or add a new
|
Chris@16
|
369 /// vertex with that name if no such vertex is available.
|
Chris@16
|
370 /// Note: This is enabled only when the vertex property type is different
|
Chris@16
|
371 /// from the vertex name to avoid ambiguous overload problems with
|
Chris@16
|
372 /// the add_vertex() function that takes a vertex property.
|
Chris@16
|
373 template<BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
374 typename disable_if<is_same<
|
Chris@16
|
375 typename BGL_NAMED_GRAPH::vertex_name_type,
|
Chris@16
|
376 VertexProperty
|
Chris@16
|
377 >,
|
Chris@16
|
378 Vertex>::type
|
Chris@16
|
379 add_vertex(typename BGL_NAMED_GRAPH::vertex_name_type const& name,
|
Chris@16
|
380 BGL_NAMED_GRAPH& g)
|
Chris@16
|
381 {
|
Chris@16
|
382 if (optional<Vertex> vertex = find_vertex(name, g))
|
Chris@16
|
383 /// We found the vertex, so return it
|
Chris@16
|
384 return *vertex;
|
Chris@16
|
385 else
|
Chris@16
|
386 /// There is no vertex with the given name, so create one
|
Chris@16
|
387 return add_vertex(g.vertex_constructor(name), g.derived());
|
Chris@16
|
388 }
|
Chris@16
|
389
|
Chris@16
|
390 /// Add an edge using vertex names to refer to the vertices
|
Chris@16
|
391 template<BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
392 std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
|
Chris@16
|
393 add_edge(typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
|
Chris@16
|
394 typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
|
Chris@16
|
395 BGL_NAMED_GRAPH& g)
|
Chris@16
|
396 {
|
Chris@16
|
397 return add_edge(add_vertex(u_name, g.derived()),
|
Chris@16
|
398 add_vertex(v_name, g.derived()),
|
Chris@16
|
399 g.derived());
|
Chris@16
|
400 }
|
Chris@16
|
401
|
Chris@16
|
402 /// Add an edge using vertex descriptors or names to refer to the vertices
|
Chris@16
|
403 template<BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
404 std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
|
Chris@16
|
405 add_edge(typename BGL_NAMED_GRAPH::vertex_descriptor const& u,
|
Chris@16
|
406 typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
|
Chris@16
|
407 BGL_NAMED_GRAPH& g)
|
Chris@16
|
408 {
|
Chris@16
|
409 return add_edge(u,
|
Chris@16
|
410 add_vertex(v_name, g.derived()),
|
Chris@16
|
411 g.derived());
|
Chris@16
|
412 }
|
Chris@16
|
413
|
Chris@16
|
414 /// Add an edge using vertex descriptors or names to refer to the vertices
|
Chris@16
|
415 template<BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
416 std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
|
Chris@16
|
417 add_edge(typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
|
Chris@16
|
418 typename BGL_NAMED_GRAPH::vertex_descriptor const& v,
|
Chris@16
|
419 BGL_NAMED_GRAPH& g)
|
Chris@16
|
420 {
|
Chris@16
|
421 return add_edge(add_vertex(u_name, g.derived()),
|
Chris@16
|
422 v,
|
Chris@16
|
423 g.derived());
|
Chris@16
|
424 }
|
Chris@16
|
425
|
Chris@16
|
426 // Overloads to support EdgeMutablePropertyGraph graphs
|
Chris@16
|
427 template <BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
428 std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
|
Chris@16
|
429 add_edge(typename BGL_NAMED_GRAPH::vertex_descriptor const& u,
|
Chris@16
|
430 typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
|
Chris@16
|
431 typename edge_property_type<Graph>::type const& p,
|
Chris@16
|
432 BGL_NAMED_GRAPH& g) {
|
Chris@16
|
433 return add_edge(u, add_vertex(v_name, g.derived()), p, g.derived());
|
Chris@16
|
434 }
|
Chris@16
|
435
|
Chris@16
|
436 template <BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
437 std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
|
Chris@16
|
438 add_edge(typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
|
Chris@16
|
439 typename BGL_NAMED_GRAPH::vertex_descriptor const& v,
|
Chris@16
|
440 typename edge_property_type<Graph>::type const& p,
|
Chris@16
|
441 BGL_NAMED_GRAPH& g) {
|
Chris@16
|
442 return add_edge(add_vertex(u_name, g.derived()), v, p, g.derived());
|
Chris@16
|
443 }
|
Chris@16
|
444
|
Chris@16
|
445 template <BGL_NAMED_GRAPH_PARAMS>
|
Chris@16
|
446 std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
|
Chris@16
|
447 add_edge(typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
|
Chris@16
|
448 typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
|
Chris@16
|
449 typename edge_property_type<Graph>::type const& p,
|
Chris@16
|
450 BGL_NAMED_GRAPH& g) {
|
Chris@16
|
451 return add_edge(add_vertex(u_name, g.derived()),
|
Chris@16
|
452 add_vertex(v_name, g.derived()), p, g.derived());
|
Chris@16
|
453 }
|
Chris@16
|
454
|
Chris@16
|
455 #undef BGL_NAMED_GRAPH
|
Chris@16
|
456 #undef BGL_NAMED_GRAPH_PARAMS
|
Chris@16
|
457
|
Chris@16
|
458 /*******************************************************************
|
Chris@16
|
459 * Maybe named graph mixin *
|
Chris@16
|
460 *******************************************************************/
|
Chris@16
|
461
|
Chris@16
|
462 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
Chris@16
|
463 /**
|
Chris@16
|
464 * A graph mixin that can provide a mapping from names to vertices,
|
Chris@16
|
465 * and use that mapping to simplify creation and manipulation of
|
Chris@16
|
466 * graphs.
|
Chris@16
|
467 */
|
Chris@16
|
468 template<typename Graph, typename Vertex, typename VertexProperty,
|
Chris@16
|
469 typename ExtractName
|
Chris@16
|
470 = typename internal_vertex_name<VertexProperty>::type>
|
Chris@16
|
471 struct maybe_named_graph : public named_graph<Graph, Vertex, VertexProperty>
|
Chris@16
|
472 {
|
Chris@16
|
473 };
|
Chris@16
|
474
|
Chris@16
|
475 /**
|
Chris@16
|
476 * A graph mixin that can provide a mapping from names to vertices,
|
Chris@16
|
477 * and use that mapping to simplify creation and manipulation of
|
Chris@16
|
478 * graphs. This partial specialization turns off this functionality
|
Chris@16
|
479 * when the @c VertexProperty does not have an internal vertex name.
|
Chris@16
|
480 */
|
Chris@16
|
481 template<typename Graph, typename Vertex, typename VertexProperty>
|
Chris@16
|
482 struct maybe_named_graph<Graph, Vertex, VertexProperty, void>
|
Chris@16
|
483 {
|
Chris@16
|
484 /// The type of the "bundled" property, from which the name can be
|
Chris@16
|
485 /// extracted.
|
Chris@16
|
486 typedef typename lookup_one_property<VertexProperty, vertex_bundle_t>::type
|
Chris@16
|
487 bundled_vertex_property_type;
|
Chris@16
|
488
|
Chris@16
|
489 /// Notify the named_graph that we have added the given vertex. This
|
Chris@16
|
490 /// is a no-op.
|
Chris@16
|
491 void added_vertex(Vertex) { }
|
Chris@16
|
492
|
Chris@16
|
493 /// Notify the named_graph that we are removing the given
|
Chris@16
|
494 /// vertex. This is a no-op.
|
Chris@16
|
495 template <typename VertexIterStability>
|
Chris@16
|
496 void removing_vertex(Vertex, VertexIterStability) { }
|
Chris@16
|
497
|
Chris@16
|
498 /// Notify the named_graph that we are clearing the graph. This is a
|
Chris@16
|
499 /// no-op.
|
Chris@16
|
500 void clearing_graph() { }
|
Chris@16
|
501
|
Chris@16
|
502 /// Search for a vertex that has the given property (based on its
|
Chris@16
|
503 /// name). This always returns an empty optional<>
|
Chris@16
|
504 optional<Vertex>
|
Chris@16
|
505 vertex_by_property(const bundled_vertex_property_type&)
|
Chris@16
|
506 {
|
Chris@16
|
507 return optional<Vertex>();
|
Chris@16
|
508 }
|
Chris@16
|
509 };
|
Chris@16
|
510 #else
|
Chris@16
|
511 template<typename Graph, typename Vertex, typename VertexProperty,
|
Chris@16
|
512 typename ExtractName
|
Chris@16
|
513 = typename internal_vertex_name<VertexProperty>::type>
|
Chris@16
|
514 struct maybe_named_graph
|
Chris@16
|
515 {
|
Chris@16
|
516 /// The type of the "bundled" property, from which the name can be
|
Chris@16
|
517 /// extracted.
|
Chris@16
|
518 typedef typename detail::extract_bundled_vertex<VertexProperty>::type
|
Chris@16
|
519 bundled_vertex_property_type;
|
Chris@16
|
520
|
Chris@16
|
521 /// Notify the named_graph that we have added the given vertex. This
|
Chris@16
|
522 /// is a no-op.
|
Chris@16
|
523 void added_vertex(Vertex) { }
|
Chris@16
|
524
|
Chris@16
|
525 /// Notify the named_graph that we are removing the given
|
Chris@16
|
526 /// vertex. This is a no-op.
|
Chris@16
|
527 template <typename VertexIterStability>
|
Chris@16
|
528 void removing_vertex(Vertex, VertexIterStability) { }
|
Chris@16
|
529
|
Chris@16
|
530 /// Notify the named_graph that we are clearing the graph. This is a
|
Chris@16
|
531 /// no-op.
|
Chris@16
|
532 void clearing_graph() { }
|
Chris@16
|
533
|
Chris@16
|
534 /// Search for a vertex that has the given property (based on its
|
Chris@16
|
535 /// name). This always returns an empty optional<>
|
Chris@16
|
536 template<typename Property>
|
Chris@16
|
537 optional<Vertex>
|
Chris@16
|
538 vertex_by_property(const bundled_vertex_property_type&)
|
Chris@16
|
539 {
|
Chris@16
|
540 return optional<Vertex>();
|
Chris@16
|
541 }
|
Chris@16
|
542 };
|
Chris@16
|
543 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
Chris@16
|
544
|
Chris@16
|
545 } } // end namespace boost::graph
|
Chris@16
|
546
|
Chris@16
|
547 #endif // BOOST_GRAPH_NAMED_GRAPH_HPP
|