Vcsn  2.4
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  {
13 
18  template <typename InExpSet, typename OutExpSet = InExpSet>
19  typename OutExpSet::value_t
20  copy(const InExpSet& in_rs, const OutExpSet& out_rs,
21  const typename InExpSet::value_t& v);
22 
27  template <typename InExpSet, typename OutExpSet>
28  class copy_impl
29  : public InExpSet::const_visitor
30  {
31  public:
32  using in_expressionset_t = InExpSet;
33  using out_expressionset_t = OutExpSet;
34  using super_t = typename in_expressionset_t::const_visitor;
35  using self_t = copy_impl;
36 
38  using in_expression_t = typename in_expressionset_t::value_t;
39  using out_expression_t = typename out_expressionset_t::value_t;
40  template <type_t Type>
41  using unary_t = typename super_t::template unary_t<Type>;
42  template <type_t Type>
43  using variadic_t = typename super_t::template variadic_t<Type>;
44 
46  const out_expressionset_t& out_rs)
47  : in_rs_{in_rs}
48  , out_rs_{out_rs}
49  {}
50 
54  {
55  return rec_(v);
56  }
57 
58  protected:
61  {
62  v->accept(*this);
63  return res_;
64  }
65 
67  template <exp::type_t Type, typename Fun>
68  void
69  rec_(const unary_t<Type>& v, Fun&& fun)
70  {
71  // FIXME: C++17: invoke.
72  res_ = (out_rs_.*fun)(rec_(v.sub()));
73  }
74 
76  template <exp::type_t Type, typename Fun>
77  void
78  rec_(const variadic_t<Type>& v, Fun&& fun)
79  {
80  auto res = rec_(v.head());
81  for (const auto& c: v.tail())
82  // FIXME: C++17: invoke.
83  res = (out_rs_.*fun)(res, rec_(c));
84  res_ = std::move(res);
85  }
86 
94  VCSN_RAT_VISIT(one,) { res_ = out_rs_.one(); }
95  using bin_t =
97  const out_expression_t&) const;
99  static_cast<bin_t>(&ors_t::mul)); }
103  VCSN_RAT_VISIT(zero,) { res_ = out_rs_.zero(); }
104 
106  {
107  res_ = out_rs_.atom(out_rs_.labelset()->conv(*in_rs_.labelset(),
108  v.value()));
109  }
110 
112  {
113  res_ = out_rs_.lweight(out_rs_.weightset()->conv(*in_rs_.weightset(),
114  v.weight()),
115  rec_(v.sub()));
116  }
117 
119  {
120  res_ = out_rs_.rweight(rec_(v.sub()),
121  out_rs_.weightset()->conv(*in_rs_.weightset(),
122  v.weight()));
123  }
124 
125  /*---------.
126  | tuple. |
127  `---------*/
128 
129  using tuple_t = typename super_t::tuple_t;
130 
131  template <typename = void>
132  struct visit_tuple
133  {
135  template <size_t I>
136  auto work_(const tuple_t& v)
137  {
138  return rat::copy(detail::project<I>(self_.in_rs_),
139  detail::project<I>(self_.out_rs_),
140  std::get<I>(v.sub()));
141  }
142 
144  template <size_t... I>
146  {
147  return self_.out_rs_.tuple(work_<I>(v)...);
148  }
149 
152  {
154  }
156  };
157 
158  void visit(const tuple_t& v, std::true_type) override
159  {
160  detail::static_if<in_context_t::is_lat>
161  ([this](auto&& v){ res_ = visit_tuple<decltype(v)>{*this}(v); })
162  (v);
163  }
164 
171  };
172 
173  template <typename InExpSet, typename OutExpSet>
174  typename OutExpSet::value_t
175  copy(const InExpSet& in_rs, const OutExpSet& out_rs,
176  const typename InExpSet::value_t& v)
177  {
178  auto copy = copy_impl<InExpSet, OutExpSet>{in_rs, out_rs};
179  return copy(v);
180  }
181  } // namespace rat
182 } // namespace vcsn
void rec_(const unary_t< Type > &v, Fun &&fun)
Factor the handling of unary operations.
Definition: copy.hh:69
auto work_(const tuple_t &v)
Copy one tape.
Definition: copy.hh:136
return res
Definition: multiply.hh:398
An inner node implementing a weight.
Definition: expression.hh:255
typename super_t::template unary_t< Type > unary_t
Definition: copy.hh:41
typename super_t::tuple_t tuple_t
Definition: copy.hh:129
Functor to copy/convert a rational expression.
Definition: copy.hh:28
typename in_expressionset_t::const_visitor super_t
Definition: copy.hh:34
copy_impl(const in_expressionset_t &in_rs, const out_expressionset_t &out_rs)
Definition: copy.hh:45
InExpSet in_expressionset_t
Definition: copy.hh:32
typename out_expressionset_t::value_t out_expression_t
Definition: copy.hh:39
VCSN_RAT_VISIT(conjunction, v)
Definition: copy.hh:91
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
VCSN_RAT_VISIT(shuffle, v)
Definition: copy.hh:100
out_expression_t operator()(const tuple_t &v)
Entry point.
Definition: copy.hh:151
out_expression_t(ors_t::*)(const out_expression_t &, const out_expression_t &) const bin_t
Definition: copy.hh:97
VCSN_RAT_VISIT(ldivide, v)
Definition: copy.hh:93
Definition: a-star.hh:8
void visit(const tuple_t &v, std::true_type) override
Definition: copy.hh:158
out_expression_t res_
Output value, under construction.
Definition: copy.hh:170
OutExpSet out_expressionset_t
Definition: copy.hh:33
VCSN_RAT_VISIT(rweight, v)
Definition: copy.hh:118
VCSN_RAT_VISIT(atom, v)
Definition: copy.hh:105
const out_expressionset_t & out_rs_
expressionset to build the output value.
Definition: copy.hh:168
const in_expressionset_t & in_rs_
expressionset to decode the input value.
Definition: copy.hh:166
void rec_(const variadic_t< Type > &v, Fun &&fun)
Factor the handling of n-ary operations.
Definition: copy.hh:78
typename super_t::template variadic_t< Type > variadic_t
Definition: copy.hh:43
VCSN_RAT_VISIT(add, v)
Definition: copy.hh:88
context_t_of< in_expressionset_t > in_context_t
Definition: copy.hh:37
VCSN_RAT_VISIT(star, v)
Definition: copy.hh:101
An inner node with multiple children.
Definition: expression.hh:118
out_expression_t operator()(const in_expression_t &v)
Entry point: copy/convert v.
Definition: copy.hh:53
return v
Definition: multiply.hh:361
VCSN_RAT_VISIT(compose, v)
Definition: copy.hh:90
VCSN_RAT_VISIT(lweight, v)
Definition: copy.hh:111
VCSN_RAT_VISIT(transposition, v)
Definition: copy.hh:102
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:175
VCSN_RAT_VISIT(zero,)
Definition: copy.hh:103
out_expression_t rec_(const in_expression_t &v)
Easy recursion.
Definition: copy.hh:60
typename in_expressionset_t::value_t in_expression_t
Definition: copy.hh:38
VCSN_RAT_VISIT(complement, v)
Definition: copy.hh:89
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
out_expressionset_t ors_t
Definition: copy.hh:87
out_expression_t work_(const tuple_t &v, detail::index_sequence< I... >)
Copy all the tapes.
Definition: copy.hh:145
VCSN_RAT_VISIT(one,)
Definition: copy.hh:94
VCSN_RAT_VISIT(mul, v)
Definition: copy.hh:98
VCSN_RAT_VISIT(infiltrate, v)
Definition: copy.hh:92