Vcsn  2.2a
Be Rational
permutation-automaton.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <queue>
5 #include <unordered_map>
6 
7 #include <vcsn/algos/copy.hh> // make_fresh_automaton
9 #include <vcsn/core/fwd.hh> // permutation_automaton
10 
11 namespace vcsn
12 {
13  namespace detail
14  {
18  template <Automaton Aut>
19  class permutation_automaton_impl
20  : public automaton_decorator<fresh_automaton_t_of<Aut>>
21  {
22  public:
24  using automaton_t = Aut;
27  template <typename Ctx = context_t>
30 
35 
36  public:
39  , input_(input)
40  {
41  map_[input_->pre()] = super_t::pre();
42  map_[input_->post()] = super_t::post();
43  todo_.push({input_->pre(), super_t::pre()});
44  }
45 
47  static symbol sname()
48  {
49  static auto res = symbol{"permutation_automaton<"
51  return res;
52  }
53 
54  std::ostream& print_set(std::ostream& o, format fmt = {}) const
55  {
56  o << "permutation_automaton<";
57  input_->print_set(o, fmt);
58  return o << '>';
59  }
60 
61  bool state_has_name(state_t) const
62  {
63  return true;
64  }
65 
66  std::ostream&
67  print_state_name(state_t s, std::ostream& o,
68  format fmt = {},
69  bool delimit = false) const
70  {
71  return input_->print_state_name(origins().at(s), o, fmt, delimit);
72  }
73 
74  state_t
75  state(state_name_t s)
76  {
77  // Benches show that the map_.emplace technique is slower, and
78  // then that operator[] is faster than emplace.
79  state_t res;
80  auto i = map_.find(s);
81  if (i == std::end(map_))
82  {
83  res = super_t::new_state();
84  map_[s] = res;
85  todo_.push({s, res});
86  }
87  else
88  res = i->second;
89  return res;
90  }
91 
93  using origins_t = std::map<state_t, state_name_t>;
94 
96  const origins_t&
97  origins() const
98  {
99  if (origins_.empty())
100  for (const auto& p: map_)
101  origins_[p.second] = p.first;
102  return origins_;
103  }
104 
105  using pair_t = std::pair<state_name_t, state_t>;
106  std::queue<pair_t> todo_;
107 
109  std::unordered_map<state_name_t, state_t> map_;
110 
111  mutable origins_t origins_;
112 
114  const automaton_t input_;
115  }; // class
116  } // namespace detail
117 } // namespace vcsn
fresh_automaton_t_of< automaton_t, Ctx > fresh_automaton_t
Generated automaton type.
symbol sname()
Definition: name.hh:67
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:53
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
typename Aut::element_type::template fresh_automaton_t< Context > fresh_automaton_t_of
Given an automaton type, the type of its copies.
Definition: traits.hh:74
AutOut make_fresh_automaton(const AutIn &model)
Create an empty, mutable, automaton, based on another one.
Definition: copy.hh:90
state_t_of< fresh_automaton_t<>> state_t
Sorted automaton state type.
static constexpr auto pre(Args &&...args) -> decltype(element_type::pre(std::forward< Args >(args)...))
static constexpr auto post(Args &&...args) -> decltype(element_type::post(std::forward< Args >(args)...))
permutation_automaton_impl(const automaton_t &input)
state_t_of< automaton_t > state_name_t
Symbolic state name: input automaton state type.
Aggregate an automaton, and forward calls to it.
std::ostream & print_set(std::ostream &o, format fmt={}) const
std::unordered_map< state_name_t, state_t > map_
Input-state -> sorted-state.
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
const automaton_t input_
Input automaton.
An input/output format for valuesets.
Definition: format.hh:11
Definition: a-star.hh:8