split: factor the code common to both split_edges() versions
* spot/twaalgos/split.cc: The two split_edges() versions only differ by the way they split a label. Let's define all the rest of the algorithm in split_edges_aux().
This commit is contained in:
parent
ef10be047c
commit
0a045e5f76
1 changed files with 197 additions and 252 deletions
|
|
@ -51,19 +51,21 @@ namespace std
|
||||||
|
|
||||||
namespace spot
|
namespace spot
|
||||||
{
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
// We attempt to add a potentially new set of symbols defined as "value" to
|
// We attempt to add a potentially new set of symbols defined as "value" to
|
||||||
// our current set of edge partitions, "current_set". We also specify a set
|
// our current set of edge partitions, "current_set". We also specify a set
|
||||||
// of valid symbols considered
|
// of valid symbols considered
|
||||||
static void add_to_lower_bound_set_helper(
|
static void
|
||||||
std::unordered_set<bdd>& current_set,
|
add_to_lower_bound_set_helper(std::unordered_set<bdd>& current_set,
|
||||||
bdd valid_symbol_set,
|
bdd valid_symbol_set, bdd value)
|
||||||
bdd value)
|
|
||||||
{
|
{
|
||||||
// This function's correctness is defined by the invariant, that we never
|
// This function's correctness is defined by the invariant, that
|
||||||
// add a bdd to our current set unless the bdd is disjoint from every other
|
// we never add a bdd to our current set unless the bdd is
|
||||||
// element in the current_set. In other words, we will only reach the final
|
// disjoint from every other element in the current_set. In
|
||||||
// set.insert(value), if we can iterate over the whole of current_set
|
// other words, we will only reach the final set.insert(value),
|
||||||
// without finding some set intersections
|
// if we can iterate over the whole of current_set without
|
||||||
|
// finding some set intersections
|
||||||
if (value == bddfalse) // Don't add empty sets, as they subsume everything
|
if (value == bddfalse) // Don't add empty sets, as they subsume everything
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
|
@ -75,8 +77,9 @@ namespace spot
|
||||||
// are disjoint.
|
// are disjoint.
|
||||||
if (bdd_implies(sym, value))
|
if (bdd_implies(sym, value))
|
||||||
{
|
{
|
||||||
add_to_lower_bound_set_helper(
|
add_to_lower_bound_set_helper(current_set,
|
||||||
current_set, valid_symbol_set, (value - sym) & valid_symbol_set);
|
valid_symbol_set,
|
||||||
|
(value - sym) & valid_symbol_set);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// If a sym is a subset of the value we're trying to add, then we
|
// If a sym is a subset of the value we're trying to add, then we
|
||||||
|
|
@ -99,21 +102,6 @@ namespace spot
|
||||||
current_set.insert(value);
|
current_set.insert(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::array<bdd, 4> create_possible_intersections(
|
|
||||||
bdd valid_symbol_set,
|
|
||||||
std::pair<bdd, bdd> const& first,
|
|
||||||
std::pair<bdd, bdd> const& second)
|
|
||||||
{
|
|
||||||
auto intermediate = second.first & valid_symbol_set;
|
|
||||||
auto intermediate2 = second.second & valid_symbol_set;
|
|
||||||
return {
|
|
||||||
first.first & intermediate,
|
|
||||||
first.second & intermediate,
|
|
||||||
first.first & intermediate2,
|
|
||||||
first.second & intermediate2,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
using bdd_set = std::unordered_set<bdd>;
|
using bdd_set = std::unordered_set<bdd>;
|
||||||
using bdd_pair_set = std::unordered_set<std::pair<bdd, bdd>>;
|
using bdd_pair_set = std::unordered_set<std::pair<bdd, bdd>>;
|
||||||
|
|
||||||
|
|
@ -123,12 +111,12 @@ namespace spot
|
||||||
bdd valid_symbol_set)
|
bdd valid_symbol_set)
|
||||||
{
|
{
|
||||||
bdd_pair_set intersections;
|
bdd_pair_set intersections;
|
||||||
for (auto& sym : basis)
|
for (bdd sym: basis)
|
||||||
{
|
{
|
||||||
auto intersection = sym & valid_symbol_set;
|
bdd intersection = sym & valid_symbol_set;
|
||||||
if (intersection != bddfalse)
|
if (intersection != bddfalse)
|
||||||
{
|
{
|
||||||
auto negation = valid_symbol_set - intersection;
|
bdd negation = valid_symbol_set - intersection;
|
||||||
intersections.insert(std::make_pair(intersection, negation));
|
intersections.insert(std::make_pair(intersection, negation));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -140,17 +128,16 @@ namespace spot
|
||||||
bdd valid_symbol_set,
|
bdd valid_symbol_set,
|
||||||
Callable callable)
|
Callable callable)
|
||||||
{
|
{
|
||||||
for (auto it = complement_pairs.begin(); it != complement_pairs.end(); ++it)
|
for (auto it = complement_pairs.begin();
|
||||||
{
|
it != complement_pairs.end(); ++it)
|
||||||
for (auto it2 = std::next(it); it2 != complement_pairs.end(); ++it2)
|
for (auto it2 = std::next(it); it2 != complement_pairs.end(); ++it2)
|
||||||
{
|
{
|
||||||
auto intersections = create_possible_intersections(
|
auto intermediate = it2->first & valid_symbol_set;
|
||||||
valid_symbol_set, *it, *it2);
|
auto intermediate2 = it2->second & valid_symbol_set;
|
||||||
for (auto& intersection : intersections)
|
callable(it->first & intermediate);
|
||||||
{
|
callable(it->second & intermediate);
|
||||||
callable(intersection);
|
callable(it->first & intermediate2);
|
||||||
}
|
callable(it->second & intermediate2);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -167,14 +154,10 @@ namespace spot
|
||||||
auto& pair = *complement_pairs.begin();
|
auto& pair = *complement_pairs.begin();
|
||||||
if (pair.first != bddfalse
|
if (pair.first != bddfalse
|
||||||
&& bdd_implies(pair.first, valid_symbol_set))
|
&& bdd_implies(pair.first, valid_symbol_set))
|
||||||
{
|
|
||||||
lower_bound.insert(pair.first);
|
lower_bound.insert(pair.first);
|
||||||
}
|
|
||||||
if (pair.second != bddfalse
|
if (pair.second != bddfalse
|
||||||
&& bdd_implies(pair.second, valid_symbol_set))
|
&& bdd_implies(pair.second, valid_symbol_set))
|
||||||
{
|
|
||||||
lower_bound.insert(pair.second);
|
lower_bound.insert(pair.second);
|
||||||
}
|
|
||||||
return lower_bound;
|
return lower_bound;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -187,34 +170,35 @@ namespace spot
|
||||||
valid_symbol_set,
|
valid_symbol_set,
|
||||||
intersection);
|
intersection);
|
||||||
});
|
});
|
||||||
|
|
||||||
return lower_bound;
|
return lower_bound;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Partitions a symbol based on a list of other bdds called the basis.
|
// Partitions a symbol based on a list of other bdds called the
|
||||||
// The resulting partition will have the property that for any paritioned
|
// basis. The resulting partition will have the property that for
|
||||||
// element and any element element in the basis, the partitioned element will
|
// any partitioned element and any element element in the basis,
|
||||||
// either by completely contained by that element of the basis, or completely
|
// the partitioned element will either by completely contained by
|
||||||
// disjoint.
|
// that element of the basis, or completely disjoint.
|
||||||
static bdd_set generate_contained_or_disjoint_symbols(bdd sym,
|
static bdd_set
|
||||||
|
generate_contained_or_disjoint_symbols(bdd sym,
|
||||||
std::vector<bdd> const& basis)
|
std::vector<bdd> const& basis)
|
||||||
{
|
{
|
||||||
auto lower_bound = lower_set_bound(basis, sym);
|
auto lower_bound = lower_set_bound(basis, sym);
|
||||||
// If the sym was disjoint from everything in the basis, we'll be left with
|
// If the sym was disjoint from everything in the basis, we'll
|
||||||
// an empty lower_bound. To fix this, we will simply return a singleton,
|
// be left with an empty lower_bound. To fix this, we will
|
||||||
// with sym as the only element. Notice, this singleton will satisfy the
|
// simply return a singleton, with sym as the only
|
||||||
// requirements of a return value from this function. Additionally, if the
|
// element. Notice, this singleton will satisfy the requirements
|
||||||
// sym is false, that means nothing can traverse it, so we simply are left
|
// of a return value from this function. Additionally, if the
|
||||||
// with no edges.
|
// sym is false, that means nothing can traverse it, so we
|
||||||
|
// simply are left with no edges.
|
||||||
if (lower_bound.empty() && sym != bddfalse)
|
if (lower_bound.empty() && sym != bddfalse)
|
||||||
{
|
|
||||||
lower_bound.insert(sym);
|
lower_bound.insert(sym);
|
||||||
}
|
|
||||||
return lower_bound;
|
return lower_bound;
|
||||||
}
|
}
|
||||||
|
|
||||||
twa_graph_ptr split_edges(const const_twa_graph_ptr& aut)
|
template<typename genlabels>
|
||||||
|
twa_graph_ptr split_edges_aux(const const_twa_graph_ptr& aut,
|
||||||
|
genlabels gen)
|
||||||
{
|
{
|
||||||
twa_graph_ptr out = make_twa_graph(aut->get_dict());
|
twa_graph_ptr out = make_twa_graph(aut->get_dict());
|
||||||
out->copy_acceptance_of(aut);
|
out->copy_acceptance_of(aut);
|
||||||
|
|
@ -235,7 +219,6 @@ namespace spot
|
||||||
typedef robin_hood::pair<unsigned, unsigned> cached_t;
|
typedef robin_hood::pair<unsigned, unsigned> cached_t;
|
||||||
robin_hood::unordered_map<unsigned, cached_t> split_cond;
|
robin_hood::unordered_map<unsigned, cached_t> split_cond;
|
||||||
|
|
||||||
bdd all = aut->ap_vars();
|
|
||||||
internal::univ_dest_mapper<twa_graph::graph_t> uniq(out->get_graph());
|
internal::univ_dest_mapper<twa_graph::graph_t> uniq(out->get_graph());
|
||||||
|
|
||||||
for (auto& e: aut->edges())
|
for (auto& e: aut->edges())
|
||||||
|
|
@ -254,7 +237,7 @@ namespace spot
|
||||||
if (begin == end)
|
if (begin == end)
|
||||||
{
|
{
|
||||||
begin = out->num_edges() + 1;
|
begin = out->num_edges() + 1;
|
||||||
for (bdd minterm: minterms_of(cond, all))
|
for (bdd minterm: gen(cond))
|
||||||
out->new_edge(e.src, dst, minterm, e.acc);
|
out->new_edge(e.src, dst, minterm, e.acc);
|
||||||
end = out->num_edges() + 1;
|
end = out->num_edges() + 1;
|
||||||
}
|
}
|
||||||
|
|
@ -267,60 +250,22 @@ namespace spot
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
twa_graph_ptr split_edges(const const_twa_graph_ptr& aut)
|
||||||
|
{
|
||||||
|
bdd all = aut->ap_vars();
|
||||||
|
return split_edges_aux(aut, [&](bdd cond) {
|
||||||
|
return minterms_of(cond, all);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
twa_graph_ptr split_edges(const const_twa_graph_ptr& aut,
|
twa_graph_ptr split_edges(const const_twa_graph_ptr& aut,
|
||||||
std::vector<bdd> const& basis)
|
std::vector<bdd> const& basis)
|
||||||
{
|
{
|
||||||
twa_graph_ptr out = make_twa_graph(aut->get_dict());
|
bdd all = aut->ap_vars();
|
||||||
out->copy_acceptance_of(aut);
|
return split_edges_aux(aut, [&](bdd cond) {
|
||||||
out->copy_ap_of(aut);
|
return generate_contained_or_disjoint_symbols(cond, basis);
|
||||||
out->prop_copy(aut, twa::prop_set::all());
|
});
|
||||||
out->new_states(aut->num_states());
|
|
||||||
out->set_init_state(aut->get_init_state_number());
|
|
||||||
|
|
||||||
// We use a cache to avoid the costly loop around minterms_of().
|
|
||||||
// Cache entries have the form (id, [begin, end]) where id is the
|
|
||||||
// number of a BDD that as been (or will be) split, and begin/end
|
|
||||||
// denotes a range of existing transition numbers that cover the
|
|
||||||
// split.
|
|
||||||
using cached_t = std::pair<unsigned, unsigned>;
|
|
||||||
std::unordered_map<unsigned, cached_t> split_cond;
|
|
||||||
internal::univ_dest_mapper<twa_graph::graph_t> uniq(out->get_graph());
|
|
||||||
|
|
||||||
for (auto& e: aut->edges())
|
|
||||||
{
|
|
||||||
bdd const& cond = e.cond;
|
|
||||||
unsigned dst = e.dst;
|
|
||||||
|
|
||||||
if (cond == bddfalse)
|
|
||||||
continue;
|
|
||||||
if (aut->is_univ_dest(dst))
|
|
||||||
{
|
|
||||||
auto d = aut->univ_dests(dst);
|
|
||||||
dst = uniq.new_univ_dests(d.begin(), d.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& [begin, end] = split_cond[cond.id()];
|
|
||||||
if (begin == end)
|
|
||||||
{
|
|
||||||
begin = out->num_edges() + 1;
|
|
||||||
auto split = generate_contained_or_disjoint_symbols(cond,
|
|
||||||
basis);
|
|
||||||
for (bdd minterm : split)
|
|
||||||
{
|
|
||||||
out->new_edge(e.src, dst, minterm, e.acc);
|
|
||||||
}
|
|
||||||
end = out->num_edges() + 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
auto& g = out->get_graph();
|
|
||||||
for (unsigned i = begin; i < end; ++i)
|
|
||||||
{
|
|
||||||
out->new_edge(e.src, dst, g.edge_storage(i).cond, e.acc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue