1 // Copyright (C) 2004-2006 The Trustees of Indiana University.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Authors: Douglas Gregor
9 #ifndef BOOST_GRAPH_LOCAL_SUBGRAPH_HPP
10 #define BOOST_GRAPH_LOCAL_SUBGRAPH_HPP
12 #ifndef BOOST_GRAPH_USE_MPI
13 #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
16 #include <boost/graph/graph_traits.hpp>
17 #include <boost/graph/filtered_graph.hpp>
18 #include <boost/type_traits/is_same.hpp>
19 #include <boost/type_traits/is_base_and_derived.hpp>
20 #include <boost/graph/parallel/container_traits.hpp>
24 namespace graph { namespace detail {
25 // Optionally, virtually derive from a base class
26 template<bool Derive, typename Base> struct derive_from_if;
27 template<typename Base> struct derive_from_if<true, Base> : virtual Base {};
28 template<typename Base> struct derive_from_if<false, Base> {};
30 template<typename NewBase, typename Tag, typename OldBase = NewBase>
31 struct derive_from_if_tag_is :
32 derive_from_if<(is_base_and_derived<OldBase, Tag>::value
33 || is_same<OldBase, Tag>::value),
37 } } // end namespace graph::detail
39 template<typename DistributedGraph>
43 typedef bool result_type;
44 typedef typename graph_traits<DistributedGraph>::edge_descriptor
47 is_local_edge() : g(0) {}
48 is_local_edge(DistributedGraph& g) : g(&g), owner(get(vertex_owner, g)) {}
50 // Since either the source or target vertex must be local, the
51 // equivalence of their owners indicates a local edge.
52 result_type operator()(const argument_type& e) const
53 { return get(owner, source(e, *g)) == get(owner, target(e, *g)); }
57 typename property_map<DistributedGraph, vertex_owner_t>::const_type owner;
60 template<typename DistributedGraph>
64 typedef bool result_type;
65 typedef typename graph_traits<DistributedGraph>::vertex_descriptor
68 is_local_vertex() : g(0) {}
69 is_local_vertex(DistributedGraph& g) : g(&g), owner(get(vertex_owner, g)) { }
71 // Since either the source or target vertex must be local, the
72 // equivalence of their owners indicates a local edge.
73 result_type operator()(const argument_type& v) const
75 return get(owner, v) == process_id(process_group(*g));
80 typename property_map<DistributedGraph, vertex_owner_t>::const_type owner;
83 template<typename DistributedGraph>
85 : public filtered_graph<DistributedGraph,
86 is_local_edge<DistributedGraph>,
87 is_local_vertex<DistributedGraph> >
89 typedef filtered_graph<DistributedGraph,
90 is_local_edge<DistributedGraph>,
91 is_local_vertex<DistributedGraph> >
93 typedef typename graph_traits<DistributedGraph>::traversal_category
97 struct traversal_category :
98 graph::detail::derive_from_if_tag_is<incidence_graph_tag,
100 graph::detail::derive_from_if_tag_is<adjacency_graph_tag,
102 graph::detail::derive_from_if_tag_is<vertex_list_graph_tag,
104 graph::detail::derive_from_if_tag_is<edge_list_graph_tag,
106 graph::detail::derive_from_if_tag_is<vertex_list_graph_tag,
108 distributed_vertex_list_graph_tag>,
109 graph::detail::derive_from_if_tag_is<edge_list_graph_tag,
111 distributed_edge_list_graph_tag>
114 local_subgraph(DistributedGraph& g)
116 is_local_edge<DistributedGraph>(g),
117 is_local_vertex<DistributedGraph>(g)),
122 // Distributed Container
123 typedef typename boost::graph::parallel::process_group_type<DistributedGraph>::type
126 process_group_type& process_group()
128 using boost::graph::parallel::process_group;
129 return process_group(g);
131 const process_group_type& process_group() const
133 using boost::graph::parallel::process_group;
134 return boost::graph::parallel::process_group(g);
137 DistributedGraph& base() { return g; }
138 const DistributedGraph& base() const { return g; }
144 template<typename DistributedGraph, typename PropertyTag>
145 class property_map<local_subgraph<DistributedGraph>, PropertyTag>
146 : public property_map<DistributedGraph, PropertyTag> { };
148 template<typename DistributedGraph, typename PropertyTag>
149 class property_map<local_subgraph<const DistributedGraph>, PropertyTag>
152 typedef typename property_map<DistributedGraph, PropertyTag>::const_type
154 typedef type const_type;
157 template<typename PropertyTag, typename DistributedGraph>
158 inline typename property_map<local_subgraph<DistributedGraph>, PropertyTag>::type
159 get(PropertyTag p, local_subgraph<DistributedGraph>& g)
160 { return get(p, g.base()); }
162 template<typename PropertyTag, typename DistributedGraph>
163 inline typename property_map<local_subgraph<DistributedGraph>, PropertyTag>
165 get(PropertyTag p, const local_subgraph<DistributedGraph>& g)
166 { return get(p, g.base()); }
168 template<typename DistributedGraph>
169 inline local_subgraph<DistributedGraph>
170 make_local_subgraph(DistributedGraph& g)
171 { return local_subgraph<DistributedGraph>(g); }
173 } // end namespace boost
175 #endif // BOOST_GRAPH_LOCAL_SUBGRAPH_HPP