Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
u.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_U_HH
2 # define VCSN_ALGOS_U_HH
3 
4 # include <vcsn/ctx/traits.hh>
5 # include <vcsn/alphabets/char.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& gens = ctx.labelset()->genset();
24  std::vector<typename context_t::labelset_t::letter_t> letters
25  {std::begin(gens), std::end(gens)};
26  require(3 <= letters.size(), "u: the alphabet needs at least 3 letters");
27  automaton_t res = make_shared_ptr<automaton_t>(ctx);
28 
29  // The states.
30  std::vector<state_t> states;
31  for (unsigned i = 0; i < n; ++i)
32  states.push_back(res->new_state());
33  res->set_initial(states[0]);
34  res->set_final(states[n-1]);
35 
36  // The 'a' transitions.
37  auto a = letters[0];
38  for (unsigned i = 0; i < n; ++i)
39  res->new_transition(states[i], states[(i+1) % n], a);
40 
41  // The 'b' transitions.
42  auto b = letters[1];
43  res->new_transition(states[0], states[1], b);
44  res->new_transition(states[1], states[0], b);
45  for (unsigned i = 2; i < n; ++i)
46  res->new_transition(states[i], states[i], b);
47 
48  // The 'c' transitions.
49  auto c = letters[2];
50  for (unsigned i = 0; i < n - 1; ++i)
51  res->new_transition(states[i], states[i], c);
52  res->new_transition(states[n - 1], states[0], c);
53 
54  return res;
55  }
56 
57  /*---------.
58  | dyn::u. |
59  `---------*/
60 
61  namespace dyn
62  {
63  namespace detail
64  {
66  template <typename Ctx, typename Unsigned>
67  automaton
68  u(const context& ctx, unsigned n)
69  {
70  const auto& c = ctx->as<Ctx>();
71  return make_automaton(::vcsn::u(c, n));
72  }
73 
75  (const context& ctx, unsigned n) -> automaton);
76  }
77  }
78 
79 }
80 
81 #endif // !VCSN_ALGOS_U_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
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
std::shared_ptr< const detail::context_base > context
Definition: context.hh:71
mutable_automaton< Context > u(const Context &ctx, unsigned n)
The Brzozowski universal witness.
Definition: u.hh:17
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
automaton u(const context &ctx, unsigned n)
Bridge.
Definition: u.hh:68
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39