Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
permutation-automaton.hh
Go to the documentation of this file.
1 #ifndef VCSN_CORE_PERMUTATION_DECORATOR_HH
2 # define VCSN_CORE_PERMUTATION_DECORATOR_HH
3 
4 # include <map>
5 # include <queue>
6 # include <unordered_map>
7 
9 # include <vcsn/core/fwd.hh> // permutation_automaton
10 
11 namespace vcsn
12 {
13  namespace detail
14  {
18  template <typename Aut>
19  class permutation_automaton_impl
20  : public automaton_decorator<typename Aut::element_type::automaton_nocv_t>
21  {
22  public:
24  using automaton_t = Aut;
26  using automaton_nocv_t = typename automaton_t::element_type::automaton_nocv_t;
28 
33 
34  public:
37  , input_(input)
38  {
39  map_[input_->pre()] = super_t::pre();
40  map_[input_->post()] = super_t::post();
41  todo_.push({input_->pre(), super_t::pre()});
42  }
43 
45  static std::string sname()
46  {
47  return "permutation_automaton<" + automaton_t::element_type::sname() + ">";
48  }
49 
51  std::string vname(bool full = true) const
52  {
53  return "permutation_automaton<" + input_->vname(full) + ">";
54  }
55 
56  bool state_has_name(state_t s) const
57  {
58  return s != super_t::pre() && s != super_t::post();
59  }
60 
61  std::ostream&
62  print_state_name(state_t s, std::ostream& o,
63  const std::string& fmt = "text",
64  bool delimit = false) const
65  {
66  return input_->print_state_name(origins().at(s), o, fmt, delimit);
67  }
68 
69  state_t
71  {
72  // Benches show that the map_.emplace technique is slower, and
73  // then that operator[] is faster than emplace.
74  state_t res;
75  auto i = map_.find(s);
76  if (i == std::end(map_))
77  {
78  res = super_t::new_state();
79  map_[s] = res;
80  todo_.push({s, res});
81  }
82  else
83  res = i->second;
84  return res;
85  }
86 
88  using origins_t = std::map<state_t, state_name_t>;
89 
91  const origins_t&
92  origins() const
93  {
94  if (origins_.empty())
95  for (const auto& p: map_)
96  origins_[p.second] = p.first;
97  return origins_;
98  }
99 
100  using pair_t = std::pair<state_name_t, state_t>;
101  std::queue<pair_t> todo_;
102 
104  std::unordered_map<state_name_t, state_t> map_;
105 
107 
110  }; // class
111  } // namespace detail
112 } // namespace vcsn
113 
114 #endif // ! VCSN_CORE_PERMUTATION_DECORATOR_HH
permutation_automaton_impl(const automaton_t &input)
std::map< state_t, state_name_t > origins_t
A map from each state to the origin state set it stands for.
std::string vname(bool full=true) const
Dynamic name.
state_t_of< automaton_t > state_name_t
Symbolic state name: input automaton state type.
std::string sname()
Definition: name.hh:31
static std::string sname()
Static name.
auto new_state(Args &&...args) -> decltype(aut_-> new_state(std::forward< Args >(args)...))
const origins_t & origins() const
Ordered map: state -> its derived term.
state_t_of< automaton_nocv_t > state_t
Sorted automaton state type.
Aggregate an automaton, and forward calls to it.
typename automaton_t::element_type::automaton_nocv_t automaton_nocv_t
Sorted automaton type.
std::ostream & print_state_name(state_t s, std::ostream &o, const std::string &fmt="text", bool delimit=false) const
const automaton_t input_
Input automaton.
auto real_context(const Aut &aut) -> decltype(real_context_impl< Aut >::context(aut))
Definition: copy.hh:157
std::pair< state_name_t, state_t > pair_t
static constexpr auto pre(Args &&...args) -> decltype(automaton_t::element_type::pre(std::forward< Args >(args)...))
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
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:15
std::unordered_map< state_name_t, state_t > map_
Input-state -> sorted-state.
static constexpr auto post(Args &&...args) -> decltype(automaton_t::element_type::post(std::forward< Args >(args)...))