named_graph.hpp 18 KB

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