Vcsn  2.3a
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 
93  VCSN_RAT_VISIT(one,) { res_ = out_rs_.one(); }
94  using bin_t =
96  const out_expression_t&) const;
98  static_cast<bin_t>(&ors_t::mul)); }
102  VCSN_RAT_VISIT(zero,) { res_ = out_rs_.zero(); }
103 
105  {
106  res_ = out_rs_.atom(out_rs_.labelset()->conv(*in_rs_.labelset(),
107  v.value()));
108  }
109 
111  {
112  res_ = out_rs_.lweight(out_rs_.weightset()->conv(*in_rs_.weightset(),
113  v.weight()),
114  rec_(v.sub()));
115  }
116 
118  {
119  res_ = out_rs_.rweight(rec_(v.sub()),
120  out_rs_.weightset()->conv(*in_rs_.weightset(),
121  v.weight()));
122  }
123 
124  /*---------.
125  | tuple. |
126  `---------*/
127 
128  using tuple_t = typename super_t::tuple_t;
129 
130  template <typename Dummy = void>
131  struct visit_tuple
132  {
134  template <size_t I>
135  auto work_(const tuple_t& v)
136  {
137  return rat::copy(detail::project<I>(self_.in_rs_),
138  detail::project<I>(self_.out_rs_),
139  std::get<I>(v.sub()));
140  }
141 
143  template <size_t... I>
145  {
146  return self_.out_rs_.tuple(work_<I>(v)...);
147  }
148 
151  {
153  }
155  };
156 
157  void visit(const tuple_t& v, std::true_type) override
158  {
159  detail::static_if<in_context_t::is_lat>
160  ([this](auto&& v){ res_ = visit_tuple<>{*this}(v); })
161  (v);
162  }
163 
170  };
171 
172  template <typename InExpSet, typename OutExpSet>
173  typename OutExpSet::value_t
174  copy(const InExpSet& in_rs, const OutExpSet& out_rs,
175  const typename InExpSet::value_t& v)
176  {
177  auto copy = copy_impl<InExpSet, OutExpSet>{in_rs, out_rs};
178  return copy(v);
179  }
180  } // namespace rat
181 } // namespace vcsn
out_expression_t operator()(const tuple_t &v)
Entry point.
Definition: copy.hh:150
void rec_(const unary_t< Type > &v, Fun &&fun)
Factor the handling of unary operations.
Definition: copy.hh:69
return v
Definition: multiply.hh:361
Definition: a-star.hh:8
return res
Definition: multiply.hh:398
typename super_t::template unary_t< Type > unary_t
Definition: copy.hh:41
out_expression_t work_(const tuple_t &v, detail::index_sequence< I... >)
Copy all the tapes.
Definition: copy.hh:144
typename super_t::tuple_t tuple_t
Definition: copy.hh:128
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:90
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:99
out_expression_t(ors_t::*)(const out_expression_t &, const out_expression_t &) const bin_t
Definition: copy.hh:96
VCSN_RAT_VISIT(ldivide, v)
Definition: copy.hh:92
An inner node implementing a weight.
Definition: expression.hh:264
void visit(const tuple_t &v, std::true_type) override
Definition: copy.hh:157
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:174
out_expression_t res_
Output value, under construction.
Definition: copy.hh:169
OutExpSet out_expressionset_t
Definition: copy.hh:33
VCSN_RAT_VISIT(rweight, v)
Definition: copy.hh:117
VCSN_RAT_VISIT(atom, v)
Definition: copy.hh:104
const out_expressionset_t & out_rs_
expressionset to build the output value.
Definition: copy.hh:167
const in_expressionset_t & in_rs_
expressionset to decode the input value.
Definition: copy.hh:165
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
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
VCSN_RAT_VISIT(star, v)
Definition: copy.hh:100
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
VCSN_RAT_VISIT(lweight, v)
Definition: copy.hh:110
VCSN_RAT_VISIT(transposition, v)
Definition: copy.hh:101
VCSN_RAT_VISIT(zero,)
Definition: copy.hh:102
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
auto work_(const tuple_t &v)
Copy one tape.
Definition: copy.hh:135
out_expressionset_t ors_t
Definition: copy.hh:87
VCSN_RAT_VISIT(one,)
Definition: copy.hh:93
VCSN_RAT_VISIT(mul, v)
Definition: copy.hh:97
VCSN_RAT_VISIT(infiltrate, v)
Definition: copy.hh:91