Commit 668f14c9 authored by Tiago Peixoto's avatar Tiago Peixoto

Implement equality operator for (Vertex/Edge)PropertyMap

parent 14011da1
......@@ -440,6 +440,13 @@ void copy_external_edge_property(const GraphInterface& src,
boost::any prop_src,
boost::any prop_tgt);
bool compare_vertex_properties(const GraphInterface& gi,
boost::any prop1,
boost::any prop2);
bool compare_edge_properties(const GraphInterface& gi,
boost::any prop1,
boost::any prop2);
void export_python_interface();
void export_openmp();
......@@ -569,6 +576,8 @@ BOOST_PYTHON_MODULE(libgraph_tool_core)
def("set_vertex_property", &set_vertex_property);
def("set_edge_property", &set_edge_property);
def("copy_external_edge_property", &copy_external_edge_property);
def("compare_vertex_properties", &compare_vertex_properties);
def("compare_edge_properties", &compare_edge_properties);
class_<LibInfo>("mod_info")
.add_property("name", &LibInfo::GetName)
......
......@@ -39,3 +39,33 @@ void GraphInterface::copy_vertex_property(const GraphInterface& src,
all_graph_views(), all_graph_views(), writable_vertex_properties())
(this->get_graph_view(), src.get_graph_view(), prop_tgt);
}
bool compare_vertex_properties(const GraphInterface& gi,
boost::any prop1,
boost::any prop2)
{
bool ret = false;
gt_dispatch<>()
([&](auto& g, auto p1, auto p2)
{
ret = compare_props<vertex_selector>(g, p1, p2);
},
all_graph_views(), vertex_properties(), vertex_properties())
(gi.get_graph_view(), prop1, prop2);
return ret;
}
bool compare_edge_properties(const GraphInterface& gi,
boost::any prop1,
boost::any prop2)
{
bool ret = false;
gt_dispatch<>()
([&](auto& g, auto p1, auto p2)
{
ret = compare_props<edge_selector>(g, p1, p2);
},
all_graph_views(), edge_properties(), edge_properties())
(gi.get_graph_view(), prop1, prop2);
return ret;
}
......@@ -21,6 +21,7 @@
#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_properties.hh"
#include "graph_util.hh"
#include <boost/mpl/contains.hpp>
#include <boost/python/extract.hpp>
......@@ -78,6 +79,48 @@ struct copy_property
};
template <class IteratorSel, class Graph, class Prop1, class Prop2>
bool compare_props(Graph& g, Prop1 p1, Prop2 p2)
{
typedef typename boost::property_traits<Prop1>::value_type t1;
typedef typename boost::property_traits<Prop2>::value_type t2;
typename IteratorSel::template apply<Graph>::type vi, vi_end;
std::tie(vi, vi_end) = IteratorSel::range(g);
try
{
for (; vi != vi_end; ++vi)
{
auto v = *vi;
if constexpr (std::is_same_v<t1, t2>)
{
if (p1[v] != p2[v])
return false;
}
else if constexpr (std::is_same_v<t1, boost::python::object>)
{
if (p1[v] != boost::python::object(p2[v]))
return false;
}
else if constexpr (std::is_same_v<t2, boost::python::object>)
{
if (p2[v] != boost::python::object(p1[v]))
return false;
}
else
{
if (boost::lexical_cast<t1>(p2[v]) != p1[v])
return false;
}
}
}
catch (boost::bad_lexical_cast&)
{
return false;
}
return true;
};
struct edge_selector
{
template <class Graph>
......
......@@ -855,6 +855,14 @@ class VertexPropertyMap(PropertyMap):
return None
return PropertyArray(a, self)
def __eq__(self, other):
g = self.get_graph()
if g.base is not other.get_graph().base:
return False
return libcore.compare_vertex_properties(g._Graph__graph,
self._get_any(),
other._get_any())
class EdgePropertyMap(PropertyMap):
"""This class provides a mapping from edges to arbitrary properties.
......@@ -889,6 +897,14 @@ class EdgePropertyMap(PropertyMap):
return None
return PropertyArray(a, self)
def __eq__(self, other):
g = self.get_graph()
if g.base is not other.get_graph().base:
return False
return libcore.compare_edge_properties(g._Graph__graph,
self._get_any(),
other._get_any())
class GraphPropertyMap(PropertyMap):
"""This class provides a mapping from graphs to arbitrary properties.
......
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