Vcsn  2.2
Be Rational
u.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/ctx/traits.hh>
4 #include <vcsn/alphabets/char.hh>
7 #include <vcsn/dyn/automaton.hh>
8 #include <vcsn/dyn/context.hh>
9 #include <vcsn/misc/raise.hh>
10 
11 namespace vcsn
12 {
13 
15  template <typename Context>
16  mutable_automaton<Context>
17  u(const Context& ctx, unsigned n)
18  {
19  using context_t = Context;
20  using automaton_t = mutable_automaton<context_t>;
21  using state_t = state_t_of<automaton_t>;
22  require(2 <= n, "u: n must be at least 3");
23  const auto& ls = *ctx.labelset();
24  const auto& gens = ls.generators();
25  std::vector<label_t_of<context_t>> letters;
26  for (auto l: gens)
27  letters.emplace_back(ls.value(l));
28  require(3 <= letters.size(), "u: the alphabet needs at least 3 letters");
29  automaton_t res = make_shared_ptr<automaton_t>(ctx);
30 
31  // The states.
32  std::vector<state_t> states;
33  for (unsigned i = 0; i < n; ++i)
34  states.push_back(res->new_state());
35  res->set_initial(states[0]);
36  res->set_final(states[n-1]);
37 
38  // The 'a' transitions.
39  auto a = letters[0];
40  for (unsigned i = 0; i < n; ++i)
41  res->new_transition(states[i], states[(i+1) % n], a);
42 
43  // The 'b' transitions.
44  auto b = letters[1];
45  res->new_transition(states[0], states[1], b);
46  res->new_transition(states[1], states[0], b);
47  for (unsigned i = 2; i < n; ++i)
48  res->new_transition(states[i], states[i], b);
49 
50  // The 'c' transitions.
51  auto c = letters[2];
52  for (unsigned i = 0; i < n - 1; ++i)
53  res->new_transition(states[i], states[i], c);
54  res->new_transition(states[n - 1], states[0], c);
55 
56  return res;
57  }
58 
59  /*---------.
60  | dyn::u. |
61  `---------*/
62 
63  namespace dyn
64  {
65  namespace detail
66  {
68  template <typename Ctx, typename Unsigned>
69  automaton
70  u(const context& ctx, unsigned n)
71  {
72  const auto& c = ctx->as<Ctx>();
73  return make_automaton(::vcsn::u(c, n));
74  }
75  }
76  }
77 
78 }
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:75
std::shared_ptr< detail::mutable_automaton_impl< Context >> mutable_automaton
Definition: fwd.hh:25
mutable_automaton< Context > u(const Context &ctx, unsigned n)
The Brzozowski universal witness.
Definition: u.hh:17
Definition: a-star.hh:8
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:78
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
std::shared_ptr< const detail::context_base > context
A dyn::context.
Definition: fwd.hh:43
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
static dyn::context ctx(const driver &d)
Get the context of the driver.
Definition: parse.cc:82
automaton u(const context &ctx, unsigned n)
Bridge.
Definition: u.hh:70