python: add easy ways to remove highlights

Fixes #554, reported by Dávid Smolka.

* python/spot/impl.i (highlight_edge, highlight_state): Add versions
where the color is nullptr and map that to None.
(remove_highlight_states, remove_highlight_edges): New function.
* tests/python/highlighting.ipynb: Demonstrate those new methods.
This commit is contained in:
Alexandre Duret-Lutz 2023-11-23 17:08:31 +01:00
parent 0dd623b358
commit 193fdd6f95
3 changed files with 315 additions and 41 deletions

View file

@ -407,6 +407,16 @@ namespace swig
$result = SWIG_FromCharPtr($1->c_str());
}
%typemap(typecheck, precedence=2000) std::nullptr_t {
$1 = $input == Py_None;
}
%typemap(in) std::nullptr_t {
if ($input != Py_None)
%argument_fail(SWIG_TypeError, "std::nullptr_t", $symname, $argnum);
$1 = nullptr;
}
// For some reason, Swig can convert [aut1,aut2,...] into
// std::vector<spot::twa_graph_ptr>, but not into
// std::vector<spot::const_twa_graph_ptr>. Let's fix that by using
@ -1037,6 +1047,21 @@ static void* ptr_for_bdddict(PyObject* obj)
return self;
}
twa* highlight_state(unsigned state, std::nullptr_t color) // color=None
{
(void) color;
if (std::map<unsigned, unsigned>* hs =
self->get_named_prop<std::map<unsigned, unsigned>>("highlight-states"))
hs->erase(state);
return self;
}
twa* remove_highlight_states()
{
self->set_named_prop("highlight-states", nullptr);
return self;
}
twa* highlight_edge(unsigned edge, unsigned color)
{
auto ht =
@ -1049,6 +1074,21 @@ static void* ptr_for_bdddict(PyObject* obj)
(*ht)[edge] = color;
return self;
}
twa* highlight_edge(unsigned edge, std::nullptr_t color) // color=None
{
(void) color;
if (std::map<unsigned, unsigned>* hs =
self->get_named_prop<std::map<unsigned, unsigned>>("highlight-edges"))
hs->erase(edge);
return self;
}
twa* remove_highlight_edges()
{
self->set_named_prop("highlight-edges", nullptr);
return self;
}
}
%extend spot::internal::state_out<spot::digraph<spot::twa_graph_state, spot::twa_graph_edge_data>> {