Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
copy.hh
Go to the documentation of this file.
1 #ifndef VCSN_CORE_RAT_COPY_HH
2 # define VCSN_CORE_RAT_COPY_HH
3 
4 # include <iostream>
5 # include <vcsn/core/rat/visitor.hh>
6 # include <vcsn/misc/attributes.hh>
7 # include <vcsn/misc/cast.hh>
8 
9 namespace vcsn
10 {
11  namespace rat
12  {
13 
14  template <typename InRatExpSet, typename OutRatExpSet = InRatExpSet>
15  class copier
16  : public InRatExpSet::const_visitor
17  {
18  public:
19  using in_ratexpset_t = InRatExpSet;
20  using out_ratexpset_t = OutRatExpSet;
21  using in_value_t = typename in_ratexpset_t::value_t;
22  using out_value_t = typename out_ratexpset_t::value_t;
23  using super_t = typename in_ratexpset_t::const_visitor;
24  using node_t = typename super_t::node_t;
25  using inner_t = typename super_t::inner_t;
26  template <type_t Type>
27  using unary_t = typename super_t::template unary_t<Type>;
28  template <type_t Type>
29  using variadic_t = typename super_t::template variadic_t<Type>;
30  using leaf_t = typename super_t::leaf_t;
31 
32  copier(const in_ratexpset_t& in_rs, const out_ratexpset_t& out_rs)
33  : in_rs_(in_rs)
34  , out_rs_(out_rs)
35  {}
36 
40  {
41  return copy(v);
42  }
43 
44  private:
46  template <exp::type_t Type>
47  void
49  {
50  using out_unary_t = typename out_ratexpset_t::template unary_t<Type>;
51  res_ = std::make_shared<out_unary_t>(copy(v.sub()));
52  }
53 
55  template <exp::type_t Type>
56  void
58  {
59  using out_variadic_t = typename out_ratexpset_t::template variadic_t<Type>;
60  typename out_ratexpset_t::values_t sub;
61  for (auto s: v)
62  sub.emplace_back(copy(s));
63  res_ = std::make_shared<out_variadic_t>(sub);
64  }
65 
67  copy(const in_value_t& v)
68  {
69  v->accept(*this);
70  return res_;
71  }
72 
73 # define DEFINE(Type) \
74  using Type ## _t = typename super_t::Type ## _t; \
75  virtual void visit(const Type ## _t& v)
76 
82 
86 
88  {
89  (void) v;
90  res_ = out_rs_.one();
91  }
92 
94  {
95  (void) v;
96  res_ = out_rs_.zero();
97  }
98 
100  {
101  res_ = out_rs_.atom(out_rs_.labelset()->conv(*in_rs_.labelset(),
102  v.value()));
103  }
104 
106  {
107  res_ = out_rs_.lmul(out_rs_.weightset()->conv(*in_rs_.weightset(),
108  v.weight()),
109  copy(v.sub()));
110  }
111 
113  {
114  res_ = out_rs_.rmul(copy(v.sub()),
115  out_rs_.weightset()->conv(*in_rs_.weightset(),
116  v.weight()));
117  }
118 
119 
120 # undef DEFINE
121  const in_ratexpset_t& in_rs_;
127  };
128 
129  template <typename InRatExpSet, typename OutRatExpSet = InRatExpSet>
130  typename OutRatExpSet::value_t
131  copy(const InRatExpSet& in_rs, const OutRatExpSet& out_rs,
132  const typename InRatExpSet::value_t& v)
133  {
135  return copy(v);
136  }
137 
138  } // namespace rat
139 } // namespace vcsn
140 
141 #endif // !VCSN_CORE_RAT_COPY_HH
typename in_ratexpset_t::const_visitor super_t
Definition: copy.hh:23
An inner node with multiple children.
Definition: fwd.hh:123
typename super_t::node_t node_t
Definition: copy.hh:24
OutRatExpSet::value_t copy(const InRatExpSet &in_rs, const OutRatExpSet &out_rs, const typename InRatExpSet::value_t &v)
Definition: copy.hh:131
#define DEFINE(Type)
Definition: copy.hh:73
typename super_t::leaf_t leaf_t
Definition: copy.hh:30
void copy_variadic(const variadic_t< Type > &v)
Factor the copy of n-ary operations.
Definition: copy.hh:57
typename in_ratexpset_t::value_t in_value_t
Definition: copy.hh:21
const in_ratexpset_t & in_rs_
ratexpset to decode the input value.
Definition: copy.hh:122
const out_ratexpset_t & out_rs_
ratexpset to build the output value.
Definition: copy.hh:124
typename super_t::template variadic_t< Type > variadic_t
Definition: copy.hh:29
typename out_ratexpset_t::value_t out_value_t
Definition: copy.hh:22
OutRatExpSet out_ratexpset_t
Definition: copy.hh:20
InRatExpSet in_ratexpset_t
Definition: copy.hh:19
copier(const in_ratexpset_t &in_rs, const out_ratexpset_t &out_rs)
Definition: copy.hh:32
typename super_t::template unary_t< Type > unary_t
Definition: copy.hh:27
out_value_t res_
Output value, under construction.
Definition: copy.hh:126
An inner node implementing a weight.
Definition: fwd.hh:145
void copy_unary(const unary_t< Type > &v)
Factor the copy of n-ary operations.
Definition: copy.hh:48
out_value_t copy(const in_value_t &v)
Definition: copy.hh:67
typename super_t::inner_t inner_t
Definition: copy.hh:25
out_value_t operator()(const in_value_t &v)
Entry point: print v.
Definition: copy.hh:39