/Src/Dependencies/Boost/boost/graph/labeled_graph.hpp

http://hadesmem.googlecode.com/ · C++ Header · 813 lines · 509 code · 123 blank · 181 comment · 7 complexity · 7a039821a8e5d22f4ed9ddd1b2936016 MD5 · raw file

  1. // Copyright (C) 2009 Andrew Sutton
  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. #ifndef BOOST_GRAPH_LABELED_GRAPH_HPP
  6. #define BOOST_GRAPH_LABELED_GRAPH_HPP
  7. #include <boost/config.hpp>
  8. #include <vector>
  9. #include <map>
  10. #include <boost/static_assert.hpp>
  11. #include <boost/mpl/if.hpp>
  12. #include <boost/mpl/bool.hpp>
  13. #include <boost/unordered_map.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. #include <boost/type_traits/is_unsigned.hpp>
  16. #include <boost/pending/container_traits.hpp>
  17. #include <boost/graph/graph_traits.hpp>
  18. // This file implements a utility for creating mappings from arbitrary
  19. // identifers to the vertices of a graph.
  20. namespace boost {
  21. // A type selector that denotes the use of some default value.
  22. struct defaultS { };
  23. /** @internal */
  24. namespace graph_detail {
  25. /** Returns true if the selector is the default selector. */
  26. template <typename Selector>
  27. struct is_default
  28. : mpl::bool_<is_same<Selector, defaultS>::value>
  29. { };
  30. /**
  31. * Choose the default map instance. If Label is an unsigned integral type
  32. * the we can use a vector to store the information.
  33. */
  34. template <typename Label, typename Vertex>
  35. struct choose_default_map {
  36. typedef typename mpl::if_<
  37. is_unsigned<Label>,
  38. std::vector<Vertex>,
  39. std::map<Label, Vertex> // TODO: Should use unordered_map?
  40. >::type type;
  41. };
  42. /**
  43. * @name Generate Label Map
  44. * These type generators are responsible for instantiating an associative
  45. * container for the the labeled graph. Note that the Selector must be
  46. * select a pair associative container or a vecS, which is only valid if
  47. * Label is an integral type.
  48. */
  49. //@{
  50. template <typename Selector, typename Label, typename Vertex>
  51. struct generate_label_map { };
  52. template <typename Label, typename Vertex>
  53. struct generate_label_map<vecS, Label, Vertex>
  54. { typedef std::vector<Vertex> type; };
  55. template <typename Label, typename Vertex>
  56. struct generate_label_map<mapS, Label, Vertex>
  57. { typedef std::map<Label, Vertex> type; };
  58. template <typename Label, typename Vertex>
  59. struct generate_label_map<multimapS, Label, Vertex>
  60. { typedef std::multimap<Label, Vertex> type; };
  61. template <typename Label, typename Vertex>
  62. struct generate_label_map<hash_mapS, Label, Vertex>
  63. { typedef boost::unordered_map<Label, Vertex> type; };
  64. template <typename Label, typename Vertex>
  65. struct generate_label_map<hash_multimapS, Label, Vertex>
  66. { typedef boost::unordered_multimap<Label, Vertex> type; };
  67. template <typename Selector, typename Label, typename Vertex>
  68. struct choose_custom_map {
  69. typedef typename generate_label_map<Selector, Label, Vertex>::type type;
  70. };
  71. //@}
  72. /**
  73. * Choose and instantiate an "associative" container. Note that this can
  74. * also choose vector.
  75. */
  76. template <typename Selector, typename Label, typename Vertex>
  77. struct choose_map {
  78. typedef typename mpl::eval_if<
  79. is_default<Selector>,
  80. choose_default_map<Label, Vertex>,
  81. choose_custom_map<Selector, Label, Vertex>
  82. >::type type;
  83. };
  84. /** @name Insert Labeled Vertex */
  85. //@{
  86. // Tag dispatch on random access containers (i.e., vectors). This function
  87. // basically requires a) that Container is vector<Label> and that Label
  88. // is an unsigned integral value. Note that this will resize the vector
  89. // to accomodate indices.
  90. template <typename Container, typename Graph, typename Label, typename Prop>
  91. std::pair<typename graph_traits<Graph>::vertex_descriptor, bool>
  92. insert_labeled_vertex(Container& c, Graph& g, Label const& l, Prop const& p,
  93. random_access_container_tag)
  94. {
  95. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  96. // If the label is out of bounds, resize the vector to accomodate.
  97. // Resize by 2x the index so we don't cause quadratic insertions over
  98. // time.
  99. if(l >= c.size()) {
  100. c.resize((l + 1) * 2);
  101. }
  102. Vertex v = add_vertex(p, g);
  103. c[l] = v;
  104. return std::make_pair(c[l], true);
  105. }
  106. // Tag dispatch on multi associative containers (i.e. multimaps).
  107. template <typename Container, typename Graph, typename Label, typename Prop>
  108. std::pair<typename graph_traits<Graph>::vertex_descriptor, bool>
  109. insert_labeled_vertex(Container& c, Graph& g, Label const& l, Prop const& p,
  110. multiple_associative_container_tag const&)
  111. {
  112. // Note that insertion always succeeds so we can add the vertex first
  113. // and then the mapping to the label.
  114. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  115. Vertex v = add_vertex(g);
  116. c.insert(std::make_pair(l, v));
  117. return std::make_pair(v, true);
  118. }
  119. // Tag dispatch on unique associative containers (i.e. maps).
  120. template <typename Container, typename Graph, typename Label, typename Prop>
  121. std::pair<typename graph_traits<Graph>::vertex_descriptor, bool>
  122. insert_labeled_vertex(Container& c, Graph& g, Label const& l, Prop const&,
  123. unique_associative_container_tag)
  124. {
  125. // Here, we actually have to try the insertion first, and only add
  126. // the vertex if we get a new element.
  127. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  128. typedef typename Container::iterator Iterator;
  129. std::pair<Iterator, bool> x = c.insert(std::make_pair(l, Vertex()));
  130. if(x.second) {
  131. x.first->second = add_vertex(g);
  132. }
  133. return std::make_pair(x.first->second, x.second);
  134. }
  135. // Dispatcher
  136. template <typename Container, typename Graph, typename Label, typename Prop>
  137. std::pair<typename graph_traits<Graph>::vertex_descriptor, bool>
  138. insert_labeled_vertex(Container& c, Graph& g, Label const& l, Prop const& p)
  139. { return insert_labeled_vertex(c, g, l, p, container_category(c)); }
  140. //@}
  141. /** @name Find Labeled Vertex */
  142. //@{
  143. // Tag dispatch for sequential maps (i.e., vectors).
  144. template <typename Container, typename Graph, typename Label>
  145. typename graph_traits<Graph>::vertex_descriptor
  146. find_labeled_vertex(Container const& c, Graph const&, Label const& l,
  147. random_access_container_tag)
  148. { return l < c.size() ? c[l] : graph_traits<Graph>::null_vertex(); }
  149. // Tag dispatch for pair associative maps (more or less).
  150. template <typename Container, typename Graph, typename Label>
  151. typename graph_traits<Graph>::vertex_descriptor
  152. find_labeled_vertex(Container const& c, Graph const&, Label const& l,
  153. associative_container_tag)
  154. {
  155. typename Container::const_iterator i = c.find(l);
  156. return i != c.end() ? i->second : graph_traits<Graph>::null_vertex();
  157. }
  158. // Dispatcher
  159. template <typename Container, typename Graph, typename Label>
  160. typename graph_traits<Graph>::vertex_descriptor
  161. find_labeled_vertex(Container const& c, Graph const& g, Label const& l)
  162. { return find_labeled_vertex(c, g, l, container_category(c)); }
  163. //@}
  164. /** @name Put Vertex Label */
  165. //@{
  166. // Tag dispatch on vectors.
  167. template <typename Container, typename Label, typename Graph, typename Vertex>
  168. bool put_vertex_label(Container& c, Graph const&, Label const& l, Vertex v,
  169. random_access_container_tag)
  170. {
  171. // If the element is already occupied, then we probably don't want to
  172. // overwrite it.
  173. if(c[l] == Graph::null_vertex()) return false;
  174. c[l] = v;
  175. return true;
  176. }
  177. // Attempt the insertion and return its result.
  178. template <typename Container, typename Label, typename Graph, typename Vertex>
  179. bool put_vertex_label(Container& c, Graph const&, Label const& l, Vertex v,
  180. unique_associative_container_tag)
  181. { return c.insert(std::make_pair(l, v)).second; }
  182. // Insert the pair and return true.
  183. template <typename Container, typename Label, typename Graph, typename Vertex>
  184. bool put_vertex_label(Container& c, Graph const&, Label const& l, Vertex v,
  185. multiple_associative_container_tag)
  186. {
  187. c.insert(std::make_pair(l, v));
  188. return true;
  189. }
  190. // Dispatcher
  191. template <typename Container, typename Label, typename Graph, typename Vertex>
  192. bool put_vertex_label(Container& c, Graph const& g, Label const& l, Vertex v)
  193. { return put_vertex_label(c, g, l, v, container_category(c)); }
  194. //@}
  195. } // namespace detail
  196. struct labeled_graph_class_tag { };
  197. /** @internal
  198. * This class is responsible for the deduction and declaration of type names
  199. * for the labeled_graph class template.
  200. */
  201. template <typename Graph, typename Label, typename Selector>
  202. struct labeled_graph_types {
  203. typedef Graph graph_type;
  204. // Label and maps
  205. typedef Label label_type;
  206. typedef typename graph_detail::choose_map<
  207. Selector, Label, typename graph_traits<Graph>::vertex_descriptor
  208. >::type map_type;
  209. };
  210. /**
  211. * The labeled_graph class is a graph adaptor that maintains a mapping between
  212. * vertex labels and vertex descriptors.
  213. *
  214. * @todo This class is somewhat redundant for adjacency_list<*, vecS> if
  215. * the intended label is an unsigned int (and perhpahs some other cases), but
  216. * it does avoid some weird ambiguities (i.e. adding a vertex with a label that
  217. * does not match its target index).
  218. *
  219. * @todo This needs to be reconciled with the named_graph, but since there is
  220. * no documentation or examples, its not going to happen.
  221. */
  222. template <typename Graph, typename Label, typename Selector = defaultS>
  223. class labeled_graph
  224. : protected labeled_graph_types<Graph, Label, Selector>
  225. {
  226. typedef labeled_graph_types<Graph, Label, Selector> Base;
  227. public:
  228. typedef labeled_graph_class_tag graph_tag;
  229. typedef typename Base::graph_type graph_type;
  230. typedef typename graph_traits<graph_type>::vertex_descriptor vertex_descriptor;
  231. typedef typename graph_traits<graph_type>::edge_descriptor edge_descriptor;
  232. typedef typename graph_traits<graph_type>::directed_category directed_category;
  233. typedef typename graph_traits<graph_type>::edge_parallel_category edge_parallel_category;
  234. typedef typename graph_traits<graph_type>::traversal_category traversal_category;
  235. typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
  236. typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
  237. typedef typename graph_traits<graph_type>::adjacency_iterator adjacency_iterator;
  238. typedef typename graph_traits<graph_type>::degree_size_type degree_size_type;
  239. typedef typename graph_traits<graph_type>::vertex_iterator vertex_iterator;
  240. typedef typename graph_traits<graph_type>::vertices_size_type vertices_size_type;
  241. typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
  242. typedef typename graph_traits<graph_type>::edges_size_type edges_size_type;
  243. typedef typename graph_type::graph_property_type graph_property_type;
  244. typedef typename graph_type::graph_bundled graph_bundled;
  245. typedef typename graph_type::vertex_property_type vertex_property_type;
  246. typedef typename graph_type::vertex_bundled vertex_bundled;
  247. typedef typename graph_type::edge_property_type edge_property_type;
  248. typedef typename graph_type::edge_bundled edge_bundled;
  249. typedef typename Base::label_type label_type;
  250. typedef typename Base::map_type map_type;
  251. public:
  252. labeled_graph(graph_property_type const& gp = graph_property_type())
  253. : _graph(gp), _map()
  254. { }
  255. labeled_graph(labeled_graph const& x)
  256. : _graph(x._graph), _map(x._map)
  257. { }
  258. // This constructor can only be used if map_type supports positional
  259. // range insertion (i.e. its a vector). This is the only case where we can
  260. // try to guess the intended labels for graph.
  261. labeled_graph(vertices_size_type n,
  262. graph_property_type const& gp = graph_property_type())
  263. : _graph(n, gp), _map()
  264. {
  265. std::pair<vertex_iterator, vertex_iterator> rng = vertices(_graph);
  266. _map.insert(_map.end(), rng.first, rng.second);
  267. }
  268. // Construct a grpah over n vertices, each of which receives a label from
  269. // the range [l, l + n). Note that the graph is not directly constructed
  270. // over the n vertices, but added sequentially. This constructor is
  271. // necessarily slower than the underlying counterpart.
  272. template <typename LabelIter>
  273. labeled_graph(vertices_size_type n, LabelIter l,
  274. graph_property_type const& gp = graph_property_type())
  275. : _graph(gp)
  276. { while(n-- >= 0) add_vertex(*l++); }
  277. // Construct the graph over n vertices each of which has a label in the
  278. // range [l, l + n) and a property in the range [p, p + n).
  279. template <typename LabelIter, typename PropIter>
  280. labeled_graph(vertices_size_type n, LabelIter l, PropIter p,
  281. graph_property_type const& gp = graph_property_type())
  282. { while(n-- >= 0) add_vertex(*l++, *p++); }
  283. labeled_graph& operator=(labeled_graph const& x) {
  284. _graph = x._graph;
  285. _map = x._map;
  286. return *this;
  287. }
  288. /** @name Graph Accessors */
  289. //@{
  290. graph_type& graph() { return _graph; }
  291. graph_type const& graph() const { return _graph; }
  292. //@}
  293. /**
  294. * Create a new label for the given vertex, returning false, if the label
  295. * cannot be created.
  296. */
  297. bool label_vertex(vertex_descriptor v, Label const& l)
  298. { return graph_detail::put_vertex_label(_map, _graph, l, v); }
  299. /** @name Add Vertex
  300. * Add a vertex to the graph, returning the descriptor. If the vertices
  301. * are uniquely labeled and the label already exists within the graph,
  302. * then no vertex is added, and the returned descriptor refers to the
  303. * existing vertex. A vertex property can be given as a parameter, if
  304. * needed.
  305. */
  306. //@{
  307. vertex_descriptor add_vertex(Label const& l) {
  308. return graph_detail::insert_labeled_vertex(
  309. _map, _graph, l, vertex_property_type()
  310. ).first;
  311. }
  312. vertex_descriptor add_vertex(Label const& l, vertex_property_type const& p)
  313. { return graph_detail::insert_labeled_vertex(_map, _graph, l, p).first; }
  314. //@}
  315. /** @name Insert Vertex
  316. * Insert a vertex into the graph, returning a pair containing the
  317. * descriptor of a vertex and a boolean value that describes whether or not
  318. * a new vertex was inserted. If vertices are not uniquely labeled, then
  319. * insertion will always succeed.
  320. */
  321. //@{
  322. std::pair<vertex_descriptor, bool> insert_vertex(Label const& l) {
  323. return graph_detail::insert_labeled_vertex(
  324. _map, _graph, l, vertex_property_type()
  325. );
  326. }
  327. std::pair<vertex_descriptor, bool>
  328. insert_vertex(Label const& l, vertex_property_type const& p)
  329. { return graph_detail::insert_labeled_vertex(_map, _graph, l, p); }
  330. //@}
  331. /** Remove the vertex with the given label. */
  332. void remove_vertex(Label const& l)
  333. { return boost::remove_vertex(vertex(l), _graph); }
  334. /** Return a descriptor for the given label. */
  335. vertex_descriptor vertex(Label const& l) const
  336. { return graph_detail::find_labeled_vertex(_map, _graph, l); }
  337. #ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
  338. /** @name Bundled Properties */
  339. //@{
  340. // Lookup the requested vertex and return the bundle.
  341. vertex_bundled& operator[](Label const& l)
  342. { return _graph[vertex(l)]; }
  343. vertex_bundled const& operator[](Label const& l) const
  344. { return _graph[vertex(l)]; }
  345. // Delegate edge lookup to the underlying graph.
  346. edge_bundled& operator[](edge_descriptor e)
  347. { return _graph[e]; }
  348. edge_bundled const& operator[](edge_descriptor e) const
  349. { return _graph[e]; }
  350. //@}
  351. #endif
  352. /** Return a null descriptor */
  353. static vertex_descriptor null_vertex()
  354. { return graph_type::null_vertex(); }
  355. private:
  356. graph_type _graph;
  357. map_type _map;
  358. };
  359. /**
  360. * The partial specialization over graph pointers allows the construction
  361. * of temporary labeled graph objects. In this case, the labels are destructed
  362. * when the wrapper goes out of scope.
  363. */
  364. template <typename Graph, typename Label, typename Selector>
  365. class labeled_graph<Graph*, Label, Selector>
  366. : protected labeled_graph_types<Graph, Label, Selector>
  367. {
  368. typedef labeled_graph_types<Graph, Label, Selector> Base;
  369. public:
  370. typedef labeled_graph_class_tag graph_tag;
  371. typedef typename Base::graph_type graph_type;
  372. typedef typename graph_traits<graph_type>::vertex_descriptor vertex_descriptor;
  373. typedef typename graph_traits<graph_type>::edge_descriptor edge_descriptor;
  374. typedef typename graph_traits<graph_type>::directed_category directed_category;
  375. typedef typename graph_traits<graph_type>::edge_parallel_category edge_parallel_category;
  376. typedef typename graph_traits<graph_type>::traversal_category traversal_category;
  377. typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
  378. typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
  379. typedef typename graph_traits<graph_type>::adjacency_iterator adjacency_iterator;
  380. typedef typename graph_traits<graph_type>::degree_size_type degree_size_type;
  381. typedef typename graph_traits<graph_type>::vertex_iterator vertex_iterator;
  382. typedef typename graph_traits<graph_type>::vertices_size_type vertices_size_type;
  383. typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
  384. typedef typename graph_traits<graph_type>::edges_size_type edges_size_type;
  385. typedef typename graph_type::vertex_property_type vertex_property_type;
  386. typedef typename graph_type::edge_property_type edge_property_type;
  387. typedef typename graph_type::graph_property_type graph_property_type;
  388. typedef typename graph_type::vertex_bundled vertex_bundled;
  389. typedef typename graph_type::edge_bundled edge_bundled;
  390. typedef typename Base::label_type label_type;
  391. typedef typename Base::map_type map_type;
  392. labeled_graph(graph_type* g)
  393. : _graph(g)
  394. { }
  395. /** @name Graph Access */
  396. //@{
  397. graph_type& graph() { return *_graph; }
  398. graph_type const& graph() const { return *_graph; }
  399. //@}
  400. /**
  401. * Create a new label for the given vertex, returning false, if the label
  402. * cannot be created.
  403. */
  404. bool label_vertex(vertex_descriptor v, Label const& l)
  405. { return graph_detail::put_vertex_label(_map, *_graph, l, v); }
  406. /** @name Add Vertex */
  407. //@{
  408. vertex_descriptor add_vertex(Label const& l) {
  409. return graph_detail::insert_labeled_vertex(
  410. _map, *_graph, l, vertex_property_type()
  411. ).first;
  412. }
  413. vertex_descriptor add_vertex(Label const& l, vertex_property_type const& p)
  414. { return graph_detail::insert_labeled_vertex(_map, *_graph, l, p).first; }
  415. std::pair<vertex_descriptor, bool> insert_vertex(Label const& l) {
  416. return graph_detail::insert_labeled_vertex(
  417. _map, *_graph, l, vertex_property_type()
  418. );
  419. }
  420. //@}
  421. /** Try to insert a vertex with the given label. */
  422. std::pair<vertex_descriptor, bool>
  423. insert_vertex(Label const& l, vertex_property_type const& p)
  424. { return graph_detail::insert_labeled_vertex(_map, *_graph, l, p); }
  425. /** Remove the vertex with the given label. */
  426. void remove_vertex(Label const& l)
  427. { return boost::remove_vertex(vertex(l), *_graph); }
  428. /** Return a descriptor for the given label. */
  429. vertex_descriptor vertex(Label const& l) const
  430. { return graph_detail::find_labeled_vertex(_map, *_graph, l); }
  431. #ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
  432. /** @name Bundled Properties */
  433. //@{
  434. // Lookup the requested vertex and return the bundle.
  435. vertex_bundled& operator[](Label const& l)
  436. { return (*_graph)[vertex(l)]; }
  437. vertex_bundled const& operator[](Label const& l) const
  438. { return (*_graph)[vertex(l)]; }
  439. // Delegate edge lookup to the underlying graph.
  440. edge_bundled& operator[](edge_descriptor e)
  441. { return (*_graph)[e]; }
  442. edge_bundled const& operator[](edge_descriptor e) const
  443. { return (*_graph)[e]; }
  444. //@}
  445. #endif
  446. static vertex_descriptor null_vertex()
  447. { return graph_type::null_vertex(); }
  448. private:
  449. graph_type* _graph;
  450. map_type _map;
  451. };
  452. #define LABELED_GRAPH_PARAMS typename G, typename L, typename S
  453. #define LABELED_GRAPH labeled_graph<G,L,S>
  454. /** @name Labeled Graph */
  455. //@{
  456. template <LABELED_GRAPH_PARAMS>
  457. inline bool label_vertex(typename LABELED_GRAPH::vertex_descriptor v,
  458. typename LABELED_GRAPH::label_type const l,
  459. LABELED_GRAPH& g)
  460. { return g.label_vertex(v, l); }
  461. template <LABELED_GRAPH_PARAMS>
  462. inline bool vertex_by_label(typename LABELED_GRAPH::label_type const l,
  463. LABELED_GRAPH& g)
  464. { return g.vertex(l); }
  465. //@}
  466. /** @name Graph */
  467. //@{
  468. template <LABELED_GRAPH_PARAMS>
  469. inline std::pair<typename LABELED_GRAPH::edge_descriptor, bool>
  470. edge(typename LABELED_GRAPH::vertex_descriptor const& u,
  471. typename LABELED_GRAPH::vertex_descriptor const& v,
  472. LABELED_GRAPH const& g)
  473. { return edge(u, v, g.graph()); }
  474. // Labeled Extensions
  475. template <LABELED_GRAPH_PARAMS>
  476. inline std::pair<typename LABELED_GRAPH::edge_descriptor, bool>
  477. edge_by_label(typename LABELED_GRAPH::label_type const& u,
  478. typename LABELED_GRAPH::label_type const& v,
  479. LABELED_GRAPH const& g)
  480. { return edge(g.vertex(u), g.vertex(v), g); }
  481. //@}
  482. /** @name Incidence Graph */
  483. //@{
  484. template <LABELED_GRAPH_PARAMS>
  485. inline std::pair<
  486. typename LABELED_GRAPH::out_edge_iterator,
  487. typename LABELED_GRAPH::out_edge_iterator>
  488. out_edges(typename LABELED_GRAPH::vertex_descriptor v, LABELED_GRAPH const& g)
  489. { return out_edges(v, g.graph()); }
  490. template <LABELED_GRAPH_PARAMS>
  491. inline typename LABELED_GRAPH::degree_size_type
  492. out_degree(typename LABELED_GRAPH::vertex_descriptor v, LABELED_GRAPH const& g)
  493. { return out_degree(v, g.graph()); }
  494. template <LABELED_GRAPH_PARAMS>
  495. inline typename LABELED_GRAPH::vertex_descriptor
  496. source(typename LABELED_GRAPH::edge_descriptor e, LABELED_GRAPH const& g)
  497. { return source(e, g.graph()); }
  498. template <LABELED_GRAPH_PARAMS>
  499. inline typename LABELED_GRAPH::vertex_descriptor
  500. target(typename LABELED_GRAPH::edge_descriptor e, LABELED_GRAPH const& g)
  501. { return target(e, g.graph()); }
  502. //@}
  503. /** @name Bidirectional Graph */
  504. //@{
  505. template <LABELED_GRAPH_PARAMS>
  506. inline std::pair<
  507. typename LABELED_GRAPH::in_edge_iterator,
  508. typename LABELED_GRAPH::in_edge_iterator>
  509. in_edges(typename LABELED_GRAPH::vertex_descriptor v, LABELED_GRAPH const& g)
  510. { return in_edges(v, g.graph()); }
  511. template <LABELED_GRAPH_PARAMS>
  512. inline typename LABELED_GRAPH::degree_size_type
  513. in_degree(typename LABELED_GRAPH::vertex_descriptor v, LABELED_GRAPH const& g)
  514. { return in_degree(v, g.graph()); }
  515. template <LABELED_GRAPH_PARAMS>
  516. inline typename LABELED_GRAPH::degree_size_type
  517. degree(typename LABELED_GRAPH::vertex_descriptor v, LABELED_GRAPH const& g)
  518. { return degree(v, g.graph()); }
  519. //@}
  520. /** @name Adjacency Graph */
  521. //@{
  522. template <LABELED_GRAPH_PARAMS>
  523. inline std::pair<
  524. typename LABELED_GRAPH::adjacency_iterator,
  525. typename LABELED_GRAPH::adjacency_iterator>
  526. adjacenct_vertices(typename LABELED_GRAPH::vertex_descriptor v, LABELED_GRAPH const& g)
  527. { return adjacent_vertices(v, g.graph()); }
  528. //@}
  529. /** @name VertexListGraph */
  530. //@{
  531. template <LABELED_GRAPH_PARAMS>
  532. inline std::pair<
  533. typename LABELED_GRAPH::vertex_iterator,
  534. typename LABELED_GRAPH::vertex_iterator>
  535. vertices(LABELED_GRAPH const& g)
  536. { return vertices(g.graph()); }
  537. template <LABELED_GRAPH_PARAMS>
  538. inline typename LABELED_GRAPH::vertices_size_type
  539. num_vertices(LABELED_GRAPH const& g)
  540. { return num_vertices(g.graph()); }
  541. //@}
  542. /** @name EdgeListGraph */
  543. //@{
  544. template <LABELED_GRAPH_PARAMS>
  545. inline std::pair<
  546. typename LABELED_GRAPH::edge_iterator,
  547. typename LABELED_GRAPH::edge_iterator>
  548. edges(LABELED_GRAPH const& g)
  549. { return edges(g.graph()); }
  550. template <LABELED_GRAPH_PARAMS>
  551. inline typename LABELED_GRAPH::edges_size_type
  552. num_edges(LABELED_GRAPH const& g)
  553. { return num_edges(g.graph()); }
  554. //@}
  555. /** @internal @name Property Lookup */
  556. //@{
  557. namespace graph_detail {
  558. struct labeled_graph_vertex_property_selector {
  559. template <class LabeledGraph, class Property, class Tag>
  560. struct bind_ {
  561. typedef typename LabeledGraph::graph_type Graph;
  562. typedef property_map<Graph, Tag> PropertyMap;
  563. typedef typename PropertyMap::type type;
  564. typedef typename PropertyMap::const_type const_type;
  565. };
  566. };
  567. struct labeled_graph_edge_property_selector {
  568. template <class LabeledGraph, class Property, class Tag>
  569. struct bind_ {
  570. typedef typename LabeledGraph::graph_type Graph;
  571. typedef property_map<Graph, Tag> PropertyMap;
  572. typedef typename PropertyMap::type type;
  573. typedef typename PropertyMap::const_type const_type;
  574. };
  575. };
  576. }
  577. template <>
  578. struct vertex_property_selector<labeled_graph_class_tag> {
  579. typedef graph_detail::labeled_graph_vertex_property_selector type;
  580. };
  581. template <>
  582. struct edge_property_selector<labeled_graph_class_tag> {
  583. typedef graph_detail::labeled_graph_edge_property_selector type;
  584. };
  585. //@}
  586. /** @name Property Graph */
  587. //@{
  588. template <LABELED_GRAPH_PARAMS, typename Prop>
  589. inline typename property_map<LABELED_GRAPH, Prop>::type
  590. get(Prop p, LABELED_GRAPH& g)
  591. { return get(p, g.graph()); }
  592. template <LABELED_GRAPH_PARAMS, typename Prop>
  593. inline typename property_map<LABELED_GRAPH, Prop>::const_type
  594. get(Prop p, LABELED_GRAPH const& g)
  595. { return get(p, g.graph()); }
  596. template <LABELED_GRAPH_PARAMS, typename Prop, typename Key>
  597. inline typename property_traits<
  598. typename property_map<typename LABELED_GRAPH::graph_type, Prop>::const_type
  599. >::value_type
  600. get(Prop p, LABELED_GRAPH const& g, const Key& k)
  601. { return get(p, g.graph(), k); }
  602. template <LABELED_GRAPH_PARAMS, typename Prop, typename Key, typename Value>
  603. inline void
  604. put(Prop p, LABELED_GRAPH& g, Key const& k, Value const& v)
  605. { put(p, g.graph(), k, v); }
  606. //@}
  607. /** @name Mutable Graph */
  608. //@{
  609. template <LABELED_GRAPH_PARAMS>
  610. inline std::pair<typename LABELED_GRAPH::edge_descriptor, bool>
  611. add_edge(typename LABELED_GRAPH::vertex_descriptor const& u,
  612. typename LABELED_GRAPH::vertex_descriptor const& v,
  613. LABELED_GRAPH& g)
  614. { return add_edge(u, v, g.graph()); }
  615. template <LABELED_GRAPH_PARAMS>
  616. inline std::pair<typename LABELED_GRAPH::edge_descriptor, bool>
  617. add_edge(typename LABELED_GRAPH::vertex_descriptor const& u,
  618. typename LABELED_GRAPH::vertex_descriptor const& v,
  619. typename LABELED_GRAPH::edge_property_type const& p,
  620. LABELED_GRAPH& g)
  621. { return add_edge(u, v, p, g.graph()); }
  622. template <LABELED_GRAPH_PARAMS>
  623. inline void
  624. clear_vertex(typename LABELED_GRAPH::vertex_descriptor v, LABELED_GRAPH& g)
  625. { return clear_vertex(v, g.graph()); }
  626. template <LABELED_GRAPH_PARAMS>
  627. inline void
  628. remove_edge(typename LABELED_GRAPH::edge_descriptor e, LABELED_GRAPH& g)
  629. { return remove_edge(e, g.graph()); }
  630. template <LABELED_GRAPH_PARAMS>
  631. inline void
  632. remove_edge(typename LABELED_GRAPH::vertex_descriptor u,
  633. typename LABELED_GRAPH::vertex_descriptor v,
  634. LABELED_GRAPH& g)
  635. { return remove_edge(u, v, g.graph()); }
  636. // Labeled extensions
  637. template <LABELED_GRAPH_PARAMS>
  638. inline std::pair<typename LABELED_GRAPH::edge_descriptor, bool>
  639. add_edge_by_label(typename LABELED_GRAPH::label_type const& u,
  640. typename LABELED_GRAPH::label_type const& v,
  641. LABELED_GRAPH& g)
  642. { return add_edge(g.vertex(u), g.vertex(v), g); }
  643. template <LABELED_GRAPH_PARAMS>
  644. inline std::pair<typename LABELED_GRAPH::edge_descriptor, bool>
  645. add_edge_by_label(typename LABELED_GRAPH::label_type const& u,
  646. typename LABELED_GRAPH::label_type const& v,
  647. typename LABELED_GRAPH::edge_property_type const& p,
  648. LABELED_GRAPH& g)
  649. { return add_edge(g.vertex(u), g.vertex(v), p, g); }
  650. template <LABELED_GRAPH_PARAMS>
  651. inline void
  652. clear_vertex_by_label(typename LABELED_GRAPH::label_type const& l, LABELED_GRAPH& g)
  653. { clear_vertex(g.vertex(l), g.graph()); }
  654. template <LABELED_GRAPH_PARAMS>
  655. inline void
  656. remove_edge_by_label(typename LABELED_GRAPH::label_type const& u,
  657. typename LABELED_GRAPH::label_type const& v,
  658. LABELED_GRAPH& g)
  659. { remove_edge(g.vertex(u), g.vertex(v), g.graph()); }
  660. //@}
  661. /** @name Labeled Mutable Graph
  662. * The labeled mutable graph hides the add_ and remove_ vertex functions from
  663. * the mutable graph concept. Note that the remove_vertex is hidden because
  664. * removing the vertex without its key could leave a dangling reference in
  665. * the map.
  666. */
  667. //@{
  668. template <LABELED_GRAPH_PARAMS>
  669. inline typename LABELED_GRAPH::vertex_descriptor
  670. add_vertex(typename LABELED_GRAPH::label_type const& l,
  671. LABELED_GRAPH& g)
  672. { return g.add_vertex(l); }
  673. // MutableLabeledPropertyGraph::add_vertex(l, vp, g)
  674. template <LABELED_GRAPH_PARAMS>
  675. inline typename LABELED_GRAPH::vertex_descriptor
  676. add_vertex(typename LABELED_GRAPH::label_type const& l,
  677. typename LABELED_GRAPH::vertex_property_type const& p,
  678. LABELED_GRAPH& g)
  679. { return g.add_vertex(l, p); }
  680. template <LABELED_GRAPH_PARAMS>
  681. inline void
  682. remove_vertex(typename LABELED_GRAPH::label_type const& l, LABELED_GRAPH& g)
  683. { g.remove_vertex(l); }
  684. //@}
  685. #undef LABELED_GRAPH_PARAMS
  686. #undef LABELED_GRAPH
  687. } // namespace boost
  688. // Pull the labeled graph traits
  689. #include <boost/graph/detail/labeled_graph_traits.hpp>
  690. #endif // BOOST_GRAPH_LABELED_GRAPH_HPP