Commit be9f0a3e authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Implement faster addition of many vertices

parent 254721ac
......@@ -122,12 +122,13 @@ vertex of an edge, respectively.
The :meth:`~graph_tool.Graph.add_vertex` method also accepts an optional
parameter which specifies the number of vertices to create. If this
value is greater than 1, it returns a list of vertex descriptors:
value is greater than 1, it returns an iterator on the added vertex
.. doctest::
>>> vlist = g.add_vertex(10)
>>> print(len(vlist))
>>> print(len(list(vlist)))
Edges and vertices can also be removed at any time with the
......@@ -126,9 +126,16 @@ python::object get_edges(python::object g)
return iter;
python::object add_vertex(python::object g)
python::object add_vertex(python::object g, size_t n)
GraphInterface& gi = python::extract<GraphInterface&>(g().attr("_Graph__graph"));
if (n > 1)
for (size_t i = 0; i < n; ++i)
return python::object();
return python::object(PythonVertex(g, add_vertex(gi.GetGraph())));
......@@ -1091,18 +1091,15 @@ class Graph(object):
def add_vertex(self, n=1):
"""Add a vertex to the graph, and return it. If ``n > 1``, ``n``
vertices are inserted and a list is returned."""
vertices are inserted and an iterator over the new vertices is returned."""
vlist = []
vfilt = self.get_vertex_filter()
for i in range(n):
v = libcore.add_vertex(weakref.ref(self))
if vfilt[0] is not None:
vfilt[0][v] = not vfilt[1]
if n == 1:
return vlist[0]
return vlist
v = libcore.add_vertex(weakref.ref(self), n)
if n <= 1:
return v
pos = self.num_vertices() - n
return (self.vertex(i) for i in xrange(pos, pos + n))
def remove_vertex(self, vertex):
"""Remove a vertex from the graph."""
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment