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