Commit eca90cc4 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Fix bug in graph_union() when an intersection is given

parent 01ddad4f
......@@ -27,7 +27,7 @@
using namespace graph_tool;
using namespace boost;
typedef property_map_type::apply<int32_t,
typedef property_map_type::apply<int64_t,
GraphInterface::vertex_index_map_t>::type
vprop_t;
......
......@@ -34,29 +34,25 @@ struct graph_union
const
{
Graph& g = *gp;
typename graph_traits<Graph>::vertex_iterator v, v_end;
for (tie(v,v_end) = vertices(g); v != v_end; ++v)
for (auto v : vertices_range(g))
{
if (vmap[*v] == 0)
if (vmap[v] < 0)
{
vmap[*v] = add_vertex(ug);
vmap[v] = add_vertex(ug);
}
else
{
typename graph_traits<UnionGraph>::vertex_descriptor w =
vertex(vmap[*v] - 1, ug);
if (w == graph_traits<UnionGraph>::null_vertex() ||
w >= num_vertices(g))
vmap[*v] = add_vertex(ug);
auto w = vertex(vmap[v], ug);
if (w == graph_traits<UnionGraph>::null_vertex() || w >= num_vertices(ug))
vmap[v] = add_vertex(ug);
else
vmap[*v] = w;
vmap[v] = w;
}
}
typename graph_traits<Graph>::edge_iterator e, e_end;
for (tie(e,e_end) = edges(g); e != e_end; ++e)
emap[*e] = add_edge(vertex(vmap[source(*e,g)], g),
vertex(vmap[target(*e,g)], g), ug).first;
for (auto e : edges_range(g))
emap[e] = add_edge(vertex(vmap[source(e, g)], ug),
vertex(vmap[target(e, g)], ug), ug).first;
}
};
......@@ -69,8 +65,7 @@ struct property_union
UnionProp uprop, boost::any aprop) const
{
Graph& g = *gp;
typename UnionProp::checked_t prop =
any_cast<typename UnionProp::checked_t>(aprop);
auto prop = any_cast<typename UnionProp::checked_t>(aprop);
dispatch(ug, g, vmap, emap, uprop, prop,
std::is_same<typename property_traits<UnionProp>::key_type,
typename graph_traits<Graph>::vertex_descriptor>());
......@@ -78,12 +73,11 @@ struct property_union
template <class UnionGraph, class Graph, class VertexMap, class EdgeMap,
class UnionProp, class Prop>
void dispatch(UnionGraph&, Graph& g, VertexMap vmap, EdgeMap,
void dispatch(UnionGraph& ug, Graph& g, VertexMap vmap, EdgeMap,
UnionProp uprop, Prop prop, std::true_type) const
{
typename graph_traits<Graph>::vertex_iterator v, v_end;
for (tie(v,v_end) = vertices(g); v != v_end; ++v)
uprop[vertex(vmap[*v], g)] = prop[*v];
for (auto v : vertices_range(g))
uprop[vertex(vmap[v], ug)] = prop[v];
}
template <class UnionGraph, class Graph, class VertexMap, class EdgeMap,
......@@ -91,9 +85,8 @@ struct property_union
void dispatch(UnionGraph&, Graph& g, VertexMap, EdgeMap emap,
UnionProp uprop, Prop prop, std::false_type) const
{
typename graph_traits<Graph>::edge_iterator e, e_end;
for (tie(e,e_end) = edges(g); e != e_end; ++e)
uprop[emap[*e]] = prop[*e];
for (auto e : edges_range(g))
uprop[emap[e]] = prop[e];
}
};
......
......@@ -29,7 +29,7 @@ using namespace graph_tool;
using namespace boost;
typedef property_map_type::apply<int32_t,
typedef property_map_type::apply<int64_t,
GraphInterface::vertex_index_map_t>::type
vprop_t;
......
......@@ -27,7 +27,7 @@
using namespace graph_tool;
using namespace boost;
typedef property_map_type::apply<int32_t,
typedef property_map_type::apply<int64_t,
GraphInterface::vertex_index_map_t>::type
vprop_t;
......
......@@ -897,8 +897,9 @@ def line_graph(g):
def graph_union(g1, g2, intersection=None, props=None, include=False,
internal_props=False):
"""Return the union of graphs g1 and g2, composed of all edges and vertices
of g1 and g2, without overlap.
"""Return the union of graphs ``g1`` and ``g2``, composed of all edges and
vertices of ``g1`` and ``g2``, without overlap (if ``intersection ==
None``).
Parameters
----------
......@@ -907,8 +908,8 @@ def graph_union(g1, g2, intersection=None, props=None, include=False,
g2 : :class:`~graph_tool.Graph`
Second graph in the union.
intersection : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
Vertex property map owned by `g1` which maps each of its vertices
to vertex indexes belonging to `g2`. Negative values mean no mapping
Vertex property map owned by `g2` which maps each of its vertices
to vertex indexes belonging to `g1`. Negative values mean no mapping
exists, and thus both vertices in `g1` and `g2` will be present in the
union graph.
props : list of tuples of :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
......@@ -1034,12 +1035,9 @@ def graph_union(g1, g2, intersection=None, props=None, include=False,
vmask_flipped = True
if intersection is None:
intersection = g1.new_vertex_property("int32_t")
intersection.a = 0
intersection = g2.new_vertex_property("int64_t", -1)
else:
intersection = intersection.copy("int32_t")
intersection.a[intersection.a >= 0] += 1
intersection.a[intersection.a < 0] = 0
intersection = intersection.copy("int64_t")
u1 = GraphView(g1, directed=True, skip_properties=True)
u2 = GraphView(g2, directed=True, skip_properties=True)
......@@ -1642,4 +1640,4 @@ class DynamicSampler(libgraph_tool_generation.DynamicSampler):
libgraph_tool_generation.DynamicSampler.__init__(self, values, probs)
def sample(self):
return libgraph_tool_generation.DynamicSampler.sample(self, _get_rng())
return libgraph_tool_generation.DynamicSampler.sample(self, _get_rng())
\ No newline at end of file
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