pdegen & toparity: minor refactor

* spot/twaalgos/degen.hh (is_partially_degeneralizable): Pass the
forbid vector by reference, and document it.  I hope that not passing
forbid by copy will get rid of a spurious "potential nullptr" warning
by gcc on Arch Linux.
* spot/twaalgos/degen.cc: Adjust, and refactor the code a bit.
* spot/twaalgos/toparity.cc: Likewise.
This commit is contained in:
Alexandre Duret-Lutz 2024-05-14 10:20:45 +02:00
parent ed91f59bbd
commit 2bd2abd4c9
3 changed files with 126 additions and 128 deletions

View file

@ -872,7 +872,7 @@ namespace spot
acc_cond::mark_t acc_cond::mark_t
is_partially_degeneralizable(const const_twa_graph_ptr& aut, is_partially_degeneralizable(const const_twa_graph_ptr& aut,
bool allow_inf, bool allow_fin, bool allow_inf, bool allow_fin,
std::vector<acc_cond::mark_t> forbid) const std::vector<acc_cond::mark_t>& forbid)
{ {
auto& code = aut->get_acceptance(); auto& code = aut->get_acceptance();
@ -881,16 +881,19 @@ namespace spot
acc_cond::mark_t res = {}; acc_cond::mark_t res = {};
unsigned res_sz = -1U; unsigned res_sz = -1U;
auto update = [&](const acc_cond::mark_t& m)
auto keep_smallest_mark = [&](const acc_cond::mark_t& m)
{ {
if (std::find(forbid.begin(), forbid.end(), m) != forbid.end())
return false;
unsigned sz = m.count(); unsigned sz = m.count();
if (sz > 1 && sz < res_sz) if (sz > 1 && sz < res_sz)
{ {
res_sz = sz; res_sz = sz;
res = m; res = m;
} }
// If we have found a pair to degeneralize, we // If we have found a pair to degeneralize, we won't find a
// won't find // smaller one.
return res_sz == 2; return res_sz == 2;
}; };
@ -906,22 +909,14 @@ namespace spot
case acc_cond::acc_op::Fin: case acc_cond::acc_op::Fin:
case acc_cond::acc_op::FinNeg: case acc_cond::acc_op::FinNeg:
pos -= 2; pos -= 2;
if (allow_fin) if (allow_fin && keep_smallest_mark(code[pos].mark))
{
auto m = code[pos].mark;
if (!std::count(forbid.begin(), forbid.end(), m) && update(m))
return res; return res;
}
break; break;
case acc_cond::acc_op::Inf: case acc_cond::acc_op::Inf:
case acc_cond::acc_op::InfNeg: case acc_cond::acc_op::InfNeg:
pos -= 2; pos -= 2;
if (allow_inf) if (allow_inf && keep_smallest_mark(code[pos].mark))
{
auto m = code[pos].mark;
if (!std::count(forbid.begin(), forbid.end(), m) && update(m))
return res; return res;
}
break; break;
} }
} }

View file

@ -158,11 +158,14 @@ namespace spot
/// ///
/// The optional arguments \a allow_inf and \a allow_fin, can be set /// The optional arguments \a allow_inf and \a allow_fin, can be set
/// to false to disallow one type of match. /// to false to disallow one type of match.
///
/// If you need to disallow certain marks from being returned, pass
/// them in the \a forbid vector.
SPOT_API acc_cond::mark_t SPOT_API acc_cond::mark_t
is_partially_degeneralizable(const const_twa_graph_ptr& aut, is_partially_degeneralizable(const const_twa_graph_ptr& aut,
bool allow_inf = true, bool allow_inf = true, bool allow_fin = true,
bool allow_fin = true, const std::vector<acc_cond::mark_t>&
std::vector<acc_cond::mark_t> forbid = {}); forbid = {});
/// \ingroup twa_algorithms /// \ingroup twa_algorithms
/// \brief Propagate marks around the automaton /// \brief Propagate marks around the automaton

View file

@ -2248,7 +2248,8 @@ namespace spot
{ {
// If we don't try to find a parity prefix, we can stop // If we don't try to find a parity prefix, we can stop
// to construct the tree when it has not parity shape. // to construct the tree when it has not parity shape.
zielonka_tree_options zopt = zielonka_tree_options::MERGE_SUBTREES zielonka_tree_options zopt =
zielonka_tree_options::MERGE_SUBTREES
| zielonka_tree_options::CHECK_PARITY; | zielonka_tree_options::CHECK_PARITY;
if (!opt_.parity_prefix) if (!opt_.parity_prefix)
zopt = zopt | zielonka_tree_options::ABORT_WRONG_SHAPE; zopt = zopt | zielonka_tree_options::ABORT_WRONG_SHAPE;
@ -2265,21 +2266,21 @@ namespace spot
&& try_parity_prefix_general(sub_aut)) && try_parity_prefix_general(sub_aut))
return; return;
if (opt_.generic_emptiness && !tried_emptiness if (opt_.generic_emptiness
&& try_emptiness(sub_aut, tried_emptiness)) && !tried_emptiness && try_emptiness(sub_aut, tried_emptiness))
return; return;
// Buchi_type_to_buchi is more general that Rabin_to_buchi so // Buchi_type_to_buchi is more general that Rabin_to_buchi so
// we just call rabin_to_buchi if buchi_type_to_buchi is false. // we just call rabin_to_buchi if buchi_type_to_buchi is false.
if (!opt_.buchi_type_to_buchi && !opt_.parity_type_to_parity if (!opt_.buchi_type_to_buchi
&& opt_.rabin_to_buchi && !opt_.parity_type_to_parity && opt_.rabin_to_buchi
&& try_rabin_to_buchi(sub_aut)) && try_rabin_to_buchi(sub_aut))
return; return;
// As parity_type_to_parity is stronger, we don't // As parity_type_to_parity is stronger, we don't
// try if this option is used. // try if this option is used.
if (opt_.buchi_type_to_buchi && !opt_.parity_type_to_parity if (opt_.buchi_type_to_buchi
&& try_buchi_type(sub_aut)) && !opt_.parity_type_to_parity && try_buchi_type(sub_aut))
return; return;
// We don't do it if parity_prefix_general is true as on a parity-type // We don't do it if parity_prefix_general is true as on a parity-type
@ -2289,16 +2290,15 @@ namespace spot
&& try_parity_type(sub_aut)) && try_parity_type(sub_aut))
return; return;
if (opt_.partial_degen if (opt_.partial_degen)
&& is_partially_degeneralizable(sub_aut, true, true))
{ {
auto deg = sub_aut; twa_graph_ptr deg = sub_aut;
std::vector<acc_cond::mark_t> forbid; std::vector<acc_cond::mark_t> forbid;
auto m = is_partially_degeneralizable(sub_aut, true, true, forbid);
bool changed = false; bool changed = false;
while (m) while (acc_cond::mark_t m =
is_partially_degeneralizable(deg, true, true, forbid))
{ {
auto tmp = partial_degeneralize(deg, m); twa_graph_ptr tmp = partial_degeneralize(deg, m);
simplify_acceptance_here(tmp); simplify_acceptance_here(tmp);
if (keep_deg(deg, tmp)) if (keep_deg(deg, tmp))
{ {
@ -2308,10 +2308,10 @@ namespace spot
changed_structure = true; changed_structure = true;
} }
else else
{
forbid.emplace_back(m); forbid.emplace_back(m);
m = is_partially_degeneralizable(deg, true, true, forbid);
} }
}
if (changed) if (changed)
{ {
sub_aut = deg; sub_aut = deg;