leda_graph.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Copyright 2004 The Trustees of Indiana University.
  4. // Copyright 2007 University of Karlsruhe
  5. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek, Douglas Gregor,
  6. // Jens Mueller
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //=======================================================================
  12. #ifndef BOOST_GRAPH_LEDA_HPP
  13. #define BOOST_GRAPH_LEDA_HPP
  14. #include <boost/config.hpp>
  15. #include <boost/iterator/iterator_facade.hpp>
  16. #include <boost/graph/graph_traits.hpp>
  17. #include <boost/graph/properties.hpp>
  18. #include <LEDA/graph/graph.h>
  19. #include <LEDA/graph/node_array.h>
  20. #include <LEDA/graph/node_map.h>
  21. // The functions and classes in this file allows the user to
  22. // treat a LEDA GRAPH object as a boost graph "as is". No
  23. // wrapper is needed for the GRAPH object.
  24. // Warning: this implementation relies on partial specialization
  25. // for the graph_traits class (so it won't compile with Visual C++)
  26. // Warning: this implementation is in alpha and has not been tested
  27. namespace boost
  28. {
  29. struct leda_graph_traversal_category : public virtual bidirectional_graph_tag,
  30. public virtual adjacency_graph_tag,
  31. public virtual vertex_list_graph_tag
  32. {
  33. };
  34. template < class vtype, class etype >
  35. struct graph_traits< leda::GRAPH< vtype, etype > >
  36. {
  37. typedef leda::node vertex_descriptor;
  38. typedef leda::edge edge_descriptor;
  39. class adjacency_iterator
  40. : public iterator_facade< adjacency_iterator, leda::node,
  41. bidirectional_traversal_tag, leda::node, const leda::node* >
  42. {
  43. public:
  44. adjacency_iterator(
  45. leda::node node = 0, const leda::GRAPH< vtype, etype >* g = 0)
  46. : base(node), g(g)
  47. {
  48. }
  49. private:
  50. leda::node dereference() const { return leda::target(base); }
  51. bool equal(const adjacency_iterator& other) const
  52. {
  53. return base == other.base;
  54. }
  55. void increment() { base = g->adj_succ(base); }
  56. void decrement() { base = g->adj_pred(base); }
  57. leda::edge base;
  58. const leda::GRAPH< vtype, etype >* g;
  59. friend class iterator_core_access;
  60. };
  61. class out_edge_iterator
  62. : public iterator_facade< out_edge_iterator, leda::edge,
  63. bidirectional_traversal_tag, const leda::edge&, const leda::edge* >
  64. {
  65. public:
  66. out_edge_iterator(
  67. leda::node node = 0, const leda::GRAPH< vtype, etype >* g = 0)
  68. : base(node), g(g)
  69. {
  70. }
  71. private:
  72. const leda::edge& dereference() const { return base; }
  73. bool equal(const out_edge_iterator& other) const
  74. {
  75. return base == other.base;
  76. }
  77. void increment() { base = g->adj_succ(base); }
  78. void decrement() { base = g->adj_pred(base); }
  79. leda::edge base;
  80. const leda::GRAPH< vtype, etype >* g;
  81. friend class iterator_core_access;
  82. };
  83. class in_edge_iterator
  84. : public iterator_facade< in_edge_iterator, leda::edge,
  85. bidirectional_traversal_tag, const leda::edge&, const leda::edge* >
  86. {
  87. public:
  88. in_edge_iterator(
  89. leda::node node = 0, const leda::GRAPH< vtype, etype >* g = 0)
  90. : base(node), g(g)
  91. {
  92. }
  93. private:
  94. const leda::edge& dereference() const { return base; }
  95. bool equal(const in_edge_iterator& other) const
  96. {
  97. return base == other.base;
  98. }
  99. void increment() { base = g->in_succ(base); }
  100. void decrement() { base = g->in_pred(base); }
  101. leda::edge base;
  102. const leda::GRAPH< vtype, etype >* g;
  103. friend class iterator_core_access;
  104. };
  105. class vertex_iterator
  106. : public iterator_facade< vertex_iterator, leda::node,
  107. bidirectional_traversal_tag, const leda::node&, const leda::node* >
  108. {
  109. public:
  110. vertex_iterator(
  111. leda::node node = 0, const leda::GRAPH< vtype, etype >* g = 0)
  112. : base(node), g(g)
  113. {
  114. }
  115. private:
  116. const leda::node& dereference() const { return base; }
  117. bool equal(const vertex_iterator& other) const
  118. {
  119. return base == other.base;
  120. }
  121. void increment() { base = g->succ_node(base); }
  122. void decrement() { base = g->pred_node(base); }
  123. leda::node base;
  124. const leda::GRAPH< vtype, etype >* g;
  125. friend class iterator_core_access;
  126. };
  127. class edge_iterator
  128. : public iterator_facade< edge_iterator, leda::edge,
  129. bidirectional_traversal_tag, const leda::edge&, const leda::edge* >
  130. {
  131. public:
  132. edge_iterator(
  133. leda::edge edge = 0, const leda::GRAPH< vtype, etype >* g = 0)
  134. : base(edge), g(g)
  135. {
  136. }
  137. private:
  138. const leda::edge& dereference() const { return base; }
  139. bool equal(const edge_iterator& other) const
  140. {
  141. return base == other.base;
  142. }
  143. void increment() { base = g->succ_edge(base); }
  144. void decrement() { base = g->pred_edge(base); }
  145. leda::node base;
  146. const leda::GRAPH< vtype, etype >* g;
  147. friend class iterator_core_access;
  148. };
  149. typedef directed_tag directed_category;
  150. typedef allow_parallel_edge_tag edge_parallel_category; // not sure here
  151. typedef leda_graph_traversal_category traversal_category;
  152. typedef int vertices_size_type;
  153. typedef int edges_size_type;
  154. typedef int degree_size_type;
  155. };
  156. template <> struct graph_traits< leda::graph >
  157. {
  158. typedef leda::node vertex_descriptor;
  159. typedef leda::edge edge_descriptor;
  160. class adjacency_iterator
  161. : public iterator_facade< adjacency_iterator, leda::node,
  162. bidirectional_traversal_tag, leda::node, const leda::node* >
  163. {
  164. public:
  165. adjacency_iterator(leda::edge edge = 0, const leda::graph* g = 0)
  166. : base(edge), g(g)
  167. {
  168. }
  169. private:
  170. leda::node dereference() const { return leda::target(base); }
  171. bool equal(const adjacency_iterator& other) const
  172. {
  173. return base == other.base;
  174. }
  175. void increment() { base = g->adj_succ(base); }
  176. void decrement() { base = g->adj_pred(base); }
  177. leda::edge base;
  178. const leda::graph* g;
  179. friend class iterator_core_access;
  180. };
  181. class out_edge_iterator
  182. : public iterator_facade< out_edge_iterator, leda::edge,
  183. bidirectional_traversal_tag, const leda::edge&, const leda::edge* >
  184. {
  185. public:
  186. out_edge_iterator(leda::edge edge = 0, const leda::graph* g = 0)
  187. : base(edge), g(g)
  188. {
  189. }
  190. private:
  191. const leda::edge& dereference() const { return base; }
  192. bool equal(const out_edge_iterator& other) const
  193. {
  194. return base == other.base;
  195. }
  196. void increment() { base = g->adj_succ(base); }
  197. void decrement() { base = g->adj_pred(base); }
  198. leda::edge base;
  199. const leda::graph* g;
  200. friend class iterator_core_access;
  201. };
  202. class in_edge_iterator
  203. : public iterator_facade< in_edge_iterator, leda::edge,
  204. bidirectional_traversal_tag, const leda::edge&, const leda::edge* >
  205. {
  206. public:
  207. in_edge_iterator(leda::edge edge = 0, const leda::graph* g = 0)
  208. : base(edge), g(g)
  209. {
  210. }
  211. private:
  212. const leda::edge& dereference() const { return base; }
  213. bool equal(const in_edge_iterator& other) const
  214. {
  215. return base == other.base;
  216. }
  217. void increment() { base = g->in_succ(base); }
  218. void decrement() { base = g->in_pred(base); }
  219. leda::edge base;
  220. const leda::graph* g;
  221. friend class iterator_core_access;
  222. };
  223. class vertex_iterator
  224. : public iterator_facade< vertex_iterator, leda::node,
  225. bidirectional_traversal_tag, const leda::node&, const leda::node* >
  226. {
  227. public:
  228. vertex_iterator(leda::node node = 0, const leda::graph* g = 0)
  229. : base(node), g(g)
  230. {
  231. }
  232. private:
  233. const leda::node& dereference() const { return base; }
  234. bool equal(const vertex_iterator& other) const
  235. {
  236. return base == other.base;
  237. }
  238. void increment() { base = g->succ_node(base); }
  239. void decrement() { base = g->pred_node(base); }
  240. leda::node base;
  241. const leda::graph* g;
  242. friend class iterator_core_access;
  243. };
  244. class edge_iterator
  245. : public iterator_facade< edge_iterator, leda::edge,
  246. bidirectional_traversal_tag, const leda::edge&, const leda::edge* >
  247. {
  248. public:
  249. edge_iterator(leda::edge edge = 0, const leda::graph* g = 0)
  250. : base(edge), g(g)
  251. {
  252. }
  253. private:
  254. const leda::edge& dereference() const { return base; }
  255. bool equal(const edge_iterator& other) const
  256. {
  257. return base == other.base;
  258. }
  259. void increment() { base = g->succ_edge(base); }
  260. void decrement() { base = g->pred_edge(base); }
  261. leda::edge base;
  262. const leda::graph* g;
  263. friend class iterator_core_access;
  264. };
  265. typedef directed_tag directed_category;
  266. typedef allow_parallel_edge_tag edge_parallel_category; // not sure here
  267. typedef leda_graph_traversal_category traversal_category;
  268. typedef int vertices_size_type;
  269. typedef int edges_size_type;
  270. typedef int degree_size_type;
  271. };
  272. } // namespace boost
  273. namespace boost
  274. {
  275. //===========================================================================
  276. // functions for GRAPH<vtype,etype>
  277. template < class vtype, class etype >
  278. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor source(
  279. typename graph_traits< leda::GRAPH< vtype, etype > >::edge_descriptor e,
  280. const leda::GRAPH< vtype, etype >& g)
  281. {
  282. return source(e);
  283. }
  284. template < class vtype, class etype >
  285. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor target(
  286. typename graph_traits< leda::GRAPH< vtype, etype > >::edge_descriptor e,
  287. const leda::GRAPH< vtype, etype >& g)
  288. {
  289. return target(e);
  290. }
  291. template < class vtype, class etype >
  292. inline std::pair<
  293. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_iterator,
  294. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_iterator >
  295. vertices(const leda::GRAPH< vtype, etype >& g)
  296. {
  297. typedef
  298. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_iterator
  299. Iter;
  300. return std::make_pair(Iter(g.first_node(), &g), Iter(0, &g));
  301. }
  302. template < class vtype, class etype >
  303. inline std::pair<
  304. typename graph_traits< leda::GRAPH< vtype, etype > >::edge_iterator,
  305. typename graph_traits< leda::GRAPH< vtype, etype > >::edge_iterator >
  306. edges(const leda::GRAPH< vtype, etype >& g)
  307. {
  308. typedef typename graph_traits< leda::GRAPH< vtype, etype > >::edge_iterator
  309. Iter;
  310. return std::make_pair(Iter(g.first_edge(), &g), Iter(0, &g));
  311. }
  312. template < class vtype, class etype >
  313. inline std::pair<
  314. typename graph_traits< leda::GRAPH< vtype, etype > >::out_edge_iterator,
  315. typename graph_traits< leda::GRAPH< vtype, etype > >::out_edge_iterator >
  316. out_edges(
  317. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor u,
  318. const leda::GRAPH< vtype, etype >& g)
  319. {
  320. typedef
  321. typename graph_traits< leda::GRAPH< vtype, etype > >::out_edge_iterator
  322. Iter;
  323. return std::make_pair(Iter(g.first_adj_edge(u, 0), &g), Iter(0, &g));
  324. }
  325. template < class vtype, class etype >
  326. inline std::pair<
  327. typename graph_traits< leda::GRAPH< vtype, etype > >::in_edge_iterator,
  328. typename graph_traits< leda::GRAPH< vtype, etype > >::in_edge_iterator >
  329. in_edges(
  330. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor u,
  331. const leda::GRAPH< vtype, etype >& g)
  332. {
  333. typedef
  334. typename graph_traits< leda::GRAPH< vtype, etype > >::in_edge_iterator
  335. Iter;
  336. return std::make_pair(Iter(g.first_adj_edge(u, 1), &g), Iter(0, &g));
  337. }
  338. template < class vtype, class etype >
  339. inline std::pair<
  340. typename graph_traits< leda::GRAPH< vtype, etype > >::adjacency_iterator,
  341. typename graph_traits< leda::GRAPH< vtype, etype > >::adjacency_iterator >
  342. adjacent_vertices(
  343. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor u,
  344. const leda::GRAPH< vtype, etype >& g)
  345. {
  346. typedef
  347. typename graph_traits< leda::GRAPH< vtype, etype > >::adjacency_iterator
  348. Iter;
  349. return std::make_pair(Iter(g.first_adj_edge(u, 0), &g), Iter(0, &g));
  350. }
  351. template < class vtype, class etype >
  352. typename graph_traits< leda::GRAPH< vtype, etype > >::vertices_size_type
  353. num_vertices(const leda::GRAPH< vtype, etype >& g)
  354. {
  355. return g.number_of_nodes();
  356. }
  357. template < class vtype, class etype >
  358. typename graph_traits< leda::GRAPH< vtype, etype > >::edges_size_type num_edges(
  359. const leda::GRAPH< vtype, etype >& g)
  360. {
  361. return g.number_of_edges();
  362. }
  363. template < class vtype, class etype >
  364. typename graph_traits< leda::GRAPH< vtype, etype > >::degree_size_type
  365. out_degree(
  366. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor u,
  367. const leda::GRAPH< vtype, etype >& g)
  368. {
  369. return g.outdeg(u);
  370. }
  371. template < class vtype, class etype >
  372. typename graph_traits< leda::GRAPH< vtype, etype > >::degree_size_type
  373. in_degree(
  374. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor u,
  375. const leda::GRAPH< vtype, etype >& g)
  376. {
  377. return g.indeg(u);
  378. }
  379. template < class vtype, class etype >
  380. typename graph_traits< leda::GRAPH< vtype, etype > >::degree_size_type degree(
  381. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor u,
  382. const leda::GRAPH< vtype, etype >& g)
  383. {
  384. return g.outdeg(u) + g.indeg(u);
  385. }
  386. template < class vtype, class etype >
  387. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor
  388. add_vertex(leda::GRAPH< vtype, etype >& g)
  389. {
  390. return g.new_node();
  391. }
  392. template < class vtype, class etype >
  393. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor
  394. add_vertex(const vtype& vp, leda::GRAPH< vtype, etype >& g)
  395. {
  396. return g.new_node(vp);
  397. }
  398. template < class vtype, class etype >
  399. void clear_vertex(
  400. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor u,
  401. leda::GRAPH< vtype, etype >& g)
  402. {
  403. typename graph_traits< leda::GRAPH< vtype, etype > >::out_edge_iterator ei,
  404. ei_end;
  405. for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ei++)
  406. remove_edge(*ei);
  407. typename graph_traits< leda::GRAPH< vtype, etype > >::in_edge_iterator iei,
  408. iei_end;
  409. for (boost::tie(iei, iei_end) = in_edges(u, g); iei != iei_end; iei++)
  410. remove_edge(*iei);
  411. }
  412. template < class vtype, class etype >
  413. void remove_vertex(
  414. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor u,
  415. leda::GRAPH< vtype, etype >& g)
  416. {
  417. g.del_node(u);
  418. }
  419. template < class vtype, class etype >
  420. std::pair<
  421. typename graph_traits< leda::GRAPH< vtype, etype > >::edge_descriptor,
  422. bool >
  423. add_edge(
  424. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor u,
  425. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor v,
  426. leda::GRAPH< vtype, etype >& g)
  427. {
  428. return std::make_pair(g.new_edge(u, v), true);
  429. }
  430. template < class vtype, class etype >
  431. std::pair<
  432. typename graph_traits< leda::GRAPH< vtype, etype > >::edge_descriptor,
  433. bool >
  434. add_edge(
  435. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor u,
  436. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor v,
  437. const etype& et, leda::GRAPH< vtype, etype >& g)
  438. {
  439. return std::make_pair(g.new_edge(u, v, et), true);
  440. }
  441. template < class vtype, class etype >
  442. void remove_edge(
  443. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor u,
  444. typename graph_traits< leda::GRAPH< vtype, etype > >::vertex_descriptor v,
  445. leda::GRAPH< vtype, etype >& g)
  446. {
  447. typename graph_traits< leda::GRAPH< vtype, etype > >::out_edge_iterator i,
  448. iend;
  449. for (boost::tie(i, iend) = out_edges(u, g); i != iend; ++i)
  450. if (target(*i, g) == v)
  451. g.del_edge(*i);
  452. }
  453. template < class vtype, class etype >
  454. void remove_edge(
  455. typename graph_traits< leda::GRAPH< vtype, etype > >::edge_descriptor e,
  456. leda::GRAPH< vtype, etype >& g)
  457. {
  458. g.del_edge(e);
  459. }
  460. //===========================================================================
  461. // functions for graph (non-templated version)
  462. graph_traits< leda::graph >::vertex_descriptor source(
  463. graph_traits< leda::graph >::edge_descriptor e, const leda::graph& g)
  464. {
  465. return source(e);
  466. }
  467. graph_traits< leda::graph >::vertex_descriptor target(
  468. graph_traits< leda::graph >::edge_descriptor e, const leda::graph& g)
  469. {
  470. return target(e);
  471. }
  472. inline std::pair< graph_traits< leda::graph >::vertex_iterator,
  473. graph_traits< leda::graph >::vertex_iterator >
  474. vertices(const leda::graph& g)
  475. {
  476. typedef graph_traits< leda::graph >::vertex_iterator Iter;
  477. return std::make_pair(Iter(g.first_node(), &g), Iter(0, &g));
  478. }
  479. inline std::pair< graph_traits< leda::graph >::edge_iterator,
  480. graph_traits< leda::graph >::edge_iterator >
  481. edges(const leda::graph& g)
  482. {
  483. typedef graph_traits< leda::graph >::edge_iterator Iter;
  484. return std::make_pair(Iter(g.first_edge(), &g), Iter(0, &g));
  485. }
  486. inline std::pair< graph_traits< leda::graph >::out_edge_iterator,
  487. graph_traits< leda::graph >::out_edge_iterator >
  488. out_edges(
  489. graph_traits< leda::graph >::vertex_descriptor u, const leda::graph& g)
  490. {
  491. typedef graph_traits< leda::graph >::out_edge_iterator Iter;
  492. return std::make_pair(Iter(g.first_adj_edge(u), &g), Iter(0, &g));
  493. }
  494. inline std::pair< graph_traits< leda::graph >::in_edge_iterator,
  495. graph_traits< leda::graph >::in_edge_iterator >
  496. in_edges(graph_traits< leda::graph >::vertex_descriptor u, const leda::graph& g)
  497. {
  498. typedef graph_traits< leda::graph >::in_edge_iterator Iter;
  499. return std::make_pair(Iter(g.first_in_edge(u), &g), Iter(0, &g));
  500. }
  501. inline std::pair< graph_traits< leda::graph >::adjacency_iterator,
  502. graph_traits< leda::graph >::adjacency_iterator >
  503. adjacent_vertices(
  504. graph_traits< leda::graph >::vertex_descriptor u, const leda::graph& g)
  505. {
  506. typedef graph_traits< leda::graph >::adjacency_iterator Iter;
  507. return std::make_pair(Iter(g.first_adj_edge(u), &g), Iter(0, &g));
  508. }
  509. graph_traits< leda::graph >::vertices_size_type num_vertices(
  510. const leda::graph& g)
  511. {
  512. return g.number_of_nodes();
  513. }
  514. graph_traits< leda::graph >::edges_size_type num_edges(const leda::graph& g)
  515. {
  516. return g.number_of_edges();
  517. }
  518. graph_traits< leda::graph >::degree_size_type out_degree(
  519. graph_traits< leda::graph >::vertex_descriptor u, const leda::graph& g)
  520. {
  521. return g.outdeg(u);
  522. }
  523. graph_traits< leda::graph >::degree_size_type in_degree(
  524. graph_traits< leda::graph >::vertex_descriptor u, const leda::graph& g)
  525. {
  526. return g.indeg(u);
  527. }
  528. graph_traits< leda::graph >::degree_size_type degree(
  529. graph_traits< leda::graph >::vertex_descriptor u, const leda::graph& g)
  530. {
  531. return g.outdeg(u) + g.indeg(u);
  532. }
  533. graph_traits< leda::graph >::vertex_descriptor add_vertex(leda::graph& g)
  534. {
  535. return g.new_node();
  536. }
  537. void remove_edge(graph_traits< leda::graph >::vertex_descriptor u,
  538. graph_traits< leda::graph >::vertex_descriptor v, leda::graph& g)
  539. {
  540. graph_traits< leda::graph >::out_edge_iterator i, iend;
  541. for (boost::tie(i, iend) = out_edges(u, g); i != iend; ++i)
  542. if (target(*i, g) == v)
  543. g.del_edge(*i);
  544. }
  545. void remove_edge(graph_traits< leda::graph >::edge_descriptor e, leda::graph& g)
  546. {
  547. g.del_edge(e);
  548. }
  549. void clear_vertex(
  550. graph_traits< leda::graph >::vertex_descriptor u, leda::graph& g)
  551. {
  552. graph_traits< leda::graph >::out_edge_iterator ei, ei_end;
  553. for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ei++)
  554. remove_edge(*ei, g);
  555. graph_traits< leda::graph >::in_edge_iterator iei, iei_end;
  556. for (boost::tie(iei, iei_end) = in_edges(u, g); iei != iei_end; iei++)
  557. remove_edge(*iei, g);
  558. }
  559. void remove_vertex(
  560. graph_traits< leda::graph >::vertex_descriptor u, leda::graph& g)
  561. {
  562. g.del_node(u);
  563. }
  564. std::pair< graph_traits< leda::graph >::edge_descriptor, bool > add_edge(
  565. graph_traits< leda::graph >::vertex_descriptor u,
  566. graph_traits< leda::graph >::vertex_descriptor v, leda::graph& g)
  567. {
  568. return std::make_pair(g.new_edge(u, v), true);
  569. }
  570. //===========================================================================
  571. // property maps for GRAPH<vtype,etype>
  572. class leda_graph_id_map : public put_get_helper< int, leda_graph_id_map >
  573. {
  574. public:
  575. typedef readable_property_map_tag category;
  576. typedef int value_type;
  577. typedef int reference;
  578. typedef leda::node key_type;
  579. leda_graph_id_map() {}
  580. template < class T > long operator[](T x) const { return x->id(); }
  581. };
  582. template < class vtype, class etype >
  583. inline leda_graph_id_map get(
  584. vertex_index_t, const leda::GRAPH< vtype, etype >& g)
  585. {
  586. return leda_graph_id_map();
  587. }
  588. template < class vtype, class etype >
  589. inline leda_graph_id_map get(edge_index_t, const leda::GRAPH< vtype, etype >& g)
  590. {
  591. return leda_graph_id_map();
  592. }
  593. template < class Tag > struct leda_property_map
  594. {
  595. };
  596. template <> struct leda_property_map< vertex_index_t >
  597. {
  598. template < class vtype, class etype > struct bind_
  599. {
  600. typedef leda_graph_id_map type;
  601. typedef leda_graph_id_map const_type;
  602. };
  603. };
  604. template <> struct leda_property_map< edge_index_t >
  605. {
  606. template < class vtype, class etype > struct bind_
  607. {
  608. typedef leda_graph_id_map type;
  609. typedef leda_graph_id_map const_type;
  610. };
  611. };
  612. template < class Data, class DataRef, class GraphPtr >
  613. class leda_graph_data_map : public put_get_helper< DataRef,
  614. leda_graph_data_map< Data, DataRef, GraphPtr > >
  615. {
  616. public:
  617. typedef Data value_type;
  618. typedef DataRef reference;
  619. typedef void key_type;
  620. typedef lvalue_property_map_tag category;
  621. leda_graph_data_map(GraphPtr g) : m_g(g) {}
  622. template < class NodeOrEdge > DataRef operator[](NodeOrEdge x) const
  623. {
  624. return (*m_g)[x];
  625. }
  626. protected:
  627. GraphPtr m_g;
  628. };
  629. template <> struct leda_property_map< vertex_all_t >
  630. {
  631. template < class vtype, class etype > struct bind_
  632. {
  633. typedef leda_graph_data_map< vtype, vtype&,
  634. leda::GRAPH< vtype, etype >* >
  635. type;
  636. typedef leda_graph_data_map< vtype, const vtype&,
  637. const leda::GRAPH< vtype, etype >* >
  638. const_type;
  639. };
  640. };
  641. template < class vtype, class etype >
  642. inline typename property_map< leda::GRAPH< vtype, etype >, vertex_all_t >::type
  643. get(vertex_all_t, leda::GRAPH< vtype, etype >& g)
  644. {
  645. typedef
  646. typename property_map< leda::GRAPH< vtype, etype >, vertex_all_t >::type
  647. pmap_type;
  648. return pmap_type(&g);
  649. }
  650. template < class vtype, class etype >
  651. inline typename property_map< leda::GRAPH< vtype, etype >,
  652. vertex_all_t >::const_type
  653. get(vertex_all_t, const leda::GRAPH< vtype, etype >& g)
  654. {
  655. typedef typename property_map< leda::GRAPH< vtype, etype >,
  656. vertex_all_t >::const_type pmap_type;
  657. return pmap_type(&g);
  658. }
  659. template <> struct leda_property_map< edge_all_t >
  660. {
  661. template < class vtype, class etype > struct bind_
  662. {
  663. typedef leda_graph_data_map< etype, etype&,
  664. leda::GRAPH< vtype, etype >* >
  665. type;
  666. typedef leda_graph_data_map< etype, const etype&,
  667. const leda::GRAPH< vtype, etype >* >
  668. const_type;
  669. };
  670. };
  671. template < class vtype, class etype >
  672. inline typename property_map< leda::GRAPH< vtype, etype >, edge_all_t >::type
  673. get(edge_all_t, leda::GRAPH< vtype, etype >& g)
  674. {
  675. typedef
  676. typename property_map< leda::GRAPH< vtype, etype >, edge_all_t >::type
  677. pmap_type;
  678. return pmap_type(&g);
  679. }
  680. template < class vtype, class etype >
  681. inline
  682. typename property_map< leda::GRAPH< vtype, etype >, edge_all_t >::const_type
  683. get(edge_all_t, const leda::GRAPH< vtype, etype >& g)
  684. {
  685. typedef typename property_map< leda::GRAPH< vtype, etype >,
  686. edge_all_t >::const_type pmap_type;
  687. return pmap_type(&g);
  688. }
  689. // property map interface to the LEDA node_array class
  690. template < class E, class ERef, class NodeMapPtr >
  691. class leda_node_property_map
  692. : public put_get_helper< ERef, leda_node_property_map< E, ERef, NodeMapPtr > >
  693. {
  694. public:
  695. typedef E value_type;
  696. typedef ERef reference;
  697. typedef leda::node key_type;
  698. typedef lvalue_property_map_tag category;
  699. leda_node_property_map(NodeMapPtr a) : m_array(a) {}
  700. ERef operator[](leda::node n) const { return (*m_array)[n]; }
  701. protected:
  702. NodeMapPtr m_array;
  703. };
  704. template < class E >
  705. leda_node_property_map< E, const E&, const leda::node_array< E >* >
  706. make_leda_node_property_map(const leda::node_array< E >& a)
  707. {
  708. typedef leda_node_property_map< E, const E&, const leda::node_array< E >* >
  709. pmap_type;
  710. return pmap_type(&a);
  711. }
  712. template < class E >
  713. leda_node_property_map< E, E&, leda::node_array< E >* >
  714. make_leda_node_property_map(leda::node_array< E >& a)
  715. {
  716. typedef leda_node_property_map< E, E&, leda::node_array< E >* > pmap_type;
  717. return pmap_type(&a);
  718. }
  719. template < class E >
  720. leda_node_property_map< E, const E&, const leda::node_map< E >* >
  721. make_leda_node_property_map(const leda::node_map< E >& a)
  722. {
  723. typedef leda_node_property_map< E, const E&, const leda::node_map< E >* >
  724. pmap_type;
  725. return pmap_type(&a);
  726. }
  727. template < class E >
  728. leda_node_property_map< E, E&, leda::node_map< E >* >
  729. make_leda_node_property_map(leda::node_map< E >& a)
  730. {
  731. typedef leda_node_property_map< E, E&, leda::node_map< E >* > pmap_type;
  732. return pmap_type(&a);
  733. }
  734. // g++ 'enumeral_type' in template unification not implemented workaround
  735. template < class vtype, class etype, class Tag >
  736. struct property_map< leda::GRAPH< vtype, etype >, Tag >
  737. {
  738. typedef typename leda_property_map< Tag >::template bind_< vtype, etype >
  739. map_gen;
  740. typedef typename map_gen::type type;
  741. typedef typename map_gen::const_type const_type;
  742. };
  743. template < class vtype, class etype, class PropertyTag, class Key >
  744. inline typename boost::property_traits< typename boost::property_map<
  745. leda::GRAPH< vtype, etype >, PropertyTag >::const_type >::value_type
  746. get(PropertyTag p, const leda::GRAPH< vtype, etype >& g, const Key& key)
  747. {
  748. return get(get(p, g), key);
  749. }
  750. template < class vtype, class etype, class PropertyTag, class Key, class Value >
  751. inline void put(PropertyTag p, leda::GRAPH< vtype, etype >& g, const Key& key,
  752. const Value& value)
  753. {
  754. typedef
  755. typename property_map< leda::GRAPH< vtype, etype >, PropertyTag >::type
  756. Map;
  757. Map pmap = get(p, g);
  758. put(pmap, key, value);
  759. }
  760. // property map interface to the LEDA edge_array class
  761. template < class E, class ERef, class EdgeMapPtr >
  762. class leda_edge_property_map
  763. : public put_get_helper< ERef, leda_edge_property_map< E, ERef, EdgeMapPtr > >
  764. {
  765. public:
  766. typedef E value_type;
  767. typedef ERef reference;
  768. typedef leda::edge key_type;
  769. typedef lvalue_property_map_tag category;
  770. leda_edge_property_map(EdgeMapPtr a) : m_array(a) {}
  771. ERef operator[](leda::edge n) const { return (*m_array)[n]; }
  772. protected:
  773. EdgeMapPtr m_array;
  774. };
  775. template < class E >
  776. leda_edge_property_map< E, const E&, const leda::edge_array< E >* >
  777. make_leda_node_property_map(const leda::node_array< E >& a)
  778. {
  779. typedef leda_edge_property_map< E, const E&, const leda::node_array< E >* >
  780. pmap_type;
  781. return pmap_type(&a);
  782. }
  783. template < class E >
  784. leda_edge_property_map< E, E&, leda::edge_array< E >* >
  785. make_leda_edge_property_map(leda::edge_array< E >& a)
  786. {
  787. typedef leda_edge_property_map< E, E&, leda::edge_array< E >* > pmap_type;
  788. return pmap_type(&a);
  789. }
  790. template < class E >
  791. leda_edge_property_map< E, const E&, const leda::edge_map< E >* >
  792. make_leda_edge_property_map(const leda::edge_map< E >& a)
  793. {
  794. typedef leda_edge_property_map< E, const E&, const leda::edge_map< E >* >
  795. pmap_type;
  796. return pmap_type(&a);
  797. }
  798. template < class E >
  799. leda_edge_property_map< E, E&, leda::edge_map< E >* >
  800. make_leda_edge_property_map(leda::edge_map< E >& a)
  801. {
  802. typedef leda_edge_property_map< E, E&, leda::edge_map< E >* > pmap_type;
  803. return pmap_type(&a);
  804. }
  805. } // namespace boost
  806. #endif // BOOST_GRAPH_LEDA_HPP