Awali
Another Weighted Automata library
double_ring.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_DOUBLE_RING_HH
18 # define AWALI_ALGOS_DOUBLE_RING_HH
19 
20 # include <map>
21 
22 #include <awali/sttc/ctx/traits.hh>
26 #include <awali/sttc/misc/raise.hh>
27 
28 namespace awali { namespace sttc {
29 
54  template <class Context>
55  mutable_automaton<Context>
56  double_ring(const Context& ctx, unsigned n,
57  const std::vector<unsigned>& finals)
58  {
59  using context_t = Context;
60  const auto& gens = ctx.labelset()->genset();
61  std::vector<typename context_t::labelset_t::letter_t> letters
62  {std::begin(gens), std::end(gens)};
63  require(2 <= letters.size(),
64  "double_ring: the alphabet needs at least 2 letters");
65  require(n>0,
66  "double_ring: the automaton should have at least one state");
67  auto a = letters[0];
68  auto b = letters[1];
69 
70  using automaton_t = mutable_automaton<context_t>;
71  automaton_t res = make_shared_ptr<automaton_t>(ctx);
72 
73  // Set initial.
74  auto p = res->add_state();
75  res->set_initial(p);
76  // Have states start on base 0. No need for pre and post states here.
77  std::map<unsigned, state_t> states;
78  // We want first state to be 0 and not 2.
79  states.emplace(0, p);
80  // Set transitions.
81  state_t x = p;
82  for (unsigned i = 1; i < n; ++i)
83  {
84  state_t y = res->add_state();
85  res->new_transition(x, y, a);
86  res->new_transition(y, x, b);
87  x = y;
88  states.emplace(i, y);
89  }
90  res->new_transition(x, p, a);
91  res->new_transition(p, x, b);
92 
93  // Add finals.
94  for (auto f: finals)
95  {
96  require(f < n, "double_ring: invalid list of finals");
97  res->set_final(states[f]);
98  }
99  res->set_name("double-ring-"+std::to_string(n));
100  return res;
101  }
102 
103 }}//end of ns awali::stc
104 
105 #endif // !AWALI_ALGOS_DOUBLE_RING_HH
The Boolean semring.
Definition: b.hh:38
The semiring of Natural numbers.
Definition: n.hh:34
std::vector< state_t > states(abstract_automaton_t const *aut, bool all)
std::string to_string(identities i)
mutable_automaton< Context > double_ring(const Context &ctx, unsigned n, const std::vector< unsigned > &finals)
Returns a double ring automaton with n states.
Definition: double_ring.hh:56
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
unsigned state_t
Definition: types.hh:21