reachiter: fix the DFS, and add a version with on_stack()

* src/tgbaalgos/reachiter.hh, src/tgbaalgos/reachiter.cc: Fix the
tgba_reachable_iterator_depth_first implementation by not making
inheriting from tgba_reachable_iterator.  Add a
tgba_reachable_iterator_depth_first_stack
* src/tgbatest/sim.test, src/tgbatest/dstar.test: Adjust.
This commit is contained in:
Alexandre Duret-Lutz 2013-02-01 08:26:59 +01:00
parent 57f712fcbd
commit 29bc087d56
4 changed files with 256 additions and 54 deletions

View file

@ -1,5 +1,5 @@
// Copyright (C) 2009, 2011 Laboratoire de Recherche et Développement
// de l'Epita (LRDE).
// Copyright (C) 2009, 2011, 2013 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
// Copyright (C) 2003, 2004 Laboratoire d'Informatique de Paris 6 (LIP6),
// département Systèmes Répartis Coopératifs (SRC), Université Pierre
// et Marie Curie.
@ -115,31 +115,6 @@ namespace spot
{
}
// tgba_reachable_iterator_depth_first
//////////////////////////////////////////////////////////////////////
tgba_reachable_iterator_depth_first::
tgba_reachable_iterator_depth_first(const tgba* a)
: tgba_reachable_iterator(a)
{
}
void
tgba_reachable_iterator_depth_first::add_state(const state* s)
{
todo.push(s);
}
const state*
tgba_reachable_iterator_depth_first::next_state()
{
if (todo.empty())
return 0;
const state* s = todo.top();
todo.pop();
return s;
}
// tgba_reachable_iterator_breadth_first
//////////////////////////////////////////////////////////////////////
@ -165,4 +140,161 @@ namespace spot
return s;
}
// tgba_reachable_iterator_depth_first
//////////////////////////////////////////////////////////////////////
tgba_reachable_iterator_depth_first::
tgba_reachable_iterator_depth_first(const tgba* a)
: aut_(a)
{
}
tgba_reachable_iterator_depth_first::~tgba_reachable_iterator_depth_first()
{
seen_map::const_iterator s = seen.begin();
while (s != seen.end())
{
// Advance the iterator before deleting the "key" pointer.
const state* ptr = s->first;
++s;
ptr->destroy();
}
}
void
tgba_reachable_iterator_depth_first::push(const state* s, int sn)
{
tgba_succ_iterator* si = aut_->succ_iter(s);
process_state(s, sn, si);
stack_item item = { s, sn, si };
todo.push_back(item);
si->first();
}
void
tgba_reachable_iterator_depth_first::pop()
{
delete todo.back().it;
todo.pop_back();
if (!todo.empty())
todo.back().it->next();
}
void
tgba_reachable_iterator_depth_first::run()
{
int n = 1;
start();
state* i = aut_->get_init_state();
if (want_state(i))
push(i, n);
seen[i] = n++;
const state* dst;
while (!todo.empty())
{
tgba_succ_iterator* si = todo.back().it;
if (si->done())
{
pop();
continue;
}
dst = si->current_state();
std::pair<seen_map::iterator, bool> res =
seen.insert(std::make_pair(dst, n));
if (!res.second)
{
// The state has already been seen.
dst->destroy();
// 0-numbered states are not wanted.
if (res.first->second == 0)
{
si->next();
continue;
}
dst = res.first->first;
}
else if (!want_state(dst))
{
// Mark this state as non-wanted in case we see it again.
res.first->second = 0;
si->next();
continue;
}
else
{
++n;
}
int dst_n = res.first->second;
process_link(todo.back().src, todo.back().src_n, dst, dst_n, si);
if (res.second)
push(dst, dst_n);
else
si->next();
}
end();
}
bool
tgba_reachable_iterator_depth_first::want_state(const state*) const
{
return true;
}
void
tgba_reachable_iterator_depth_first::start()
{
}
void
tgba_reachable_iterator_depth_first::end()
{
}
void
tgba_reachable_iterator_depth_first::process_state(const state*, int,
tgba_succ_iterator*)
{
}
void
tgba_reachable_iterator_depth_first::process_link(const state*, int,
const state*, int,
const tgba_succ_iterator*)
{
}
// tgba_reachable_iterator_depth_first_stack
//////////////////////////////////////////////////////////////////////
tgba_reachable_iterator_depth_first_stack::
tgba_reachable_iterator_depth_first_stack(const tgba* a)
: tgba_reachable_iterator_depth_first(a)
{
}
void
tgba_reachable_iterator_depth_first_stack::push(const state* s, int sn)
{
stack_.insert(sn);
this->tgba_reachable_iterator_depth_first::push(s, sn);
}
void
tgba_reachable_iterator_depth_first_stack::pop()
{
stack_.erase(todo.back().src_n);
this->tgba_reachable_iterator_depth_first::pop();
}
bool
tgba_reachable_iterator_depth_first_stack::on_stack(int sn) const
{
return stack_.find(sn) != stack_.end();
}
}