sl@0: //======================================================================= sl@0: // Copyright 2001 University of Notre Dame. sl@0: // Authors: Jeremy G. Siek and Lie-Quan Lee sl@0: // sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: //======================================================================= sl@0: sl@0: #ifndef BOOST_SUBGRAPH_HPP sl@0: #define BOOST_SUBGRAPH_HPP sl@0: sl@0: // UNDER CONSTRUCTION sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: sl@0: namespace boost { sl@0: sl@0: struct subgraph_tag { }; sl@0: sl@0: // Invariants of an induced subgraph: sl@0: // - If vertex u is in subgraph g, then u must be in g.parent(). sl@0: // - If edge e is in subgraph g, then e must be in g.parent(). sl@0: // - If edge e=(u,v) is in the root graph, then edge e sl@0: // is also in any subgraph that contains both vertex u and v. sl@0: sl@0: // The Graph template parameter must have a vertex_index sl@0: // and edge_index internal property. It is assumed that sl@0: // the vertex indices are assigned automatically by the sl@0: // graph during a call to add_vertex(). It is not sl@0: // assumed that the edge vertices are assigned automatically, sl@0: // they are explicitly assigned here. sl@0: sl@0: template sl@0: class subgraph { sl@0: typedef graph_traits Traits; sl@0: typedef std::list*> ChildrenList; sl@0: public: sl@0: // Graph requirements sl@0: typedef typename Traits::vertex_descriptor vertex_descriptor; sl@0: typedef typename Traits::edge_descriptor edge_descriptor; sl@0: typedef typename Traits::directed_category directed_category; sl@0: typedef typename Traits::edge_parallel_category edge_parallel_category; sl@0: typedef typename Traits::traversal_category traversal_category; sl@0: sl@0: static vertex_descriptor null_vertex() sl@0: { sl@0: return Traits::null_vertex(); sl@0: } sl@0: sl@0: sl@0: // IncidenceGraph requirements sl@0: typedef typename Traits::out_edge_iterator out_edge_iterator; sl@0: typedef typename Traits::degree_size_type degree_size_type; sl@0: sl@0: // AdjacencyGraph requirements sl@0: typedef typename Traits::adjacency_iterator adjacency_iterator; sl@0: sl@0: // VertexListGraph requirements sl@0: typedef typename Traits::vertex_iterator vertex_iterator; sl@0: typedef typename Traits::vertices_size_type vertices_size_type; sl@0: sl@0: // EdgeListGraph requirements sl@0: typedef typename Traits::edge_iterator edge_iterator; sl@0: typedef typename Traits::edges_size_type edges_size_type; sl@0: sl@0: typedef typename Traits::in_edge_iterator in_edge_iterator; sl@0: sl@0: typedef typename Graph::edge_property_type edge_property_type; sl@0: typedef typename Graph::vertex_property_type vertex_property_type; sl@0: typedef subgraph_tag graph_tag; sl@0: typedef Graph graph_type; sl@0: typedef typename Graph::graph_property_type graph_property_type; sl@0: sl@0: // Constructors sl@0: sl@0: // Create the main graph, the root of the subgraph tree sl@0: subgraph() sl@0: : m_parent(0), m_edge_counter(0) sl@0: { } sl@0: subgraph(const graph_property_type& p) sl@0: : m_graph(p), m_parent(0), m_edge_counter(0) sl@0: { } sl@0: subgraph(vertices_size_type n, sl@0: const graph_property_type& p = graph_property_type()) sl@0: : m_graph(n, p), m_parent(0), m_edge_counter(0), m_global_vertex(n) sl@0: { sl@0: typename Graph::vertex_iterator v, v_end; sl@0: vertices_size_type i = 0; sl@0: for (tie(v, v_end) = vertices(m_graph); v != v_end; ++v) sl@0: m_global_vertex[i++] = *v; sl@0: } sl@0: sl@0: // copy constructor sl@0: subgraph(const subgraph& x) sl@0: : m_graph(x.m_graph), m_parent(x.m_parent), sl@0: m_edge_counter(x.m_edge_counter), sl@0: m_global_vertex(x.m_global_vertex), sl@0: m_global_edge(x.m_global_edge) sl@0: { sl@0: // Do a deep copy sl@0: for (typename ChildrenList::const_iterator i = x.m_children.begin(); sl@0: i != x.m_children.end(); ++i) sl@0: m_children.push_back(new subgraph( **i )); sl@0: } sl@0: sl@0: sl@0: ~subgraph() { sl@0: for (typename ChildrenList::iterator i = m_children.begin(); sl@0: i != m_children.end(); ++i) sl@0: delete *i; sl@0: } sl@0: sl@0: sl@0: // Create a subgraph sl@0: subgraph& create_subgraph() { sl@0: m_children.push_back(new subgraph()); sl@0: m_children.back()->m_parent = this; sl@0: return *m_children.back(); sl@0: } sl@0: sl@0: // Create a subgraph with the specified vertex set. sl@0: template sl@0: subgraph& create_subgraph(VertexIterator first, sl@0: VertexIterator last) sl@0: { sl@0: m_children.push_back(new subgraph()); sl@0: m_children.back()->m_parent = this; sl@0: for (; first != last; ++first) sl@0: add_vertex(*first, *m_children.back()); sl@0: return *m_children.back(); sl@0: } sl@0: sl@0: // local <-> global descriptor conversion functions sl@0: vertex_descriptor local_to_global(vertex_descriptor u_local) const sl@0: { sl@0: return m_global_vertex[u_local]; sl@0: } sl@0: vertex_descriptor global_to_local(vertex_descriptor u_global) const sl@0: { sl@0: vertex_descriptor u_local; bool in_subgraph; sl@0: tie(u_local, in_subgraph) = this->find_vertex(u_global); sl@0: assert(in_subgraph == true); sl@0: return u_local; sl@0: } sl@0: edge_descriptor local_to_global(edge_descriptor e_local) const sl@0: { sl@0: return m_global_edge[get(get(edge_index, m_graph), e_local)]; sl@0: } sl@0: edge_descriptor global_to_local(edge_descriptor e_global) const sl@0: { sl@0: return sl@0: (*m_local_edge.find(get(get(edge_index, root().m_graph), e_global))).second; sl@0: } sl@0: sl@0: // Is vertex u (of the root graph) contained in this subgraph? sl@0: // If so, return the matching local vertex. sl@0: std::pair sl@0: find_vertex(vertex_descriptor u_global) const sl@0: { sl@0: typename std::map::const_iterator sl@0: i = m_local_vertex.find(u_global); sl@0: bool valid = i != m_local_vertex.end(); sl@0: return std::make_pair((valid ? (*i).second : null_vertex()), valid); sl@0: } sl@0: sl@0: // Return the parent graph. sl@0: subgraph& parent() { return *m_parent; } sl@0: const subgraph& parent() const { return *m_parent; } sl@0: sl@0: bool is_root() const { return m_parent == 0; } sl@0: sl@0: // Return the root graph of the subgraph tree. sl@0: subgraph& root() { sl@0: if (this->is_root()) sl@0: return *this; sl@0: else sl@0: return m_parent->root(); sl@0: } sl@0: const subgraph& root() const { sl@0: if (this->is_root()) sl@0: return *this; sl@0: else sl@0: return m_parent->root(); sl@0: } sl@0: sl@0: // Return the children subgraphs of this graph/subgraph. sl@0: // Use a list of pointers because the VC++ std::list doesn't like sl@0: // storing incomplete type. sl@0: typedef indirect_iterator< sl@0: typename ChildrenList::const_iterator sl@0: , subgraph sl@0: , std::bidirectional_iterator_tag sl@0: > sl@0: children_iterator; sl@0: sl@0: typedef indirect_iterator< sl@0: typename ChildrenList::const_iterator sl@0: , subgraph const sl@0: , std::bidirectional_iterator_tag sl@0: > sl@0: const_children_iterator; sl@0: sl@0: std::pair sl@0: children() const sl@0: { sl@0: return std::make_pair(const_children_iterator(m_children.begin()), sl@0: const_children_iterator(m_children.end())); sl@0: } sl@0: sl@0: std::pair sl@0: children() sl@0: { sl@0: return std::make_pair(children_iterator(m_children.begin()), sl@0: children_iterator(m_children.end())); sl@0: } sl@0: sl@0: std::size_t num_children() const { return m_children.size(); } sl@0: sl@0: #ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES sl@0: // Bundled properties support sl@0: template sl@0: typename graph::detail::bundled_result::type& sl@0: operator[](Descriptor x) sl@0: { sl@0: if (m_parent == 0) return m_graph[x]; sl@0: else return root().m_graph[local_to_global(x)]; sl@0: } sl@0: sl@0: template sl@0: typename graph::detail::bundled_result::type const& sl@0: operator[](Descriptor x) const sl@0: { sl@0: if (m_parent == 0) return m_graph[x]; sl@0: else return root().m_graph[local_to_global(x)]; sl@0: } sl@0: #endif // BOOST_GRAPH_NO_BUNDLED_PROPERTIES sl@0: sl@0: // private: sl@0: typedef typename property_map::type EdgeIndexMap; sl@0: typedef typename property_traits::value_type edge_index_type; sl@0: BOOST_STATIC_ASSERT((!is_same::value)); sl@0: sl@0: Graph m_graph; sl@0: subgraph* m_parent; sl@0: edge_index_type m_edge_counter; // for generating unique edge indices sl@0: ChildrenList m_children; sl@0: std::vector m_global_vertex; // local -> global sl@0: std::map m_local_vertex; // global -> local sl@0: std::vector m_global_edge; // local -> global sl@0: std::map m_local_edge; // global -> local sl@0: sl@0: edge_descriptor sl@0: local_add_edge(vertex_descriptor u_local, vertex_descriptor v_local, sl@0: edge_descriptor e_global) sl@0: { sl@0: edge_descriptor e_local; sl@0: bool inserted; sl@0: tie(e_local, inserted) = add_edge(u_local, v_local, m_graph); sl@0: put(edge_index, m_graph, e_local, m_edge_counter++); sl@0: m_global_edge.push_back(e_global); sl@0: m_local_edge[get(get(edge_index, this->root()), e_global)] = e_local; sl@0: return e_local; sl@0: } sl@0: sl@0: }; sl@0: sl@0: #ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES sl@0: template sl@0: struct vertex_bundle_type > : vertex_bundle_type { }; sl@0: sl@0: template sl@0: struct edge_bundle_type > : edge_bundle_type { }; sl@0: #endif // BOOST_GRAPH_NO_BUNDLED_PROPERTIES sl@0: sl@0: //=========================================================================== sl@0: // Functions special to the Subgraph Class sl@0: sl@0: template sl@0: typename subgraph::vertex_descriptor sl@0: add_vertex(typename subgraph::vertex_descriptor u_global, sl@0: subgraph& g) sl@0: { sl@0: assert(!g.is_root()); sl@0: typename subgraph::vertex_descriptor u_local, v_global, uu_global; sl@0: typename subgraph::edge_descriptor e_global; sl@0: sl@0: u_local = add_vertex(g.m_graph); sl@0: g.m_global_vertex.push_back(u_global); sl@0: g.m_local_vertex[u_global] = u_local; sl@0: sl@0: subgraph& r = g.root(); sl@0: sl@0: // remember edge global and local maps sl@0: { sl@0: typename subgraph::out_edge_iterator ei, ei_end; sl@0: for (tie(ei, ei_end) = out_edges(u_global, r); sl@0: ei != ei_end; ++ei) { sl@0: e_global = *ei; sl@0: v_global = target(e_global, r); sl@0: if (g.find_vertex(v_global).second == true) sl@0: g.local_add_edge(u_local, g.global_to_local(v_global), e_global); sl@0: } sl@0: } sl@0: if (is_directed(g)) { // not necessary for undirected graph sl@0: typename subgraph::vertex_iterator vi, vi_end; sl@0: typename subgraph::out_edge_iterator ei, ei_end; sl@0: for (tie(vi, vi_end) = vertices(r); vi != vi_end; ++vi) { sl@0: v_global = *vi; sl@0: if (g.find_vertex(v_global).second) sl@0: for (tie(ei, ei_end) = out_edges(*vi, r); ei != ei_end; ++ei) { sl@0: e_global = *ei; sl@0: uu_global = target(e_global, r); sl@0: if (uu_global == u_global && g.find_vertex(v_global).second) sl@0: g.local_add_edge(g.global_to_local(v_global), u_local, e_global); sl@0: } sl@0: } sl@0: } sl@0: sl@0: return u_local; sl@0: } sl@0: sl@0: //=========================================================================== sl@0: // Functions required by the IncidenceGraph concept sl@0: sl@0: template sl@0: std::pair::out_edge_iterator, sl@0: typename graph_traits::out_edge_iterator> sl@0: out_edges(typename graph_traits::vertex_descriptor u_local, sl@0: const subgraph& g) sl@0: { return out_edges(u_local, g.m_graph); } sl@0: sl@0: template sl@0: typename graph_traits::degree_size_type sl@0: out_degree(typename graph_traits::vertex_descriptor u_local, sl@0: const subgraph& g) sl@0: { return out_degree(u_local, g.m_graph); } sl@0: sl@0: template sl@0: typename graph_traits::vertex_descriptor sl@0: source(typename graph_traits::edge_descriptor e_local, sl@0: const subgraph& g) sl@0: { return source(e_local, g.m_graph); } sl@0: sl@0: template sl@0: typename graph_traits::vertex_descriptor sl@0: target(typename graph_traits::edge_descriptor e_local, sl@0: const subgraph& g) sl@0: { return target(e_local, g.m_graph); } sl@0: sl@0: //=========================================================================== sl@0: // Functions required by the BidirectionalGraph concept sl@0: sl@0: template sl@0: std::pair::in_edge_iterator, sl@0: typename graph_traits::in_edge_iterator> sl@0: in_edges(typename graph_traits::vertex_descriptor u_local, sl@0: const subgraph& g) sl@0: { return in_edges(u_local, g.m_graph); } sl@0: sl@0: template sl@0: typename graph_traits::degree_size_type sl@0: in_degree(typename graph_traits::vertex_descriptor u_local, sl@0: const subgraph& g) sl@0: { return in_degree(u_local, g.m_graph); } sl@0: sl@0: template sl@0: typename graph_traits::degree_size_type sl@0: degree(typename graph_traits::vertex_descriptor u_local, sl@0: const subgraph& g) sl@0: { return degree(u_local, g.m_graph); } sl@0: sl@0: //=========================================================================== sl@0: // Functions required by the AdjacencyGraph concept sl@0: sl@0: template sl@0: std::pair::adjacency_iterator, sl@0: typename subgraph::adjacency_iterator> sl@0: adjacent_vertices(typename subgraph::vertex_descriptor u_local, sl@0: const subgraph& g) sl@0: { return adjacent_vertices(u_local, g.m_graph); } sl@0: sl@0: //=========================================================================== sl@0: // Functions required by the VertexListGraph concept sl@0: sl@0: template sl@0: std::pair::vertex_iterator, sl@0: typename subgraph::vertex_iterator> sl@0: vertices(const subgraph& g) sl@0: { return vertices(g.m_graph); } sl@0: sl@0: template sl@0: typename subgraph::vertices_size_type sl@0: num_vertices(const subgraph& g) sl@0: { return num_vertices(g.m_graph); } sl@0: sl@0: //=========================================================================== sl@0: // Functions required by the EdgeListGraph concept sl@0: sl@0: template sl@0: std::pair::edge_iterator, sl@0: typename subgraph::edge_iterator> sl@0: edges(const subgraph& g) sl@0: { return edges(g.m_graph); } sl@0: sl@0: template sl@0: typename subgraph::edges_size_type sl@0: num_edges(const subgraph& g) sl@0: { return num_edges(g.m_graph); } sl@0: sl@0: //=========================================================================== sl@0: // Functions required by the AdjacencyMatrix concept sl@0: sl@0: template sl@0: std::pair::edge_descriptor, bool> sl@0: edge(typename subgraph::vertex_descriptor u_local, sl@0: typename subgraph::vertex_descriptor v_local, sl@0: const subgraph& g) sl@0: { sl@0: return edge(u_local, v_local, g.m_graph); sl@0: } sl@0: sl@0: //=========================================================================== sl@0: // Functions required by the MutableGraph concept sl@0: sl@0: namespace detail { sl@0: sl@0: template sl@0: void add_edge_recur_down sl@0: (Vertex u_global, Vertex v_global, Edge e_global, subgraph& g); sl@0: sl@0: template sl@0: void children_add_edge(Vertex u_global, Vertex v_global, Edge e_global, sl@0: Children& c, subgraph* orig) sl@0: { sl@0: for (typename Children::iterator i = c.begin(); i != c.end(); ++i) sl@0: if ((*i)->find_vertex(u_global).second sl@0: && (*i)->find_vertex(v_global).second) sl@0: add_edge_recur_down(u_global, v_global, e_global, **i, orig); sl@0: } sl@0: sl@0: template sl@0: void add_edge_recur_down sl@0: (Vertex u_global, Vertex v_global, Edge e_global, subgraph& g, sl@0: subgraph* orig) sl@0: { sl@0: if (&g != orig ) { sl@0: // add local edge only if u_global and v_global are in subgraph g sl@0: Vertex u_local, v_local; sl@0: bool u_in_subgraph, v_in_subgraph; sl@0: tie(u_local, u_in_subgraph) = g.find_vertex(u_global); sl@0: tie(v_local, v_in_subgraph) = g.find_vertex(v_global); sl@0: if (u_in_subgraph && v_in_subgraph) sl@0: g.local_add_edge(u_local, v_local, e_global); sl@0: } sl@0: children_add_edge(u_global, v_global, e_global, g.m_children, orig); sl@0: } sl@0: sl@0: template sl@0: std::pair::edge_descriptor, bool> sl@0: add_edge_recur_up(Vertex u_global, Vertex v_global, sl@0: const typename Graph::edge_property_type& ep, sl@0: subgraph& g, subgraph* orig) sl@0: { sl@0: if (g.is_root()) { sl@0: typename subgraph::edge_descriptor e_global; sl@0: bool inserted; sl@0: tie(e_global, inserted) = add_edge(u_global, v_global, ep, g.m_graph); sl@0: put(edge_index, g.m_graph, e_global, g.m_edge_counter++); sl@0: g.m_global_edge.push_back(e_global); sl@0: children_add_edge(u_global, v_global, e_global, g.m_children, orig); sl@0: return std::make_pair(e_global, inserted); sl@0: } else sl@0: return add_edge_recur_up(u_global, v_global, ep, *g.m_parent, orig); sl@0: } sl@0: sl@0: } // namespace detail sl@0: sl@0: // Add an edge to the subgraph g, specified by the local vertex sl@0: // descriptors u and v. In addition, the edge will be added to any sl@0: // other subgraphs which contain vertex descriptors u and v. sl@0: sl@0: template sl@0: std::pair::edge_descriptor, bool> sl@0: add_edge(typename subgraph::vertex_descriptor u_local, sl@0: typename subgraph::vertex_descriptor v_local, sl@0: const typename G::edge_property_type& ep, sl@0: subgraph& g) sl@0: { sl@0: if (g.is_root()) // u_local and v_local are really global sl@0: return detail::add_edge_recur_up(u_local, v_local, ep, g, &g); sl@0: else { sl@0: typename subgraph::edge_descriptor e_local, e_global; sl@0: bool inserted; sl@0: tie(e_global, inserted) = detail::add_edge_recur_up sl@0: (g.local_to_global(u_local), g.local_to_global(v_local), ep, g, &g); sl@0: e_local = g.local_add_edge(u_local, v_local, e_global); sl@0: return std::make_pair(e_local, inserted); sl@0: } sl@0: } sl@0: sl@0: template sl@0: std::pair::edge_descriptor, bool> sl@0: add_edge(typename subgraph::vertex_descriptor u, sl@0: typename subgraph::vertex_descriptor v, sl@0: subgraph& g) sl@0: { sl@0: typename G::edge_property_type ep; sl@0: return add_edge(u, v, ep, g); sl@0: } sl@0: sl@0: namespace detail { sl@0: sl@0: //------------------------------------------------------------------------- sl@0: // implementation of remove_edge(u,v,g) sl@0: sl@0: template sl@0: void remove_edge_recur_down(Vertex u_global, Vertex v_global, sl@0: subgraph& g); sl@0: sl@0: template sl@0: void children_remove_edge(Vertex u_global, Vertex v_global, sl@0: Children& c) sl@0: { sl@0: for (typename Children::iterator i = c.begin(); i != c.end(); ++i) sl@0: if ((*i)->find_vertex(u_global).second sl@0: && (*i)->find_vertex(v_global).second) sl@0: remove_edge_recur_down(u_global, v_global, **i); sl@0: } sl@0: sl@0: template sl@0: void remove_edge_recur_down(Vertex u_global, Vertex v_global, sl@0: subgraph& g) sl@0: { sl@0: Vertex u_local, v_local; sl@0: u_local = g.m_local_vertex[u_global]; sl@0: v_local = g.m_local_vertex[v_global]; sl@0: remove_edge(u_local, v_local, g.m_graph); sl@0: children_remove_edge(u_global, v_global, g.m_children); sl@0: } sl@0: sl@0: template sl@0: void remove_edge_recur_up(Vertex u_global, Vertex v_global, sl@0: subgraph& g) sl@0: { sl@0: if (g.is_root()) { sl@0: remove_edge(u_global, v_global, g.m_graph); sl@0: children_remove_edge(u_global, v_global, g.m_children); sl@0: } else sl@0: remove_edge_recur_up(u_global, v_global, *g.m_parent); sl@0: } sl@0: sl@0: //------------------------------------------------------------------------- sl@0: // implementation of remove_edge(e,g) sl@0: sl@0: template sl@0: void remove_edge_recur_down(Edge e_global, subgraph& g); sl@0: sl@0: template sl@0: void children_remove_edge(Edge e_global, Children& c) sl@0: { sl@0: for (typename Children::iterator i = c.begin(); i != c.end(); ++i) sl@0: if ((*i)->find_vertex(source(e_global, **i)).second sl@0: && (*i)->find_vertex(target(e_global, **i)).second) sl@0: remove_edge_recur_down(source(e_global, **i), sl@0: target(e_global, **i), **i); sl@0: } sl@0: sl@0: template sl@0: void remove_edge_recur_down(Edge e_global, subgraph& g) sl@0: { sl@0: remove_edge(g.global_to_local(e_global), g.m_graph); sl@0: children_remove_edge(e_global, g.m_children); sl@0: } sl@0: sl@0: template sl@0: void remove_edge_recur_up(Edge e_global, subgraph& g) sl@0: { sl@0: if (g.is_root()) { sl@0: remove_edge(e_global, g.m_graph); sl@0: children_remove_edge(e_global, g.m_children); sl@0: } else sl@0: remove_edge_recur_up(e_global, *g.m_parent); sl@0: } sl@0: sl@0: } // namespace detail sl@0: sl@0: template sl@0: void sl@0: remove_edge(typename subgraph::vertex_descriptor u_local, sl@0: typename subgraph::vertex_descriptor v_local, sl@0: subgraph& g) sl@0: { sl@0: if (g.is_root()) sl@0: detail::remove_edge_recur_up(u_local, v_local, g); sl@0: else sl@0: detail::remove_edge_recur_up(g.local_to_global(u_local), sl@0: g.local_to_global(v_local), g); sl@0: } sl@0: sl@0: template sl@0: void sl@0: remove_edge(typename subgraph::edge_descriptor e_local, sl@0: subgraph& g) sl@0: { sl@0: if (g.is_root()) sl@0: detail::remove_edge_recur_up(e_local, g); sl@0: else sl@0: detail::remove_edge_recur_up(g.local_to_global(e_local), g); sl@0: } sl@0: sl@0: template sl@0: void sl@0: remove_edge_if(Predicate p, subgraph& g) sl@0: { sl@0: // This is wrong... sl@0: remove_edge_if(p, g.m_graph); sl@0: } sl@0: sl@0: template sl@0: void sl@0: clear_vertex(typename subgraph::vertex_descriptor v_local, sl@0: subgraph& g) sl@0: { sl@0: // this is wrong... sl@0: clear_vertex(v_local, g.m_graph); sl@0: } sl@0: sl@0: namespace detail { sl@0: sl@0: template sl@0: typename subgraph::vertex_descriptor sl@0: add_vertex_recur_up(subgraph& g) sl@0: { sl@0: typename subgraph::vertex_descriptor u_local, u_global; sl@0: if (g.is_root()) { sl@0: u_global = add_vertex(g.m_graph); sl@0: g.m_global_vertex.push_back(u_global); sl@0: } else { sl@0: u_global = add_vertex_recur_up(*g.m_parent); sl@0: u_local = add_vertex(g.m_graph); sl@0: g.m_global_vertex.push_back(u_global); sl@0: g.m_local_vertex[u_global] = u_local; sl@0: } sl@0: return u_global; sl@0: } sl@0: sl@0: } // namespace detail sl@0: sl@0: template sl@0: typename subgraph::vertex_descriptor sl@0: add_vertex(subgraph& g) sl@0: { sl@0: typename subgraph::vertex_descriptor u_local, u_global; sl@0: if (g.is_root()) { sl@0: u_global = add_vertex(g.m_graph); sl@0: g.m_global_vertex.push_back(u_global); sl@0: u_local = u_global; sl@0: } else { sl@0: u_global = detail::add_vertex_recur_up(g.parent()); sl@0: u_local = add_vertex(g.m_graph); sl@0: g.m_global_vertex.push_back(u_global); sl@0: g.m_local_vertex[u_global] = u_local; sl@0: } sl@0: return u_local; sl@0: } sl@0: sl@0: template sl@0: void remove_vertex(typename subgraph::vertex_descriptor u, sl@0: subgraph& g) sl@0: { sl@0: // UNDER CONSTRUCTION sl@0: assert(false); sl@0: } sl@0: sl@0: sl@0: //=========================================================================== sl@0: // Functions required by the PropertyGraph concept sl@0: sl@0: template sl@0: class subgraph_global_property_map sl@0: : public put_get_helper< sl@0: typename property_traits::reference, sl@0: subgraph_global_property_map > sl@0: { sl@0: typedef property_traits Traits; sl@0: public: sl@0: typedef typename Traits::category category; sl@0: typedef typename Traits::value_type value_type; sl@0: typedef typename Traits::key_type key_type; sl@0: typedef typename Traits::reference reference; sl@0: sl@0: subgraph_global_property_map() { } sl@0: sl@0: subgraph_global_property_map(GraphPtr g) sl@0: : m_g(g) { } sl@0: sl@0: inline reference operator[](key_type e_local) const { sl@0: PropertyMap pmap = get(Tag(), m_g->root().m_graph); sl@0: if (m_g->m_parent == 0) sl@0: return pmap[e_local]; sl@0: else sl@0: return pmap[m_g->local_to_global(e_local)]; sl@0: } sl@0: GraphPtr m_g; sl@0: }; sl@0: sl@0: template sl@0: class subgraph_local_property_map sl@0: : public put_get_helper< sl@0: typename property_traits::reference, sl@0: subgraph_local_property_map > sl@0: { sl@0: typedef property_traits Traits; sl@0: public: sl@0: typedef typename Traits::category category; sl@0: typedef typename Traits::value_type value_type; sl@0: typedef typename Traits::key_type key_type; sl@0: typedef typename Traits::reference reference; sl@0: sl@0: subgraph_local_property_map() { } sl@0: sl@0: subgraph_local_property_map(GraphPtr g) sl@0: : m_g(g) { } sl@0: sl@0: inline reference operator[](key_type e_local) const { sl@0: PropertyMap pmap = get(Tag(), *m_g); sl@0: return pmap[e_local]; sl@0: } sl@0: GraphPtr m_g; sl@0: }; sl@0: sl@0: namespace detail { sl@0: sl@0: struct subgraph_any_pmap { sl@0: template sl@0: class bind_ { sl@0: typedef typename SubGraph::graph_type Graph; sl@0: typedef SubGraph* SubGraphPtr; sl@0: typedef const SubGraph* const_SubGraphPtr; sl@0: typedef typename property_map::type PMap; sl@0: typedef typename property_map::const_type const_PMap; sl@0: public: sl@0: typedef subgraph_global_property_map type; sl@0: typedef subgraph_global_property_map sl@0: const_type; sl@0: }; sl@0: }; sl@0: struct subgraph_id_pmap { sl@0: template sl@0: struct bind_ { sl@0: typedef typename SubGraph::graph_type Graph; sl@0: typedef SubGraph* SubGraphPtr; sl@0: typedef const SubGraph* const_SubGraphPtr; sl@0: typedef typename property_map::type PMap; sl@0: typedef typename property_map::const_type const_PMap; sl@0: public: sl@0: typedef subgraph_local_property_map type; sl@0: typedef subgraph_local_property_map sl@0: const_type; sl@0: }; sl@0: }; sl@0: template sl@0: struct subgraph_choose_pmap_helper { sl@0: typedef subgraph_any_pmap type; sl@0: }; sl@0: template <> sl@0: struct subgraph_choose_pmap_helper { sl@0: typedef subgraph_id_pmap type; sl@0: }; sl@0: template sl@0: struct subgraph_choose_pmap { sl@0: typedef typename subgraph_choose_pmap_helper::type Helper; sl@0: typedef typename Helper::template bind_ Bind; sl@0: typedef typename Bind::type type; sl@0: typedef typename Bind::const_type const_type; sl@0: }; sl@0: struct subgraph_property_generator { sl@0: template sl@0: struct bind_ { sl@0: typedef subgraph_choose_pmap Choice; sl@0: typedef typename Choice::type type; sl@0: typedef typename Choice::const_type const_type; sl@0: }; sl@0: }; sl@0: sl@0: } // namespace detail sl@0: sl@0: template <> sl@0: struct vertex_property_selector { sl@0: typedef detail::subgraph_property_generator type; sl@0: }; sl@0: sl@0: template <> sl@0: struct edge_property_selector { sl@0: typedef detail::subgraph_property_generator type; sl@0: }; sl@0: sl@0: template sl@0: typename property_map< subgraph, Property>::type sl@0: get(Property, subgraph& g) sl@0: { sl@0: typedef typename property_map< subgraph, Property>::type PMap; sl@0: return PMap(&g); sl@0: } sl@0: sl@0: template sl@0: typename property_map< subgraph, Property>::const_type sl@0: get(Property, const subgraph& g) sl@0: { sl@0: typedef typename property_map< subgraph, Property>::const_type PMap; sl@0: return PMap(&g); sl@0: } sl@0: sl@0: template sl@0: typename property_traits< sl@0: typename property_map< subgraph, Property>::const_type sl@0: >::value_type sl@0: get(Property, const subgraph& g, const Key& k) sl@0: { sl@0: typedef typename property_map< subgraph, Property>::const_type PMap; sl@0: PMap pmap(&g); sl@0: return pmap[k]; sl@0: } sl@0: sl@0: template sl@0: void sl@0: put(Property, subgraph& g, const Key& k, const Value& val) sl@0: { sl@0: typedef typename property_map< subgraph, Property>::type PMap; sl@0: PMap pmap(&g); sl@0: pmap[k] = val; sl@0: } sl@0: sl@0: template sl@0: inline sl@0: typename graph_property::type& sl@0: get_property(subgraph& g, Tag tag) { sl@0: return get_property(g.m_graph, tag); sl@0: } sl@0: sl@0: template sl@0: inline sl@0: const typename graph_property::type& sl@0: get_property(const subgraph& g, Tag tag) { sl@0: return get_property(g.m_graph, tag); sl@0: } sl@0: sl@0: //=========================================================================== sl@0: // Miscellaneous Functions sl@0: sl@0: template sl@0: typename subgraph::vertex_descriptor sl@0: vertex(typename subgraph::vertices_size_type n, const subgraph& g) sl@0: { sl@0: return vertex(n, g.m_graph); sl@0: } sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_SUBGRAPH_HPP