named_graph.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 { namespace graph {
  32. /*******************************************************************
  33. * User-customized traits *
  34. *******************************************************************/
  35. /**
  36. * @brief Trait used to extract the internal vertex name from a vertex
  37. * property.
  38. *
  39. * To enable the use of internal vertex names in a graph type,
  40. * specialize the @c internal_vertex_name trait for your graph
  41. * property (e.g., @c a City class, which stores information about the
  42. * vertices in a road map).
  43. */
  44. template<typename VertexProperty>
  45. struct internal_vertex_name
  46. {
  47. /**
  48. * The @c type field provides a function object that extracts a key
  49. * from the @c VertexProperty. The function object type must have a
  50. * nested @c result_type that provides the type of the key. For
  51. * more information, see the @c KeyExtractor concept in the
  52. * Boost.MultiIndex documentation: @c type must either be @c void
  53. * (if @c VertexProperty does not have an internal vertex name) or
  54. * a model of @c KeyExtractor.
  55. */
  56. typedef void type;
  57. };
  58. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  59. /**
  60. * Extract the internal vertex name from a @c property structure by
  61. * looking at its base.
  62. */
  63. template<typename Tag, typename T, typename Base>
  64. struct internal_vertex_name<property<Tag, T, Base> >
  65. : internal_vertex_name<Base> { };
  66. #endif
  67. /**
  68. * Construct an instance of @c VertexProperty directly from its
  69. * name. This function object should be used within the @c
  70. * internal_vertex_constructor trait.
  71. */
  72. template<typename VertexProperty>
  73. struct vertex_from_name
  74. {
  75. private:
  76. typedef typename internal_vertex_name<VertexProperty>::type extract_name_type;
  77. typedef typename remove_cv<
  78. typename remove_reference<
  79. typename extract_name_type::result_type>::type>::type
  80. vertex_name_type;
  81. public:
  82. typedef vertex_name_type argument_type;
  83. typedef VertexProperty result_type;
  84. VertexProperty operator()(const vertex_name_type& name)
  85. {
  86. return VertexProperty(name);
  87. }
  88. };
  89. /**
  90. * Throw an exception whenever one tries to construct a @c
  91. * VertexProperty instance from its name.
  92. */
  93. template<typename VertexProperty>
  94. struct cannot_add_vertex
  95. {
  96. private:
  97. typedef typename internal_vertex_name<VertexProperty>::type extract_name_type;
  98. typedef typename remove_cv<
  99. 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(std::runtime_error("add_vertex: "
  108. "unable to create a vertex from its name"));
  109. }
  110. };
  111. /**
  112. * @brief Trait used to construct an instance of a @c VertexProperty,
  113. * which is a class type that stores the properties associated with a
  114. * vertex in a graph, from just the name of that vertex property. This
  115. * operation is used when an operation is required to map from a
  116. * vertex name to a vertex descriptor (e.g., to add an edge outgoing
  117. * from that vertex), but no vertex by the name exists. The function
  118. * object provided by this trait will be used to add new vertices
  119. * based only on their names. Since this cannot be done for arbitrary
  120. * types, the default behavior is to throw an exception when this
  121. * routine is called, which requires that all named vertices be added
  122. * before adding any edges by name.
  123. */
  124. template<typename VertexProperty>
  125. 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. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  144. /**
  145. * Extract the internal vertex constructor from a @c property structure by
  146. * looking at its base.
  147. */
  148. template<typename Tag, typename T, typename Base>
  149. struct internal_vertex_constructor<property<Tag, T, Base> >
  150. : internal_vertex_constructor<Base> { };
  151. #endif
  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 extract_name_type;
  177. /// The type of the "bundled" property, from which the name can be
  178. /// extracted.
  179. typedef typename lookup_one_property<VertexProperty, vertex_bundle_t>::type
  180. bundled_vertex_property_type;
  181. /// The type of the function object that generates vertex properties
  182. /// from names, for the implicit addition of vertices.
  183. typedef typename internal_vertex_constructor<VertexProperty>::type
  184. vertex_constructor_type;
  185. /// The type used to name vertices in the graph
  186. typedef typename remove_cv<
  187. 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(Graph& graph, const extract_name_type& extract)
  198. : graph(graph), extract(extract) { }
  199. const result_type& operator()(Vertex vertex) const
  200. {
  201. return extract(graph[vertex]);
  202. }
  203. Graph& graph;
  204. extract_name_type extract;
  205. };
  206. public:
  207. /// The type that maps names to vertices
  208. typedef multi_index::multi_index_container<
  209. Vertex,
  210. multi_index::indexed_by<
  211. multi_index::hashed_unique<multi_index::tag<vertex_name_t>,
  212. extract_name_from_vertex> >
  213. > named_vertices_type;
  214. /// The set of vertices, indexed by name
  215. typedef typename named_vertices_type::template index<vertex_name_t>::type
  216. vertices_by_name_type;
  217. /// Construct an instance of the named graph mixin, using the given
  218. /// function object to extract a name from the bundled property
  219. /// associated with a vertex.
  220. named_graph(const extract_name_type& extract = extract_name_type(),
  221. const vertex_constructor_type& vertex_constructor
  222. = vertex_constructor_type());
  223. /// Notify the named_graph that we have added the given vertex. The
  224. /// name of the vertex will be added to the mapping.
  225. void added_vertex(Vertex vertex);
  226. /// Notify the named_graph that we are removing the given
  227. /// vertex. The name of the vertex will be removed from the mapping.
  228. template <typename VertexIterStability>
  229. void removing_vertex(Vertex vertex, VertexIterStability);
  230. /// Notify the named_graph that we are clearing the graph.
  231. /// This will clear out all of the name->vertex mappings
  232. void clearing_graph();
  233. /// Retrieve the derived instance
  234. Graph& derived() { return static_cast<Graph&>(*this); }
  235. const Graph& derived() const { return static_cast<const Graph&>(*this); }
  236. /// Extract the name from a vertex property instance
  237. typename extract_name_type::result_type
  238. extract_name(const bundled_vertex_property_type& property);
  239. /// Search for a vertex that has the given property (based on its
  240. /// name)
  241. optional<vertex_descriptor>
  242. vertex_by_property(const bundled_vertex_property_type& property);
  243. /// Mapping from names to vertices
  244. named_vertices_type named_vertices;
  245. /// Constructs a vertex from the name of that vertex
  246. vertex_constructor_type vertex_constructor;
  247. };
  248. /// Helper macro containing the template parameters of named_graph
  249. #define BGL_NAMED_GRAPH_PARAMS \
  250. typename Graph, typename Vertex, typename VertexProperty
  251. /// Helper macro containing the named_graph<...> instantiation
  252. #define BGL_NAMED_GRAPH \
  253. named_graph<Graph, Vertex, VertexProperty>
  254. template<BGL_NAMED_GRAPH_PARAMS>
  255. BGL_NAMED_GRAPH::named_graph(const extract_name_type& extract,
  256. const vertex_constructor_type& vertex_constructor)
  257. : named_vertices(
  258. typename named_vertices_type::ctor_args_list(
  259. boost::make_tuple(
  260. boost::make_tuple(
  261. 0, // initial number of buckets
  262. extract_name_from_vertex(derived(), extract),
  263. boost::hash<vertex_name_type>(),
  264. std::equal_to<vertex_name_type>())))),
  265. vertex_constructor(vertex_constructor)
  266. {
  267. }
  268. template<BGL_NAMED_GRAPH_PARAMS>
  269. inline void BGL_NAMED_GRAPH::added_vertex(Vertex vertex)
  270. {
  271. named_vertices.insert(vertex);
  272. }
  273. template<BGL_NAMED_GRAPH_PARAMS>
  274. template<typename VertexIterStability>
  275. inline void BGL_NAMED_GRAPH::removing_vertex(Vertex vertex, VertexIterStability)
  276. {
  277. 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.");
  278. typedef typename BGL_NAMED_GRAPH::vertex_name_type vertex_name_type;
  279. const vertex_name_type& vertex_name = extract_name(derived()[vertex]);
  280. named_vertices.erase(vertex_name);
  281. }
  282. template<BGL_NAMED_GRAPH_PARAMS>
  283. inline void BGL_NAMED_GRAPH::clearing_graph()
  284. {
  285. named_vertices.clear();
  286. }
  287. template<BGL_NAMED_GRAPH_PARAMS>
  288. typename BGL_NAMED_GRAPH::extract_name_type::result_type
  289. BGL_NAMED_GRAPH::extract_name(const bundled_vertex_property_type& property)
  290. {
  291. return named_vertices.key_extractor().extract(property);
  292. }
  293. template<BGL_NAMED_GRAPH_PARAMS>
  294. optional<typename BGL_NAMED_GRAPH::vertex_descriptor>
  295. BGL_NAMED_GRAPH::
  296. vertex_by_property(const bundled_vertex_property_type& property)
  297. {
  298. return find_vertex(extract_name(property), *this);
  299. }
  300. /// Retrieve the vertex associated with the given name
  301. template<BGL_NAMED_GRAPH_PARAMS>
  302. optional<Vertex>
  303. find_vertex(typename BGL_NAMED_GRAPH::vertex_name_type const& name,
  304. const BGL_NAMED_GRAPH& g)
  305. {
  306. typedef typename BGL_NAMED_GRAPH::vertices_by_name_type
  307. vertices_by_name_type;
  308. // Retrieve the set of vertices indexed by name
  309. vertices_by_name_type const& vertices_by_name
  310. = g.named_vertices.template get<vertex_name_t>();
  311. /// Look for a vertex with the given name
  312. typename vertices_by_name_type::const_iterator iter
  313. = vertices_by_name.find(name);
  314. if (iter == vertices_by_name.end())
  315. return optional<Vertex>(); // vertex not found
  316. else
  317. return *iter;
  318. }
  319. /// Retrieve the vertex associated with the given name, or add a new
  320. /// vertex with that name if no such vertex is available.
  321. /// Note: This is enabled only when the vertex property type is different
  322. /// from the vertex name to avoid ambiguous overload problems with
  323. /// the add_vertex() function that takes a vertex property.
  324. template<BGL_NAMED_GRAPH_PARAMS>
  325. typename disable_if<is_same<
  326. typename BGL_NAMED_GRAPH::vertex_name_type,
  327. VertexProperty
  328. >,
  329. Vertex>::type
  330. add_vertex(typename BGL_NAMED_GRAPH::vertex_name_type const& name,
  331. BGL_NAMED_GRAPH& g)
  332. {
  333. if (optional<Vertex> vertex = find_vertex(name, g))
  334. /// We found the vertex, so return it
  335. return *vertex;
  336. else
  337. /// There is no vertex with the given name, so create one
  338. return add_vertex(g.vertex_constructor(name), g.derived());
  339. }
  340. /// Add an edge using vertex names to refer to the vertices
  341. template<BGL_NAMED_GRAPH_PARAMS>
  342. std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
  343. add_edge(typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
  344. typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
  345. BGL_NAMED_GRAPH& g)
  346. {
  347. return add_edge(add_vertex(u_name, g.derived()),
  348. add_vertex(v_name, g.derived()),
  349. g.derived());
  350. }
  351. /// Add an edge using vertex descriptors or names to refer to the vertices
  352. template<BGL_NAMED_GRAPH_PARAMS>
  353. std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
  354. add_edge(typename BGL_NAMED_GRAPH::vertex_descriptor const& u,
  355. typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
  356. BGL_NAMED_GRAPH& g)
  357. {
  358. return add_edge(u,
  359. add_vertex(v_name, g.derived()),
  360. g.derived());
  361. }
  362. /// Add an edge using vertex descriptors or names to refer to the vertices
  363. template<BGL_NAMED_GRAPH_PARAMS>
  364. std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
  365. add_edge(typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
  366. typename BGL_NAMED_GRAPH::vertex_descriptor const& v,
  367. BGL_NAMED_GRAPH& g)
  368. {
  369. return add_edge(add_vertex(u_name, g.derived()),
  370. v,
  371. g.derived());
  372. }
  373. // Overloads to support EdgeMutablePropertyGraph graphs
  374. template <BGL_NAMED_GRAPH_PARAMS>
  375. std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
  376. add_edge(typename BGL_NAMED_GRAPH::vertex_descriptor const& u,
  377. typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
  378. typename edge_property_type<Graph>::type const& p,
  379. BGL_NAMED_GRAPH& g) {
  380. return add_edge(u, add_vertex(v_name, g.derived()), p, g.derived());
  381. }
  382. template <BGL_NAMED_GRAPH_PARAMS>
  383. std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
  384. add_edge(typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
  385. typename BGL_NAMED_GRAPH::vertex_descriptor const& v,
  386. typename edge_property_type<Graph>::type const& p,
  387. BGL_NAMED_GRAPH& g) {
  388. return add_edge(add_vertex(u_name, g.derived()), v, p, g.derived());
  389. }
  390. template <BGL_NAMED_GRAPH_PARAMS>
  391. std::pair<typename graph_traits<Graph>::edge_descriptor, bool>
  392. add_edge(typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
  393. typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
  394. typename edge_property_type<Graph>::type const& p,
  395. BGL_NAMED_GRAPH& g) {
  396. return add_edge(add_vertex(u_name, g.derived()),
  397. add_vertex(v_name, g.derived()), p, g.derived());
  398. }
  399. #undef BGL_NAMED_GRAPH
  400. #undef BGL_NAMED_GRAPH_PARAMS
  401. /*******************************************************************
  402. * Maybe named graph mixin *
  403. *******************************************************************/
  404. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  405. /**
  406. * A graph mixin that can provide a mapping from names to vertices,
  407. * and use that mapping to simplify creation and manipulation of
  408. * graphs.
  409. */
  410. template<typename Graph, typename Vertex, typename VertexProperty,
  411. typename ExtractName
  412. = typename internal_vertex_name<VertexProperty>::type>
  413. struct maybe_named_graph : public named_graph<Graph, Vertex, VertexProperty>
  414. {
  415. };
  416. /**
  417. * A graph mixin that can provide a mapping from names to vertices,
  418. * and use that mapping to simplify creation and manipulation of
  419. * graphs. This partial specialization turns off this functionality
  420. * when the @c VertexProperty does not have an internal vertex name.
  421. */
  422. template<typename Graph, typename Vertex, typename VertexProperty>
  423. struct maybe_named_graph<Graph, Vertex, VertexProperty, void>
  424. {
  425. /// The type of the "bundled" property, from which the name can be
  426. /// extracted.
  427. typedef typename lookup_one_property<VertexProperty, vertex_bundle_t>::type
  428. bundled_vertex_property_type;
  429. /// Notify the named_graph that we have added the given vertex. This
  430. /// is a no-op.
  431. void added_vertex(Vertex) { }
  432. /// Notify the named_graph that we are removing the given
  433. /// vertex. This is a no-op.
  434. template <typename VertexIterStability>
  435. void removing_vertex(Vertex, VertexIterStability) { }
  436. /// Notify the named_graph that we are clearing the graph. This is a
  437. /// no-op.
  438. void clearing_graph() { }
  439. /// Search for a vertex that has the given property (based on its
  440. /// name). This always returns an empty optional<>
  441. optional<Vertex>
  442. vertex_by_property(const bundled_vertex_property_type&)
  443. {
  444. return optional<Vertex>();
  445. }
  446. };
  447. #else
  448. template<typename Graph, typename Vertex, typename VertexProperty,
  449. typename ExtractName
  450. = typename internal_vertex_name<VertexProperty>::type>
  451. struct maybe_named_graph
  452. {
  453. /// The type of the "bundled" property, from which the name can be
  454. /// extracted.
  455. typedef typename detail::extract_bundled_vertex<VertexProperty>::type
  456. bundled_vertex_property_type;
  457. /// Notify the named_graph that we have added the given vertex. This
  458. /// is a no-op.
  459. void added_vertex(Vertex) { }
  460. /// Notify the named_graph that we are removing the given
  461. /// vertex. This is a no-op.
  462. template <typename VertexIterStability>
  463. void removing_vertex(Vertex, VertexIterStability) { }
  464. /// Notify the named_graph that we are clearing the graph. This is a
  465. /// no-op.
  466. void clearing_graph() { }
  467. /// Search for a vertex that has the given property (based on its
  468. /// name). This always returns an empty optional<>
  469. template<typename Property>
  470. optional<Vertex>
  471. vertex_by_property(const bundled_vertex_property_type&)
  472. {
  473. return optional<Vertex>();
  474. }
  475. };
  476. #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  477. } } // end namespace boost::graph
  478. #endif // BOOST_GRAPH_NAMED_GRAPH_HPP