| 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 | |
| 6 | #include <iostream> |
| 7 | |
| 8 | #include <boost/graph/adjacency_list.hpp> |
| 9 | #include <boost/graph/subgraph.hpp> |
| 10 | #include "typestr.hpp" |
| 11 | |
| 12 | using namespace boost; |
| 13 | |
| 14 | struct TestProps |
| 15 | { |
| 16 | typedef property< vertex_name_t, std::size_t > VertexProp; |
| 17 | typedef property< edge_name_t, std::size_t > EdgeName; |
| 18 | typedef property< edge_index_t, std::size_t, EdgeName > EdgeProp; |
| 19 | |
| 20 | typedef adjacency_list< vecS, vecS, bidirectionalS, VertexProp, EdgeProp > |
| 21 | Graph; |
| 22 | |
| 23 | typedef subgraph< Graph > Subgraph; |
| 24 | typedef graph_traits< Subgraph >::vertex_descriptor Vertex; |
| 25 | typedef graph_traits< Subgraph >::edge_descriptor Edge; |
| 26 | typedef graph_traits< Subgraph >::vertex_iterator VertexIter; |
| 27 | typedef std::pair< VertexIter, VertexIter > VertexRange; |
| 28 | |
| 29 | static void run() |
| 30 | { |
| 31 | // Create a graph with some vertices. |
| 32 | Subgraph g(5); |
| 33 | VertexRange r = vertices(g); |
| 34 | |
| 35 | // Create a child subgraph and add some vertices. |
| 36 | Subgraph& sg = g.create_subgraph(); |
| 37 | Vertex v = add_vertex(u_global: *r.first, g&: sg); |
| 38 | |
| 39 | typedef property_map< Subgraph, vertex_name_t >::type DefaultMap; |
| 40 | DefaultMap map = get(p: vertex_name, g); |
| 41 | BOOST_ASSERT(get(map, v) == 0); |
| 42 | put(pa: map, k: v, v: 5); |
| 43 | BOOST_ASSERT(get(map, v) == 5); |
| 44 | |
| 45 | typedef global_property< vertex_name_t > GlobalProp; |
| 46 | typedef property_map< Subgraph, GlobalProp >::type GlobalVertMap; |
| 47 | GlobalVertMap groot = get(p: global(x: vertex_name), g); |
| 48 | GlobalVertMap gsub = get(p: global(x: vertex_name), g&: sg); |
| 49 | BOOST_ASSERT(get(groot, v) == 5); |
| 50 | BOOST_ASSERT(get(gsub, v) == 5); |
| 51 | put(pa: gsub, k: v, v: 10); |
| 52 | BOOST_ASSERT(get(groot, v) == 10); |
| 53 | BOOST_ASSERT(get(gsub, v) == 10); |
| 54 | BOOST_ASSERT(get(map, v) == 10); |
| 55 | |
| 56 | typedef local_property< vertex_name_t > LocalProp; |
| 57 | typedef property_map< Subgraph, LocalProp >::type LocalVertMap; |
| 58 | LocalVertMap lroot = get(p: local(x: vertex_name), g); // Actually global! |
| 59 | LocalVertMap lsub = get(p: local(x: vertex_name), g&: sg); |
| 60 | BOOST_ASSERT(get(lroot, v) == 10); // Recall it's 10 from above! |
| 61 | BOOST_ASSERT(get(lsub, v) == 0); |
| 62 | put(pa: lsub, k: v, v: 5); |
| 63 | BOOST_ASSERT(get(lsub, v) == 5); |
| 64 | BOOST_ASSERT(get(lroot, v) == 10); // Don't change the root prop |
| 65 | BOOST_ASSERT(get(map, v) == 10); // Don't change the root prop |
| 66 | |
| 67 | // typedef detail::subgraph_local_pmap::bind_<LocalProp, |
| 68 | // Subgraph, void> PM; std::cout << typestr<PM::TagType>() << |
| 69 | // "\n"; std::cout << typestr<PM::PMap>() << "\n"; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | struct TestBundles |
| 74 | { |
| 75 | struct Node |
| 76 | { |
| 77 | Node() : value(-1) {} |
| 78 | int value; |
| 79 | }; |
| 80 | struct Arc |
| 81 | { |
| 82 | Arc() : value(-1) {} |
| 83 | int value; |
| 84 | }; |
| 85 | typedef property< edge_index_t, std::size_t, Arc > EdgeProp; |
| 86 | |
| 87 | typedef adjacency_list< vecS, vecS, bidirectionalS, Node, EdgeProp > Graph; |
| 88 | |
| 89 | typedef subgraph< Graph > Subgraph; |
| 90 | typedef graph_traits< Subgraph >::vertex_descriptor Vertex; |
| 91 | typedef graph_traits< Subgraph >::edge_descriptor Edge; |
| 92 | typedef graph_traits< Subgraph >::vertex_iterator VertexIter; |
| 93 | typedef std::pair< VertexIter, VertexIter > VertexRange; |
| 94 | |
| 95 | static void run() |
| 96 | { |
| 97 | // Create a graph with some vertices. |
| 98 | Subgraph g(5); |
| 99 | VertexRange r = vertices(g); |
| 100 | |
| 101 | // Create a child subgraph and add some vertices. |
| 102 | Subgraph& sg = g.create_subgraph(); |
| 103 | Vertex v = add_vertex(u_global: *r.first, g&: sg); |
| 104 | |
| 105 | sg[v].value = 1; |
| 106 | BOOST_ASSERT(sg[v].value == 1); |
| 107 | BOOST_ASSERT(sg[global(v)].value == 1); |
| 108 | BOOST_ASSERT(sg[local(v)].value == -1); |
| 109 | |
| 110 | sg[local(x: v)].value = 5; |
| 111 | BOOST_ASSERT(sg[local(v)].value == 5); |
| 112 | BOOST_ASSERT(sg[global(v)].value == 1); |
| 113 | BOOST_ASSERT(sg[v].value == 1); |
| 114 | |
| 115 | typedef property_map< Subgraph, local_property< int Node::* > >::type |
| 116 | LocalVertMap; |
| 117 | LocalVertMap lvm = get(p: local(x: &Node::value), g&: sg); |
| 118 | BOOST_ASSERT(get(lvm, v) == 5); |
| 119 | |
| 120 | typedef property_map< Subgraph, global_property< int Node::* > >::type |
| 121 | GlobalVertMap; |
| 122 | GlobalVertMap gvm = get(p: global(x: &Node::value), g&: sg); |
| 123 | BOOST_ASSERT(get(gvm, v) == 1); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | int main(int argc, char* argv[]) |
| 128 | { |
| 129 | TestProps::run(); |
| 130 | TestBundles::run(); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |