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