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

Add support for weighted in/out-degrees via weight parameter to Vertex.in/out_degree()

parent d1d660cc
......@@ -441,14 +441,18 @@ void export_python_interface()
class_<PythonVertex>
("Vertex", no_init)
.def("in_degree", &PythonVertex::GetInDegree,
.def("__in_degree", &PythonVertex::GetInDegree,
"Return the in-degree.")
.def("out_degree", &PythonVertex::GetOutDegree,
.def("__weighted_in_degree", &PythonVertex::GetWeightedOutDegree,
"Return the weighted in-degree.")
.def("__out_degree", &PythonVertex::GetOutDegree,
"Return the out-degree.")
.def("out_edges", &PythonVertex::OutEdges,
"Return an iterator over the out-edges.")
.def("__weighted_out_degree", &PythonVertex::GetWeightedOutDegree,
"Return the weighted out-degree.")
.def("in_edges", &PythonVertex::InEdges,
"Return an iterator over the in-edges.")
.def("out_edges", &PythonVertex::OutEdges,
"Return an iterator over the out-edges.")
.def("is_valid", &PythonVertex::IsValid,
"Return whether the vertex is valid.")
.def("get_graph", &PythonVertex::GetGraph,
......
......@@ -139,6 +139,15 @@ public:
{
deg = DegSelector()(v, g);
}
template<class Graph, class PMap>
void operator()(const Graph& g,
typename boost::graph_traits<Graph>::vertex_descriptor v,
const PMap& weight,
boost::python::object& deg) const
{
deg = boost::python::object(DegSelector()(v, g, weight));
}
};
size_t GetInDegree() const
......@@ -152,6 +161,21 @@ public:
return in_deg;
}
boost::python::object GetWeightedInDegree(boost::any pmap) const
{
if (!belongs<edge_scalar_properties>()(pmap))
throw ValueException("edge weight property must be of scalar type");
CheckValid();
GraphInterface& gi = boost::python::extract<GraphInterface&>(_g().attr("_Graph__graph"));
boost::python::object in_deg;
run_action<>()(gi, std::bind(get_degree<in_degreeS>(),
std::placeholders::_1, _v,
std::placeholders::_2,
std::ref(in_deg)),
edge_scalar_properties())(pmap);
return in_deg;
}
size_t GetOutDegree() const
{
CheckValid();
......@@ -163,6 +187,22 @@ public:
return out_deg;
}
boost::python::object GetWeightedOutDegree(boost::any pmap) const
{
if (!belongs<edge_scalar_properties>()(pmap))
throw ValueException("edge weight property must be of scalar type");
CheckValid();
GraphInterface& gi = boost::python::extract<GraphInterface&>(_g().attr("_Graph__graph"));
boost::python::object out_deg;
run_action<>()(gi, std::bind(get_degree<out_degreeS>(),
std::placeholders::_1, _v,
std::placeholders::_2,
std::ref(out_deg)),
edge_scalar_properties())(pmap);
return out_deg;
}
// provide iterator support for out_edges
struct get_out_edges
......
......@@ -2346,6 +2346,32 @@ def _all_neighbours(self):
yield v
Vertex.all_neighbours = _all_neighbours
def _in_degree(self, weight=None):
"""Return the in-degree of the vertex. If provided, ``weight`` should be a
scalar edge property map, and the in-degree will correspond to the sum of
the weights of the in-edges.
"""
if weight is None:
return self.__in_degree()
else:
return self.__weighted_in_degree(_prop("e", self.get_graph(), weight))
Vertex.in_degree = _in_degree
def _out_degree(self, weight=None):
"""Return the out-degree of the vertex. If provided, ``weight`` should be a
scalar edge property map, and the out-degree will correspond to the sum of
the weights of the out-edges.
"""
if weight is None:
return self.__out_degree()
else:
return self.__weighted_out_degree(_prop("e", self.get_graph(), weight))
Vertex.out_degree = _out_degree
def _vertex_repr(self):
if not self.is_valid():
......
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