tags:

views:

49

answers:

2

Hi,

I am writing a Graph-class using boost-graph-library. I use custom vertex and edge properties and a map to store/find the vertices/edges for a given property.
I'm satisfied with how it works, so far.
However, I have a small problem, where I'm not sure how to solve it "nicely". The class provides a method

Vertex getVertex(Vertexproperties v_prop)

and a method

bool hasVertex(Vertexproperties v_prop)

The question now is, would you judge this as good programming practice in C++?
My opinion is, that I have first to check if something is available before I can get it. So, before getting a vertex with a desired property, one has to check if hasVertex() would return true for those properties.
However, I would like to make getVertex() a bit more robust. ATM it will segfault when one would directly call getVertex() without prior checking if the graph has a corresponding vertex. A first idea was to return a NULL-pointer or a pointer that points past the last stored vertex. For the latter, I haven't found out how to do this.
But even with this "robust" version, one would have to check for correctness after getting a vertex or one would also run into a SegFault when dereferencing that vertex-pointer for example. Therefore I am wondering if it is "ok" to let getVertex() SegFault if one does not check for availability beforehand?

A: 

I would either change this to:

bool getVertex(Vertex& vertex, Vertexproperties v_prop);

or have getVertex raise an exception if the vertex is not found.

Andreas Brinck
A: 

Hey 'Shadow',

how you are implementing getVertex, me too have similar requirement.

Are you storing some map for properties to vertex, so bgl provides something for this.

get(vertex_custom_prop_t, g)::type p_map, return/add property value,but it does not fetch vertex_id for property value.

Thanks Rahul...

Rahul
Hi Rahul, you're right. I provide a map prop2vertex that stores for a given property-key the associated vertex.What BGL provides and you have posted there works the other way around. It returns you a map that helps you retrieve the associated property for a given vertex, which might be helpful but not for "our" getVertex().Unfortunately, I don't know if there is a more sophisticated and standard-BGL-way to do this. However, I rather don't think so. Because a graph might store property associated with a vertex but some property X must not necessarily be associated with a stored vertex V.
Shadow