Vcsn  2.8
Be Rational
copy.hh
Go to the documentation of this file.
1 #pragma once
2 
5 #include <vcsn/misc/cast.hh>
6 
7 #include <vcsn/algos/project.hh> // project.
8 
9 namespace vcsn
10 {
11  namespace rat
12  {
17  template <typename InExpSet, typename OutExpSet>
18  std::enable_if_t<std::is_same<InExpSet, OutExpSet>{},
19  typename OutExpSet::value_t>
20  copy(const InExpSet& in_rs, const OutExpSet& out_rs,
21  const typename InExpSet::value_t& v);
22 
23  template <typename InExpSet, typename OutExpSet>
24  std::enable_if_t<!std::is_same<InExpSet, OutExpSet>{},
25  typename OutExpSet::value_t>
26  copy(const InExpSet& in_rs, const OutExpSet& out_rs,
27  const typename InExpSet::value_t& v);
28 
33  template <typename InExpSet, typename OutExpSet>
34  class copy_impl
35  : public InExpSet::const_visitor
36  {
37  public:
38  using in_expressionset_t = InExpSet;
39  using out_expressionset_t = OutExpSet;
40  using super_t = typename in_expressionset_t::const_visitor;
41  using self_t = copy_impl;
42 
44  using in_expression_t = typename in_expressionset_t::value_t;
45  using out_expression_t = typename out_expressionset_t::value_t;
46  template <type_t Type>
47  using unary_t = typename super_t::template unary_t<Type>;
48  template <type_t Type>
49  using variadic_t = typename super_t::template variadic_t<Type>;
50 
52  const out_expressionset_t& out_rs)
53  : in_rs_{in_rs}
54  , out_rs_{out_rs}
55  {}
56 
60  {
61  return rec_(v);
62  }
63 
64  protected:
67  {
68  v->accept(*this);
69  return res_;
70  }
71 
73  template <exp::type_t Type, typename Fun>
74  void
75  rec_(const unary_t<Type>& v, Fun&& fun)
76  {
77  // FIXME: C++17: invoke.
78  res_ = (out_rs_.*fun)(rec_(v.sub()));
79  }
80 
82  template <exp::type_t Type, typename Fun>
83  void
84  rec_(const variadic_t<Type>& v, Fun&& fun)
85  {
86  auto res = rec_(v.head());
87  for (const auto& c: v.tail())
88  // FIXME: C++17: invoke.
89  res = (out_rs_.*fun)(res, rec_(c));
90  res_ = std::move(res);
91  }
92 
101  using bin_t =
103  const out_expression_t&) const;
105  static_cast<bin_t>(&ors_t::mul)); }
109  VCSN_RAT_VISIT(zero,) { res_ = out_rs_.zero(); }
110 
112  {
113  res_ = out_rs_.atom(out_rs_.labelset()->conv(*in_rs_.labelset(),
114  v.value()));
115  }
116 
118  {
119  res_ = out_rs_.name(rec_(v.sub()), v.name_get());
120  }
121 
123  {
124  res_ = out_rs_.lweight(out_rs_.weightset()->conv(*in_rs_.weightset(),
125  v.weight()),
126  rec_(v.sub()));
127  }
128 
130  {
131  res_ = out_rs_.rweight(rec_(v.sub()),
132  out_rs_.weightset()->conv(*in_rs_.weightset(),
133  v.weight()));
134  }
135 
136  /*---------.
137  | tuple. |
138  `---------*/
139 
140  using tuple_t = typename super_t::tuple_t;
141 
142  template <typename = void>
143  struct visit_tuple
144  {
146  template <size_t I>
147  auto work_(const tuple_t& v)
148  {
149  return rat::copy(detail::project<I>(self_.in_rs_),
150  detail::project<I>(self_.out_rs_),
151  std::get<I>(v.sub()));
152  }
153 
155  template <size_t... I>
157  {
158  return self_.out_rs_.tuple(work_<I>(v)...);
159  }
160 
163  {
165  }
167  };
168 
169  void visit(const tuple_t& v, std::true_type) override
170  {
171  detail::static_if<in_context_t::is_lat>
172  ([this](auto&& v){ res_ = visit_tuple<decltype(v)>{*this}(v); })
173  (v);
174  }
175 
182  };
183 
184 
185  // If possible, avoid useless copies.
186  template <typename InExpSet, typename OutExpSet>
187  std::enable_if_t<std::is_same<InExpSet, OutExpSet>{},
188  typename OutExpSet::value_t>
189  copy(const InExpSet& in_rs, const OutExpSet& out_rs,
190  const typename InExpSet::value_t& v)
191  {
192  // This should be enough: we don't need to check the context
193  // as its dynamic value for the labelset and weightset do
194  // not have any influence on the expression itself.
195  if (in_rs.identities() == out_rs.identities())
196  return v;
197  auto copy = copy_impl<InExpSet, OutExpSet>{in_rs, out_rs};
198  try
199  {
200  return copy(v);
201  }
202  catch (const std::runtime_error& e)
203  {
204  raise(e,
205  " while converting expression ",
206  str_quote(to_string(in_rs, v)),
207  " to ", to_string(out_rs));
208  }
209  }
210 
211  template <typename InExpSet, typename OutExpSet>
212  std::enable_if_t<!std::is_same<InExpSet, OutExpSet>{},
213  typename OutExpSet::value_t>
214  copy(const InExpSet& in_rs, const OutExpSet& out_rs,
215  const typename InExpSet::value_t& v)
216  {
217  auto copy = copy_impl<InExpSet, OutExpSet>{in_rs, out_rs};
218  try
219  {
220  return copy(v);
221  }
222  catch (const std::runtime_error& e)
223  {
224  raise(e,
225  " while converting expression ",
226  str_quote(to_string(in_rs, v)),
227  " to ", to_string(out_rs));
228  }
229  }
230  }
231 }
void rec_(const unary_t< Type > &v, Fun &&fun)
Factor the handling of unary operations.
Definition: copy.hh:75
VCSN_RAT_VISIT(infiltrate, v)
Definition: copy.hh:98
VCSN_RAT_VISIT(compose, v)
Definition: copy.hh:96
auto work_(const tuple_t &v)
Copy one tape.
Definition: copy.hh:147
void visit(const tuple_t &v, std::true_type) override
Definition: copy.hh:169
InExpSet in_expressionset_t
Definition: copy.hh:38
An inner node with multiple children.
Definition: expression.hh:119
VCSN_RAT_VISIT(mul, v)
Definition: copy.hh:104
OutExpSet out_expressionset_t
Definition: copy.hh:39
VCSN_RAT_VISIT(rweight, v)
Definition: copy.hh:129
VCSN_RAT_VISIT(conjunction, v)
Definition: copy.hh:97
VCSN_RAT_VISIT(star, v)
Definition: copy.hh:107
VCSN_RAT_VISIT(shuffle, v)
Definition: copy.hh:106
An inner node to name the subexpression.
Definition: expression.hh:290
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
Definition: traits.hh:61
typename in_expressionset_t::value_t in_expression_t
Definition: copy.hh:44
VCSN_RAT_VISIT(add, v)
Definition: copy.hh:94
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
Definition: traits.hh:63
typename in_expressionset_t::const_visitor super_t
Definition: copy.hh:40
out_expression_t(ors_t::*)(const out_expression_t &, const out_expression_t &) const bin_t
Definition: copy.hh:103
const in_expressionset_t & in_rs_
Expressionset to decode the input value.
Definition: copy.hh:177
Definition: a-star.hh:8
out_expression_t work_(const tuple_t &v, detail::index_sequence< I... >)
Copy all the tapes.
Definition: copy.hh:156
VCSN_RAT_VISIT(transposition, v)
Definition: copy.hh:108
out_expression_t operator()(const tuple_t &v)
Entry point.
Definition: copy.hh:162
VCSN_RAT_VISIT(complement, v)
Definition: copy.hh:95
std::string to_string(identities i)
Wrapper around operator<<.
Definition: identities.cc:38
An inner node implementing a weight.
Definition: expression.hh:256
A static list of size_t.
Definition: tuple.hh:32
void rec_(const variadic_t< Type > &v, Fun &&fun)
Factor the handling of n-ary operations.
Definition: copy.hh:84
out_expression_t rec_(const in_expression_t &v)
Easy recursion.
Definition: copy.hh:66
context_t_of< in_expressionset_t > in_context_t
Definition: copy.hh:43
VCSN_RAT_VISIT(zero,)
Definition: copy.hh:109
VCSN_RAT_VISIT(ldivide, v)
Definition: copy.hh:99
return v
Definition: multiply.hh:362
copy_impl(const in_expressionset_t &in_rs, const out_expressionset_t &out_rs)
Definition: copy.hh:51
typename super_t::template variadic_t< Type > variadic_t
Definition: copy.hh:49
VCSN_RAT_VISIT(name, v)
Definition: copy.hh:117
Functor to copy/convert a rational expression.
Definition: copy.hh:34
typename super_t::template unary_t< Type > unary_t
Definition: copy.hh:47
std::string str_quote(Args &&... args)
Convert to a string, in quotes.
Definition: escape.hh:49
out_expressionset_t ors_t
Definition: copy.hh:93
const out_expressionset_t & out_rs_
Expressionset to build the output value.
Definition: copy.hh:179
VCSN_RAT_VISIT(lweight, v)
Definition: copy.hh:122
typename super_t::tuple_t tuple_t
Definition: copy.hh:140
std::enable_if_t< std::is_same< InExpSet, OutExpSet >{}, typename OutExpSet::value_t > copy(const InExpSet &in_rs, const OutExpSet &out_rs, const typename InExpSet::value_t &v)
Copy/convert a rational expression.
Definition: copy.hh:189
typename out_expressionset_t::value_t out_expression_t
Definition: copy.hh:45
return res
Definition: multiply.hh:399
VCSN_RAT_VISIT(atom, v)
Definition: copy.hh:111
out_expression_t res_
Output value, under construction.
Definition: copy.hh:181
out_expression_t operator()(const in_expression_t &v)
Entry point: copy/convert v.
Definition: copy.hh:59
VCSN_RAT_VISIT(one,)
Definition: copy.hh:100