Vcsn  2.3
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  auto sub = typename out_expressionset_t::values_t{};
87  for (const auto& s: v)
88  sub.emplace_back(copy(s));
89  res_ = std::make_shared<out_variadic_t>(sub);
90  }
91 
99  VCSN_RAT_VISIT(one,) { res_ = out_rs_.one(); }
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  copy(v.sub()));
116  }
117 
119  {
120  res_ = out_rs_.rweight(copy(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 <bool = in_context_t::is_lat,
132  typename Dummy = void>
133  struct visit_tuple
134  {
136  template <size_t I>
137  auto work_(const tuple_t& v)
138  {
139  return rat::copy(detail::project<I>(visitor_.in_rs_),
140  detail::project<I>(visitor_.out_rs_),
141  std::get<I>(v.sub()));
142  }
143 
145  template <size_t... I>
147  {
148  return visitor_.out_rs_.tuple(work_<I>(v)...);
149  }
150 
153  {
155  }
157  };
158 
159  template <typename Dummy>
160  struct visit_tuple<false, Dummy>
161  {
163  {
165  }
167  };
168 
169  void visit(const tuple_t& v, std::true_type) override
170  {
171  res_ = visit_tuple<>{*this}(v);
172  }
173 
180  };
181 
182  template <typename InExpSet, typename OutExpSet>
183  typename OutExpSet::value_t
184  copy(const InExpSet& in_rs, const OutExpSet& out_rs,
185  const typename InExpSet::value_t& v)
186  {
187  auto copy = copier<InExpSet, OutExpSet>{in_rs, out_rs};
188  return copy(v);
189  }
190  } // namespace rat
191 } // namespace vcsn
VCSN_RAT_VISIT(conjunction, v)
Definition: copy.hh:95
return v
Definition: multiply.hh:361
typename super_t::inner_t inner_t
Definition: copy.hh:42
VCSN_RAT_VISIT(mul, v)
Definition: copy.hh:98
const out_expressionset_t & out_rs_
expressionset to build the output value.
Definition: copy.hh:177
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
VCSN_RAT_VISIT(shuffle, v)
Definition: copy.hh:100
VCSN_RAT_VISIT(complement, v)
Definition: copy.hh:93
VCSN_RAT_VISIT(zero,)
Definition: copy.hh:103
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
out_value_t operator()(const tuple_t &)
Definition: copy.hh:162
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
out_value_t res_
Output value, under construction.
Definition: copy.hh:179
VCSN_RAT_VISIT(atom, v)
Definition: copy.hh:105
Functor to copy/convert a rational expression.
Definition: copy.hh:29
out_value_t operator()(const in_value_t &v)
Entry point: print v.
Definition: copy.hh:56
An inner node implementing a weight.
Definition: expression.hh:264
VCSN_RAT_VISIT(rweight, v)
Definition: copy.hh:118
typename super_t::node_t node_t
Definition: copy.hh:41
VCSN_RAT_VISIT(infiltrate, v)
Definition: copy.hh:96
VCSN_RAT_VISIT(one,)
Definition: copy.hh:99
auto work_(const tuple_t &v)
Copy one tape.
Definition: copy.hh:137
typename in_expressionset_t::const_visitor super_t
Definition: copy.hh:35
Definition: a-star.hh:8
typename super_t::tuple_t tuple_t
Definition: copy.hh:129
VCSN_RAT_VISIT(star, v)
Definition: copy.hh:101
void visit(const tuple_t &v, std::true_type) override
Definition: copy.hh:169
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:102
out_value_t work_(const tuple_t &v, detail::index_sequence< I... >)
Copy all the tapes.
Definition: copy.hh:146
typename in_expressionset_t::value_t in_value_t
Definition: copy.hh:39
OutExpSet out_expressionset_t
Definition: copy.hh:34
out_value_t operator()(const tuple_t &v)
Entry point.
Definition: copy.hh:152
VCSN_RAT_VISIT(ldivide, v)
Definition: copy.hh:97
typename out_expressionset_t::value_t out_value_t
Definition: copy.hh:40
VCSN_RAT_VISIT(add, v)
Definition: copy.hh:92
out_value_t copy(const in_value_t &v)
Easy recursion.
Definition: copy.hh:63
const in_expressionset_t & in_rs_
expressionset to decode the input value.
Definition: copy.hh:175
typename super_t::template variadic_t< Type > variadic_t
Definition: copy.hh:46
InExpSet in_expressionset_t
Definition: copy.hh:33
typename super_t::template unary_t< Type > unary_t
Definition: copy.hh:44
VCSN_RAT_VISIT(lweight, v)
Definition: copy.hh:111
typename super_t::leaf_t leaf_t
Definition: copy.hh:47
An inner node with multiple children.
Definition: expression.hh:118
void copy_(const variadic_t< Type > &v)
Factor the handling of n-ary operations.
Definition: copy.hh:82
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:184
void copy_(const unary_t< Type > &v)
Factor the handling of unary operations.
Definition: copy.hh:72
VCSN_RAT_VISIT(compose, v)
Definition: copy.hh:94