property_iter.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //=======================================================================
  2. //
  3. // Copyright (c) 2003 Institute of Transport,
  4. // Railway Construction and Operation,
  5. // University of Hanover, Germany
  6. //
  7. // Author: Juergen Hunold
  8. //
  9. // Use, modification and distribution are subject to the
  10. // Boost Software License, Version 1.0. (See accompanying file
  11. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  12. //
  13. //=======================================================================
  14. #include <boost/config.hpp>
  15. #include <iostream>
  16. #include <vector>
  17. #include <set>
  18. #include <utility>
  19. #include <algorithm>
  20. #define VERBOSE 0
  21. #include <boost/utility.hpp>
  22. #include <boost/graph/property_iter_range.hpp>
  23. #include <boost/graph/graph_utility.hpp>
  24. #include <boost/graph/random.hpp>
  25. #include <boost/pending/indirect_cmp.hpp>
  26. #include <boost/random/mersenne_twister.hpp>
  27. enum vertex_id_t { vertex_id = 500 };
  28. enum edge_id_t { edge_id = 501 };
  29. namespace boost {
  30. BOOST_INSTALL_PROPERTY(vertex, id);
  31. BOOST_INSTALL_PROPERTY(edge, id);
  32. }
  33. #include "graph_type.hpp" // this provides a typedef for Graph
  34. using namespace boost;
  35. /*
  36. This program tests the property range iterators
  37. */
  38. using std::cout;
  39. using std::endl;
  40. using std::cerr;
  41. using std::find;
  42. int main(int, char* [])
  43. {
  44. int ret = 0;
  45. std::size_t N = 5, E = 0;
  46. typedef ::Graph Graph;
  47. Graph g;
  48. typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
  49. typedef boost::graph_traits<Graph>::edge_descriptor Edge;
  50. int i, j;
  51. std::size_t current_vertex_id = 0;
  52. std::size_t current_edge_id = 0;
  53. property_map<Graph, vertex_id_t>::type vertex_id_map = get(vertex_id, g);
  54. (void)vertex_id_map;
  55. property_map<Graph, edge_id_t>::type edge_id_map = get(edge_id, g);
  56. (void)edge_id_map;
  57. for (std::size_t k = 0; k < N; ++k)
  58. add_vertex(current_vertex_id++, g);
  59. mt19937 gen;
  60. for (j=0; j < 10; ++j) {
  61. // add_edge
  62. #if VERBOSE
  63. cerr << "Testing add_edge ..." << endl;
  64. #endif
  65. for (i=0; i < 6; ++i) {
  66. Vertex a, b;
  67. a = random_vertex(g, gen);
  68. do {
  69. b = random_vertex(g, gen);
  70. } while ( a == b ); // don't do self edges
  71. #if VERBOSE
  72. cerr << "add_edge(" << vertex_id_map[a] << "," << vertex_id_map[b] <<")" << endl;
  73. #endif
  74. Edge e;
  75. bool inserted;
  76. boost::tie(e, inserted) = add_edge(a, b, current_edge_id++, g);
  77. #if VERBOSE
  78. std::cout << "inserted: " << inserted << std::endl;
  79. std::cout << "source(e,g)" << source(e,g) << endl;
  80. std::cout << "target(e,g)" << target(e,g) << endl;
  81. std::cout << "edge_id[e] = " << edge_id_map[e] << std::endl;
  82. print_edges2(g, vertex_id_map, edge_id_map);
  83. print_graph(g, vertex_id_map);
  84. std::cout << "finished printing" << std::endl;
  85. #endif
  86. }
  87. ++E;
  88. }
  89. typedef boost::graph_property_iter_range< Graph, vertex_id_t>::iterator TNodeIterator;
  90. typedef boost::graph_property_iter_range< Graph, edge_id_t>::iterator TLinkIterator;
  91. TLinkIterator itEdgeBegin, itEdgeEnd;
  92. boost::tie(itEdgeBegin, itEdgeEnd) = get_property_iter_range(g, edge_id);
  93. cout << "Edge iteration:" << endl;
  94. for (; itEdgeBegin != itEdgeEnd; ++itEdgeBegin)
  95. {
  96. cout << *itEdgeBegin;
  97. }
  98. cout << endl;
  99. TNodeIterator itVertexBegin, itVertexEnd;
  100. boost::tie(itVertexBegin, itVertexEnd) = get_property_iter_range(g, vertex_id);
  101. cout << "Vertex iteration:" << endl;
  102. for (; itVertexBegin != itVertexEnd; ++itVertexBegin)
  103. {
  104. cout << *itVertexBegin;
  105. }
  106. cout << endl;
  107. return ret;
  108. }