Awali
Another Weighted Automata library
permutation_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_PERMUTATION_DECORATOR_HH
18 # define AWALI_CORE_PERMUTATION_DECORATOR_HH
19 
20 # include <map>
21 # include <queue>
22 # include <unordered_map>
23 
25 
26 namespace awali { namespace sttc {
27 
28 
29  namespace internal
30  {
31  template <typename Aut>
33  : public automaton_decorator<typename Aut::element_type::automaton_nocv_t>
34  {
35  public:
37  using automaton_t = Aut;
39  using automaton_nocv_t = typename automaton_t::element_type::automaton_nocv_t;
42 
43  public:
45  : super_t(make_shared_ptr<automaton_nocv_t>(real_context(input)))
46  , input_(input)
47  {
48  map_[input_->pre()] = super_t::pre();
49  map_[input_->post()] = super_t::post();
50  todo_.push({input_->pre(), super_t::pre()});
51  }
52 
53  static std::string sname()
54  {
55  return "permutation_automaton<" + automaton_t::element_type::sname() + ">";
56  }
57 
58  std::string vname(bool full = true) const
59  {
60  return "permutation_automaton<" + input_->vname(full) + ">";
61  }
62 
63  std::ostream&
64  print_state_name(state_t s, std::ostream& o,
65  const std::string& fmt = "text") const
66  {
67  return input_->print_state_name(origins().at(s), o, fmt);
68  }
69 
70  state_t
72  {
73  // Benches show that the map_.emplace technique is slower, and
74  // then that operator[] is faster than emplace.
75  state_t res;
76  auto i = map_.find(s);
77  if (i == std::end(map_))
78  {
79  res = super_t::add_state();
80  map_[s] = res;
81  todo_.push({s, res});
82  }
83  else
84  res = i->second;
85  return res;
86  }
87 
89  using origins_t = std::map<state_t, state_t>;
90 
92  const origins_t&
93  origins() const
94  {
95  if (origins_.empty())
96  for (const auto& p: map_)
97  origins_[p.second] = p.first;
98  return origins_;
99  }
100 
101  using pair_t = std::pair<state_t, state_t>;
102  std::queue<pair_t> todo_;
103 
105  std::unordered_map<state_t, state_t> map_;
106 
108 
111  }; // class
112  } // namespace internal
113 
115  template <typename Aut>
117  = std::shared_ptr<internal::permutation_automaton_impl<Aut>>;
118 
119 }}//end of ns awali::stc
120 
121 #endif // ! AWALI_CORE_PERMUTATION_DECORATOR_HH
Aggregate an automaton, and forward calls to it.
Definition: automaton_decorator.hh:36
static constexpr auto post(Args &&... args) -> decltype(automaton_t::element_type::post(std::forward< Args >(args)...))
Definition: automaton_decorator.hh:110
static constexpr auto pre(Args &&... args) -> decltype(automaton_t::element_type::pre(std::forward< Args >(args)...))
Definition: automaton_decorator.hh:111
auto add_state(Args &&... args) -> decltype(aut_-> add_state(std::forward< Args >(args)...))
Definition: automaton_decorator.hh:187
Definition: permutation_automaton.hh:34
std::queue< pair_t > todo_
Definition: permutation_automaton.hh:102
Aut automaton_t
Input automaton type.
Definition: permutation_automaton.hh:37
std::map< state_t, state_t > origins_t
A map from each state to the origin state set it stands for.
Definition: permutation_automaton.hh:89
context_t_of< Aut > full_context_t
Definition: permutation_automaton.hh:41
std::unordered_map< state_t, state_t > map_
Input-state -> sorted-state.
Definition: permutation_automaton.hh:105
std::string vname(bool full=true) const
Definition: permutation_automaton.hh:58
typename automaton_t::element_type::automaton_nocv_t automaton_nocv_t
Sorted automaton type.
Definition: permutation_automaton.hh:39
const origins_t & origins() const
Ordered map: state -> its derived term.
Definition: permutation_automaton.hh:93
std::ostream & print_state_name(state_t s, std::ostream &o, const std::string &fmt="text") const
Definition: permutation_automaton.hh:64
const automaton_t input_
Input automaton.
Definition: permutation_automaton.hh:110
std::pair< state_t, state_t > pair_t
Definition: permutation_automaton.hh:101
state_t state(state_t s)
Definition: permutation_automaton.hh:71
static std::string sname()
Definition: permutation_automaton.hh:53
permutation_automaton_impl(const automaton_t &input)
Definition: permutation_automaton.hh:44
origins_t origins_
Definition: permutation_automaton.hh:107
std::shared_ptr< internal::permutation_automaton_impl< Aut > > permutation_automaton
A permutation automaton as a shared pointer.
Definition: permutation_automaton.hh:117
SharedPtr make_shared_ptr(Args &&... args)
Same as std::make_shared, but parameterized by the shared_ptr type, not the (pointed to) element_type...
Definition: memory.hh:29
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
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