Commit 4571d6dd authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz

copy: rewrite as a BFS without using reachiter

* spot/twaalgos/copy.hh: Trim includes.
* spot/twaalgos/copy.cc: Rewrite.
* tests/python/ltsmin.ipynb: Adjust.
parent 9b95b697
// -*- coding: utf-8 -*-
// Copyright (C) 2009, 2011, 2012, 2014, 2015 Laboratoire de Recherche
// Copyright (C) 2009, 2011, 2012, 2014, 2015, 2016 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
......@@ -22,73 +22,68 @@
#include <spot/twaalgos/copy.hh>
#include <spot/twa/twagraph.hh>
#include <sstream>
#include <string>
#include <map>
#include <spot/twaalgos/reachiter.hh>
#include <spot/twaalgos/dot.hh>
#include <deque>
namespace spot
{
namespace
twa_graph_ptr
copy(const const_twa_ptr& aut, twa::prop_set p, bool preserve_names)
{
class copy_iter: public tgba_reachable_iterator_depth_first
{
public:
copy_iter(const const_twa_ptr& a, twa::prop_set p,
bool preserve_names)
: tgba_reachable_iterator_depth_first(a),
out_(make_twa_graph(a->get_dict()))
twa_graph_ptr out = make_twa_graph(aut->get_dict());
out->copy_acceptance_of(aut);
out->copy_ap_of(aut);
out->prop_copy(aut, p);
std::vector<std::string>* names = nullptr;
if (preserve_names)
{
out_->copy_acceptance_of(a);
out_->copy_ap_of(a);
out_->prop_copy(a, p);
if (preserve_names)
{
names_ = new std::vector<std::string>;
out_->set_named_prop("state-names", names_);
}
names = new std::vector<std::string>;
out->set_named_prop("state-names", names);
}
twa_graph_ptr
result()
{
return out_;
}
// States already seen.
state_map<unsigned> seen;
// States to process
std::deque<state_map<unsigned>::const_iterator> todo;
virtual void
process_state(const state* s, int n, twa_succ_iterator*)
auto new_state = [&](const state* s) -> unsigned
{
unsigned ns = out_->new_state();
if (names_)
names_->emplace_back(aut_->format_state(s));
assert(ns == static_cast<unsigned>(n) - 1);
(void)ns;
(void)n;
}
auto p = seen.emplace(s, 0);
if (p.second)
{
p.first->second = out->new_state();
todo.push_back(p.first);
if (names)
names->push_back(aut->format_state(s));
}
else
{
s->destroy();
}
return p.first->second;
};
virtual void
process_link(const state*, int in,
const state*, int out,
const twa_succ_iterator* si)
out->set_init_state(new_state(aut->get_init_state()));
while (!todo.empty())
{
out_->new_edge
(in - 1, out - 1, si->cond(),
si->acc());
const state* src1;
unsigned src2;
std::tie(src1, src2) = *todo.front();
todo.pop_front();
for (auto* t: aut->succ(src1))
out->new_edge(src2, new_state(t->dst()), t->cond(), t->acc());
}
protected:
twa_graph_ptr out_;
std::vector<std::string>* names_ = nullptr;
};
} // anonymous
auto s = seen.begin();
while (s != seen.end())
{
// Advance the iterator before deleting the "key" pointer.
const state* ptr = s->first;
++s;
ptr->destroy();
}
twa_graph_ptr
copy(const const_twa_ptr& aut, twa::prop_set p, bool preserve_names)
{
copy_iter di(aut, p, preserve_names);
di.run();
return di.result();
return out;
}
}
// -*- coding: utf-8 -*-
// Copyright (C) 2012, 2013, 2014, 2015 Laboratoire de Recherche et
// Copyright (C) 2012, 2013, 2014, 2015, 2016 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
// Copyright (C) 2003, 2004, 2005 Laboratoire d'Informatique de Paris
// 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
......@@ -25,7 +25,6 @@
#include <spot/misc/common.hh>
#include <spot/twa/fwd.hh>
#include <spot/twa/twa.hh>
#include <vector>
namespace spot
{
......@@ -34,6 +33,5 @@ namespace spot
///
/// This works for using the abstract interface for automata
SPOT_API twa_graph_ptr
copy(const const_twa_ptr& aut, twa::prop_set p,
bool preserve_names = false);
copy(const const_twa_ptr& aut, twa::prop_set p, bool preserve_names = false);
}
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment