Chris@16: // Chris@16: //======================================================================= Chris@16: // Copyright 1997-2001 University of Notre Dame. Chris@16: // Authors: Jeremy G. Siek, Lie-Quan Lee, Andrew Lumsdaine Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: //======================================================================= Chris@16: // Chris@16: Chris@16: /* Chris@16: This file implements the following functions: Chris@16: Chris@16: Chris@16: template Chris@16: void copy_graph(const VertexListGraph& g_in, MutableGraph& g_out) Chris@16: Chris@16: template Chris@16: void copy_graph(const VertexListGraph& g_in, MutableGraph& g_out, Chris@16: const bgl_named_params& params) Chris@16: Chris@16: Chris@16: template Chris@16: typename graph_traits::vertex_descriptor Chris@16: copy_component(IncidenceGraph& g_in, Chris@16: typename graph_traits::vertex_descriptor src, Chris@16: MutableGraph& g_out) Chris@16: Chris@16: template Chris@16: typename graph_traits::vertex_descriptor Chris@16: copy_component(IncidenceGraph& g_in, Chris@16: typename graph_traits::vertex_descriptor src, Chris@16: MutableGraph& g_out, Chris@16: const bgl_named_params& params) Chris@16: */ Chris@16: Chris@16: Chris@16: #ifndef BOOST_GRAPH_COPY_HPP Chris@16: #define BOOST_GRAPH_COPY_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: // Hack to make transpose_graph work with the same interface as before Chris@16: template Chris@16: struct remove_reverse_edge_descriptor { Chris@16: typedef Desc type; Chris@16: static Desc convert(const Desc& d, const Graph&) {return d;} Chris@16: }; Chris@16: Chris@16: template Chris@16: struct remove_reverse_edge_descriptor > { Chris@16: typedef Desc type; Chris@16: static Desc convert(const reverse_graph_edge_descriptor& d, const Graph& g) { Chris@16: return get(edge_underlying, g, d); Chris@16: } Chris@16: }; Chris@16: Chris@16: // Add a reverse_graph_edge_descriptor wrapper if the Graph is a Chris@16: // reverse_graph but the edge descriptor is from the original graph (this Chris@16: // case comes from the fact that transpose_graph uses reverse_graph Chris@16: // internally but doesn't expose the different edge descriptor type to the Chris@16: // user). Chris@16: template Chris@16: struct add_reverse_edge_descriptor { Chris@16: typedef Desc type; Chris@16: static Desc convert(const Desc& d) {return d;} Chris@16: }; Chris@16: Chris@16: template Chris@16: struct add_reverse_edge_descriptor > { Chris@16: typedef reverse_graph_edge_descriptor type; Chris@16: static reverse_graph_edge_descriptor convert(const Desc& d) { Chris@16: return reverse_graph_edge_descriptor(d); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct add_reverse_edge_descriptor, boost::reverse_graph > { Chris@16: typedef reverse_graph_edge_descriptor type; Chris@16: static reverse_graph_edge_descriptor convert(const reverse_graph_edge_descriptor& d) { Chris@16: return d; Chris@16: } Chris@16: }; Chris@16: Chris@16: // Default edge and vertex property copiers Chris@16: Chris@16: template Chris@16: struct edge_copier { Chris@16: edge_copier(const Graph1& g1, Graph2& g2) Chris@16: : edge_all_map1(get(edge_all, g1)), Chris@16: edge_all_map2(get(edge_all, g2)) { } Chris@16: Chris@16: template Chris@16: void operator()(const Edge1& e1, Edge2& e2) const { Chris@16: put(edge_all_map2, e2, get(edge_all_map1, add_reverse_edge_descriptor::convert(e1))); Chris@16: } Chris@16: typename property_map::const_type edge_all_map1; Chris@16: mutable typename property_map::type edge_all_map2; Chris@16: }; Chris@16: template Chris@16: inline edge_copier Chris@16: make_edge_copier(const Graph1& g1, Graph2& g2) Chris@16: { Chris@16: return edge_copier(g1, g2); Chris@16: } Chris@16: Chris@16: template Chris@16: struct vertex_copier { Chris@16: vertex_copier(const Graph1& g1, Graph2& g2) Chris@16: : vertex_all_map1(get(vertex_all, g1)), Chris@16: vertex_all_map2(get(vertex_all, g2)) { } Chris@16: Chris@16: template Chris@16: void operator()(const Vertex1& v1, Vertex2& v2) const { Chris@16: put(vertex_all_map2, v2, get(vertex_all_map1, v1)); Chris@16: } Chris@16: typename property_map::const_type vertex_all_map1; Chris@16: mutable typename property_map::type Chris@16: vertex_all_map2; Chris@16: }; Chris@16: template Chris@16: inline vertex_copier Chris@16: make_vertex_copier(const Graph1& g1, Graph2& g2) Chris@16: { Chris@16: return vertex_copier(g1, g2); Chris@16: } Chris@16: Chris@16: // Copy all the vertices and edges of graph g_in into graph g_out. Chris@16: // The copy_vertex and copy_edge function objects control how vertex Chris@16: // and edge properties are copied. Chris@16: Chris@16: template Chris@16: struct copy_graph_impl { }; Chris@16: Chris@16: template <> struct copy_graph_impl<0> Chris@16: { Chris@16: template Chris@16: static void apply(const Graph& g_in, MutableGraph& g_out, Chris@16: CopyVertex copy_vertex, CopyEdge copy_edge, Chris@16: Orig2CopyVertexIndexMap orig2copy, IndexMap) Chris@16: { Chris@16: typedef remove_reverse_edge_descriptor::edge_descriptor> cvt; Chris@16: typename graph_traits::vertex_iterator vi, vi_end; Chris@16: for (boost::tie(vi, vi_end) = vertices(g_in); vi != vi_end; ++vi) { Chris@16: typename graph_traits::vertex_descriptor Chris@16: new_v = add_vertex(g_out); Chris@16: put(orig2copy, *vi, new_v); Chris@16: copy_vertex(*vi, new_v); Chris@16: } Chris@16: typename graph_traits::edge_iterator ei, ei_end; Chris@16: for (boost::tie(ei, ei_end) = edges(g_in); ei != ei_end; ++ei) { Chris@16: typename graph_traits::edge_descriptor new_e; Chris@16: bool inserted; Chris@16: boost::tie(new_e, inserted) = add_edge(get(orig2copy, source(*ei, g_in)), Chris@16: get(orig2copy, target(*ei, g_in)), Chris@16: g_out); Chris@16: copy_edge(cvt::convert(*ei, g_in), new_e); Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: // for directed graphs Chris@16: template <> struct copy_graph_impl<1> Chris@16: { Chris@16: template Chris@16: static void apply(const Graph& g_in, MutableGraph& g_out, Chris@16: CopyVertex copy_vertex, CopyEdge copy_edge, Chris@16: Orig2CopyVertexIndexMap orig2copy, IndexMap) Chris@16: { Chris@16: typedef remove_reverse_edge_descriptor::edge_descriptor> cvt; Chris@16: typename graph_traits::vertex_iterator vi, vi_end; Chris@16: for (boost::tie(vi, vi_end) = vertices(g_in); vi != vi_end; ++vi) { Chris@16: typename graph_traits::vertex_descriptor Chris@16: new_v = add_vertex(g_out); Chris@16: put(orig2copy, *vi, new_v); Chris@16: copy_vertex(*vi, new_v); Chris@16: } Chris@16: for (boost::tie(vi, vi_end) = vertices(g_in); vi != vi_end; ++vi) { Chris@16: typename graph_traits::out_edge_iterator ei, ei_end; Chris@16: for (boost::tie(ei, ei_end) = out_edges(*vi, g_in); ei != ei_end; ++ei) { Chris@16: typename graph_traits::edge_descriptor new_e; Chris@16: bool inserted; Chris@16: boost::tie(new_e, inserted) = add_edge(get(orig2copy, source(*ei, g_in)), Chris@16: get(orig2copy, target(*ei, g_in)), Chris@16: g_out); Chris@16: copy_edge(cvt::convert(*ei, g_in), new_e); Chris@16: } Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: // for undirected graphs Chris@16: template <> struct copy_graph_impl<2> Chris@16: { Chris@16: template Chris@16: static void apply(const Graph& g_in, MutableGraph& g_out, Chris@16: CopyVertex copy_vertex, CopyEdge copy_edge, Chris@16: Orig2CopyVertexIndexMap orig2copy, Chris@16: IndexMap index_map) Chris@16: { Chris@16: typedef remove_reverse_edge_descriptor::edge_descriptor> cvt; Chris@16: typedef color_traits Color; Chris@16: std::vector Chris@16: color(num_vertices(g_in), Color::white()); Chris@16: typename graph_traits::vertex_iterator vi, vi_end; Chris@16: for (boost::tie(vi, vi_end) = vertices(g_in); vi != vi_end; ++vi) { Chris@16: typename graph_traits::vertex_descriptor Chris@16: new_v = add_vertex(g_out); Chris@16: put(orig2copy, *vi, new_v); Chris@16: copy_vertex(*vi, new_v); Chris@16: } Chris@16: for (boost::tie(vi, vi_end) = vertices(g_in); vi != vi_end; ++vi) { Chris@16: typename graph_traits::out_edge_iterator ei, ei_end; Chris@16: for (boost::tie(ei, ei_end) = out_edges(*vi, g_in); ei != ei_end; ++ei) { Chris@16: typename graph_traits::edge_descriptor new_e; Chris@16: bool inserted; Chris@16: if (color[get(index_map, target(*ei, g_in))] == Color::white()) { Chris@16: boost::tie(new_e, inserted) = add_edge(get(orig2copy, source(*ei,g_in)), Chris@16: get(orig2copy, target(*ei,g_in)), Chris@16: g_out); Chris@16: copy_edge(cvt::convert(*ei, g_in), new_e); Chris@16: } Chris@16: } Chris@16: color[get(index_map, *vi)] = Color::black(); Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct choose_graph_copy { Chris@16: typedef typename Graph::traversal_category Trv; Chris@16: typedef typename Graph::directed_category Dr; Chris@16: enum { algo = Chris@16: (is_convertible::value Chris@16: && is_convertible::value) Chris@16: ? 0 : is_convertible::value ? 1 : 2 }; Chris@16: typedef copy_graph_impl type; Chris@16: }; Chris@16: Chris@16: //------------------------------------------------------------------------- Chris@16: struct choose_copier_parameter { Chris@16: template Chris@16: struct bind_ { Chris@16: typedef const P& result_type; Chris@16: static result_type apply(const P& p, const G1&, G2&) Chris@16: { return p; } Chris@16: }; Chris@16: }; Chris@16: struct choose_default_edge_copier { Chris@16: template Chris@16: struct bind_ { Chris@16: typedef edge_copier result_type; Chris@16: static result_type apply(const P&, const G1& g1, G2& g2) { Chris@16: return result_type(g1, g2); Chris@16: } Chris@16: }; Chris@16: }; Chris@16: template Chris@16: struct choose_edge_copy { Chris@16: typedef choose_copier_parameter type; Chris@16: }; Chris@16: template <> Chris@16: struct choose_edge_copy { Chris@16: typedef choose_default_edge_copier type; Chris@16: }; Chris@16: template Chris@16: struct choose_edge_copier_helper { Chris@16: typedef typename choose_edge_copy::type Selector; Chris@16: typedef typename Selector:: template bind_ Bind; Chris@16: typedef Bind type; Chris@16: typedef typename Bind::result_type result_type; Chris@16: }; Chris@16: template Chris@16: typename detail::choose_edge_copier_helper::result_type Chris@16: choose_edge_copier(const Param& params, const G1& g_in, G2& g_out) Chris@16: { Chris@16: typedef typename Chris@16: detail::choose_edge_copier_helper::type Choice; Chris@16: return Choice::apply(params, g_in, g_out); Chris@16: } Chris@16: Chris@16: Chris@16: struct choose_default_vertex_copier { Chris@16: template Chris@16: struct bind_ { Chris@16: typedef vertex_copier result_type; Chris@16: static result_type apply(const P&, const G1& g1, G2& g2) { Chris@16: return result_type(g1, g2); Chris@16: } Chris@16: }; Chris@16: }; Chris@16: template Chris@16: struct choose_vertex_copy { Chris@16: typedef choose_copier_parameter type; Chris@16: }; Chris@16: template <> Chris@16: struct choose_vertex_copy { Chris@16: typedef choose_default_vertex_copier type; Chris@16: }; Chris@16: template Chris@16: struct choose_vertex_copier_helper { Chris@16: typedef typename choose_vertex_copy::type Selector; Chris@16: typedef typename Selector:: template bind_ Bind; Chris@16: typedef Bind type; Chris@16: typedef typename Bind::result_type result_type; Chris@16: }; Chris@16: template Chris@16: typename detail::choose_vertex_copier_helper::result_type Chris@16: choose_vertex_copier(const Param& params, const G1& g_in, G2& g_out) Chris@16: { Chris@16: typedef typename Chris@16: detail::choose_vertex_copier_helper::type Choice; Chris@16: return Choice::apply(params, g_in, g_out); Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: Chris@16: template Chris@16: void copy_graph(const VertexListGraph& g_in, MutableGraph& g_out) Chris@16: { Chris@16: if (num_vertices(g_in) == 0) Chris@16: return; Chris@16: typedef typename graph_traits::vertex_descriptor vertex_t; Chris@16: std::vector orig2copy(num_vertices(g_in)); Chris@16: typedef typename detail::choose_graph_copy::type Chris@16: copy_impl; Chris@16: copy_impl::apply Chris@16: (g_in, g_out, Chris@16: detail::make_vertex_copier(g_in, g_out), Chris@16: detail::make_edge_copier(g_in, g_out), Chris@16: make_iterator_property_map(orig2copy.begin(), Chris@16: get(vertex_index, g_in), orig2copy[0]), Chris@16: get(vertex_index, g_in) Chris@16: ); Chris@16: } Chris@16: Chris@16: template Chris@16: void copy_graph(const VertexListGraph& g_in, MutableGraph& g_out, Chris@16: const bgl_named_params& params) Chris@16: { Chris@16: typename std::vector::size_type n; Chris@16: n = is_default_param(get_param(params, orig_to_copy_t())) Chris@16: ? num_vertices(g_in) : 1; Chris@16: if (n == 0) Chris@16: return; Chris@16: std::vector::vertex_descriptor> Chris@16: orig2copy(n); Chris@16: Chris@16: typedef typename detail::choose_graph_copy::type Chris@16: copy_impl; Chris@16: copy_impl::apply Chris@16: (g_in, g_out, Chris@16: detail::choose_vertex_copier(get_param(params, vertex_copy_t()), Chris@16: g_in, g_out), Chris@16: detail::choose_edge_copier(get_param(params, edge_copy_t()), Chris@16: g_in, g_out), Chris@16: choose_param(get_param(params, orig_to_copy_t()), Chris@16: make_iterator_property_map Chris@16: (orig2copy.begin(), Chris@16: choose_const_pmap(get_param(params, vertex_index), Chris@16: g_in, vertex_index), orig2copy[0])), Chris@16: choose_const_pmap(get_param(params, vertex_index), g_in, vertex_index) Chris@16: ); Chris@16: } Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: struct graph_copy_visitor : public bfs_visitor<> Chris@16: { Chris@16: graph_copy_visitor(NewGraph& graph, Copy2OrigIndexMap c, Chris@16: CopyVertex cv, CopyEdge ce) Chris@16: : g_out(graph), orig2copy(c), copy_vertex(cv), copy_edge(ce) { } Chris@16: Chris@16: template Chris@16: typename graph_traits::vertex_descriptor copy_one_vertex(Vertex u) const { Chris@16: typename graph_traits::vertex_descriptor Chris@16: new_u = add_vertex(g_out); Chris@16: put(orig2copy, u, new_u); Chris@16: copy_vertex(u, new_u); Chris@16: return new_u; Chris@16: } Chris@16: Chris@16: template Chris@16: void tree_edge(Edge e, const Graph& g_in) const { Chris@16: // For a tree edge, the target vertex has not been copied yet. Chris@16: typename graph_traits::edge_descriptor new_e; Chris@16: bool inserted; Chris@16: boost::tie(new_e, inserted) = add_edge(get(orig2copy, source(e, g_in)), Chris@16: this->copy_one_vertex(target(e, g_in)), Chris@16: g_out); Chris@16: copy_edge(e, new_e); Chris@16: } Chris@16: Chris@16: template Chris@16: void non_tree_edge(Edge e, const Graph& g_in) const { Chris@16: // For a non-tree edge, the target vertex has already been copied. Chris@16: typename graph_traits::edge_descriptor new_e; Chris@16: bool inserted; Chris@16: boost::tie(new_e, inserted) = add_edge(get(orig2copy, source(e, g_in)), Chris@16: get(orig2copy, target(e, g_in)), Chris@16: g_out); Chris@16: copy_edge(e, new_e); Chris@16: } Chris@16: private: Chris@16: NewGraph& g_out; Chris@16: Copy2OrigIndexMap orig2copy; Chris@16: CopyVertex copy_vertex; Chris@16: CopyEdge copy_edge; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename graph_traits::vertex_descriptor Chris@16: copy_component_impl Chris@16: (const Graph& g_in, Chris@16: typename graph_traits::vertex_descriptor src, Chris@16: MutableGraph& g_out, Chris@16: CopyVertex copy_vertex, CopyEdge copy_edge, Chris@16: Orig2CopyVertexIndexMap orig2copy, Chris@16: const Params& params) Chris@16: { Chris@16: graph_copy_visitor vis(g_out, orig2copy, copy_vertex, copy_edge); Chris@16: typename graph_traits::vertex_descriptor src_copy Chris@16: = vis.copy_one_vertex(src); Chris@16: breadth_first_search(g_in, src, params.visitor(vis)); Chris@16: return src_copy; Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: Chris@16: // Copy all the vertices and edges of graph g_in that are reachable Chris@16: // from the source vertex into graph g_out. Return the vertex Chris@16: // in g_out that matches the source vertex of g_in. Chris@16: template Chris@16: typename graph_traits::vertex_descriptor Chris@16: copy_component(IncidenceGraph& g_in, Chris@16: typename graph_traits::vertex_descriptor src, Chris@16: MutableGraph& g_out, Chris@16: const bgl_named_params& params) Chris@16: { Chris@16: typename std::vector::size_type n; Chris@16: n = is_default_param(get_param(params, orig_to_copy_t())) Chris@16: ? num_vertices(g_in) : 1; Chris@16: std::vector::vertex_descriptor> Chris@16: orig2copy(n); Chris@16: Chris@16: return detail::copy_component_impl Chris@16: (g_in, src, g_out, Chris@16: detail::choose_vertex_copier(get_param(params, vertex_copy_t()), Chris@16: g_in, g_out), Chris@16: detail::choose_edge_copier(get_param(params, edge_copy_t()), Chris@16: g_in, g_out), Chris@16: choose_param(get_param(params, orig_to_copy_t()), Chris@16: make_iterator_property_map Chris@16: (orig2copy.begin(), Chris@16: choose_pmap(get_param(params, vertex_index), Chris@16: g_in, vertex_index), orig2copy[0])), Chris@16: params Chris@16: ); Chris@16: } Chris@16: Chris@16: template Chris@16: typename graph_traits::vertex_descriptor Chris@16: copy_component(IncidenceGraph& g_in, Chris@16: typename graph_traits::vertex_descriptor src, Chris@16: MutableGraph& g_out) Chris@16: { Chris@16: std::vector::vertex_descriptor> Chris@16: orig2copy(num_vertices(g_in)); Chris@16: Chris@16: return detail::copy_component_impl Chris@16: (g_in, src, g_out, Chris@16: make_vertex_copier(g_in, g_out), Chris@16: make_edge_copier(g_in, g_out), Chris@16: make_iterator_property_map(orig2copy.begin(), Chris@16: get(vertex_index, g_in), orig2copy[0]), Chris@16: bgl_named_params('x') // dummy param object Chris@16: ); Chris@16: } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_GRAPH_COPY_HPP