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