Awali
Another Weighted Automata library
ratexp_automaton.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_CORE_RATEXP_AUTOMATON_HH
18 # define AWALI_CORE_RATEXP_AUTOMATON_HH
19 
20 # include <memory>
21 # include <stack>
22 # include <string>
23 
25 #include <awali/sttc/misc/map.hh>
26 #include <awali/sttc/misc/stream.hh> // format
28 
31 
32 //# define DEBUG 1
33 
34 # if DEBUG
35 # define DEBUG_IFELSE(Then, Else) Then
36 # else
37 # define DEBUG_IFELSE(Then, Else) Else
38 # endif
39 
40 # define DEBUG_IF(Then) DEBUG_IFELSE(Then,)
41 
42 namespace awali { namespace sttc {
43 
44  /*-------------------.
45  | ratexp_automaton. |
46  `-------------------*/
47 
48  namespace internal
49  {
51  template <typename Aut>
53  {
54  public:
55  using automaton_t = Aut;
58  using ratexp_t = typename ratexpset_t::value_t;
62 
64  : super_t(ctx)
65  , rs_(ctx, rat::identities::trivial)
66  {}
67 
68  static std::string sname()
69  {
70  return "ratexp_automaton<" + super_t::sname() + ">";
71  }
72 
73  std::string vname(bool full = true) const
74  {
75  return "ratexp_automaton<" + super_t::vname(full) + ">";
76  }
77 
79  using smap = std::unordered_map<ratexp_t, state_t,
80  internal::hash<ratexpset_t>,
82 
86  {
87  // Benches show that the map_.emplace technique is slower, and
88  // then that operator[] is faster than emplace.
89  state_t res;
90  auto i = map_.find(r);
91  if (i == std::end(map_))
92  {
93  DEBUG_IF(
94  std::cerr << "New state: ";
95  rs_.print(r, std::cerr) << '\n';
96  );
97  res = super_t::add_state();
98  map_[r] = res;
99  todo_.push(r);
100  }
101  else
102  res = i->second;
103  return res;
104  }
105 
107  void
108  add_transition(const ratexp_t& src, const ratexp_t& dst,
109  const label_t& l, const weight_t& w)
110  {
111  super_t::add_transition(state(src), state(dst), l, w);
112  }
113 
114  void
115  add_transition(state_t src, const ratexp_t& dst,
116  const label_t& l, const weight_t& w)
117  {
118  super_t::add_transition(src, state(dst), l, w);
119  }
120 
121  using super_t::set_initial;
122  void
123  set_initial(const ratexp_t& s, const weight_t& w)
124  {
126  }
127 
128  std::ostream&
129  print_state_name(state_t s, std::ostream& o,
130  const std::string& fmt) const
131  {
132  auto i = origins().find(s);
133  if (i == std::end(origins()))
134  this->print_state(s, o);
135  else
136  o << str_escape(format(rs_, i->second, fmt));
137  return o;
138  }
139 
141  using origins_t = std::map<state_t, ratexp_t>;
143  const origins_t&
144  origins() const
145  {
146  if (origins_.empty())
147  for (const auto& p: map_)
148  origins_[p.second] = p.first;
149  return origins_;
150  }
151 
155  std::stack<ratexp_t> todo_;
158  };
159  }
160 
162  template <typename Aut>
164  = std::shared_ptr<internal::ratexp_automaton_impl<Aut>>;
165 
166 }}//end of ns awali::stc
167 
168 #endif // !AWALI_CORE_RATEXP_AUTOMATON_HH
Aggregate an automaton, and forward calls to it.
Definition: automaton_decorator.hh:36
Aut automaton_t
The type of automaton to wrap.
Definition: automaton_decorator.hh:39
auto set_initial(Args &&... args) -> decltype(aut_-> set_initial(std::forward< Args >(args)...))
Definition: automaton_decorator.hh:192
static constexpr auto sname(Args &&... args) -> decltype(automaton_t::element_type::sname(std::forward< Args >(args)...))
Definition: automaton_decorator.hh:112
auto print_state(Args &&... args) const -> decltype(aut_-> print_state(std::forward< Args >(args)...))
Definition: automaton_decorator.hh:154
typename labelset_t::value_t label_t
Definition: automaton_decorator.hh:50
auto add_state(Args &&... args) -> decltype(aut_-> add_state(std::forward< Args >(args)...))
Definition: automaton_decorator.hh:187
Context context_t
Definition: automaton_decorator.hh:45
typename weightset_t::value_t weight_t
Definition: automaton_decorator.hh:54
auto vname(Args &&... args) const -> decltype(aut_-> vname(std::forward< Args >(args)...))
Definition: automaton_decorator.hh:159
auto add_transition(Args &&... args) -> decltype(aut_-> add_transition(std::forward< Args >(args)...))
Definition: automaton_decorator.hh:181
An incremental automaton whose states are ratexps.
Definition: ratexp_automaton.hh:53
std::ostream & print_state_name(state_t s, std::ostream &o, const std::string &fmt) const
Definition: ratexp_automaton.hh:129
std::stack< ratexp_t > todo_
States to visit.
Definition: ratexp_automaton.hh:155
std::unordered_map< ratexp_t, state_t, internal::hash< ratexpset_t >, utils::equal_to< ratexpset_t > > smap
Symbolic states to state handlers.
Definition: ratexp_automaton.hh:81
std::map< state_t, ratexp_t > origins_t
Ordered map: state -> its derived term.
Definition: ratexp_automaton.hh:141
void add_transition(const ratexp_t &src, const ratexp_t &dst, const label_t &l, const weight_t &w)
Definition: ratexp_automaton.hh:108
void add_transition(state_t src, const ratexp_t &dst, const label_t &l, const weight_t &w)
Definition: ratexp_automaton.hh:115
void set_initial(const ratexp_t &s, const weight_t &w)
Definition: ratexp_automaton.hh:123
state_t state(const ratexp_t &r)
The state for ratexp r.
Definition: ratexp_automaton.hh:85
const origins_t & origins() const
Definition: ratexp_automaton.hh:144
std::string vname(bool full=true) const
Definition: ratexp_automaton.hh:73
ratexp_automaton_impl(const context_t &ctx)
Definition: ratexp_automaton.hh:63
typename ratexpset_t::value_t ratexp_t
Definition: ratexp_automaton.hh:58
origins_t origins_
Definition: ratexp_automaton.hh:142
smap map_
ratexp -> state.
Definition: ratexp_automaton.hh:157
ratexpset_t rs_
The ratexp's set.
Definition: ratexp_automaton.hh:153
static std::string sname()
Definition: ratexp_automaton.hh:68
The semiring of floating Numbers.
Definition: r.hh:35
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: hash.hh:42
identities
A ratexpset can implement several different sets of identities on expressions.
Definition: identities.hh:32
typename internal::label_t_of_impl< internal::base_t< ValueSet > >::type label_t_of
Helper to retrieve the type of the labels of a value set.
Definition: traits.hh:71
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
std::shared_ptr< internal::ratexp_automaton_impl< Aut > > ratexp_automaton
A ratexp automaton as a shared pointer.
Definition: ratexp_automaton.hh:164
std::ostream & str_escape(std::ostream &os, const int c)
Definition: escape.hh:30
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
static const std::string full
Completely version of Awali as a std::string.
Definition: version.hh:42
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
#define DEBUG_IF(Then)
Definition: ratexp_automaton.hh:40
Provide a variadic mul on top of a binary mul(), and one().
Definition: weightset.hh:38