Vcsn  2.1
Be Rational
partition-automaton.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <set>
5 #include <vector>
6 
9 
10 namespace vcsn
11 {
12  namespace detail
13  {
14 
19  template <typename Aut>
21  : public automaton_decorator<fresh_automaton_t_of<Aut>>
22  {
23  public:
25  using automaton_t = Aut;
28  template <typename Ctx = context_t>
32 
35 
37  using state_name_t = std::set<state_t>;
38 
40  using origins_t = std::map<state_t, state_name_t>;
41 
43  : super_t(input->context())
44  , input_(input)
45  {
46  origins_[super_t::pre()] = {input_->pre()};
47  origins_[super_t::post()] = {input_->post()};
48  }
49 
51  const automaton_t& input,
52  const origins_t& origins)
53  : super_t(res)
54  , input_(input)
55  , origins_(origins)
56  {
57  origins_[super_t::pre()] = {input_->pre()};
58  origins_[super_t::post()] = {input_->post()};
59  }
60 
62  static symbol sname()
63  {
64  static symbol res("partition_automaton<"
66  return res;
67  }
68 
69  std::ostream& print_set(std::ostream& o, format fmt = {}) const
70  {
71  o << "partition_automaton<";
72  input_->print_set(o, fmt);
73  return o << '>';
74  }
75 
76  bool state_has_name(state_t) const
77  {
78  return true;
79  }
80 
81  std::ostream&
82  print_state_name(state_t s, std::ostream& o,
83  format fmt = {},
84  bool delimit = false) const
85  {
86  const auto& set = origins_.at(s);
87  const char* separator = "";
88  if (delimit)
89  o << '{';
90  for (auto s : set)
91  {
92  o << separator;
93  input_->print_state_name(s, o, fmt, true);
94  separator = ", ";
95  }
96  if (delimit)
97  o << '}';
98  return o;
99  }
100 
102  const origins_t& origins() const
103  {
104  return origins_;
105  }
106 
108  const automaton_t& input_automaton() const
109  {
110  return input_;
111  }
112 
113  using super_t::new_state;
114 
117  state_t new_state(const state_name_t& set)
118  {
119  state_t res = new_state();
120  origins_[res] = set;
121  return res;
122  }
123 
124  state_t new_state(const std::vector<state_t>& v)
125  {
126  return new_state(state_name_t{begin(v), end(v)});
127  }
128 
129  private:
131  const automaton_t input_;
133  origins_t origins_;
134  }; // partition_automaton_impl
135  } // namespace detail
136 
138  template <typename Aut>
139  using partition_automaton
140  = std::shared_ptr<detail::partition_automaton_impl<Aut>>;
141 
142 
143 
144  namespace detail
145  {
147  template <typename Aut>
148  struct origins_t_of_impl;
149 
152  template <typename Aut>
153  using origins_t_of = typename origins_t_of_impl<Aut>::type;
154 
155  template <typename Aut>
156  struct origins_t_of_impl<partition_automaton<Aut>>
157  {
158  using type = typename partition_automaton<Aut>::element_type::origins_t;
159  };
160 
161  template <typename Aut>
162  struct origins_t_of_impl<transpose_automaton<Aut>>
163  {
164  using type = origins_t_of<Aut>;
165  };
166 
167 
168 
177  template <typename Aut>
178  struct partition_automaton_t_impl
179  {
180  using type = partition_automaton<Aut>;
181  };
182 
183  template <typename Aut>
184  struct partition_automaton_t_impl<partition_automaton<Aut>>
185  : partition_automaton_t_impl<Aut>
186  {};
187 
188  template <typename Aut>
189  struct partition_automaton_t_impl<transpose_automaton<Aut>>
190  {
191  using type
192  = transpose_automaton<typename partition_automaton_t_impl<Aut>::type>;
193  };
194  }
195 
197  template <typename Aut>
198  using partition_automaton_t
199  = typename detail::partition_automaton_t_impl<Aut>::type;
200 
201 
207  template <typename Aut>
208  auto
209  make_partition_automaton(const fresh_automaton_t_of<Aut>& res,
210  const Aut& input,
211  const typename detail::partition_automaton_impl<Aut>::origins_t origins)
212  -> partition_automaton_t<Aut>
213  {
214  return make_shared_ptr<partition_automaton<Aut>>(res, input, origins);
215  }
216 
223  template <typename Aut>
224  auto
225  make_partition_automaton(const fresh_automaton_t_of<Aut>& res,
226  const partition_automaton<Aut>& input,
227  const typename detail::partition_automaton_impl<Aut>::origins_t origins)
228  -> partition_automaton_t<Aut>
229  {
230  const auto& input_origins = input->origins();
231  using origins_t
232  = typename detail::partition_automaton_impl<Aut>::origins_t;
233  auto new_origins = origins_t{};
234 
235  for (const auto& p: origins)
236  for (auto s: p.second)
237  new_origins[p.first]
238  .insert(begin(input_origins.at(s)), end(input_origins.at(s)));
239 
240  return make_partition_automaton(res, input->input_automaton(), new_origins);
241  }
242 
249  template <typename Aut>
250  auto
251  make_partition_automaton(const fresh_automaton_t_of<transpose_automaton<Aut>>& res,
252  const transpose_automaton<Aut>& input,
253  const typename detail::partition_automaton_impl<Aut>::origins_t origins)
254  -> transpose_automaton<partition_automaton_t<Aut>>
255  {
256  return transpose(make_partition_automaton(res->naked_automaton(),
257  input->naked_automaton(),
258  origins));
259  }
260 
261 } // namespace vcsn
const origins_t & origins() const
Accessor to the states' origins.
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:46
static constexpr auto pre(Args &&...args) -> decltype(element_type::pre(std::forward< Args >(args)...))
origins_t origins_
A map from each state to the origin state set it stands for.
static constexpr auto post(Args &&...args) -> decltype(element_type::post(std::forward< Args >(args)...))
partition_automaton_impl(const automaton_t &input)
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:48
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:45
Aggregate an automaton, and forward calls to it.
const automaton_t input_
The input automaton.
typename Aut::element_type::template fresh_automaton_t< Context > fresh_automaton_t_of
Definition: traits.hh:57
An input/output format.
Definition: format.hh:11
partition_automaton_impl(const fresh_automaton_t<> &res, const automaton_t &input, const origins_t &origins)
fresh_automaton_t_of< automaton_t, Ctx > fresh_automaton_t
Generated automaton type.
state_t_of< automaton_t > state_t
The underlying state type.
std::set< state_t > state_name_t
The state names: a set of the original automaton states.
symbol sname()
Definition: name.hh:67
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
std::ostream & print_set(std::ostream &o, format fmt={}) const
std::map< state_t, state_name_t > origins_t
A map from each state to the origin state set it stands for.
An automaton wrapper whose states form a partition of the state set of another automaton.