// //======================================================================= // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek // // Copyright 2009, Andrew Sutton // // 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 BOOST_GRAPH_CONCEPTS_HPP #define BOOST_GRAPH_CONCEPTS_HPP #include #include #include #include #include #include #include #include #include #include namespace boost { // dwa 2003/7/11 -- This clearly shouldn't be necessary, but if // you want to use vector_as_graph, it is! I'm sure the graph // library leaves these out all over the place. Probably a // redesign involving specializing a template with a static // member function is in order :( // // It is needed in order to allow us to write using boost::vertices as // needed for ADL when using vector_as_graph below. #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) \ && !BOOST_WORKAROUND(__GNUC__, <= 2) \ && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) # define BOOST_VECTOR_AS_GRAPH_GRAPH_ADL_HACK #endif #ifdef BOOST_VECTOR_AS_GRAPH_GRAPH_ADL_HACK template typename T::ThereReallyIsNoMemberByThisNameInT vertices(T const&); #endif namespace concepts { BOOST_concept(MultiPassInputIterator,(T)) { BOOST_CONCEPT_USAGE(MultiPassInputIterator) { BOOST_CONCEPT_ASSERT((InputIterator)); } }; BOOST_concept(Graph,(G)) { typedef typename graph_traits::vertex_descriptor vertex_descriptor; typedef typename graph_traits::directed_category directed_category; typedef typename graph_traits::edge_parallel_category edge_parallel_category; typedef typename graph_traits::traversal_category traversal_category; BOOST_CONCEPT_USAGE(Graph) { BOOST_CONCEPT_ASSERT((DefaultConstructible)); BOOST_CONCEPT_ASSERT((EqualityComparable)); BOOST_CONCEPT_ASSERT((Assignable)); } G g; }; BOOST_concept(IncidenceGraph,(G)) : Graph { typedef typename graph_traits::edge_descriptor edge_descriptor; typedef typename graph_traits::out_edge_iterator out_edge_iterator; typedef typename graph_traits::traversal_category traversal_category; BOOST_CONCEPT_USAGE(IncidenceGraph) { BOOST_CONCEPT_ASSERT((MultiPassInputIterator)); BOOST_CONCEPT_ASSERT((DefaultConstructible)); BOOST_CONCEPT_ASSERT((EqualityComparable)); BOOST_CONCEPT_ASSERT((Assignable)); BOOST_CONCEPT_ASSERT((Convertible)); p = out_edges(u, g); n = out_degree(u, g); e = *p.first; u = source(e, g); v = target(e, g); const_constraints(g); } void const_constraints(const G& cg) { p = out_edges(u, cg); n = out_degree(u, cg); e = *p.first; u = source(e, cg); v = target(e, cg); } std::pair p; typename graph_traits::vertex_descriptor u, v; typename graph_traits::edge_descriptor e; typename graph_traits::degree_size_type n; G g; }; BOOST_concept(BidirectionalGraph,(G)) : IncidenceGraph { typedef typename graph_traits::in_edge_iterator in_edge_iterator; typedef typename graph_traits::traversal_category traversal_category; BOOST_CONCEPT_USAGE(BidirectionalGraph) { BOOST_CONCEPT_ASSERT((MultiPassInputIterator)); BOOST_CONCEPT_ASSERT((Convertible)); p = in_edges(v, g); n = in_degree(v, g); e = *p.first; const_constraints(g); } void const_constraints(const G& cg) { p = in_edges(v, cg); n = in_degree(v, cg); e = *p.first; } std::pair p; typename graph_traits::vertex_descriptor v; typename graph_traits::edge_descriptor e; typename graph_traits::degree_size_type n; G g; }; BOOST_concept(AdjacencyGraph,(G)) : Graph { typedef typename graph_traits::adjacency_iterator adjacency_iterator; typedef typename graph_traits::traversal_category traversal_category; BOOST_CONCEPT_USAGE(AdjacencyGraph) { BOOST_CONCEPT_ASSERT((MultiPassInputIterator)); BOOST_CONCEPT_ASSERT((Convertible)); p = adjacent_vertices(v, g); v = *p.first; const_constraints(g); } void const_constraints(const G& cg) { p = adjacent_vertices(v, cg); } std::pair p; typename graph_traits::vertex_descriptor v; G g; }; BOOST_concept(VertexListGraph,(G)) : Graph { typedef typename graph_traits::vertex_iterator vertex_iterator; typedef typename graph_traits::vertices_size_type vertices_size_type; typedef typename graph_traits::traversal_category traversal_category; BOOST_CONCEPT_USAGE(VertexListGraph) { BOOST_CONCEPT_ASSERT((MultiPassInputIterator)); BOOST_CONCEPT_ASSERT((Convertible)); #ifdef BOOST_VECTOR_AS_GRAPH_GRAPH_ADL_HACK // dwa 2003/7/11 -- This clearly shouldn't be necessary, but if // you want to use vector_as_graph, it is! I'm sure the graph // library leaves these out all over the place. Probably a // redesign involving specializing a template with a static // member function is in order :( using boost::vertices; #endif p = vertices(g); v = *p.first; const_constraints(g); } void const_constraints(const G& cg) { #ifdef BOOST_VECTOR_AS_GRAPH_GRAPH_ADL_HACK // dwa 2003/7/11 -- This clearly shouldn't be necessary, but if // you want to use vector_as_graph, it is! I'm sure the graph // library leaves these out all over the place. Probably a // redesign involving specializing a template with a static // member function is in order :( using boost::vertices; #endif p = vertices(cg); v = *p.first; V = num_vertices(cg); } std::pair p; typename graph_traits::vertex_descriptor v; G g; vertices_size_type V; }; BOOST_concept(EdgeListGraph,(G)) : Graph { typedef typename graph_traits::edge_descriptor edge_descriptor; typedef typename graph_traits::edge_iterator edge_iterator; typedef typename graph_traits::edges_size_type edges_size_type; typedef typename graph_traits::traversal_category traversal_category; BOOST_CONCEPT_USAGE(EdgeListGraph) { BOOST_CONCEPT_ASSERT((MultiPassInputIterator)); BOOST_CONCEPT_ASSERT((DefaultConstructible)); BOOST_CONCEPT_ASSERT((EqualityComparable)); BOOST_CONCEPT_ASSERT((Assignable)); BOOST_CONCEPT_ASSERT((Convertible)); p = edges(g); e = *p.first; u = source(e, g); v = target(e, g); const_constraints(g); } void const_constraints(const G& cg) { p = edges(cg); E = num_edges(cg); e = *p.first; u = source(e, cg); v = target(e, cg); } std::pair p; typename graph_traits::vertex_descriptor u, v; typename graph_traits::edge_descriptor e; edges_size_type E; G g; }; BOOST_concept(VertexAndEdgeListGraph,(G)) : VertexListGraph , EdgeListGraph { }; // Where to put the requirement for this constructor? // G g(n_vertices); // Not in mutable graph, then LEDA graph's can't be models of // MutableGraph. BOOST_concept(EdgeMutableGraph,(G)) { typedef typename graph_traits::edge_descriptor edge_descriptor; BOOST_CONCEPT_USAGE(EdgeMutableGraph) { p = add_edge(u, v, g); remove_edge(u, v, g); remove_edge(e, g); clear_vertex(v, g); } G g; edge_descriptor e; std::pair p; typename graph_traits::vertex_descriptor u, v; }; BOOST_concept(VertexMutableGraph,(G)) { BOOST_CONCEPT_USAGE(VertexMutableGraph) { v = add_vertex(g); remove_vertex(v, g); } G g; typename graph_traits::vertex_descriptor u, v; }; BOOST_concept(MutableGraph,(G)) : EdgeMutableGraph , VertexMutableGraph { }; template struct dummy_edge_predicate { bool operator()(const edge_descriptor&) const { return false; } }; BOOST_concept(MutableIncidenceGraph,(G)) : MutableGraph { BOOST_CONCEPT_USAGE(MutableIncidenceGraph) { remove_edge(iter, g); remove_out_edge_if(u, p, g); } G g; typedef typename graph_traits::edge_descriptor edge_descriptor; dummy_edge_predicate p; typename boost::graph_traits::vertex_descriptor u; typename boost::graph_traits::out_edge_iterator iter; }; BOOST_concept(MutableBidirectionalGraph,(G)) : MutableIncidenceGraph { BOOST_CONCEPT_USAGE(MutableBidirectionalGraph) { remove_in_edge_if(u, p, g); } G g; typedef typename graph_traits::edge_descriptor edge_descriptor; dummy_edge_predicate p; typename boost::graph_traits::vertex_descriptor u; }; BOOST_concept(MutableEdgeListGraph,(G)) : EdgeMutableGraph { BOOST_CONCEPT_USAGE(MutableEdgeListGraph) { remove_edge_if(p, g); } G g; typedef typename graph_traits::edge_descriptor edge_descriptor; dummy_edge_predicate p; }; BOOST_concept(VertexMutablePropertyGraph,(G)) : VertexMutableGraph { BOOST_CONCEPT_USAGE(VertexMutablePropertyGraph) { v = add_vertex(vp, g); } G g; typename graph_traits::vertex_descriptor v; typename vertex_property_type::type vp; }; BOOST_concept(EdgeMutablePropertyGraph,(G)) : EdgeMutableGraph { typedef typename graph_traits::edge_descriptor edge_descriptor; BOOST_CONCEPT_USAGE(EdgeMutablePropertyGraph) { p = add_edge(u, v, ep, g); } G g; std::pair p; typename graph_traits::vertex_descriptor u, v; typename edge_property_type::type ep; }; BOOST_concept(AdjacencyMatrix,(G)) : Graph { typedef typename graph_traits::edge_descriptor edge_descriptor; BOOST_CONCEPT_USAGE(AdjacencyMatrix) { p = edge(u, v, g); const_constraints(g); } void const_constraints(const G& cg) { p = edge(u, v, cg); } typename graph_traits::vertex_descriptor u, v; std::pair p; G g; }; BOOST_concept(ReadablePropertyGraph,(G)(X)(Property)) : Graph { typedef typename property_map::const_type const_Map; BOOST_CONCEPT_USAGE(ReadablePropertyGraph) { BOOST_CONCEPT_ASSERT((ReadablePropertyMapConcept)); const_constraints(g); } void const_constraints(const G& cg) { const_Map pmap = get(Property(), cg); pval = get(Property(), cg, x); ignore_unused_variable_warning(pmap); } G g; X x; typename property_traits::value_type pval; }; BOOST_concept(PropertyGraph,(G)(X)(Property)) : ReadablePropertyGraph { typedef typename property_map::type Map; BOOST_CONCEPT_USAGE(PropertyGraph) { BOOST_CONCEPT_ASSERT((ReadWritePropertyMapConcept)); Map pmap = get(Property(), g); pval = get(Property(), g, x); put(Property(), g, x, pval); ignore_unused_variable_warning(pmap); } G g; X x; typename property_traits::value_type pval; }; BOOST_concept(LvaluePropertyGraph,(G)(X)(Property)) : ReadablePropertyGraph { typedef typename property_map::type Map; typedef typename property_map::const_type const_Map; BOOST_CONCEPT_USAGE(LvaluePropertyGraph) { BOOST_CONCEPT_ASSERT((LvaluePropertyMapConcept)); pval = get(Property(), g, x); put(Property(), g, x, pval); } G g; X x; typename property_traits::value_type pval; }; // The *IndexGraph concepts are "semantic" graph concpepts. These can be // applied to describe any graph that has an index map that can be accessed // using the get(*_index, g) method. For example, adjacency lists with // VertexSet == vecS are implicitly models of this concept. // // NOTE: We could require an associated type vertex_index_type, but that // would mean propagating that type name into graph_traits and all of the // other graph implementations. Much easier to simply call it unsigned. BOOST_concept(VertexIndexGraph,(Graph)) { BOOST_CONCEPT_USAGE(VertexIndexGraph) { typedef typename graph_traits::vertex_descriptor Vertex; typedef typename property_map::type Map; typedef unsigned Index; // This could be Graph::vertex_index_type Map m = get(vertex_index, g); Index x = get(vertex_index, g, Vertex()); ignore_unused_variable_warning(m); ignore_unused_variable_warning(x); // This is relaxed renumber_vertex_indices(g); const_constraints(g); } void const_constraints(const Graph& g) { typedef typename property_map::const_type Map; Map m = get(vertex_index, g); ignore_unused_variable_warning(m); } private: Graph g; }; BOOST_concept(EdgeIndexGraph,(Graph)) { BOOST_CONCEPT_USAGE(EdgeIndexGraph) { typedef typename graph_traits::edge_descriptor Edge; typedef typename property_map::type Map; typedef unsigned Index; // This could be Graph::vertex_index_type Map m = get(edge_index, g); Index x = get(edge_index, g, Edge()); ignore_unused_variable_warning(m); ignore_unused_variable_warning(x); // This is relaxed renumber_edge_indices(g); const_constraints(g); } void const_constraints(const Graph& g) { typedef typename property_map::const_type Map; Map m = get(edge_index, g); ignore_unused_variable_warning(m); } private: Graph g; }; BOOST_concept(ColorValue,(C)) : EqualityComparable , DefaultConstructible { BOOST_CONCEPT_USAGE(ColorValue) { c = color_traits::white(); c = color_traits::gray(); c = color_traits::black(); } C c; }; BOOST_concept(BasicMatrix,(M)(I)(V)) { BOOST_CONCEPT_USAGE(BasicMatrix) { V& elt = A[i][j]; const_constraints(A); ignore_unused_variable_warning(elt); } void const_constraints(const M& cA) { const V& elt = cA[i][j]; ignore_unused_variable_warning(elt); } M A; I i, j; }; // The following concepts describe aspects of numberic values and measure // functions. We're extending the notion of numeric values to include // emulation for zero and infinity. BOOST_concept(NumericValue,(Numeric)) { BOOST_CONCEPT_USAGE(NumericValue) { BOOST_CONCEPT_ASSERT(( DefaultConstructible )); BOOST_CONCEPT_ASSERT(( CopyConstructible )); numeric_values::zero(); numeric_values::infinity(); } }; BOOST_concept(DegreeMeasure,(Measure)(Graph)) { BOOST_CONCEPT_USAGE(DegreeMeasure) { typedef typename Measure::degree_type Degree; typedef typename Measure::vertex_type Vertex; Degree d = m(Vertex(), g); ignore_unused_variable_warning(d); } private: Measure m; Graph g; }; BOOST_concept(DistanceMeasure,(Measure)(Graph)) { BOOST_CONCEPT_USAGE(DistanceMeasure) { typedef typename Measure::distance_type Distance; typedef typename Measure::result_type Result; Result r = m(Distance(), g); ignore_unused_variable_warning(r); } private: Measure m; Graph g; }; } /* namespace concepts */ using boost::concepts::MultiPassInputIteratorConcept; // Graph concepts using boost::concepts::GraphConcept; using boost::concepts::IncidenceGraphConcept; using boost::concepts::BidirectionalGraphConcept; using boost::concepts::AdjacencyGraphConcept; using boost::concepts::VertexListGraphConcept; using boost::concepts::EdgeListGraphConcept; using boost::concepts::VertexAndEdgeListGraphConcept; using boost::concepts::EdgeMutableGraphConcept; using boost::concepts::VertexMutableGraphConcept; using boost::concepts::MutableGraphConcept; using boost::concepts::MutableIncidenceGraphConcept; using boost::concepts::MutableBidirectionalGraphConcept; using boost::concepts::MutableEdgeListGraphConcept; using boost::concepts::VertexMutablePropertyGraphConcept; using boost::concepts::EdgeMutablePropertyGraphConcept; using boost::concepts::AdjacencyMatrixConcept; using boost::concepts::ReadablePropertyGraphConcept; using boost::concepts::PropertyGraphConcept; using boost::concepts::LvaluePropertyGraphConcept; using boost::concepts::VertexIndexGraphConcept; using boost::concepts::EdgeIndexGraphConcept; // Utility concepts using boost::concepts::ColorValueConcept; using boost::concepts::BasicMatrixConcept; using boost::concepts::NumericValueConcept; using boost::concepts::DistanceMeasureConcept; using boost::concepts::DegreeMeasureConcept; } /* namespace boost */ #include #endif /* BOOST_GRAPH_CONCEPTS_H */