Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't like the nested-template property approach of boost::graph, so I wrote a small wrapper around everything, that basically allows to put any struct/class as a vertex/edge property. One can access properties accessing the struct members.</p> <p>To keep it flexible these structs are defined as template parameter.</p> <p>Here the Code:</p> <pre><code>/* definition of basic boost::graph properties */ enum vertex_properties_t { vertex_properties }; enum edge_properties_t { edge_properties }; namespace boost { BOOST_INSTALL_PROPERTY(vertex, properties); BOOST_INSTALL_PROPERTY(edge, properties); } /* the graph base class template */ template &lt; typename VERTEXPROPERTIES, typename EDGEPROPERTIES &gt; class Graph { public: /* an adjacency_list like we need it */ typedef adjacency_list&lt; setS, // disallow parallel edges listS, // vertex container bidirectionalS, // directed graph property&lt;vertex_properties_t, VERTEXPROPERTIES&gt;, property&lt;edge_properties_t, EDGEPROPERTIES&gt; &gt; GraphContainer; /* a bunch of graph-specific typedefs */ typedef typename graph_traits&lt;GraphContainer&gt;::vertex_descriptor Vertex; typedef typename graph_traits&lt;GraphContainer&gt;::edge_descriptor Edge; typedef std::pair&lt;Edge, Edge&gt; EdgePair; typedef typename graph_traits&lt;GraphContainer&gt;::vertex_iterator vertex_iter; typedef typename graph_traits&lt;GraphContainer&gt;::edge_iterator edge_iter; typedef typename graph_traits&lt;GraphContainer&gt;::adjacency_iterator adjacency_iter; typedef typename graph_traits&lt;GraphContainer&gt;::out_edge_iterator out_edge_iter; typedef typename graph_traits&lt;GraphContainer&gt;::degree_size_type degree_t; typedef std::pair&lt;adjacency_iter, adjacency_iter&gt; adjacency_vertex_range_t; typedef std::pair&lt;out_edge_iter, out_edge_iter&gt; out_edge_range_t; typedef std::pair&lt;vertex_iter, vertex_iter&gt; vertex_range_t; typedef std::pair&lt;edge_iter, edge_iter&gt; edge_range_t; /* constructors etc. */ Graph() {} Graph(const Graph&amp; g) : graph(g.graph) {} virtual ~Graph() {} /* structure modification methods */ void Clear() { graph.clear(); } Vertex AddVertex(const VERTEXPROPERTIES&amp; prop) { Vertex v = add_vertex(graph); properties(v) = prop; return v; } void RemoveVertex(const Vertex&amp; v) { clear_vertex(v, graph); remove_vertex(v, graph); } EdgePair AddEdge(const Vertex&amp; v1, const Vertex&amp; v2, const EDGEPROPERTIES&amp; prop_12, const EDGEPROPERTIES&amp; prop_21) { /* TODO: maybe one wants to check if this edge could be inserted */ Edge addedEdge1 = add_edge(v1, v2, graph).first; Edge addedEdge2 = add_edge(v2, v1, graph).first; properties(addedEdge1) = prop_12; properties(addedEdge2) = prop_21; return EdgePair(addedEdge1, addedEdge2); } /* property access */ VERTEXPROPERTIES&amp; properties(const Vertex&amp; v) { typename property_map&lt;GraphContainer, vertex_properties_t&gt;::type param = get(vertex_properties, graph); return param[v]; } const VERTEXPROPERTIES&amp; properties(const Vertex&amp; v) const { typename property_map&lt;GraphContainer, vertex_properties_t&gt;::const_type param = get(vertex_properties, graph); return param[v]; } EDGEPROPERTIES&amp; properties(const Edge&amp; v) { typename property_map&lt;GraphContainer, edge_properties_t&gt;::type param = get(edge_properties, graph); return param[v]; } const EDGEPROPERTIES&amp; properties(const Edge&amp; v) const { typename property_map&lt;GraphContainer, edge_properties_t&gt;::const_type param = get(edge_properties, graph); return param[v]; } /* selectors and properties */ const GraphContainer&amp; getGraph() const { return graph; } vertex_range_t getVertices() const { return vertices(graph); } adjacency_vertex_range_t getAdjacentVertices(const Vertex&amp; v) const { return adjacent_vertices(v, graph); } int getVertexCount() const { return num_vertices(graph); } int getVertexDegree(const Vertex&amp; v) const { return out_degree(v, graph); } /* operators */ Graph&amp; operator=(const Graph &amp;rhs) { graph = rhs.graph; return *this; } protected: GraphContainer graph; }; </code></pre> <p>Using this you can access properties like this:</p> <pre><code>struct VertexProperties { int i; }; struct EdgeProperties { }; typedef Graph&lt;VertexProperties, EdgeProperties&gt; MyGraph; MyGraph g; VertexProperties vp; vp.i = 42; MyGraph::Vertex v = g.AddVertex(vp); g.properties(v).i = 23; </code></pre> <p>Of course you may have other needs for your graph's structure, but modification of the code above should be pretty easy.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload