123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- #ifndef BOOST_GRAPH_NAMED_GRAPH_HPP
- #define BOOST_GRAPH_NAMED_GRAPH_HPP
- #include <boost/config.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/functional/hash.hpp>
- #include <boost/graph/graph_traits.hpp>
- #include <boost/graph/properties.hpp>
- #include <boost/multi_index/hashed_index.hpp>
- #include <boost/multi_index/member.hpp>
- #include <boost/multi_index_container.hpp>
- #include <boost/optional.hpp>
- #include <boost/pending/property.hpp> // for boost::lookup_one_property
- #include <boost/pending/container_traits.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/tuple/tuple.hpp> // for boost::make_tuple
- #include <boost/type_traits/is_same.hpp>
- #include <boost/type_traits/is_base_of.hpp>
- #include <boost/type_traits/remove_cv.hpp>
- #include <boost/type_traits/remove_reference.hpp>
- #include <boost/utility/enable_if.hpp>
- #include <functional> // for std::equal_to
- #include <stdexcept> // for std::runtime_error
- #include <utility> // for std::pair
- namespace boost
- {
- namespace graph
- {
-
-
- template < typename VertexProperty > struct internal_vertex_name
- {
-
- typedef void type;
- };
-
- template < typename Tag, typename T, typename Base >
- struct internal_vertex_name< property< Tag, T, Base > >
- : internal_vertex_name< Base >
- {
- };
-
- template < typename VertexProperty > struct vertex_from_name
- {
- private:
- typedef typename internal_vertex_name< VertexProperty >::type
- extract_name_type;
- typedef typename remove_cv< typename remove_reference<
- typename extract_name_type::result_type >::type >::type
- vertex_name_type;
- public:
- typedef vertex_name_type argument_type;
- typedef VertexProperty result_type;
- VertexProperty operator()(const vertex_name_type& name)
- {
- return VertexProperty(name);
- }
- };
-
- template < typename VertexProperty > struct cannot_add_vertex
- {
- private:
- typedef typename internal_vertex_name< VertexProperty >::type
- extract_name_type;
- typedef typename remove_cv< typename remove_reference<
- typename extract_name_type::result_type >::type >::type
- vertex_name_type;
- public:
- typedef vertex_name_type argument_type;
- typedef VertexProperty result_type;
- VertexProperty operator()(const vertex_name_type&)
- {
- boost::throw_exception(
- std::runtime_error("add_vertex: "
- "unable to create a vertex from its name"));
- }
- };
-
- template < typename VertexProperty > struct internal_vertex_constructor
- {
-
- typedef cannot_add_vertex< VertexProperty > type;
- };
-
- template < typename Tag, typename T, typename Base >
- struct internal_vertex_constructor< property< Tag, T, Base > >
- : internal_vertex_constructor< Base >
- {
- };
-
-
- template < typename Graph, typename Vertex, typename VertexProperty >
- class named_graph
- {
- public:
-
-
- typedef typename internal_vertex_name< VertexProperty >::type
- extract_name_type;
-
-
- typedef typename lookup_one_property< VertexProperty,
- vertex_bundle_t >::type bundled_vertex_property_type;
-
-
- typedef typename internal_vertex_constructor< VertexProperty >::type
- vertex_constructor_type;
-
- typedef typename remove_cv< typename remove_reference<
- typename extract_name_type::result_type >::type >::type
- vertex_name_type;
-
- typedef Vertex vertex_descriptor;
- private:
-
- struct extract_name_from_vertex
- {
- typedef vertex_name_type result_type;
- extract_name_from_vertex(
- Graph& graph, const extract_name_type& extract)
- : graph(graph), extract(extract)
- {
- }
- const result_type& operator()(Vertex vertex) const
- {
- return extract(graph[vertex]);
- }
- Graph& graph;
- extract_name_type extract;
- };
- public:
-
- typedef multi_index::multi_index_container< Vertex,
- multi_index::indexed_by<
- multi_index::hashed_unique< multi_index::tag< vertex_name_t >,
- extract_name_from_vertex > > >
- named_vertices_type;
-
- typedef
- typename named_vertices_type::template index< vertex_name_t >::type
- vertices_by_name_type;
-
-
-
- named_graph(const extract_name_type& extract = extract_name_type(),
- const vertex_constructor_type& vertex_constructor
- = vertex_constructor_type());
-
-
- void added_vertex(Vertex vertex);
-
-
- template < typename VertexIterStability >
- void removing_vertex(Vertex vertex, VertexIterStability);
-
-
- void clearing_graph();
-
- Graph& derived() { return static_cast< Graph& >(*this); }
- const Graph& derived() const
- {
- return static_cast< const Graph& >(*this);
- }
-
- typename extract_name_type::result_type extract_name(
- const bundled_vertex_property_type& property);
-
-
- optional< vertex_descriptor > vertex_by_property(
- const bundled_vertex_property_type& property);
-
- named_vertices_type named_vertices;
-
- vertex_constructor_type vertex_constructor;
- };
- #define BGL_NAMED_GRAPH_PARAMS \
- typename Graph, typename Vertex, typename VertexProperty
- #define BGL_NAMED_GRAPH named_graph< Graph, Vertex, VertexProperty >
- template < BGL_NAMED_GRAPH_PARAMS >
- BGL_NAMED_GRAPH::named_graph(const extract_name_type& extract,
- const vertex_constructor_type& vertex_constructor)
- : named_vertices(typename named_vertices_type::ctor_args_list(
- boost::make_tuple(boost::make_tuple(0,
- extract_name_from_vertex(derived(), extract),
- boost::hash< vertex_name_type >(),
- std::equal_to< vertex_name_type >()))))
- , vertex_constructor(vertex_constructor)
- {
- }
- template < BGL_NAMED_GRAPH_PARAMS >
- inline void BGL_NAMED_GRAPH::added_vertex(Vertex vertex)
- {
- named_vertices.insert(vertex);
- }
- template < BGL_NAMED_GRAPH_PARAMS >
- template < typename VertexIterStability >
- inline void BGL_NAMED_GRAPH::removing_vertex(
- Vertex vertex, VertexIterStability)
- {
- BOOST_STATIC_ASSERT_MSG(
- (boost::is_base_of< boost::graph_detail::stable_tag,
- VertexIterStability >::value),
- "Named graphs cannot use vecS as vertex container and remove "
- "vertices; the lack of vertex descriptor stability (which iterator "
- "stability is a proxy for) means that the name -> vertex mapping "
- "would need to be completely rebuilt after each deletion. See "
- "https://svn.boost.org/trac/boost/ticket/7863 for more information "
- "and a test case.");
- typedef typename BGL_NAMED_GRAPH::vertex_name_type vertex_name_type;
- const vertex_name_type& vertex_name = extract_name(derived()[vertex]);
- named_vertices.erase(vertex_name);
- }
- template < BGL_NAMED_GRAPH_PARAMS >
- inline void BGL_NAMED_GRAPH::clearing_graph()
- {
- named_vertices.clear();
- }
- template < BGL_NAMED_GRAPH_PARAMS >
- typename BGL_NAMED_GRAPH::extract_name_type::result_type
- BGL_NAMED_GRAPH::extract_name(const bundled_vertex_property_type& property)
- {
- return named_vertices.key_extractor().extract(property);
- }
- template < BGL_NAMED_GRAPH_PARAMS >
- optional< typename BGL_NAMED_GRAPH::vertex_descriptor >
- BGL_NAMED_GRAPH::vertex_by_property(
- const bundled_vertex_property_type& property)
- {
- return find_vertex(extract_name(property), *this);
- }
-
- template < BGL_NAMED_GRAPH_PARAMS >
- optional< Vertex > find_vertex(
- typename BGL_NAMED_GRAPH::vertex_name_type const& name,
- const BGL_NAMED_GRAPH& g)
- {
- typedef typename BGL_NAMED_GRAPH::vertices_by_name_type
- vertices_by_name_type;
-
- vertices_by_name_type const& vertices_by_name
- = g.named_vertices.template get< vertex_name_t >();
-
- typename vertices_by_name_type::const_iterator iter
- = vertices_by_name.find(name);
- if (iter == vertices_by_name.end())
- return optional< Vertex >();
- else
- return *iter;
- }
-
-
-
-
-
- template < BGL_NAMED_GRAPH_PARAMS >
- typename disable_if<
- is_same< typename BGL_NAMED_GRAPH::vertex_name_type, VertexProperty >,
- Vertex >::type
- add_vertex(typename BGL_NAMED_GRAPH::vertex_name_type const& name,
- BGL_NAMED_GRAPH& g)
- {
- if (optional< Vertex > vertex = find_vertex(name, g))
-
- return *vertex;
- else
-
- return add_vertex(g.vertex_constructor(name), g.derived());
- }
-
- template < BGL_NAMED_GRAPH_PARAMS >
- std::pair< typename graph_traits< Graph >::edge_descriptor, bool > add_edge(
- typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
- typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
- BGL_NAMED_GRAPH& g)
- {
- return add_edge(add_vertex(u_name, g.derived()),
- add_vertex(v_name, g.derived()), g.derived());
- }
-
- template < BGL_NAMED_GRAPH_PARAMS >
- std::pair< typename graph_traits< Graph >::edge_descriptor, bool > add_edge(
- typename BGL_NAMED_GRAPH::vertex_descriptor const& u,
- typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
- BGL_NAMED_GRAPH& g)
- {
- return add_edge(u, add_vertex(v_name, g.derived()), g.derived());
- }
-
- template < BGL_NAMED_GRAPH_PARAMS >
- std::pair< typename graph_traits< Graph >::edge_descriptor, bool > add_edge(
- typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
- typename BGL_NAMED_GRAPH::vertex_descriptor const& v,
- BGL_NAMED_GRAPH& g)
- {
- return add_edge(add_vertex(u_name, g.derived()), v, g.derived());
- }
-
- template < BGL_NAMED_GRAPH_PARAMS >
- std::pair< typename graph_traits< Graph >::edge_descriptor, bool > add_edge(
- typename BGL_NAMED_GRAPH::vertex_descriptor const& u,
- typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
- typename edge_property_type< Graph >::type const& p, BGL_NAMED_GRAPH& g)
- {
- return add_edge(u, add_vertex(v_name, g.derived()), p, g.derived());
- }
- template < BGL_NAMED_GRAPH_PARAMS >
- std::pair< typename graph_traits< Graph >::edge_descriptor, bool > add_edge(
- typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
- typename BGL_NAMED_GRAPH::vertex_descriptor const& v,
- typename edge_property_type< Graph >::type const& p, BGL_NAMED_GRAPH& g)
- {
- return add_edge(add_vertex(u_name, g.derived()), v, p, g.derived());
- }
- template < BGL_NAMED_GRAPH_PARAMS >
- std::pair< typename graph_traits< Graph >::edge_descriptor, bool > add_edge(
- typename BGL_NAMED_GRAPH::vertex_name_type const& u_name,
- typename BGL_NAMED_GRAPH::vertex_name_type const& v_name,
- typename edge_property_type< Graph >::type const& p, BGL_NAMED_GRAPH& g)
- {
- return add_edge(add_vertex(u_name, g.derived()),
- add_vertex(v_name, g.derived()), p, g.derived());
- }
- #undef BGL_NAMED_GRAPH
- #undef BGL_NAMED_GRAPH_PARAMS
-
-
- template < typename Graph, typename Vertex, typename VertexProperty,
- typename ExtractName =
- typename internal_vertex_name< VertexProperty >::type >
- struct maybe_named_graph
- : public named_graph< Graph, Vertex, VertexProperty >
- {
- };
-
- template < typename Graph, typename Vertex, typename VertexProperty >
- struct maybe_named_graph< Graph, Vertex, VertexProperty, void >
- {
-
-
- typedef typename lookup_one_property< VertexProperty,
- vertex_bundle_t >::type bundled_vertex_property_type;
-
-
- void added_vertex(Vertex) {}
-
-
- template < typename VertexIterStability >
- void removing_vertex(Vertex, VertexIterStability)
- {
- }
-
-
- void clearing_graph() {}
-
-
- optional< Vertex > vertex_by_property(
- const bundled_vertex_property_type&)
- {
- return optional< Vertex >();
- }
- };
- }
- }
- #endif
|