Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ladybird.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_LADYBIRD_HH
2 # define VCSN_ALGOS_LADYBIRD_HH
3 
4 # include <vector>
5 
6 # include <vcsn/alphabets/char.hh>
9 # include <vcsn/dyn/context.hh>
10 # include <vcsn/misc/raise.hh>
11 
12 namespace vcsn
13 {
14  template <class Context>
15  mutable_automaton<Context>
16  ladybird(const Context& ctx, unsigned n)
17  {
18  using context_t = Context;
19  const auto& gens = ctx.labelset()->genset();
20  std::vector<typename context_t::labelset_t::letter_t> letters
21  {std::begin(gens), std::end(gens)};
22  require(3 <= letters.size(),
23  "ladybird: the alphabet needs at least 3 letters");
24  auto a = letters[0];
25  auto b = letters[1];
26  auto c = letters[2];
27 
28  using automaton_t = mutable_automaton<Context>;
29  automaton_t res = make_shared_ptr<automaton_t>(ctx);
30 
31  auto p = res->new_state();
32  res->set_initial(p);
33  res->set_final(p);
34  auto x = p;
35  for (unsigned i = 1; i < n; ++i)
36  {
37  auto y = res->new_state();
38  res->new_transition(x, y, a);
39  res->new_transition(y, y, b);
40  res->new_transition(y, y, c);
41  res->new_transition(y, p, c);
42  x = y;
43  }
44  res->new_transition(x, p, a);
45  return res;
46  }
47 
48  /*----------------.
49  | dyn::ladybird. |
50  `----------------*/
51 
52  namespace dyn
53  {
54  namespace detail
55  {
57  template <typename Ctx, typename Unsigned>
58  automaton
59  ladybird(const dyn::context& ctx, unsigned n)
60  {
61  const auto& c = ctx->as<Ctx>();
62  return make_automaton(::vcsn::ladybird(c, n));
63  }
64 
66  (const dyn::context& ctx, unsigned n) -> automaton);
67  }
68  }
69 }
70 
71 #endif // !VCSN_ALGOS_LADYBIRD_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
automaton ladybird(const dyn::context &ctx, unsigned n)
Bridge.
Definition: ladybird.hh:59
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 > ladybird(const Context &ctx, unsigned n)
Definition: ladybird.hh:16
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39