Vcsn  2.2
Be Rational
ladybird.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include <vcsn/alphabets/char.hh>
8 #include <vcsn/dyn/automaton.hh>
9 #include <vcsn/dyn/context.hh>
10 #include <vcsn/misc/raise.hh>
11 
12 namespace vcsn
13 {
14  template <typename Context>
15  mutable_automaton<Context>
16  ladybird(const Context& ctx, unsigned n)
17  {
18  const auto& ls = *ctx.labelset();
19  auto letters = detail::make_vector(ls.generators());
20  require(3 <= letters.size(),
21  "ladybird: the alphabet needs at least 3 letters");
22  auto a = ls.value(letters[0]);
23  auto b = ls.value(letters[1]);
24  auto c = ls.value(letters[2]);
25 
26  auto res = make_mutable_automaton(ctx);
27 
28  auto s = res->new_state();
29  res->set_initial(s);
30  res->set_final(s);
31  auto x = s;
32  for (unsigned i = 1; i < n; ++i)
33  {
34  auto y = res->new_state();
35  res->new_transition(x, y, a);
36  res->new_transition(y, y, b);
37  res->new_transition(y, y, c);
38  res->new_transition(y, s, c);
39  x = y;
40  }
41  res->new_transition(x, s, a);
42  return res;
43  }
44 
45  /*----------------.
46  | dyn::ladybird. |
47  `----------------*/
48 
49  namespace dyn
50  {
51  namespace detail
52  {
54  template <typename Ctx, typename Unsigned>
55  automaton
56  ladybird(const dyn::context& ctx, unsigned n)
57  {
58  const auto& c = ctx->as<Ctx>();
59  return make_automaton(::vcsn::ladybird(c, n));
60  }
61  }
62  }
63 }
automaton ladybird(const dyn::context &ctx, unsigned n)
Bridge.
Definition: ladybird.hh:56
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:75
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
mutable_automaton< Context > ladybird(const Context &ctx, unsigned n)
Definition: ladybird.hh:16
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
std::shared_ptr< const detail::context_base > context
A dyn::context.
Definition: fwd.hh:43
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
Definition: vector.hh:20
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
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)