Chris@16
|
1 //
|
Chris@16
|
2 //=======================================================================
|
Chris@16
|
3 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
|
Chris@16
|
4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
|
Chris@16
|
5 //
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
7 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //=======================================================================
|
Chris@16
|
10 //
|
Chris@16
|
11 #ifndef BOOST_GRAPH_GRAPH_AS_TREE_HPP
|
Chris@16
|
12 #define BOOST_GRAPH_GRAPH_AS_TREE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <vector>
|
Chris@16
|
15 #include <boost/config.hpp>
|
Chris@16
|
16 #include <boost/property_map/property_map.hpp>
|
Chris@16
|
17 #include <boost/graph/tree_traits.hpp>
|
Chris@16
|
18 #include <boost/graph/graph_traits.hpp>
|
Chris@16
|
19 #include <boost/graph/breadth_first_search.hpp>
|
Chris@16
|
20 #include <boost/graph/visitors.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost {
|
Chris@16
|
23
|
Chris@16
|
24 template <class Graph, class Node, class ChIt, class Derived>
|
Chris@16
|
25 class graph_as_tree_base
|
Chris@16
|
26 {
|
Chris@16
|
27 typedef Derived Tree;
|
Chris@16
|
28 public:
|
Chris@16
|
29 typedef Node node_descriptor;
|
Chris@16
|
30 typedef ChIt children_iterator;
|
Chris@16
|
31
|
Chris@16
|
32 graph_as_tree_base(Graph& g, Node root) : _g(g), _root(root) { }
|
Chris@16
|
33
|
Chris@16
|
34 friend Node root(const Tree& t) { return t._root; }
|
Chris@16
|
35
|
Chris@16
|
36 template <class N>
|
Chris@16
|
37 friend std::pair<ChIt,ChIt>
|
Chris@16
|
38 children(N n, const Tree& t) { return adjacent_vertices(n, t._g); }
|
Chris@16
|
39
|
Chris@16
|
40 template<class N>
|
Chris@16
|
41 friend Node parent(N n, const Tree& t) {
|
Chris@16
|
42 return boost::get(t.parent_pa(), n);
|
Chris@16
|
43 }
|
Chris@16
|
44
|
Chris@16
|
45 Graph& _g;
|
Chris@16
|
46 Node _root;
|
Chris@16
|
47 };
|
Chris@16
|
48
|
Chris@16
|
49 struct graph_as_tree_tag { };
|
Chris@16
|
50
|
Chris@16
|
51 template <class Graph, class ParentMap
|
Chris@16
|
52 , class Node
|
Chris@16
|
53 = typename graph_traits<Graph>::vertex_descriptor
|
Chris@16
|
54 , class ChIt
|
Chris@16
|
55 = typename graph_traits<Graph>::adjacency_iterator
|
Chris@16
|
56 >
|
Chris@16
|
57 class graph_as_tree
|
Chris@16
|
58 : public graph_as_tree_base<Graph, Node, ChIt,
|
Chris@16
|
59 graph_as_tree<Graph,ParentMap,Node,ChIt> >
|
Chris@16
|
60 {
|
Chris@16
|
61 typedef graph_as_tree self;
|
Chris@16
|
62 typedef graph_as_tree_base<Graph, Node, ChIt, self> super;
|
Chris@16
|
63 public:
|
Chris@16
|
64 graph_as_tree(Graph& g, Node root) : super(g, root) { }
|
Chris@16
|
65
|
Chris@16
|
66 graph_as_tree(Graph& g, Node root, ParentMap p) : super(g, root), _p(p) {
|
Chris@16
|
67 breadth_first_search(g, root,
|
Chris@16
|
68 visitor(make_bfs_visitor
|
Chris@16
|
69 (record_predecessors(p, boost::on_tree_edge()))));
|
Chris@16
|
70 }
|
Chris@16
|
71 ParentMap parent_pa() const { return _p; }
|
Chris@16
|
72 typedef graph_as_tree_tag graph_tag; // for property_map
|
Chris@16
|
73 protected:
|
Chris@16
|
74 ParentMap _p;
|
Chris@16
|
75 };
|
Chris@16
|
76
|
Chris@16
|
77
|
Chris@16
|
78 namespace detail {
|
Chris@16
|
79
|
Chris@16
|
80 struct graph_as_tree_vertex_property_selector {
|
Chris@16
|
81 template <typename GraphAsTree, typename Property, typename Tag>
|
Chris@16
|
82 struct bind_ {
|
Chris@16
|
83 typedef typename GraphAsTree::base_type Graph;
|
Chris@16
|
84 typedef property_map<Graph, Tag> PMap;
|
Chris@16
|
85 typedef typename PMap::type type;
|
Chris@16
|
86 typedef typename PMap::const_type const_type;
|
Chris@16
|
87 };
|
Chris@16
|
88 };
|
Chris@16
|
89
|
Chris@16
|
90 struct graph_as_tree_edge_property_selector {
|
Chris@16
|
91 template <typename GraphAsTree, typename Property, typename Tag>
|
Chris@16
|
92 struct bind_ {
|
Chris@16
|
93 typedef typename GraphAsTree::base_type Graph;
|
Chris@16
|
94 typedef property_map<Graph, Tag> PMap;
|
Chris@16
|
95 typedef typename PMap::type type;
|
Chris@16
|
96 typedef typename PMap::const_type const_type;
|
Chris@16
|
97 };
|
Chris@16
|
98 };
|
Chris@16
|
99
|
Chris@16
|
100 } // namespace detail
|
Chris@16
|
101
|
Chris@16
|
102 template <>
|
Chris@16
|
103 struct vertex_property_selector<graph_as_tree_tag> {
|
Chris@16
|
104 typedef detail::graph_as_tree_vertex_property_selector type;
|
Chris@16
|
105 };
|
Chris@16
|
106
|
Chris@16
|
107 template <>
|
Chris@16
|
108 struct edge_property_selector<graph_as_tree_tag> {
|
Chris@16
|
109 typedef detail::graph_as_tree_edge_property_selector type;
|
Chris@16
|
110 };
|
Chris@16
|
111
|
Chris@16
|
112 template <typename Graph, typename P, typename N, typename C,
|
Chris@16
|
113 typename Property>
|
Chris@16
|
114 typename property_map<Graph, Property>::type
|
Chris@16
|
115 get(Property p, graph_as_tree<Graph,P,N,C>& g)
|
Chris@16
|
116 {
|
Chris@16
|
117 return get(p, g._g);
|
Chris@16
|
118 }
|
Chris@16
|
119
|
Chris@16
|
120 template <typename Graph, typename P, typename N, typename C,
|
Chris@16
|
121 typename Property>
|
Chris@16
|
122 typename property_map<Graph, Property>::const_type
|
Chris@16
|
123 get(Property p, const graph_as_tree<Graph,P,N,C>& g)
|
Chris@16
|
124 {
|
Chris@16
|
125 const Graph& gref = g._g; // in case GRef is non-const
|
Chris@16
|
126 return get(p, gref);
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 template <typename Graph, typename P, typename N, typename C,
|
Chris@16
|
130 typename Property, typename Key>
|
Chris@16
|
131 typename property_traits<
|
Chris@16
|
132 typename property_map<Graph, Property>::const_type
|
Chris@16
|
133 >::value_type
|
Chris@16
|
134 get(Property p, const graph_as_tree<Graph,P,N,C>& g, const Key& k)
|
Chris@16
|
135 {
|
Chris@16
|
136 return get(p, g._g, k);
|
Chris@16
|
137 }
|
Chris@16
|
138
|
Chris@16
|
139 template <typename Graph, typename P, typename N, typename C,
|
Chris@16
|
140 typename Property, typename Key, typename Value>
|
Chris@16
|
141 void
|
Chris@16
|
142 put(Property p, const graph_as_tree<Graph,P,N,C>& g, const Key& k,
|
Chris@16
|
143 const Value& val)
|
Chris@16
|
144 {
|
Chris@16
|
145 put(p, g._g, k, val);
|
Chris@16
|
146 }
|
Chris@16
|
147
|
Chris@16
|
148 } // namespace boost
|
Chris@16
|
149
|
Chris@16
|
150 #endif // BOOST_GRAPH_GRAPH_AS_TREE_HPP
|