labeled_graph.hpp 32 KB

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