Commit 8ae75c9f authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Add direct support for degree propery map creation

i.e. g.degree_property_map("in") will create and return a vertex property map
which corresponds to the in-degrees of the vertices. This is useful for
temporarily modifying or getting an array of degrees.
parent e84aaf3c
......@@ -116,6 +116,8 @@ public:
python::object AddEdge(const python::object& s, const python::object& t);
void RemoveEdge(const python::object& e);
python::object DegreeMap(string deg) const;
// used for graph properties
graph_property_tag GetDescriptor() const { return graph_property_tag(); }
bool CheckValid() const {return true;}
......@@ -314,6 +314,7 @@ BOOST_PYTHON_MODULE(libgraph_tool_core)
.def("AddEdge", &GraphInterface::AddEdge)
.def("RemoveVertex", &GraphInterface::RemoveVertex)
.def("RemoveEdge", &GraphInterface::RemoveEdge)
.def("DegreeMap", &GraphInterface::DegreeMap)
.def("Clear", &GraphInterface::Clear)
.def("ClearEdges", &GraphInterface::ClearEdges)
.def("GetVertexIndex", &GraphInterface::GetVertexIndex)
......@@ -228,6 +228,48 @@ void GraphInterface::RemoveEdge(const python::object& e)
remove_edge(de, _mg);
struct get_degree_map
template <class Graph, class DegreeMap, class DegS>
void operator()(const Graph* gp, DegreeMap deg_map, DegS deg) const
const Graph& g = *gp;
int i, N = num_vertices(g);
#pragma omp parallel for default(shared) private(i) schedule(dynamic)
for (i = 0; i < N; ++i)
typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
if (v == graph_traits<Graph>::null_vertex())
deg_map[v] = deg(v, g);
python::object GraphInterface::DegreeMap(string deg) const
typedef property_map_type::apply<double,
map_t deg_map(_vertex_index);
if (deg == "in")
lambda::bind<void>(get_degree_map(), lambda::_1,
deg_map, in_degreeS()))();
else if (deg == "out")
lambda::bind<void>(get_degree_map(), lambda::_1,
deg_map, out_degreeS()))();
else if (deg == "total")
lambda::bind<void>(get_degree_map(), lambda::_1,
deg_map, total_degreeS()))();
return python::object(PythonPropertyMap<map_t>(deg_map));
// Below are the functions with will properly register all the types to python,
// for every filter, type, etc.
......@@ -532,6 +532,15 @@ class Graph(object):
if g != self:
# degree property map
def degree_property_map(self, deg):
"""Create and return a vertex property map containing the degree type
given by `deg`"""
return PropertyMap(self.__graph.DegreeMap(deg), self, "v")
# I/O operations
# ==============
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