Vcsn  2.3
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 <Automaton 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 auto res = symbol{"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  template <typename States>
125  auto new_state(const States& ss)
126  -> decltype(*begin(ss) == std::declval<state_t>(),
127  state_t{})
128  {
129  return new_state(state_name_t{begin(ss), end(ss)});
130  }
131 
132  private:
134  const automaton_t input_;
136  origins_t origins_;
137  }; // partition_automaton_impl
138  } // namespace detail
139 
141  template <Automaton Aut>
142  using partition_automaton
143  = std::shared_ptr<detail::partition_automaton_impl<Aut>>;
144 
145 
146 
147  namespace detail
148  {
150  template <Automaton Aut>
151  struct origins_t_of_impl;
152 
155  template <Automaton Aut>
156  using origins_t_of = typename origins_t_of_impl<Aut>::type;
157 
158  template <Automaton Aut>
159  struct origins_t_of_impl<partition_automaton<Aut>>
160  {
161  using type = typename partition_automaton<Aut>::element_type::origins_t;
162  };
163 
164  template <Automaton Aut>
165  struct origins_t_of_impl<transpose_automaton<Aut>>
166  {
167  using type = origins_t_of<Aut>;
168  };
169 
170 
171 
180  template <Automaton Aut>
181  struct partition_automaton_t_impl
182  {
183  using type = partition_automaton<Aut>;
184  };
185 
186  template <Automaton Aut>
187  struct partition_automaton_t_impl<partition_automaton<Aut>>
188  : partition_automaton_t_impl<Aut>
189  {};
190 
191  template <Automaton Aut>
192  struct partition_automaton_t_impl<transpose_automaton<Aut>>
193  {
194  using type
195  = transpose_automaton<typename partition_automaton_t_impl<Aut>::type>;
196  };
197  }
198 
200  template <Automaton Aut>
201  using partition_automaton_t
202  = typename detail::partition_automaton_t_impl<Aut>::type;
203 
204 
210  template <Automaton Aut>
211  auto
212  make_partition_automaton(const fresh_automaton_t_of<Aut>& res,
213  const Aut& input,
214  const typename detail::partition_automaton_impl<Aut>::origins_t origins)
215  -> partition_automaton_t<Aut>
216  {
217  return make_shared_ptr<partition_automaton<Aut>>(res, input, origins);
218  }
219 
226  template <Automaton Aut>
227  auto
228  make_partition_automaton(const fresh_automaton_t_of<Aut>& res,
229  const partition_automaton<Aut>& input,
230  const typename detail::partition_automaton_impl<Aut>::origins_t origins)
231  -> partition_automaton_t<Aut>
232  {
233  const auto& input_origins = input->origins();
234  using origins_t
235  = typename detail::partition_automaton_impl<Aut>::origins_t;
236  auto new_origins = origins_t{};
237 
238  for (const auto& p: origins)
239  for (auto s: p.second)
240  new_origins[p.first]
241  .insert(begin(input_origins.at(s)), end(input_origins.at(s)));
242 
243  return make_partition_automaton(res, input->input_automaton(), new_origins);
244  }
245 
252  template <Automaton Aut>
253  auto
254  make_partition_automaton(const fresh_automaton_t_of<transpose_automaton<Aut>>& res,
255  const transpose_automaton<Aut>& input,
256  const typename detail::partition_automaton_impl<Aut>::origins_t origins)
257  -> transpose_automaton<partition_automaton_t<Aut>>
258  {
259  return transpose(make_partition_automaton(res->naked_automaton(),
260  input->naked_automaton(),
261  origins));
262  }
263 
264 } // namespace vcsn
An automaton wrapper whose states form a partition of the state set of another automaton.
partition_automaton_impl(const fresh_automaton_t<> &res, const automaton_t &input, const origins_t &origins)
partition_automaton_impl(const automaton_t &input)
std::set< state_t > state_name_t
The state names: a set of the original automaton states.
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
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.
symbol sname()
Definition: name.hh:65
Aggregate an automaton, and forward calls to it.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
return res
Definition: multiply.hh:398
std::map< state_t, state_name_t > origins_t
A map from each state to the origin state set it stands for.
const origins_t & origins() const
Accessor to the states' origins.
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:64
static constexpr auto post(Args &&...args) -> decltype(element_type::post(std::forward< Args >(args)...))
Definition: a-star.hh:8
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:62
fresh_automaton_t_of< automaton_t, Ctx > fresh_automaton_t
Generated automaton type.
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:82
An input/output format for valuesets.
Definition: format.hh:13
std::ostream & print_set(std::ostream &o, format fmt={}) const
const automaton_t input_
The input automaton.
state_t_of< automaton_t > state_t
The underlying state type.