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())
continue;
deg_map[v] = deg(v, g);
}
}
};
python::object GraphInterface::DegreeMap(string deg) const
{
typedef property_map_type::apply<double,
GraphInterface::vertex_index_map_t>::type
map_t;
map_t deg_map(_vertex_index);
deg_map.reserve(num_vertices(_mg));
if (deg == "in")
run_action<>()(*this,
lambda::bind<void>(get_degree_map(), lambda::_1,
deg_map, in_degreeS()))();
else if (deg == "out")
run_action<>()(*this,
lambda::bind<void>(get_degree_map(), lambda::_1,
deg_map, out_degreeS()))();
else if (deg == "total")
run_action<>()(*this,
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:
g.pop_filter()
# degree property map
@_handle_exceptions
@_limit_args({"deg":["in","out","total"]})
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
# ==============
......
Markdown is supported
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