cycles: rewrite using the tgba_digraph interface

Fixes #50.

* src/tgbaalgos/cycles.cc, src/tgbaalgos/cycles.hh: Rewrite using
unsigned instead of state*, and std::vector instead of std::map.
* src/tgbaalgos/isweakscc.cc, src/tgbaalgos/powerset.cc: Adjust.
This commit is contained in:
Alexandre Duret-Lutz 2015-01-18 14:03:16 +01:00
parent 8fd594f5d0
commit 1411fa6063
4 changed files with 76 additions and 122 deletions

View file

@ -1,6 +1,6 @@
// -*- coding: utf-8 -*-
// Copyright (C) 2012, 2014 Laboratoire de Recherche et Developpement de
// l'Epita (LRDE).
// Copyright (C) 2012, 2014, 2015 Laboratoire de Recherche et
// Developpement de l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
//
@ -23,23 +23,25 @@
namespace spot
{
enumerate_cycles::enumerate_cycles(const scc_info& map)
: aut_(map.get_aut()), sm_(map)
: aut_(map.get_aut()),
info_(aut_->num_states(), aut_->num_states()),
sm_(map)
{
}
void
enumerate_cycles::nocycle(tagged_state x, tagged_state y)
enumerate_cycles::nocycle(unsigned x, unsigned y)
{
// insert x in B(y)
y->second.b.insert(x->first);
info_[y].b.push_back(x);
// remove y from A(x)
x->second.del.insert(y->first);
info_[x].del[y] = true;
}
void
enumerate_cycles::unmark(tagged_state y)
enumerate_cycles::unmark(unsigned y)
{
std::deque<tagged_state> q;
std::vector<unsigned> q;
q.push_back(y);
while (!q.empty())
@ -47,102 +49,82 @@ namespace spot
y = q.back();
q.pop_back();
y->second.mark = false;
for (auto s: y->second.b)
info_[y].mark = false;
for (auto x: info_[y].b)
{
tagged_state x = tags_.find(s);
assert(x != tags_.end());
assert(info_[x].seen);
// insert y in A(x)
x->second.del.erase(y->first);
info_[x].del[y] = false;
// unmark x recursively if marked
if (x->second.mark)
if (info_[x].mark)
q.push_back(x);
}
// empty B(y)
y->second.b.clear();
info_[y].b.clear();
}
}
enumerate_cycles::tagged_state
enumerate_cycles::tag_state(const state* s)
{
auto p = tags_.emplace(s, state_info());
if (!p.second)
s->destroy();
return p.first;
}
void
enumerate_cycles::push_state(tagged_state ts)
enumerate_cycles::push_state(unsigned s)
{
ts->second.mark = true;
dfs_entry e;
e.ts = ts;
e.succ = 0;
e.f = false;
dfs_.push_back(e);
info_[s].mark = true;
dfs_.emplace_back(s);
}
// FIXME: Recode this algorithm using unsigned states.
void
enumerate_cycles::run(unsigned scc)
{
bool keep_going = true;
push_state(tag_state(aut_->state_from_number(sm_.one_state_of(scc))));
{
unsigned s = sm_.one_state_of(scc);
info_[s].seen = true;
push_state(s);
}
while (keep_going && !dfs_.empty())
{
dfs_entry& cur = dfs_.back();
bool cont;
if (cur.succ == 0)
{
cur.succ = aut_->succ_iter(cur.ts->first);
cont = cur.succ->first();
}
cur.succ = aut_->get_graph().state_storage(cur.s).succ;
else
cont = cur.succ->next();
if (cont)
cur.succ = aut_->trans_storage(cur.succ).next_succ;
if (cur.succ)
{
// Explore one successor.
// Ignore those that are not on the SCC, or destination
// that have been "virtually" deleted from A(v).
state* s = cur.succ->current_state();
if ((sm_.scc_of(aut_->state_number(s)) != scc)
|| (cur.ts->second.del.find(s) != cur.ts->second.del.end()))
{
s->destroy();
continue;
}
unsigned s = aut_->trans_storage(cur.succ).dst;
tagged_state w = tag_state(s);
if (!w->second.mark)
if ((sm_.scc_of(s) != scc) || (info_[cur.s].del[s]))
continue;
info_[s].seen = true;
if (!info_[s].mark)
{
push_state(w);
push_state(s);
}
else if (!w->second.reach)
else if (!info_[s].reach)
{
keep_going = cycle_found(w->first);
keep_going = cycle_found(s);
cur.f = true;
}
else
{
nocycle(cur.ts, w);
nocycle(cur.s, s);
}
}
else
{
// No more successors.
bool f = cur.f;
tagged_state v = cur.ts;
aut_->release_iter(cur.succ);
unsigned v = cur.s;
dfs_.pop_back();
if (f)
unmark(v);
v->second.reach = true;
info_[v].reach = true;
// Update the predecessor in the stack if there is one.
if (!dfs_.empty())
@ -150,39 +132,25 @@ namespace spot
if (f)
dfs_.back().f = true;
else
nocycle(dfs_.back().ts, v);
nocycle(dfs_.back().s, v);
}
}
}
// Purge the dfs_ stack, in case we aborted because cycle_found()
// returned false.
while (!dfs_.empty())
{
aut_->release_iter(dfs_.back().succ);
dfs_.pop_back();
}
hash_type::iterator i = tags_.begin();
while (i != tags_.end())
{
hash_type::iterator old = i;
++i;
old->first->destroy();
}
tags_.clear();
dfs_.clear();
}
bool
enumerate_cycles::cycle_found(const state* start)
enumerate_cycles::cycle_found(unsigned start)
{
dfs_stack::const_iterator i = dfs_.begin();
while (i->ts->first != start)
while (i->s != start)
++i;
do
{
std::cout << aut_->format_state(i->ts->first) << ' ';
std::cout << i->s << ' ';
++i;
}
while (i != dfs_.end());