// (C) Copyright David Abrahams 2000. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef REVERSE_GRAPH_DWA092300_H_ # define REVERSE_GRAPH_DWA092300_H_ #include #include #include #include #include #include #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) // Stay out of the way of the concept checking class # define BidirectionalGraph BidirectionalGraph_ #endif namespace boost { struct reverse_graph_tag { }; namespace detail { template class reverse_graph_edge_descriptor { public: EdgeDesc underlying_descx; // Odd name is because this needs to be public but shouldn't be exposed to users anymore private: typedef EdgeDesc base_descriptor_type; public: explicit reverse_graph_edge_descriptor(const EdgeDesc& underlying_descx = EdgeDesc()) : underlying_descx(underlying_descx) {} friend bool operator==(const reverse_graph_edge_descriptor& a, const reverse_graph_edge_descriptor& b) { return a.underlying_descx == b.underlying_descx; } friend bool operator!=(const reverse_graph_edge_descriptor& a, const reverse_graph_edge_descriptor& b) { return a.underlying_descx != b.underlying_descx; } friend bool operator<(const reverse_graph_edge_descriptor& a, const reverse_graph_edge_descriptor& b) { return a.underlying_descx < b.underlying_descx; } friend bool operator>(const reverse_graph_edge_descriptor& a, const reverse_graph_edge_descriptor& b) { return a.underlying_descx > b.underlying_descx; } friend bool operator<=(const reverse_graph_edge_descriptor& a, const reverse_graph_edge_descriptor& b) { return a.underlying_descx <= b.underlying_descx; } friend bool operator>=(const reverse_graph_edge_descriptor& a, const reverse_graph_edge_descriptor& b) { return a.underlying_descx >= b.underlying_descx; } }; template struct reverse_graph_edge_descriptor_maker { typedef reverse_graph_edge_descriptor result_type; reverse_graph_edge_descriptor operator()(const EdgeDesc& ed) const { return reverse_graph_edge_descriptor(ed); } }; template std::pair, Iter>, transform_iterator, Iter> > reverse_edge_iter_pair(const std::pair& ip) { return std::make_pair(make_transform_iterator(ip.first, reverse_graph_edge_descriptor_maker()), make_transform_iterator(ip.second, reverse_graph_edge_descriptor_maker())); } // Get the underlying descriptor from a vertex or edge descriptor template struct get_underlying_descriptor_from_reverse_descriptor { typedef Desc type; static Desc convert(const Desc& d) {return d;} }; template struct get_underlying_descriptor_from_reverse_descriptor > { typedef Desc type; static Desc convert(const reverse_graph_edge_descriptor& d) {return d.underlying_descx;} }; template struct choose_rev_edge_iter { }; template <> struct choose_rev_edge_iter { template struct bind_ { typedef transform_iterator::edge_descriptor>, typename graph_traits::edge_iterator> type; }; }; template <> struct choose_rev_edge_iter { template struct bind_ { typedef void type; }; }; } // namespace detail template class reverse_graph { typedef reverse_graph Self; typedef graph_traits Traits; public: typedef BidirectionalGraph base_type; typedef GraphRef base_ref_type; // Constructor reverse_graph(GraphRef g) : m_g(g) {} // Graph requirements typedef typename Traits::vertex_descriptor vertex_descriptor; typedef detail::reverse_graph_edge_descriptor edge_descriptor; typedef typename Traits::directed_category directed_category; typedef typename Traits::edge_parallel_category edge_parallel_category; typedef typename Traits::traversal_category traversal_category; // IncidenceGraph requirements typedef transform_iterator, typename Traits::in_edge_iterator> out_edge_iterator; typedef typename Traits::degree_size_type degree_size_type; // BidirectionalGraph requirements typedef transform_iterator, typename Traits::out_edge_iterator> in_edge_iterator; // AdjacencyGraph requirements typedef typename adjacency_iterator_generator::type adjacency_iterator; // VertexListGraph requirements typedef typename Traits::vertex_iterator vertex_iterator; // EdgeListGraph requirements enum { is_edge_list = is_convertible::value }; typedef detail::choose_rev_edge_iter ChooseEdgeIter; typedef typename ChooseEdgeIter:: template bind_::type edge_iterator; typedef typename Traits::vertices_size_type vertices_size_type; typedef typename Traits::edges_size_type edges_size_type; typedef reverse_graph_tag graph_tag; #ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES // Bundled properties support template typename graph::detail::bundled_result< BidirectionalGraph, typename detail::get_underlying_descriptor_from_reverse_descriptor::type >::type& operator[](Descriptor x) { return m_g[detail::get_underlying_descriptor_from_reverse_descriptor::convert(x)]; } template typename graph::detail::bundled_result< BidirectionalGraph, typename detail::get_underlying_descriptor_from_reverse_descriptor::type >::type const& operator[](Descriptor x) const { return m_g[detail::get_underlying_descriptor_from_reverse_descriptor::convert(x)]; } #endif // BOOST_GRAPH_NO_BUNDLED_PROPERTIES static vertex_descriptor null_vertex() { return Traits::null_vertex(); } // would be private, but template friends aren't portable enough. // private: GraphRef m_g; }; // These are separate so they are not instantiated unless used (see bug 1021) template struct vertex_property_type > { typedef typename boost::vertex_property_type::type type; }; template struct edge_property_type > { typedef typename boost::edge_property_type::type type; }; template struct graph_property_type > { typedef typename boost::graph_property_type::type type; }; #ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES template struct vertex_bundle_type > : vertex_bundle_type { }; template struct edge_bundle_type > : edge_bundle_type { }; template struct graph_bundle_type > : graph_bundle_type { }; #endif // BOOST_GRAPH_NO_BUNDLED_PROPERTIES template inline reverse_graph make_reverse_graph(const BidirectionalGraph& g) { return reverse_graph(g); } template inline reverse_graph make_reverse_graph(BidirectionalGraph& g) { return reverse_graph(g); } template std::pair::vertex_iterator, typename reverse_graph::vertex_iterator> vertices(const reverse_graph& g) { return vertices(g.m_g); } template std::pair::edge_iterator, typename reverse_graph::edge_iterator> edges(const reverse_graph& g) { return detail::reverse_edge_iter_pair::edge_descriptor>(edges(g.m_g)); } template inline std::pair::out_edge_iterator, typename reverse_graph::out_edge_iterator> out_edges(const typename graph_traits::vertex_descriptor u, const reverse_graph& g) { return detail::reverse_edge_iter_pair::edge_descriptor>(in_edges(u, g.m_g)); } template inline typename graph_traits::vertices_size_type num_vertices(const reverse_graph& g) { return num_vertices(g.m_g); } template inline typename reverse_graph::edges_size_type num_edges(const reverse_graph& g) { return num_edges(g.m_g); } template inline typename graph_traits::degree_size_type out_degree(const typename graph_traits::vertex_descriptor u, const reverse_graph& g) { return in_degree(u, g.m_g); } template inline typename graph_traits::vertex_descriptor vertex(const typename graph_traits::vertices_size_type v, const reverse_graph& g) { return vertex(v, g.m_g); } template inline std::pair< typename graph_traits >::edge_descriptor, bool> edge(const typename graph_traits::vertex_descriptor u, const typename graph_traits::vertex_descriptor v, const reverse_graph& g) { typedef typename graph_traits::edge_descriptor underlying_edge_descriptor; std::pair e = edge(v, u, g.m_g); return std::make_pair(detail::reverse_graph_edge_descriptor(e.first), e.second); } template inline std::pair::in_edge_iterator, typename reverse_graph::in_edge_iterator> in_edges(const typename graph_traits::vertex_descriptor u, const reverse_graph& g) { return detail::reverse_edge_iter_pair::edge_descriptor>(out_edges(u, g.m_g)); } template inline std::pair::adjacency_iterator, typename reverse_graph::adjacency_iterator> adjacent_vertices(typename graph_traits::vertex_descriptor u, const reverse_graph& g) { typedef reverse_graph Graph; typename graph_traits::out_edge_iterator first, last; boost::tie(first, last) = out_edges(u, g); typedef typename graph_traits::adjacency_iterator adjacency_iterator; return std::make_pair(adjacency_iterator(first, const_cast(&g)), adjacency_iterator(last, const_cast(&g))); } template inline typename graph_traits::degree_size_type in_degree(const typename graph_traits::vertex_descriptor u, const reverse_graph& g) { return out_degree(u, g.m_g); } template inline typename graph_traits::vertex_descriptor source(const detail::reverse_graph_edge_descriptor& e, const reverse_graph& g) { return target(e.underlying_descx, g.m_g); } template inline typename graph_traits::vertex_descriptor target(const detail::reverse_graph_edge_descriptor& e, const reverse_graph& g) { return source(e.underlying_descx, g.m_g); } namespace detail { template struct reverse_graph_edge_property_map { private: PM underlying_pm; public: typedef reverse_graph_edge_descriptor::key_type> key_type; typedef typename property_traits::value_type value_type; typedef typename property_traits::reference reference; typedef typename property_traits::category category; explicit reverse_graph_edge_property_map(const PM& pm): underlying_pm(pm) {} friend reference get(const reverse_graph_edge_property_map& m, const key_type& e) { return get(m.underlying_pm, e.underlying_descx); } friend void put(const reverse_graph_edge_property_map& m, const key_type& e, const value_type& v) { put(m.underlying_pm, e.underlying_descx, v); } reference operator[](const key_type& k) const { return (this->underlying_pm)[k.underlying_descx]; } }; } // namespace detail template struct property_map, Property> { typedef boost::is_same::type, edge_property_tag> is_edge_prop; typedef typename property_map::type orig_type; typedef typename property_map::const_type orig_const_type; typedef typename boost::mpl::if_, orig_type>::type type; typedef typename boost::mpl::if_, orig_const_type>::type const_type; }; template struct property_map, Property> { typedef boost::is_same::type, edge_property_tag> is_edge_prop; typedef typename property_map::const_type orig_const_type; typedef typename boost::mpl::if_, orig_const_type>::type const_type; typedef const_type type; }; template typename disable_if< is_same, typename property_map, Property>::type>::type get(Property p, reverse_graph& g) { return typename property_map, Property>::type(get(p, g.m_g)); } template typename disable_if< is_same, typename property_map, Property>::const_type>::type get(Property p, const reverse_graph& g) { const BidirGraph& gref = g.m_g; // in case GRef is non-const return typename property_map, Property>::const_type(get(p, gref)); } template typename disable_if< is_same, typename property_traits< typename property_map, Property>::const_type >::value_type>::type get(Property p, const reverse_graph& g, const Key& k) { return get(get(p, g), k); } template void put(Property p, reverse_graph& g, const Key& k, const Value& val) { put(get(p, g), k, val); } // Get the underlying descriptor from a reverse_graph's wrapped edge descriptor namespace detail { template struct underlying_edge_desc_map_type { E operator[](const reverse_graph_edge_descriptor& k) const { return k.underlying_descx; } }; template E get(underlying_edge_desc_map_type m, const reverse_graph_edge_descriptor& k) { return m[k]; } }; template struct property_traits > { typedef detail::reverse_graph_edge_descriptor key_type; typedef E value_type; typedef const E& reference; typedef readable_property_map_tag category; }; template struct property_map, edge_underlying_t> { private: typedef typename graph_traits::edge_descriptor ed; public: typedef detail::underlying_edge_desc_map_type type; typedef detail::underlying_edge_desc_map_type const_type; }; template struct is_reverse_graph: boost::mpl::false_ {}; template struct is_reverse_graph >: boost::mpl::true_ {}; template typename enable_if, detail::underlying_edge_desc_map_type::edge_descriptor> >::type get(edge_underlying_t, G& g) { return detail::underlying_edge_desc_map_type::edge_descriptor>(); } template typename enable_if, typename graph_traits::edge_descriptor>::type get(edge_underlying_t, G& g, const typename graph_traits::edge_descriptor& k) { return k.underlying_descx; } template typename enable_if, detail::underlying_edge_desc_map_type::edge_descriptor> >::type get(edge_underlying_t, const G& g) { return detail::underlying_edge_desc_map_type::edge_descriptor>(); } template typename enable_if, typename graph_traits::edge_descriptor>::type get(edge_underlying_t, const G& g, const typename graph_traits::edge_descriptor& k) { return k.underlying_descx; } // Access to wrapped graph's graph properties template inline void set_property(const reverse_graph& g, Tag tag, const Value& value) { set_property(g.m_g, tag, value); } template inline typename boost::mpl::if_< boost::is_const::type>, const typename graph_property::type&, typename graph_property::type& >::type get_property(const reverse_graph& g, Tag tag) { return get_property(g.m_g, tag); } } // namespace boost #endif