A type such as 'const tgba_digraph*' and 'tgba_digraph*' are replaced by 'const_tgba_digraph_ptr' and 'tgba_digraph_ptr'. Additionally 'new tgba_digraph(...)' is replaced by 'make_tgba_digraph(...)'. This convention is followed by all automata types. Those smart pointers should normally be passed by const reference as input of function to avoid the atomic increments/decrements, but I probably missed a few, as this huge patch took me nearly 12h. * src/kripke/fwd.hh, src/tgba/fwd.hh: New files. * src/kripke/Makefile.am, src/tgba/Makefile.am: Adjust. * iface/dve2/dve2.cc, iface/dve2/dve2.hh, iface/dve2/dve2check.cc, src/bin/common_output.hh, src/bin/dstar2tgba.cc, src/bin/ltl2tgba.cc, src/bin/ltl2tgta.cc, src/bin/ltlcross.cc, src/bin/ltlfilt.cc, src/dstarparse/dra2ba.cc, src/dstarparse/dstar2tgba.cc, src/dstarparse/dstarparse.yy, src/dstarparse/nra2nba.cc, src/dstarparse/nsa2tgba.cc, src/dstarparse/public.hh, src/graphtest/tgbagraph.cc, src/kripke/fairkripke.hh, src/kripke/kripke.hh, src/kripke/kripkeexplicit.cc, src/kripke/kripkeexplicit.hh, src/kripke/kripkeprint.cc, src/kripke/kripkeprint.hh, src/kripkeparse/kripkeparse.yy, src/kripkeparse/public.hh, src/kripketest/parse_print_test.cc, src/ltlvisit/apcollect.cc, src/ltlvisit/apcollect.hh, src/ltlvisit/contain.cc, src/ltlvisit/contain.hh, src/neverparse/neverclaimparse.yy, src/neverparse/public.hh, src/priv/accmap.hh, src/priv/countstates.cc, src/priv/countstates.hh, src/saba/saba.hh, src/saba/sabacomplementtgba.cc, src/saba/sabacomplementtgba.hh, src/sabaalgos/sabadotty.cc, src/sabaalgos/sabadotty.hh, src/sabaalgos/sabareachiter.cc, src/sabaalgos/sabareachiter.hh, src/sabatest/sabacomplementtgba.cc, src/ta/ta.hh, src/ta/taexplicit.cc, src/ta/taexplicit.hh, src/ta/taproduct.cc, src/ta/taproduct.hh, src/ta/tgta.hh, src/ta/tgtaexplicit.cc, src/ta/tgtaexplicit.hh, src/ta/tgtaproduct.cc, src/ta/tgtaproduct.hh, src/taalgos/dotty.cc, src/taalgos/dotty.hh, src/taalgos/emptinessta.cc, src/taalgos/emptinessta.hh, src/taalgos/minimize.cc, src/taalgos/minimize.hh, src/taalgos/reachiter.cc, src/taalgos/reachiter.hh, src/taalgos/statessetbuilder.cc, src/taalgos/statessetbuilder.hh, src/taalgos/stats.cc, src/taalgos/stats.hh, src/taalgos/tgba2ta.cc, src/taalgos/tgba2ta.hh, src/tgba/bdddict.cc, src/tgba/bdddict.hh, src/tgba/formula2bdd.hh, src/tgba/futurecondcol.cc, src/tgba/futurecondcol.hh, src/tgba/taatgba.hh, src/tgba/tgba.cc, src/tgba/tgba.hh, src/tgba/tgbagraph.hh, src/tgba/tgbakvcomplement.cc, src/tgba/tgbakvcomplement.hh, src/tgba/tgbamask.cc, src/tgba/tgbamask.hh, src/tgba/tgbaproduct.cc, src/tgba/tgbaproduct.hh, src/tgba/tgbaproxy.cc, src/tgba/tgbaproxy.hh, src/tgba/tgbasafracomplement.cc, src/tgba/tgbasafracomplement.hh, src/tgba/tgbascc.cc, src/tgba/tgbascc.hh, src/tgba/tgbasgba.cc, src/tgba/tgbasgba.hh, src/tgba/wdbacomp.cc, src/tgba/wdbacomp.hh, src/tgbaalgos/bfssteps.cc, src/tgbaalgos/bfssteps.hh, src/tgbaalgos/complete.cc, src/tgbaalgos/complete.hh, src/tgbaalgos/compsusp.cc, src/tgbaalgos/compsusp.hh, src/tgbaalgos/cycles.hh, src/tgbaalgos/degen.cc, src/tgbaalgos/degen.hh, src/tgbaalgos/dotty.cc, src/tgbaalgos/dotty.hh, src/tgbaalgos/dottydec.cc, src/tgbaalgos/dottydec.hh, src/tgbaalgos/dtbasat.cc, src/tgbaalgos/dtbasat.hh, src/tgbaalgos/dtgbacomp.cc, src/tgbaalgos/dtgbacomp.hh, src/tgbaalgos/dtgbasat.cc, src/tgbaalgos/dtgbasat.hh, src/tgbaalgos/dupexp.cc, src/tgbaalgos/dupexp.hh, src/tgbaalgos/emptiness.cc, src/tgbaalgos/emptiness.hh, src/tgbaalgos/gtec/gtec.cc, src/tgbaalgos/gtec/gtec.hh, src/tgbaalgos/gtec/status.cc, src/tgbaalgos/gtec/status.hh, src/tgbaalgos/gv04.cc, src/tgbaalgos/gv04.hh, src/tgbaalgos/isdet.cc, src/tgbaalgos/isdet.hh, src/tgbaalgos/isweakscc.cc, src/tgbaalgos/lbtt.cc, src/tgbaalgos/lbtt.hh, src/tgbaalgos/ltl2taa.cc, src/tgbaalgos/ltl2taa.hh, src/tgbaalgos/ltl2tgba_fm.cc, src/tgbaalgos/ltl2tgba_fm.hh, src/tgbaalgos/magic.cc, src/tgbaalgos/magic.hh, src/tgbaalgos/minimize.cc, src/tgbaalgos/minimize.hh, src/tgbaalgos/ndfs_result.hxx, src/tgbaalgos/neverclaim.cc, src/tgbaalgos/neverclaim.hh, src/tgbaalgos/postproc.cc, src/tgbaalgos/postproc.hh, src/tgbaalgos/powerset.cc, src/tgbaalgos/powerset.hh, src/tgbaalgos/projrun.cc, src/tgbaalgos/projrun.hh, src/tgbaalgos/randomgraph.cc, src/tgbaalgos/randomgraph.hh, src/tgbaalgos/reachiter.cc, src/tgbaalgos/reachiter.hh, src/tgbaalgos/reducerun.cc, src/tgbaalgos/reducerun.hh, src/tgbaalgos/replayrun.cc, src/tgbaalgos/replayrun.hh, src/tgbaalgos/rundotdec.cc, src/tgbaalgos/rundotdec.hh, src/tgbaalgos/safety.cc, src/tgbaalgos/safety.hh, src/tgbaalgos/save.cc, src/tgbaalgos/save.hh, src/tgbaalgos/scc.cc, src/tgbaalgos/scc.hh, src/tgbaalgos/sccfilter.cc, src/tgbaalgos/sccfilter.hh, src/tgbaalgos/sccinfo.cc, src/tgbaalgos/sccinfo.hh, src/tgbaalgos/se05.cc, src/tgbaalgos/se05.hh, src/tgbaalgos/simulation.cc, src/tgbaalgos/simulation.hh, src/tgbaalgos/stats.cc, src/tgbaalgos/stats.hh, src/tgbaalgos/stripacc.cc, src/tgbaalgos/stripacc.hh, src/tgbaalgos/tau03.cc, src/tgbaalgos/tau03.hh, src/tgbaalgos/tau03opt.cc, src/tgbaalgos/tau03opt.hh, src/tgbaalgos/translate.cc, src/tgbaalgos/translate.hh, src/tgbaparse/public.hh, src/tgbaparse/tgbaparse.yy, src/tgbatest/complementation.cc, src/tgbatest/explprod.cc, src/tgbatest/ltl2tgba.cc, src/tgbatest/ltlprod.cc, src/tgbatest/maskacc.cc, src/tgbatest/powerset.cc, src/tgbatest/randtgba.cc, src/tgbatest/taatgba.cc, src/tgbatest/tgbaread.cc, src/tgbatest/tripprod.cc, wrap/python/ajax/spot.in, wrap/python/spot.i, wrap/python/tests/interdep.py: Use shared pointers for automata.
607 lines
17 KiB
C++
607 lines
17 KiB
C++
// -*- coding: utf-8 -*-
|
|
// Copyright (C) 2011, 2013, 2014 Laboratoire de recherche et
|
|
// développement de l'Epita (LRDE).
|
|
// Copyright (C) 2004, 2005 Laboratoire d'Informatique de Paris 6 (LIP6),
|
|
// département Systèmes Répartis Coopératifs (SRC), Université Pierre
|
|
// et Marie Curie.
|
|
//
|
|
// This file is part of Spot, a model checking library.
|
|
//
|
|
// Spot is free software; you can redistribute it and/or modify it
|
|
// under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation; either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Spot is distributed in the hope that it will be useful, but WITHOUT
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
// License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//#define TRACE
|
|
|
|
#include <iostream>
|
|
#ifdef TRACE
|
|
#define trace std::cerr
|
|
#else
|
|
#define trace while (0) std::cerr
|
|
#endif
|
|
|
|
#include <cassert>
|
|
#include <list>
|
|
#include "misc/hash.hh"
|
|
#include "tgba/tgba.hh"
|
|
#include "emptiness.hh"
|
|
#include "emptiness_stats.hh"
|
|
#include "magic.hh"
|
|
#include "ndfs_result.hxx"
|
|
|
|
namespace spot
|
|
{
|
|
namespace
|
|
{
|
|
enum color {WHITE, BLUE, RED};
|
|
|
|
/// \brief Emptiness checker on spot::tgba automata having at most one
|
|
/// acceptance condition (i.e. a TBA).
|
|
template <typename heap>
|
|
class magic_search_ : public emptiness_check, public ec_statistics
|
|
{
|
|
public:
|
|
/// \brief Initialize the Magic Search algorithm on the automaton \a a
|
|
///
|
|
/// \pre The automaton \a a must have at most one acceptance
|
|
/// condition (i.e. it is a TBA).
|
|
magic_search_(const const_tgba_ptr& a, size_t size,
|
|
option_map o = option_map())
|
|
: emptiness_check(a, o),
|
|
h(size),
|
|
all_cond(a->all_acceptance_conditions())
|
|
{
|
|
assert(a->number_of_acceptance_conditions() <= 1);
|
|
}
|
|
|
|
virtual ~magic_search_()
|
|
{
|
|
// Release all iterators on the stacks.
|
|
while (!st_blue.empty())
|
|
{
|
|
h.pop_notify(st_blue.front().s);
|
|
a_->release_iter(st_blue.front().it);
|
|
st_blue.pop_front();
|
|
}
|
|
while (!st_red.empty())
|
|
{
|
|
h.pop_notify(st_red.front().s);
|
|
a_->release_iter(st_red.front().it);
|
|
st_red.pop_front();
|
|
}
|
|
}
|
|
|
|
/// \brief Perform a Magic Search.
|
|
///
|
|
/// \return non null pointer iff the algorithm has found a
|
|
/// new accepting path.
|
|
///
|
|
/// check() can be called several times (until it returns a null
|
|
/// pointer) to enumerate all the visited accepting paths. The method
|
|
/// visits only a finite set of accepting paths.
|
|
virtual emptiness_check_result* check()
|
|
{
|
|
if (st_red.empty())
|
|
{
|
|
assert(st_blue.empty());
|
|
const state* s0 = a_->get_init_state();
|
|
inc_states();
|
|
h.add_new_state(s0, BLUE);
|
|
push(st_blue, s0, bddfalse, bddfalse);
|
|
if (dfs_blue())
|
|
return new magic_search_result(*this, options());
|
|
}
|
|
else
|
|
{
|
|
h.pop_notify(st_red.front().s);
|
|
pop(st_red);
|
|
if (!st_red.empty() && dfs_red())
|
|
return new magic_search_result(*this, options());
|
|
else
|
|
if (dfs_blue())
|
|
return new magic_search_result(*this, options());
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
virtual std::ostream& print_stats(std::ostream &os) const
|
|
{
|
|
os << states() << " distinct nodes visited" << std::endl;
|
|
os << transitions() << " transitions explored" << std::endl;
|
|
os << max_depth() << " nodes for the maximal stack depth" << std::endl;
|
|
if (!st_red.empty())
|
|
{
|
|
assert(!st_blue.empty());
|
|
os << st_blue.size() + st_red.size() - 1
|
|
<< " nodes for the counter example" << std::endl;
|
|
}
|
|
return os;
|
|
}
|
|
|
|
virtual bool safe() const
|
|
{
|
|
return heap::Safe;
|
|
}
|
|
|
|
const heap& get_heap() const
|
|
{
|
|
return h;
|
|
}
|
|
|
|
const stack_type& get_st_blue() const
|
|
{
|
|
return st_blue;
|
|
}
|
|
|
|
const stack_type& get_st_red() const
|
|
{
|
|
return st_red;
|
|
}
|
|
private:
|
|
|
|
void push(stack_type& st, const state* s,
|
|
const bdd& label, const bdd& acc)
|
|
{
|
|
inc_depth();
|
|
tgba_succ_iterator* i = a_->succ_iter(s);
|
|
i->first();
|
|
st.emplace_front(s, i, label, acc);
|
|
}
|
|
|
|
void pop(stack_type& st)
|
|
{
|
|
dec_depth();
|
|
a_->release_iter(st.front().it);
|
|
st.pop_front();
|
|
}
|
|
|
|
/// \brief Stack of the blue dfs.
|
|
stack_type st_blue;
|
|
|
|
/// \brief Stack of the red dfs.
|
|
stack_type st_red;
|
|
|
|
/// \brief Map where each visited state is colored
|
|
/// by the last dfs visiting it.
|
|
heap h;
|
|
|
|
/// State targeted by the red dfs.
|
|
const state* target;
|
|
|
|
/// The unique acceptance condition of the automaton \a a.
|
|
bdd all_cond;
|
|
|
|
bool dfs_blue()
|
|
{
|
|
while (!st_blue.empty())
|
|
{
|
|
stack_item& f = st_blue.front();
|
|
trace << "DFS_BLUE treats: " << a_->format_state(f.s) << std::endl;
|
|
if (!f.it->done())
|
|
{
|
|
const state *s_prime = f.it->current_state();
|
|
trace << " Visit the successor: "
|
|
<< a_->format_state(s_prime) << std::endl;
|
|
bdd label = f.it->current_condition();
|
|
bdd acc = f.it->current_acceptance_conditions();
|
|
// Go down the edge (f.s, <label, acc>, s_prime)
|
|
f.it->next();
|
|
inc_transitions();
|
|
typename heap::color_ref c = h.get_color_ref(s_prime);
|
|
if (c.is_white())
|
|
{
|
|
trace << " It is white, go down" << std::endl;
|
|
inc_states();
|
|
h.add_new_state(s_prime, BLUE);
|
|
push(st_blue, s_prime, label, acc);
|
|
}
|
|
else
|
|
{
|
|
if (acc == all_cond && c.get_color() != RED)
|
|
{
|
|
// the test 'c.get_color() != RED' is added to limit
|
|
// the number of runs reported by successive
|
|
// calls to the check method. Without this
|
|
// functionnality, the test can be ommited.
|
|
trace << " It is blue and the arc is "
|
|
<< "accepting, start a red dfs" << std::endl;
|
|
target = f.s;
|
|
c.set_color(RED);
|
|
push(st_red, s_prime, label, acc);
|
|
if (dfs_red())
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
trace << " It is blue or red, pop it" << std::endl;
|
|
h.pop_notify(s_prime);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
// Backtrack the edge
|
|
// (predecessor of f.s in st_blue, <f.label, f.acc>, f.s)
|
|
{
|
|
trace << " All the successors have been visited" << std::endl;
|
|
stack_item f_dest(f);
|
|
pop(st_blue);
|
|
typename heap::color_ref c = h.get_color_ref(f_dest.s);
|
|
assert(!c.is_white());
|
|
if (!st_blue.empty() &&
|
|
f_dest.acc == all_cond && c.get_color() != RED)
|
|
{
|
|
// the test 'c.get_color() != RED' is added to limit
|
|
// the number of runs reported by successive
|
|
// calls to the check method. Without this
|
|
// functionnality, the test can be ommited.
|
|
trace << " It is blue and the arc from "
|
|
<< a_->format_state(st_blue.front().s)
|
|
<< " to it is accepting, start a red dfs"
|
|
<< std::endl;
|
|
target = st_blue.front().s;
|
|
c.set_color(RED);
|
|
push(st_red, f_dest.s, f_dest.label, f_dest.acc);
|
|
if (dfs_red())
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
trace << " Pop it" << std::endl;
|
|
h.pop_notify(f_dest.s);
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool dfs_red()
|
|
{
|
|
assert(!st_red.empty());
|
|
if (target->compare(st_red.front().s) == 0)
|
|
return true;
|
|
|
|
while (!st_red.empty())
|
|
{
|
|
stack_item& f = st_red.front();
|
|
trace << "DFS_RED treats: " << a_->format_state(f.s) << std::endl;
|
|
if (!f.it->done())
|
|
{
|
|
const state *s_prime = f.it->current_state();
|
|
trace << " Visit the successor: "
|
|
<< a_->format_state(s_prime) << std::endl;
|
|
bdd label = f.it->current_condition();
|
|
bdd acc = f.it->current_acceptance_conditions();
|
|
// Go down the edge (f.s, <label, acc>, s_prime)
|
|
f.it->next();
|
|
inc_transitions();
|
|
typename heap::color_ref c = h.get_color_ref(s_prime);
|
|
if (c.is_white())
|
|
{
|
|
// If the red dfs find a white here, it must have crossed
|
|
// the blue stack and the target must be reached soon.
|
|
// Notice that this property holds only for explicit search.
|
|
// Collisions in bit-state hashing search can also lead
|
|
// to the visit of white state. Anyway, it is not necessary
|
|
// to visit white states either if a cycle can be missed
|
|
// with bit-state hashing search.
|
|
trace << " It is white, pop it" << std::endl;
|
|
s_prime->destroy();
|
|
}
|
|
else if (c.get_color() == BLUE)
|
|
{
|
|
trace << " It is blue, go down" << std::endl;
|
|
c.set_color(RED);
|
|
push(st_red, s_prime, label, acc);
|
|
if (target->compare(s_prime) == 0)
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
trace << " It is red, pop it" << std::endl;
|
|
h.pop_notify(s_prime);
|
|
}
|
|
}
|
|
else // Backtrack
|
|
{
|
|
trace << " All the successors have been visited, pop it"
|
|
<< std::endl;
|
|
h.pop_notify(f.s);
|
|
pop(st_red);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
class result_from_stack: public emptiness_check_result,
|
|
public acss_statistics
|
|
{
|
|
public:
|
|
result_from_stack(magic_search_& ms)
|
|
: emptiness_check_result(ms.automaton()), ms_(ms)
|
|
{
|
|
}
|
|
|
|
virtual tgba_run* accepting_run()
|
|
{
|
|
assert(!ms_.st_blue.empty());
|
|
assert(!ms_.st_red.empty());
|
|
|
|
tgba_run* run = new tgba_run;
|
|
|
|
typename stack_type::const_reverse_iterator i, j, end;
|
|
tgba_run::steps* l;
|
|
|
|
l = &run->prefix;
|
|
|
|
i = ms_.st_blue.rbegin();
|
|
end = ms_.st_blue.rend(); --end;
|
|
j = i; ++j;
|
|
for (; i != end; ++i, ++j)
|
|
{
|
|
tgba_run::step s = { i->s->clone(), j->label, j->acc };
|
|
l->push_back(s);
|
|
}
|
|
|
|
l = &run->cycle;
|
|
|
|
j = ms_.st_red.rbegin();
|
|
tgba_run::step s = { i->s->clone(), j->label, j->acc };
|
|
l->push_back(s);
|
|
|
|
i = j; ++j;
|
|
end = ms_.st_red.rend(); --end;
|
|
for (; i != end; ++i, ++j)
|
|
{
|
|
tgba_run::step s = { i->s->clone(), j->label, j->acc };
|
|
l->push_back(s);
|
|
}
|
|
|
|
return run;
|
|
}
|
|
|
|
unsigned acss_states() const
|
|
{
|
|
return 0;
|
|
}
|
|
private:
|
|
magic_search_& ms_;
|
|
};
|
|
|
|
# define FROM_STACK "ar:from_stack"
|
|
|
|
class magic_search_result: public emptiness_check_result
|
|
{
|
|
public:
|
|
magic_search_result(magic_search_& m, option_map o = option_map())
|
|
: emptiness_check_result(m.automaton(), o), ms(m)
|
|
{
|
|
if (options()[FROM_STACK])
|
|
computer = new result_from_stack(ms);
|
|
else
|
|
computer = new ndfs_result<magic_search_<heap>, heap>(ms);
|
|
}
|
|
|
|
virtual void options_updated(const option_map& old)
|
|
{
|
|
if (old[FROM_STACK] && !options()[FROM_STACK])
|
|
{
|
|
delete computer;
|
|
computer = new ndfs_result<magic_search_<heap>, heap>(ms);
|
|
}
|
|
else if (!old[FROM_STACK] && options()[FROM_STACK])
|
|
{
|
|
delete computer;
|
|
computer = new result_from_stack(ms);
|
|
}
|
|
}
|
|
|
|
virtual ~magic_search_result()
|
|
{
|
|
delete computer;
|
|
}
|
|
|
|
virtual tgba_run* accepting_run()
|
|
{
|
|
return computer->accepting_run();
|
|
}
|
|
|
|
virtual const unsigned_statistics* statistics() const
|
|
{
|
|
return computer->statistics();
|
|
}
|
|
|
|
private:
|
|
emptiness_check_result* computer;
|
|
magic_search_& ms;
|
|
};
|
|
};
|
|
|
|
class explicit_magic_search_heap
|
|
{
|
|
public:
|
|
enum { Safe = 1 };
|
|
|
|
class color_ref
|
|
{
|
|
public:
|
|
color_ref(color* c) :p(c)
|
|
{
|
|
}
|
|
color get_color() const
|
|
{
|
|
return *p;
|
|
}
|
|
void set_color(color c)
|
|
{
|
|
assert(!is_white());
|
|
*p=c;
|
|
}
|
|
bool is_white() const
|
|
{
|
|
return p == 0;
|
|
}
|
|
private:
|
|
color *p;
|
|
};
|
|
|
|
explicit_magic_search_heap(size_t)
|
|
{
|
|
}
|
|
|
|
~explicit_magic_search_heap()
|
|
{
|
|
hash_type::const_iterator s = h.begin();
|
|
while (s != h.end())
|
|
{
|
|
// Advance the iterator before deleting the "key" pointer.
|
|
const state* ptr = s->first;
|
|
++s;
|
|
ptr->destroy();
|
|
}
|
|
}
|
|
|
|
color_ref get_color_ref(const state*& s)
|
|
{
|
|
hash_type::iterator it = h.find(s);
|
|
if (it == h.end())
|
|
return color_ref(0);
|
|
if (s != it->first)
|
|
{
|
|
s->destroy();
|
|
s = it->first;
|
|
}
|
|
return color_ref(&it->second);
|
|
}
|
|
|
|
void add_new_state(const state* s, color c)
|
|
{
|
|
assert(h.find(s) == h.end());
|
|
h.emplace(s, c);
|
|
}
|
|
|
|
void pop_notify(const state*) const
|
|
{
|
|
}
|
|
|
|
bool has_been_visited(const state* s) const
|
|
{
|
|
hash_type::const_iterator it = h.find(s);
|
|
return (it != h.end());
|
|
}
|
|
|
|
enum { Has_Size = 1 };
|
|
int size() const
|
|
{
|
|
return h.size();
|
|
}
|
|
|
|
private:
|
|
|
|
typedef std::unordered_map<const state*, color,
|
|
state_ptr_hash, state_ptr_equal> hash_type;
|
|
hash_type h;
|
|
};
|
|
|
|
class bsh_magic_search_heap
|
|
{
|
|
public:
|
|
enum { Safe = 0 };
|
|
|
|
class color_ref
|
|
{
|
|
public:
|
|
color_ref(unsigned char *b, unsigned char o): base(b), offset(o*2)
|
|
{
|
|
}
|
|
color get_color() const
|
|
{
|
|
return color(((*base) >> offset) & 3U);
|
|
}
|
|
void set_color(color c)
|
|
{
|
|
*base = (*base & ~(3U << offset)) | (c << offset);
|
|
}
|
|
bool is_white() const
|
|
{
|
|
return get_color() == WHITE;
|
|
}
|
|
private:
|
|
unsigned char *base;
|
|
unsigned char offset;
|
|
};
|
|
|
|
bsh_magic_search_heap(size_t s)
|
|
{
|
|
size_ = s;
|
|
h = new unsigned char[size_];
|
|
memset(h, WHITE, size_);
|
|
}
|
|
|
|
~bsh_magic_search_heap()
|
|
{
|
|
delete[] h;
|
|
}
|
|
|
|
color_ref get_color_ref(const state*& s)
|
|
{
|
|
size_t ha = s->hash();
|
|
return color_ref(&(h[ha%size_]), ha%4);
|
|
}
|
|
|
|
void add_new_state(const state* s, color c)
|
|
{
|
|
color_ref cr(get_color_ref(s));
|
|
assert(cr.is_white());
|
|
cr.set_color(c);
|
|
}
|
|
|
|
void pop_notify(const state* s) const
|
|
{
|
|
s->destroy();
|
|
}
|
|
|
|
bool has_been_visited(const state* s) const
|
|
{
|
|
size_t ha = s->hash();
|
|
return color((h[ha%size_] >> ((ha%4)*2)) & 3U) != WHITE;
|
|
}
|
|
|
|
enum { Has_Size = 0 };
|
|
|
|
private:
|
|
size_t size_;
|
|
unsigned char* h;
|
|
};
|
|
|
|
} // anonymous
|
|
|
|
emptiness_check* explicit_magic_search(const const_tgba_ptr& a, option_map o)
|
|
{
|
|
return new magic_search_<explicit_magic_search_heap>(a, 0, o);
|
|
}
|
|
|
|
emptiness_check* bit_state_hashing_magic_search(const const_tgba_ptr& a,
|
|
size_t size, option_map o)
|
|
{
|
|
return new magic_search_<bsh_magic_search_heap>(a, size, o);
|
|
}
|
|
|
|
emptiness_check*
|
|
magic_search(const const_tgba_ptr& a, option_map o)
|
|
{
|
|
size_t size = o.get("bsh");
|
|
if (size)
|
|
return bit_state_hashing_magic_search(a, size, o);
|
|
return explicit_magic_search(a, o);
|
|
}
|
|
|
|
}
|