Commit 7581ee16 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Improve Graph.remove_edge()

It now accepts edge descriptors from all graph views and invalidates
them after removal.
parent 6323a77d
Pipeline #376 passed with stage
in 175 minutes and 8 seconds
......@@ -218,25 +218,12 @@ python::object add_edge(GraphInterface& gi, size_t s, size_t t)
return new_e;
}
void remove_edge(GraphInterface& gi, const python::object& e)
void remove_edge(GraphInterface& gi, EdgeBase& e)
{
bool found = false;
run_action<>()(gi,
[&](auto& g)
{
typedef typename std::remove_reference<decltype(g)>::type g_t;
python::extract<PythonEdge<g_t>&> get_e(e);
if (get_e.check())
{
PythonEdge<g_t>& pe = get_e();
pe.check_valid();
auto edge = pe.get_descriptor();
remove_edge(edge, g);
found = true;
}
})();
if (!found)
throw ValueException("invalid edge descriptor");
e.check_valid();
auto edge = e.get_descriptor();
run_action<>()(gi, [&](auto& g) { remove_edge(edge, g); })();
e.invalidate();
}
struct get_edge_dispatch
......@@ -687,7 +674,7 @@ void export_python_interface()
using namespace boost::python;
class_<VertexBase>("VertexBase", no_init);
class_<EdgeBase>("EdgeBase", no_init);
class_<EdgeBase, boost::noncopyable>("EdgeBase", no_init);
typedef boost::mpl::transform<graph_tool::all_graph_views,
boost::mpl::quote1<std::add_const> >::type const_graph_views;
......
......@@ -293,7 +293,14 @@ private:
// below are classes related to the PythonEdge type
class EdgeBase {}; // useful to unite all edge types
class EdgeBase // useful to unite all edge types
{
public:
virtual bool is_valid() const = 0;
virtual void check_valid() const = 0;
virtual void invalidate() = 0;
virtual GraphInterface::edge_t get_descriptor() const = 0;
};
template <class Graph>
class PythonEdge : public EdgeBase
......@@ -303,7 +310,7 @@ public:
PythonEdge(std::weak_ptr<Graph> g, edge_descriptor e)
: _g(g), _e(e) {}
bool is_valid() const
virtual bool is_valid() const
{
if (_g.expired())
return false;
......@@ -316,13 +323,18 @@ public:
return ((s < num_vertices(g)) && (t < num_vertices(g)));
}
void check_valid() const
virtual void check_valid() const
{
if (!is_valid())
throw ValueException("invalid edge descriptor");
}
GraphInterface::edge_t get_descriptor() const
virtual void invalidate()
{
_g.reset();
}
virtual GraphInterface::edge_t get_descriptor() const
{
return _e;
}
......
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