Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
de-bruijn.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_DE_BRUIJN_HH
2 # define VCSN_ALGOS_DE_BRUIJN_HH
3 
4 # include <iterator> // std::distance
5 # include <stdexcept>
6 
7 # include <vcsn/alphabets/char.hh>
10 # include <vcsn/dyn/context.hh>
11 # include <vcsn/misc/raise.hh>
12 
13 namespace vcsn
14 {
15  // (a+b)*a(a+b)^n.
16  template <typename Context>
17  mutable_automaton<Context>
18  de_bruijn(const Context& ctx, unsigned n)
19  {
20  const auto& gens = ctx.labelset()->genset();
21  size_t sz = std::distance(std::begin(gens), std::end(gens));
22  require(2 <= sz, "de_bruijn: the alphabet needs at least 2 letters");
23  using context_t = Context;
24  using automaton_t = mutable_automaton<context_t>;
25  automaton_t res = make_shared_ptr<automaton_t>(ctx);
26 
27  auto init = res->new_state();
28  res->set_initial(init);
29  for (auto l: gens)
30  res->new_transition(init, init, l);
31 
32  auto prev = res->new_state();
33  res->new_transition(init, prev, *std::begin(gens));
34 
35  while (n--)
36  {
37  auto next = res->new_state();
38  for (auto l: gens)
39  res->new_transition(prev, next, l);
40  prev = next;
41  }
42  res->set_final(prev);
43  return res;
44  }
45 
46  /*-----------------.
47  | dyn::de_bruijn. |
48  `-----------------*/
49 
50  namespace dyn
51  {
52  namespace detail
53  {
55  template <typename Ctx, typename Unsigned>
56  automaton
57  de_bruijn(const dyn::context& ctx, unsigned n)
58  {
59  const auto& c = ctx->as<Ctx>();
60  return make_automaton(::vcsn::de_bruijn<Ctx>(c, n));
61  }
62 
64  (const context& ctx, unsigned n) -> automaton);
65  }
66  }
67 
68 }
69 
70 #endif // !VCSN_ALGOS_DE_BRUIJN_HH
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:77
std::shared_ptr< detail::mutable_automaton_impl< Context >> mutable_automaton
Definition: fwd.hh:25
std::shared_ptr< const detail::context_base > context
Definition: context.hh:71
mutable_automaton< Context > de_bruijn(const Context &ctx, unsigned n)
Definition: de-bruijn.hh:18
automaton de_bruijn(const dyn::context &ctx, unsigned n)
Bridge.
Definition: de-bruijn.hh:57
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39