Vcsn  2.2
Be Rational
copy.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
6 #include <vcsn/misc/cast.hh>
7 
8 #include <vcsn/algos/project.hh> // project.
9 
10 namespace vcsn
11 {
12  namespace rat
13  {
14 
19  template <typename InExpSet, typename OutExpSet = InExpSet>
20  typename OutExpSet::value_t
21  copy(const InExpSet& in_rs, const OutExpSet& out_rs,
22  const typename InExpSet::value_t& v);
23 
28  template <typename InExpSet, typename OutExpSet = InExpSet>
29  class copier
30  : public InExpSet::const_visitor
31  {
32  public:
33  using in_expressionset_t = InExpSet;
34  using out_expressionset_t = OutExpSet;
35  using super_t = typename in_expressionset_t::const_visitor;
36  using self_t = copier;
37 
39  using in_value_t = typename in_expressionset_t::value_t;
40  using out_value_t = typename out_expressionset_t::value_t;
41  using node_t = typename super_t::node_t;
42  using inner_t = typename super_t::inner_t;
43  template <type_t Type>
44  using unary_t = typename super_t::template unary_t<Type>;
45  template <type_t Type>
46  using variadic_t = typename super_t::template variadic_t<Type>;
47  using leaf_t = typename super_t::leaf_t;
48 
49  copier(const in_expressionset_t& in_rs, const out_expressionset_t& out_rs)
50  : in_rs_(in_rs)
51  , out_rs_(out_rs)
52  {}
53 
57  {
58  return copy(v);
59  }
60 
61  private:
64  {
65  v->accept(*this);
66  return res_;
67  }
68 
70  template <exp::type_t Type>
71  void
72  copy_(const unary_t<Type>& v)
73  {
74  using out_unary_t
75  = typename out_expressionset_t::template unary_t<Type>;
76  res_ = std::make_shared<out_unary_t>(copy(v.sub()));
77  }
78 
80  template <exp::type_t Type>
81  void
82  copy_(const variadic_t<Type>& v)
83  {
84  using out_variadic_t
85  = typename out_expressionset_t::template variadic_t<Type>;
86  typename out_expressionset_t::values_t sub;
87  for (const auto& s: v)
88  sub.emplace_back(copy(s));
89  res_ = std::make_shared<out_variadic_t>(sub);
90  }
91 
96  VCSN_RAT_VISIT(one,) { res_ = out_rs_.one(); }
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_.lmul(out_rs_.weightset()->conv(*in_rs_.weightset(),
113  v.weight()),
114  copy(v.sub()));
115  }
116 
118  {
119  res_ = out_rs_.rmul(copy(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 <bool = in_context_t::is_lat,
131  typename Dummy = 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>(visitor_.in_rs_),
139  detail::project<I>(visitor_.out_rs_),
140  std::get<I>(v.sub()));
141  }
142 
144  template <size_t... I>
146  {
147  return visitor_.out_rs_.tuple(work_<I>(v)...);
148  }
149 
152  {
154  }
156  };
157 
158  template <typename Dummy>
159  struct visit_tuple<false, Dummy>
160  {
162  {
164  }
166  };
167 
168  void visit(const tuple_t& v, std::true_type) override
169  {
170  res_ = visit_tuple<>{*this}(v);
171  }
172 
179  };
180 
181  template <typename InExpSet, typename OutExpSet>
182  typename OutExpSet::value_t
183  copy(const InExpSet& in_rs, const OutExpSet& out_rs,
184  const typename InExpSet::value_t& v)
185  {
186  auto copy = copier<InExpSet, OutExpSet>{in_rs, out_rs};
187  return copy(v);
188  }
189 
190  } // namespace rat
191 } // namespace vcsn
VCSN_RAT_VISIT(shuffle, v)
Definition: copy.hh:98
typename in_expressionset_t::value_t in_value_t
Definition: copy.hh:39
typename in_expressionset_t::const_visitor super_t
Definition: copy.hh:35
copier(const in_expressionset_t &in_rs, const out_expressionset_t &out_rs)
Definition: copy.hh:49
context_t_of< in_expressionset_t > in_context_t
Definition: copy.hh:38
VCSN_RAT_VISIT(transposition, v)
Definition: copy.hh:101
out_value_t copy(const in_value_t &v)
Easy recursion.
Definition: copy.hh:63
typename out_expressionset_t::value_t out_value_t
Definition: copy.hh:40
Definition: a-star.hh:8
VCSN_RAT_VISIT(sum, v)
Definition: copy.hh:100
VCSN_RAT_VISIT(ldiv, v)
Definition: copy.hh:95
typename super_t::template variadic_t< Type > variadic_t
Definition: copy.hh:46
VCSN_RAT_VISIT(infiltration, v)
Definition: copy.hh:94
out_value_t work_(const tuple_t &v, detail::index_sequence< I... >)
Copy all the tapes.
Definition: copy.hh:145
auto work_(const tuple_t &v)
Copy one tape.
Definition: copy.hh:136
VCSN_RAT_VISIT(zero,)
Definition: copy.hh:102
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:55
An inner node with multiple children.
Definition: expression.hh:118
void copy_(const variadic_t< Type > &v)
Factor the copy of n-ary operations.
Definition: copy.hh:82
void copy_(const unary_t< Type > &v)
Factor the copy of n-ary operations.
Definition: copy.hh:72
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
Functor to copy/convert a rational expression.
Definition: copy.hh:29
typename super_t::tuple_t tuple_t
Definition: copy.hh:128
void visit(const tuple_t &v, std::true_type) override
Definition: copy.hh:168
out_value_t operator()(const tuple_t &)
Definition: copy.hh:161
typename super_t::inner_t inner_t
Definition: copy.hh:42
VCSN_RAT_VISIT(complement, v)
Definition: copy.hh:92
const out_expressionset_t & out_rs_
expressionset to build the output value.
Definition: copy.hh:176
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:53
VCSN_RAT_VISIT(star, v)
Definition: copy.hh:99
typename super_t::template unary_t< Type > unary_t
Definition: copy.hh:44
VCSN_RAT_VISIT(lweight, v)
Definition: copy.hh:110
OutExpSet out_expressionset_t
Definition: copy.hh:34
An inner node implementing a weight.
Definition: expression.hh:264
typename super_t::leaf_t leaf_t
Definition: copy.hh:47
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:183
VCSN_RAT_VISIT(rweight, v)
Definition: copy.hh:117
const in_expressionset_t & in_rs_
expressionset to decode the input value.
Definition: copy.hh:174
out_value_t res_
Output value, under construction.
Definition: copy.hh:178
VCSN_RAT_VISIT(atom, v)
Definition: copy.hh:104
VCSN_RAT_VISIT(prod, v)
Definition: copy.hh:97
InExpSet in_expressionset_t
Definition: copy.hh:33
VCSN_RAT_VISIT(one,)
Definition: copy.hh:96
out_value_t operator()(const in_value_t &v)
Entry point: print v.
Definition: copy.hh:56
typename super_t::node_t node_t
Definition: copy.hh:41
VCSN_RAT_VISIT(conjunction, v)
Definition: copy.hh:93
out_value_t operator()(const tuple_t &v)
Entry point.
Definition: copy.hh:151