subgraph_props.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // (C) Copyright Andrew Sutton 2009
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <iostream>
  6. #include <boost/graph/adjacency_list.hpp>
  7. #include <boost/graph/subgraph.hpp>
  8. #include "typestr.hpp"
  9. using namespace boost;
  10. struct TestProps {
  11. typedef property<vertex_name_t, std::size_t> VertexProp;
  12. typedef property<edge_name_t, std::size_t> EdgeName;
  13. typedef property<edge_index_t, std::size_t, EdgeName> EdgeProp;
  14. typedef adjacency_list<
  15. vecS, vecS, bidirectionalS, VertexProp, EdgeProp
  16. > Graph;
  17. typedef subgraph<Graph> Subgraph;
  18. typedef graph_traits<Subgraph>::vertex_descriptor Vertex;
  19. typedef graph_traits<Subgraph>::edge_descriptor Edge;
  20. typedef graph_traits<Subgraph>::vertex_iterator VertexIter;
  21. typedef std::pair<VertexIter, VertexIter> VertexRange;
  22. static void run() {
  23. // Create a graph with some vertices.
  24. Subgraph g(5);
  25. VertexRange r = vertices(g);
  26. // Create a child subgraph and add some vertices.
  27. Subgraph& sg = g.create_subgraph();
  28. Vertex v = add_vertex(*r.first, sg);
  29. typedef property_map<Subgraph, vertex_name_t>::type DefaultMap;
  30. DefaultMap map = get(vertex_name, g);
  31. BOOST_ASSERT(get(map, v) == 0);
  32. put(map, v, 5);
  33. BOOST_ASSERT(get(map, v) == 5);
  34. typedef global_property<vertex_name_t> GlobalProp;
  35. typedef property_map<Subgraph, GlobalProp>::type GlobalVertMap;
  36. GlobalVertMap groot = get(global(vertex_name), g);
  37. GlobalVertMap gsub = get(global(vertex_name), sg);
  38. BOOST_ASSERT(get(groot, v) == 5);
  39. BOOST_ASSERT(get(gsub, v) == 5);
  40. put(gsub, v, 10);
  41. BOOST_ASSERT(get(groot, v) == 10);
  42. BOOST_ASSERT(get(gsub, v) == 10);
  43. BOOST_ASSERT(get(map, v) == 10);
  44. typedef local_property<vertex_name_t> LocalProp;
  45. typedef property_map<Subgraph, LocalProp>::type LocalVertMap;
  46. LocalVertMap lroot = get(local(vertex_name), g); // Actually global!
  47. LocalVertMap lsub = get(local(vertex_name), sg);
  48. BOOST_ASSERT(get(lroot, v) == 10); // Recall it's 10 from above!
  49. BOOST_ASSERT(get(lsub, v) == 0);
  50. put(lsub, v, 5);
  51. BOOST_ASSERT(get(lsub, v) == 5);
  52. BOOST_ASSERT(get(lroot, v) == 10); // Don't change the root prop
  53. BOOST_ASSERT(get(map, v) == 10); // Don't change the root prop
  54. // typedef detail::subgraph_local_pmap::bind_<LocalProp, Subgraph, void> PM;
  55. // std::cout << typestr<PM::TagType>() << "\n";
  56. // std::cout << typestr<PM::PMap>() << "\n";
  57. }
  58. };
  59. struct TestBundles {
  60. struct Node {
  61. Node() : value(-1) { }
  62. int value;
  63. };
  64. struct Arc {
  65. Arc() : value(-1) { }
  66. int value;
  67. };
  68. typedef property<edge_index_t, std::size_t, Arc> EdgeProp;
  69. typedef adjacency_list<
  70. vecS, vecS, bidirectionalS, Node, EdgeProp
  71. > Graph;
  72. typedef subgraph<Graph> Subgraph;
  73. typedef graph_traits<Subgraph>::vertex_descriptor Vertex;
  74. typedef graph_traits<Subgraph>::edge_descriptor Edge;
  75. typedef graph_traits<Subgraph>::vertex_iterator VertexIter;
  76. typedef std::pair<VertexIter, VertexIter> VertexRange;
  77. static void run() {
  78. // Create a graph with some vertices.
  79. Subgraph g(5);
  80. VertexRange r = vertices(g);
  81. // Create a child subgraph and add some vertices.
  82. Subgraph& sg = g.create_subgraph();
  83. Vertex v = add_vertex(*r.first, sg);
  84. sg[v].value = 1;
  85. BOOST_ASSERT(sg[v].value == 1);
  86. BOOST_ASSERT(sg[global(v)].value == 1);
  87. BOOST_ASSERT(sg[local(v)].value == -1);
  88. sg[local(v)].value = 5;
  89. BOOST_ASSERT(sg[local(v)].value == 5);
  90. BOOST_ASSERT(sg[global(v)].value == 1);
  91. BOOST_ASSERT(sg[v].value == 1);
  92. typedef property_map<
  93. Subgraph, local_property<int Node::*>
  94. >::type LocalVertMap;
  95. LocalVertMap lvm = get(local(&Node::value), sg);
  96. BOOST_ASSERT(get(lvm, v) == 5);
  97. typedef property_map<
  98. Subgraph, global_property<int Node::*>
  99. >::type GlobalVertMap;
  100. GlobalVertMap gvm = get(global(&Node::value), sg);
  101. BOOST_ASSERT(get(gvm, v) == 1);
  102. }
  103. };
  104. int main(int argc, char* argv[])
  105. {
  106. TestProps::run();
  107. TestBundles::run();
  108. return 0;
  109. }