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. ...@@ -122,12 +122,13 @@ vertex of an edge, respectively.
The :meth:`~graph_tool.Graph.add_vertex` method also accepts an optional The :meth:`~graph_tool.Graph.add_vertex` method also accepts an optional
parameter which specifies the number of vertices to create. If this 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
descriptors:
.. doctest:: .. doctest::
>>> vlist = g.add_vertex(10) >>> vlist = g.add_vertex(10)
>>> print(len(vlist)) >>> print(len(list(vlist)))
10 10
Edges and vertices can also be removed at any time with the Edges and vertices can also be removed at any time with the
......
...@@ -126,9 +126,16 @@ python::object get_edges(python::object g) ...@@ -126,9 +126,16 @@ python::object get_edges(python::object g)
return iter; 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")); GraphInterface& gi = python::extract<GraphInterface&>(g().attr("_Graph__graph"));
if (n > 1)
{
for (size_t i = 0; i < n; ++i)
add_vertex(gi.GetGraph());
return python::object();
}
return python::object(PythonVertex(g, add_vertex(gi.GetGraph()))); return python::object(PythonVertex(g, add_vertex(gi.GetGraph())));
} }
......
...@@ -1091,18 +1091,15 @@ class Graph(object): ...@@ -1091,18 +1091,15 @@ class Graph(object):
def add_vertex(self, n=1): def add_vertex(self, n=1):
"""Add a vertex to the graph, and return it. If ``n > 1``, ``n`` """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."""
self.__check_perms("add_vertex") self.__check_perms("add_vertex")
vlist = [] v = libcore.add_vertex(weakref.ref(self), n)
vfilt = self.get_vertex_filter()
for i in range(n): if n <= 1:
v = libcore.add_vertex(weakref.ref(self)) return v
if vfilt[0] is not None: else:
vfilt[0][v] = not vfilt[1] pos = self.num_vertices() - n
vlist.append(v) return (self.vertex(i) for i in xrange(pos, pos + n))
if n == 1:
return vlist[0]
return vlist
def remove_vertex(self, vertex): def remove_vertex(self, vertex):
"""Remove a vertex from the graph.""" """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