labeled_graph.hpp 30 KB

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