Vcsn  2.5.dev
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_.name(rec_(v.sub()), v.name_get());
114  }
115 
117  {
118  res_ = out_rs_.lweight(out_rs_.weightset()->conv(*in_rs_.weightset(),
119  v.weight()),
120  rec_(v.sub()));
121  }
122 
124  {
125  res_ = out_rs_.rweight(rec_(v.sub()),
126  out_rs_.weightset()->conv(*in_rs_.weightset(),
127  v.weight()));
128  }
129 
130  /*---------.
131  | tuple. |
132  `---------*/
133 
134  using tuple_t = typename super_t::tuple_t;
135 
136  template <typename = void>
137  struct visit_tuple
138  {
140  template <size_t I>
141  auto work_(const tuple_t& v)
142  {
143  return rat::copy(detail::project<I>(self_.in_rs_),
144  detail::project<I>(self_.out_rs_),
145  std::get<I>(v.sub()));
146  }
147 
149  template <size_t... I>
151  {
152  return self_.out_rs_.tuple(work_<I>(v)...);
153  }
154 
157  {
159  }
161  };
162 
163  void visit(const tuple_t& v, std::true_type) override
164  {
165  detail::static_if<in_context_t::is_lat>
166  ([this](auto&& v){ res_ = visit_tuple<decltype(v)>{*this}(v); })
167  (v);
168  }
169 
176  };
177 
178  template <typename InExpSet, typename OutExpSet>
179  typename OutExpSet::value_t
180  copy(const InExpSet& in_rs, const OutExpSet& out_rs,
181  const typename InExpSet::value_t& v)
182  {
183  auto copy = copy_impl<InExpSet, OutExpSet>{in_rs, out_rs};
184  return copy(v);
185  }
186  } // namespace rat
187 } // namespace vcsn
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
Definition: traits.hh:61
VCSN_RAT_VISIT(add, v)
Definition: copy.hh:88
VCSN_RAT_VISIT(transposition, v)
Definition: copy.hh:102
VCSN_RAT_VISIT(mul, v)
Definition: copy.hh:98
auto work_(const tuple_t &v)
Copy one tape.
Definition: copy.hh:141
VCSN_RAT_VISIT(complement, v)
Definition: copy.hh:89
VCSN_RAT_VISIT(conjunction, v)
Definition: copy.hh:91
VCSN_RAT_VISIT(compose, v)
Definition: copy.hh:90
out_expression_t operator()(const tuple_t &v)
Entry point.
Definition: copy.hh:156
typename out_expressionset_t::value_t out_expression_t
Definition: copy.hh:39
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:180
VCSN_RAT_VISIT(zero,)
Definition: copy.hh:103
void rec_(const unary_t< Type > &v, Fun &&fun)
Factor the handling of unary operations.
Definition: copy.hh:69
copy_impl(const in_expressionset_t &in_rs, const out_expressionset_t &out_rs)
Definition: copy.hh:45
OutExpSet out_expressionset_t
Definition: copy.hh:33
typename super_t::template unary_t< Type > unary_t
Definition: copy.hh:41
VCSN_RAT_VISIT(name, v)
Definition: copy.hh:111
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
Definition: traits.hh:63
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:78
return v
Definition: multiply.hh:362
const in_expressionset_t & in_rs_
expressionset to decode the input value.
Definition: copy.hh:171
VCSN_RAT_VISIT(lweight, v)
Definition: copy.hh:116
return res
Definition: multiply.hh:399
InExpSet in_expressionset_t
Definition: copy.hh:32
Functor to copy/convert a rational expression.
Definition: copy.hh:28
VCSN_RAT_VISIT(atom, v)
Definition: copy.hh:105
out_expression_t res_
Output value, under construction.
Definition: copy.hh:175
VCSN_RAT_VISIT(rweight, v)
Definition: copy.hh:123
An inner node with multiple children.
Definition: expression.hh:119
VCSN_RAT_VISIT(star, v)
Definition: copy.hh:101
out_expression_t operator()(const in_expression_t &v)
Entry point: copy/convert v.
Definition: copy.hh:53
void visit(const tuple_t &v, std::true_type) override
Definition: copy.hh:163
Definition: a-star.hh:8
An inner node to name the subexpression.
Definition: expression.hh:290
VCSN_RAT_VISIT(ldivide, v)
Definition: copy.hh:93
typename super_t::tuple_t tuple_t
Definition: copy.hh:134
out_expression_t work_(const tuple_t &v, detail::index_sequence< I... >)
Copy all the tapes.
Definition: copy.hh:150
context_t_of< in_expressionset_t > in_context_t
Definition: copy.hh:37
out_expressionset_t ors_t
Definition: copy.hh:87
typename in_expressionset_t::value_t in_expression_t
Definition: copy.hh:38
typename in_expressionset_t::const_visitor super_t
Definition: copy.hh:34
const out_expressionset_t & out_rs_
expressionset to build the output value.
Definition: copy.hh:173
out_expression_t(ors_t::*)(const out_expression_t &, const out_expression_t &) const bin_t
Definition: copy.hh:97
out_expression_t rec_(const in_expression_t &v)
Easy recursion.
Definition: copy.hh:60
typename super_t::template variadic_t< Type > variadic_t
Definition: copy.hh:43
VCSN_RAT_VISIT(shuffle, v)
Definition: copy.hh:100
VCSN_RAT_VISIT(infiltrate, v)
Definition: copy.hh:92
VCSN_RAT_VISIT(one,)
Definition: copy.hh:94