Permalink
Browse files

Fix and improve Edge/Vertex ordering and comparisons

This fixes a bug where equal edge descriptors would be considered
unequal. Additionally, now Vertex and Edges can be ordered (via >, <,
>=, <=).
  • Loading branch information...
1 parent 247d3c9 commit 26f7d07b3359098fcc551b0a1159703bb4c10e18 @count0 committed Oct 16, 2014
Showing with 29 additions and 24 deletions.
  1. +0 −4 src/graph/graph_python_interface.cc
  2. +0 −20 src/graph/graph_python_interface.hh
  3. +29 −0 src/graph_tool/__init__.py
@@ -306,8 +306,6 @@ struct export_python_interface
"Return whether the edge is valid.")
.def("get_graph", &PythonEdge<Graph>::GetGraph,
"Return the graph to which the edge belongs.")
- .def(python::self == python::self)
- .def(python::self != python::self)
.def("__str__", &PythonEdge<Graph>::GetString)
.def("__hash__", &PythonEdge<Graph>::GetHash);
@@ -457,8 +455,6 @@ void export_python_interface()
"Return whether the vertex is valid.")
.def("get_graph", &PythonVertex::GetGraph,
"Return the graph to which the vertex belongs.")
- .def(boost::python::self == boost::python::self)
- .def(boost::python::self != boost::python::self)
.def("__str__", &PythonVertex::GetString)
.def("__int__", &PythonVertex::GetIndex)
.def("__hash__", &PythonVertex::GetHash);
@@ -272,16 +272,6 @@ public:
return _v;
}
- bool operator==(const PythonVertex& other) const
- {
- return other._v == _v;
- }
-
- bool operator!=(const PythonVertex& other) const
- {
- return other._v != _v;
- }
-
private:
boost::python::object _g;
GraphInterface::vertex_t _v;
@@ -403,16 +393,6 @@ public:
return std::hash<size_t>()(gi._edge_index[_e]);
}
- bool operator==(const PythonEdge& other) const
- {
- return other._e == _e;
- }
-
- bool operator!=(const PythonEdge& other) const
- {
- return other._e != _e;
- }
-
private:
boost::python::object _g;
edge_descriptor _e;
@@ -2388,6 +2388,13 @@ def _vertex_repr(self):
return "<Vertex object with index '%d' at 0x%x>" % (int(self), id(self))
Vertex.__repr__ = _vertex_repr
+Vertex.__eq__ = lambda v1, v2 : int(v1) == int(v2)
+Vertex.__ne__ = lambda v1, v2 : int(v1) != int(v2)
+Vertex.__lt__ = lambda v1, v2 : int(v1) < int(v2)
+Vertex.__gt__ = lambda v1, v2 : int(v1) > int(v2)
+Vertex.__le__ = lambda v1, v2 : int(v1) <= int(v2)
+Vertex.__ge__ = lambda v1, v2 : int(v1) >= int(v2)
+
_edge_doc = """Edge descriptor.
This class represents an edge in a :class:`~graph_tool.Graph`.
@@ -2396,6 +2403,21 @@ def _vertex_repr(self):
tuple, which contains the source and target vertices.
"""
+def _edge_cmp(e1, e2):
+ te1, te2 = tuple(e1), tuple(e2)
+ g1 = e1.get_graph()
+ g2 = e2.get_graph()
+ if not g1.is_directed():
+ te1 = sorted(te1)
+ if not g2.is_directed():
+ te2 = sorted(te2)
+ te1 = (te1, g1.edge_index[e1])
+ te2 = (te2, g2.edge_index[e2])
+ if te1 < te2:
+ return -1
+ if te1 > te2:
+ return 1
+ return 0
def _edge_iter(self):
"""Iterate over the source and target"""
@@ -2436,6 +2458,13 @@ def init_edge_classes():
e.__class__.__iter__ = _edge_iter
e.__class__.__doc__ = _edge_doc
+ e.__class__.__eq__ = lambda e1, e2 : _edge_cmp(e1, e2) == 0
+ e.__class__.__ne__ = lambda e1, e2 : _edge_cmp(e1, e2) != 0
+ e.__class__.__lt__ = lambda e1, e2 : _edge_cmp(e1, e2) < 0
+ e.__class__.__gt__ = lambda e1, e2 : _edge_cmp(e1, e2) > 0
+ e.__class__.__le__ = lambda e1, e2 : _edge_cmp(e1, e2) <= 0
+ e.__class__.__ge__ = lambda e1, e2 : _edge_cmp(e1, e2) >= 0
+
init_edge_classes()
# some shenanigans to make it seem there is only a single edge class

0 comments on commit 26f7d07

Please sign in to comment.