Awali
Another Weighted Automata library
ladybird.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_LADYBIRD_HH
18 # define AWALI_ALGOS_LADYBIRD_HH
19 
20 # include <vector>
21 
25 #include <awali/sttc/misc/raise.hh>
26 
27 namespace awali { namespace sttc {
28 
52  template <class Context>
53  mutable_automaton<Context>
54  ladybird(const Context& ctx, unsigned n)
55  {
56  using context_t = Context;
57  const auto& gens = ctx.labelset()->genset();
58  std::vector<typename context_t::labelset_t::letter_t> letters
59  {std::begin(gens), std::end(gens)};
60  require(3 <= letters.size(),
61  "ladybird: the alphabet needs at least 3 letters");
62  require(n>0,
63  "ladybird: the automaton should have at least one state");
64  auto a = letters[0];
65  auto b = letters[1];
66  auto c = letters[2];
67 
68  using automaton_t = mutable_automaton<Context>;
69  automaton_t res = make_shared_ptr<automaton_t>(ctx);
70 
71  auto p = res->add_state();
72  res->set_initial(p);
73  res->set_final(p);
74  auto x = p;
75  for (unsigned i = 1; i < n; ++i)
76  {
77  auto y = res->add_state();
78  res->new_transition(x, y, a);
79  res->new_transition(y, y, b);
80  res->new_transition(y, y, c);
81  res->new_transition(y, p, c);
82  x = y;
83  }
84  res->new_transition(x, p, a);
85  res->set_name("ladybird-"+std::to_string(n));
86  return res;
87  }
88 
89 }}//end of ns awali::stc
90 
91 #endif // !AWALI_ALGOS_LADYBIRD_HH
The Boolean semring.
Definition: b.hh:38
The semiring of complex numbers.
Definition: c.hh:44
The semiring of Natural numbers.
Definition: n.hh:34
std::string to_string(identities i)
mutable_automaton< Context > ladybird(const Context &ctx, unsigned n)
Returns a "ladybird" automaton with n states.
Definition: ladybird.hh:54
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
Main namespace of Awali.
Definition: ato.hh:22