Commit f2bdc674 authored by Tiago Peixoto's avatar Tiago Peixoto

Improve label_parallel_edges()

parent fa2b7b99
...@@ -27,12 +27,13 @@ using namespace std; ...@@ -27,12 +27,13 @@ using namespace std;
using namespace boost; using namespace boost;
using namespace graph_tool; using namespace graph_tool;
void do_label_parallel_edges(GraphInterface& gi, boost::any property) void do_label_parallel_edges(GraphInterface& gi, boost::any property,
bool mark_only, bool count_all)
{ {
GraphInterface::edge_index_map_t edge_index = GraphInterface::edge_index_map_t edge_index =
any_cast<GraphInterface::edge_index_map_t>(gi.GetEdgeIndex()); any_cast<GraphInterface::edge_index_map_t>(gi.GetEdgeIndex());
run_action<>()(gi, bind<void>(label_parallel_edges(), _1, run_action<>()(gi, bind<void>(label_parallel_edges(), _1,
edge_index, _2), edge_index, _2, mark_only, count_all),
edge_scalar_properties())(property); edge_scalar_properties())(property);
} }
......
...@@ -33,7 +33,7 @@ struct label_parallel_edges ...@@ -33,7 +33,7 @@ struct label_parallel_edges
{ {
template <class Graph, class EdgeIndexMap, class ParallelMap> template <class Graph, class EdgeIndexMap, class ParallelMap>
void operator()(const Graph& g, EdgeIndexMap edge_index, void operator()(const Graph& g, EdgeIndexMap edge_index,
ParallelMap parallel) const ParallelMap parallel, bool mark_only, bool count_all) const
{ {
typedef typename graph_traits<Graph>::edge_descriptor edge_t; typedef typename graph_traits<Graph>::edge_descriptor edge_t;
...@@ -45,14 +45,11 @@ struct label_parallel_edges ...@@ -45,14 +45,11 @@ struct label_parallel_edges
if (v == graph_traits<Graph>::null_vertex()) if (v == graph_traits<Graph>::null_vertex())
continue; continue;
tr1::unordered_set<edge_t,DescriptorHash<EdgeIndexMap> >
p_edges(0, DescriptorHash<EdgeIndexMap>(edge_index));
typename graph_traits<Graph>::out_edge_iterator e1, e2, typename graph_traits<Graph>::out_edge_iterator e1, e2,
e_end1, e_end2; e_end1, e_end2;
for (tie(e1, e_end1) = out_edges(v, g); e1 != e_end1; ++e1) for (tie(e1, e_end1) = out_edges(v, g); e1 != e_end1; ++e1)
{ {
if (p_edges.find(*e1) != p_edges.end()) if (get(parallel, *e1) != 0)
continue; continue;
// do not visit edges twice in undirected graphs // do not visit edges twice in undirected graphs
...@@ -60,14 +57,11 @@ struct label_parallel_edges ...@@ -60,14 +57,11 @@ struct label_parallel_edges
target(*e1, g) < v) target(*e1, g) < v)
continue; continue;
size_t n = 0; size_t n = count_all ? 1 : 0;
put(parallel, *e1, n); put(parallel, *e1, n);
for (tie(e2, e_end2) = out_edges(v, g); e2 != e_end2; ++e2) for (tie(e2, e_end2) = out_edges(v, g); e2 != e_end2; ++e2)
if (*e2 != *e1 && target(*e1, g) == target(*e2, g)) if (*e2 != *e1 && target(*e1, g) == target(*e2, g))
{ put(parallel, *e2, mark_only ? 1 : ++n);
put(parallel, *e2, ++n);
p_edges.insert(*e2);
}
} }
} }
} }
......
...@@ -19,8 +19,8 @@ ...@@ -19,8 +19,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
``graph_tool.stats`` - Graph Statistics ``graph_tool.stats`` - Miscellaneous statistics
--------------------------------------- -----------------------------------------------
Summary Summary
+++++++ +++++++
...@@ -278,15 +278,21 @@ def remove_labeled_edges(g, label): ...@@ -278,15 +278,21 @@ def remove_labeled_edges(g, label):
g.pop_filter(all=False, directed=True, reversed=True) g.pop_filter(all=False, directed=True, reversed=True)
def label_parallel_edges(g, eprop=None): def label_parallel_edges(g, mark_only=False, count_all=False, eprop=None):
r"""Label edges which are parallel, i.e, have the same source and target r"""Label edges which are parallel, i.e, have the same source and target
vertices. For each parallel edge set :math:`PE`, the labelling starts from 0 vertices. For each parallel edge set :math:`PE`, the labelling starts from 0
to :math:`|PE|-1`. If the `eprop` parameter is given (a to :math:`|PE|-1`. (If `count_all==True`, the range is 0 to :math:`|PE|`
:class:`~graph_tool.PropertyMap`), the labelling is stored there.""" instead). If `mark_only==True`, all parallel edges are simply marked with
if eprop == None: the value 1. If the `eprop` parameter is given
eprop = g.new_edge_property("int32_t") (a :class:`~graph_tool.PropertyMap`), the labelling is stored there."""
if eprop is None:
if mark_only:
eprop = g.new_edge_property("bool")
else:
eprop = g.new_edge_property("int32_t")
libgraph_tool_stats.\ libgraph_tool_stats.\
label_parallel_edges(g._Graph__graph, _prop("e", g, eprop)) label_parallel_edges(g._Graph__graph, _prop("e", g, eprop),
mark_only, count_all)
return eprop return eprop
......
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