named_graph.hpp 20 KB

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