Hi folks,
The bgl-python documentation says that the _graph.Graph.add_vertex()
function returns a Vertex object.
From my testing, the Vertex object that gets returned doesn't actually
seem to be a member of the graph.
Is this intentional? I was hoping that the returned Vertex object was
valid, since I need access to it immediately.
Here's a test program. I woudl expect the values for v1 and v2 to be in
the graph, but they are not:
===================
import boost.graph as bgl
graph = bgl.Graph()
v1 = graph.add_vertex()
print "new v1=", v1
v2 = graph.add_vertex()
print "new v2=", v2
e = graph.add_edge(v1, v2)
print "new edge=", e
for vertex in graph.vertices:
print "vertex: ", vertex
for edge in graph.edges:
print "edge: ", edge
print "edge source:", graph.source(e)
=========================
Here's what I see:
new v1=
On Feb 27, 2006, at 2:33 PM, David E. Konerding wrote:
Here's what I see: new v1=
new v2= new edge= vertex: vertex: edge: edge source: As you can see, v1 and v2 return and have different addresses than the Vertex objects that are listed by the vertices iterator. My guess here is that the Vertex objects do point to the same underlying vertex in the C++ implementation of the graph.
Your guess is correct. We don't cache the Python objects corresponding to vertex and edge descriptors. Doug
Doug Gregor wrote:
As you can see, v1 and v2 return and have different addresses than the Vertex objects that are listed by the vertices iterator. My guess here is that the Vertex objects do point to the same underlying vertex in the C++ implementation of the graph.
Your guess is correct. We don't cache the Python objects corresponding to vertex and edge descriptors.
Ah. OK. Well, I guess that leads me to my next question. I am writing a model-view graph editor using bgl-python (which the model implemented as a boost graph). Originally my idea was to keep the mapping between on-screen widgets and the graph vertices they corresponded to in a dictionary: self._widgetmap[vertex] = widget However, since Vertex objects aren't cached, placing the vertex object I get back from add_vertex obviously isn't going to work. I got to thinking, and it seems to me like the 'right way' to do this would be to invert the problem (somewhat violating the model/view abstraction): add a vertex property map to the graph with mappings from the graph vertex object to the widget. I can't see another way to do it if Vertex objects aren't cached. Dave
On Feb 27, 2006, at 5:09 PM, David E. Konerding wrote:
I got to thinking, and it seems to me like the 'right way' to do this would be to invert the problem (somewhat violating the model/view abstraction): add a vertex property map to the graph with mappings from the graph vertex object to the widget. I can't see another way to do it if Vertex objects aren't cached.
Yes, I think this is your best bet. Future revisions of the BGL- Python bindings will fix this problem, but it'll happen faster if someone goes ahead and implements it for me :) Doug
Doug Gregor
On Feb 27, 2006, at 2:33 PM, David E. Konerding wrote:
Here's what I see: new v1=
new v2= new edge= vertex: vertex: edge: edge source: As you can see, v1 and v2 return and have different addresses than the Vertex objects that are listed by the vertices iterator. My guess here is that the Vertex objects do point to the same underlying vertex in the C++ implementation of the graph.
Your guess is correct. We don't cache the Python objects corresponding to vertex and edge descriptors.
You probably don't want to pay this cost, but you might be able to make the behavior more predictable if you used shared_ptrs inside the C++ graph representation, since they will automatically retain the identity of the Python object wrappers. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (4)
-
David Abrahams
-
David E. Konerding
-
Doug Gregor
-
Douglas Gregor