Vcsn  2.4
Be Rational
transpose.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/algos/copy.hh>
4 #include <vcsn/algos/fwd.hh>
5 #include <vcsn/algos/strip.hh>
9 #include <vcsn/ctx/context.hh>
10 #include <vcsn/dyn/automaton.hh>
11 #include <vcsn/misc/attributes.hh>
12 
13 namespace vcsn
14 {
15 
16  /*-----------------------.
17  | transpose(automaton). |
18  `-----------------------*/
19  namespace detail
20  {
22  template <Automaton Aut>
23  class transpose_automaton_impl
24  : public automaton_decorator<Aut>
25  {
26  public:
28  using automaton_t = Aut;
30 
33 
35  template <Automaton Fresh>
36  struct fresh_impl_
37  {
39  };
40 
42  template <Automaton Fresh>
44  {
45  using type = Fresh;
46  };
47 
51  template <typename Ctx = context_t>
52  using fresh_automaton_t =
54 
59 
62  using kind_t = typename automaton_t::element_type::kind_t;
63 
64  using super_t::super_t;
65  using super_t::aut_;
66 
67  static symbol sname()
68  {
69  static auto res = symbol{"transpose_automaton<"
71  return res;
72  }
73 
74  std::ostream& print_set(std::ostream& o, format fmt = {}) const
75  {
76  o << "transpose_automaton<";
77  aut_->print_set(o, fmt);
78  return o << '>';
79  }
80 
82  automaton_t
83  naked_automaton()
84  {
85  return aut_;
86  }
87 
90  auto
91  strip()
92  -> decltype(transpose(vcsn::strip(this->naked_automaton())))
93  {
94  return transpose(vcsn::strip(this->naked_automaton()));
95  }
96 
99  auto automaton() const
100  {
101  return copy<const transpose_automaton_impl*, automaton_t>(this);
102  }
103 
104  /*-------------------------------.
105  | const methods that transpose. |
106  `-------------------------------*/
107 #define DEFINE(Signature, Value) \
108  auto \
109  Signature const \
110  -> decltype(aut_->Value) \
111  { \
112  return aut_->Value; \
113  }
114 
115  DEFINE(all_in(state_t s), all_out(s));
116  DEFINE(all_out(state_t s), all_in(s));
117  DEFINE(dst_of(transition_t t), src_of(t));
118  DEFINE(is_final(state_t s), is_initial(s));
119  DEFINE(num_finals(), num_initials());
120  DEFINE(num_initials(), num_finals());
121  DEFINE(src_of(transition_t t), dst_of(t));
122  DEFINE(is_initial(state_t s), is_final(s));
123 
124  DEFINE(is_lazy(state_t s), is_lazy_in(s));
125  DEFINE(is_lazy_in(state_t s), is_lazy(s));
126 
127  DEFINE(get_transition(state_t s, state_t d, label_t l),
128  get_transition(d, s, aut_->labelset()->transpose(l)));
129  DEFINE(has_transition(transition_t t),
130  has_transition(t));
131  DEFINE(has_transition(state_t s, state_t d, label_t l),
132  has_transition(d, s, aut_->labelset()->transpose(l)));
133  DEFINE(label_of(transition_t t),
134  labelset()->transpose(aut_->label_of(t)));
135 
136  ATTRIBUTE_PURE
137  DEFINE(get_initial_weight(state_t s),
138  weightset()->transpose(aut_->get_final_weight(s)));
139 
140  ATTRIBUTE_PURE
141  DEFINE(get_final_weight(state_t s),
142  weightset()->transpose(aut_->get_initial_weight(s)));
143 
144  ATTRIBUTE_PURE
145  DEFINE(weight_of(transition_t t),
146  weightset()->transpose(aut_->weight_of(t)));
147 
148 #undef DEFINE
149 
150 
151  /*-----------------------------------.
152  | non-const methods that transpose. |
153  `-----------------------------------*/
154 
155 #define DEFINE(Signature, Value) \
156  auto \
157  Signature \
158  -> decltype(aut_->Value) \
159  { \
160  return aut_->Value; \
161  }
162 
163  DEFINE(set_lazy(state_t s, bool l = true), set_lazy_in(s, l));
164  DEFINE(set_lazy_in(state_t s, bool l = true), set_lazy(s, l));
165 
166  DEFINE(set_initial(state_t s), set_final(s));
167  DEFINE(set_final(state_t s), set_initial(s));
168  DEFINE(unset_initial(state_t s), unset_final(s));
169  DEFINE(unset_final(state_t s), unset_initial(s));
170 
171  DEFINE(set_weight(transition_t t, weight_t k),
172  set_weight(t, aut_->weightset()->transpose(k)));
173  DEFINE(add_weight(transition_t t, weight_t k),
174  add_weight(t, aut_->weightset()->transpose(k)));
175  DEFINE(lweight(transition_t t, weight_t k),
176  lweight(t, aut_->weightset()->transpose(k)));
177  DEFINE(rweight(transition_t t, weight_t k),
178  rweight(t, aut_->weightset()->transpose(k)));
179 
180  DEFINE(del_transition(transition_t t), del_transition(t));
181  DEFINE(del_transition(state_t s, state_t d, label_t l),
182  del_transition(d, s, aut_->labelset()->transpose(l)));
183 
184  DEFINE(add_transition(state_t s, state_t d, label_t l, weight_t k),
185  add_transition(d, s,
186  aut_->labelset()->transpose(l),
187  aut_->weightset()->transpose(k)));
188  DEFINE(add_transition(state_t s, state_t d, label_t l),
189  add_transition(d, s, aut_->labelset()->transpose(l)));
190 
191  DEFINE(new_transition(state_t s, state_t d, label_t l, weight_t k),
192  new_transition(d, s,
193  aut_->labelset()->transpose(l),
194  aut_->weightset()->transpose(k)));
195  DEFINE(new_transition(state_t s, state_t d, label_t l),
196  new_transition(d, s,
197  aut_->labelset()->transpose(l)));
198 
199  DEFINE(set_transition(state_t s, state_t d, label_t l, weight_t k),
200  set_transition(d, s,
201  aut_->labelset()->transpose(l),
202  aut_->weightset()->transpose(k)));
203  DEFINE(set_initial(state_t s, weight_t k),
204  set_final(s, aut_->weightset()->transpose(k)));
205  DEFINE(set_final(state_t s, weight_t k),
206  set_initial(s, aut_->weightset()->transpose(k)));
207  DEFINE(add_initial(state_t s, weight_t k),
208  add_final(s, aut_->weightset()->transpose(k)));
209  DEFINE(add_final(state_t s, weight_t k),
210  add_initial(s, aut_->weightset()->transpose(k)));
211 
212  template <Automaton A>
213  DEFINE(add_transition_copy(state_t src, state_t dst,
214  const A& aut,
215  transition_t_of<A> t,
216  bool transpose = false),
217  add_transition_copy(dst, src, aut, t, !transpose));
218 
219  template <Automaton A>
220  DEFINE(new_transition_copy(state_t src, state_t dst,
221  const A& aut,
222  transition_t_of<A> t,
223  bool transpose = false),
224  new_transition_copy(dst, src, aut, t, !transpose));
225 
226 #undef DEFINE
227 
228 
229 
230  /*-----------------------------------.
231  | constexpr methods that transpose. |
232  `-----------------------------------*/
233 
234 #define DEFINE(Signature, Value) \
235  static constexpr \
236  auto \
237  Signature \
238  -> decltype(automaton_t::element_type::Value) \
239  { \
240  return automaton_t::element_type::Value; \
241  }
242 
243  DEFINE(post(), pre());
244  DEFINE(pre(), post());
245 
246 #undef DEFINE
247  };
248  }
249 
251  template <Automaton Aut>
252  Aut
253  transpose(const transpose_automaton<Aut>& aut)
254  {
255  return aut->naked_automaton();
256  }
257 
259  template <Automaton Aut>
260  transpose_automaton<Aut>
261  transpose(Aut aut)
262  {
263  return make_shared_ptr<transpose_automaton<Aut>>(aut);
264  }
265 
266  namespace dyn
267  {
268  namespace detail
269  {
271  template <Automaton Aut>
272  automaton
273  transpose(automaton& aut)
274  {
275  auto& a = aut->as<Aut>();
276  return vcsn::transpose(a);
277  }
278  }
279  }
280 
281 
282  /*------------------------------.
283  | dyn::transpose(expression). |
284  `------------------------------*/
285  namespace dyn
286  {
287  namespace detail
288  {
290  template <typename ExpSet>
291  expression
292  transpose_expression(const expression& exp)
293  {
294  const auto& e = exp->as<ExpSet>();
295  return {e.valueset(), ::vcsn::transpose(e.valueset(), e.value())};
296  }
297  }
298  }
299 
300  /*----------------------------------.
301  | dyn::transposition(expression). |
302  `----------------------------------*/
303  namespace dyn
304  {
305  namespace detail
306  {
308  template <typename ExpSet>
309  expression
310  transposition_expression(const expression& exp)
311  {
312  const auto& e = exp->as<ExpSet>();
313  return {e.valueset(), e.valueset().transposition(e.value())};
314  }
315  }
316  }
317 } // vcsn::
The transpose of a fresh non-transposed automaton.
Definition: transpose.hh:36
return res
Definition: multiply.hh:398
Aut automaton_t
The type of automaton to wrap.
Definition: transpose.hh:28
labelset_t_of< automaton_t > labelset_t
Definition: transpose.hh:60
std::shared_ptr< detail::transpose_automaton_impl< Aut >> transpose_automaton
An automaton wrapper that presents the transposed automaton.
Definition: fwd.hh:108
Aggregate an automaton, and forward calls to it.
An input/output format for valuesets.
Definition: format.hh:13
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:62
typename automaton_t::element_type::kind_t kind_t
Definition: transpose.hh:62
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
weight_t_of< automaton_t > weight_t
Definition: transpose.hh:58
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
std::string type(const automaton &a)
The implementation type of a.
Definition: others.cc:239
Definition: a-star.hh:8
typename fresh_impl_< fresh_automaton_t_of< automaton_t, Ctx >>::type fresh_automaton_t
The type to use to build an automaton of the same type: remove the inner const-volatile qualifiers...
Definition: transpose.hh:53
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:66
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:64
transition_t_of< automaton_t > transition_t
Definition: transpose.hh:56
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: transpose.hh:74
weightset_t_of< automaton_t > weightset_t
Definition: transpose.hh:61
label_t_of< automaton_t > label_t
Definition: transpose.hh:57
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:65
transpose_automaton< automaton_t > self_t
Definition: transpose.hh:29
symbol sname()
Definition: name.hh:65
context_t_of< automaton_t > context_t
Definition: transpose.hh:32
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
state_t_of< automaton_t > state_t
Definition: transpose.hh:55
automaton_t aut_
The wrapped automaton, possibly const.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61