labeled_graph.hpp 30 KB

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