Awali
Another Weighted Automata library
copy.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_CORE_RAT_COPY_HH
18 # define AWALI_CORE_RAT_COPY_HH
19 
20 # include <iostream>
23 #include <awali/sttc/misc/cast.hh>
24 
25 namespace awali { namespace sttc
26 {
27  namespace rat
28  {
29 
30  template <typename InRatExpSet, typename OutRatExpSet = InRatExpSet>
31  class copier
32  : public InRatExpSet::const_visitor
33  {
34  public:
35  using in_ratexpset_t = InRatExpSet;
36  using out_ratexpset_t = OutRatExpSet;
37  using in_value_t = typename in_ratexpset_t::value_t;
38  using out_value_t = typename out_ratexpset_t::value_t;
39  using super_type = typename in_ratexpset_t::const_visitor;
40  using node_t = typename super_type::node_t;
41  using inner_t = typename super_type::inner_t;
42  template <type_t Type>
43  using unary_t = typename super_type::template unary_t<Type>;
44  template <type_t Type>
45  using variadic_t = typename super_type::template variadic_t<Type>;
46  using leaf_t = typename super_type::leaf_t;
47 
48  copier(const in_ratexpset_t& in_rs, const out_ratexpset_t& out_rs)
49  : in_rs_(in_rs)
50  , out_rs_(out_rs)
51  {}
52 
56  {
57  return copy(v);
58  }
59 
60  private:
62  template <exp::type_t Type>
63  void
64  copy_unary(const unary_t<Type>& v)
65  {
66  using out_unary_t = typename out_ratexpset_t::template unary_t<Type>;
67  res_ = std::make_shared<out_unary_t>(copy(v.sub()));
68  }
69 
71  template <exp::type_t Type>
72  void
73  copy_variadic(const variadic_t<Type>& v)
74  {
75  using out_variadic_t = typename out_ratexpset_t::template variadic_t<Type>;
76  typename out_ratexpset_t::ratexps_t sub;
77  for (auto s: v)
78  sub.emplace_back(copy(s));
79  res_ = std::make_shared<out_variadic_t>(sub);
80  }
81 
83  copy(const in_value_t& v)
84  {
85  v->accept(*this);
86  return res_;
87  }
88 
89 # define DEFINE(Type) \
90  using Type ## _t = typename super_type::Type ## _t; \
91  virtual void visit(const Type ## _t& v)
92 
93  DEFINE(conjunction) { copy_variadic(v); }
94  DEFINE(ldiv) { copy_variadic(v); }
95  DEFINE(prod) { copy_variadic(v); }
96  DEFINE(shuffle) { copy_variadic(v); }
97  DEFINE(sum) { copy_variadic(v); }
98 
99  DEFINE(complement) { copy_unary(v); }
100  DEFINE(star) { copy_unary(v); }
101  DEFINE(transposition){ copy_unary(v); }
102 
103  DEFINE(one)
104  {
105  (void) v;
106  res_ = out_rs_.one();
107  }
108 
109  DEFINE(zero)
110  {
111  (void) v;
112  res_ = out_rs_.zero();
113  }
114 
115  DEFINE(atom)
116  {
117  res_ = out_rs_.atom(out_rs_.labelset()->conv(*in_rs_.labelset(),
118  v.value()));
119  }
120 
121  DEFINE(lweight)
122  {
123  res_ = out_rs_.lmul(out_rs_.weightset()->conv(*in_rs_.weightset(),
124  v.weight()),
125  copy(v.sub()));
126  }
127 
128  DEFINE(rweight)
129  {
130  res_ = out_rs_.rmul(copy(v.sub()),
131  out_rs_.weightset()->conv(*in_rs_.weightset(),
132  v.weight()));
133  }
134 
135 
136 # undef DEFINE
138  const in_ratexpset_t& in_rs_;
140  const out_ratexpset_t& out_rs_;
142  out_value_t res_;
143  };
144 
145  template <typename InRatExpSet, typename OutRatExpSet = InRatExpSet>
146  typename OutRatExpSet::value_t
147  copy(const InRatExpSet& in_rs, const OutRatExpSet& out_rs,
148  const typename InRatExpSet::value_t& v)
149  {
151  return copy(v);
152  }
153 
154  } // namespace rat
155 }}//end of ns awali::stc
156 
157 #endif // !AWALI_CORE_RAT_COPY_HH
Definition: copy.hh:33
typename in_ratexpset_t::const_visitor super_type
Definition: copy.hh:39
typename super_type::node_t node_t
Definition: copy.hh:40
typename super_type::template unary_t< Type > unary_t
Definition: copy.hh:43
copier(const in_ratexpset_t &in_rs, const out_ratexpset_t &out_rs)
Definition: copy.hh:48
InRatExpSet in_ratexpset_t
Definition: copy.hh:35
OutRatExpSet out_ratexpset_t
Definition: copy.hh:36
typename super_type::template variadic_t< Type > variadic_t
Definition: copy.hh:45
typename out_ratexpset_t::value_t out_value_t
Definition: copy.hh:38
typename super_type::inner_t inner_t
Definition: copy.hh:41
out_value_t operator()(const in_value_t &v)
Entry point: print v.
Definition: copy.hh:55
typename super_type::leaf_t leaf_t
Definition: copy.hh:46
typename in_ratexpset_t::value_t in_value_t
Definition: copy.hh:37
#define DEFINE(Type)
Definition: copy.hh:89
variadic< type_t::sum, Label, Weight > sum
Definition: fwd.hh:154
weight_node< type_t::lweight, Label, Weight > lweight
Definition: fwd.hh:164
unary< type_t::star, Label, Weight > star
Definition: fwd.hh:129
constant< type_t::zero, Label, Weight > zero
Definition: fwd.hh:113
variadic< type_t::ldiv, Label, Weight > ldiv
Definition: fwd.hh:148
variadic< type_t::conjunction, Label, Weight > conjunction
Definition: fwd.hh:145
variadic< type_t::shuffle, Label, Weight > shuffle
Definition: fwd.hh:151
weight_node< type_t::rweight, Label, Weight > rweight
Definition: fwd.hh:167
OutRatExpSet::value_t copy(const InRatExpSet &in_rs, const OutRatExpSet &out_rs, const typename InRatExpSet::value_t &v)
Definition: copy.hh:147
unary< type_t::transposition, Label, Weight > transposition
Definition: fwd.hh:132
constant< type_t::one, Label, Weight > one
Definition: fwd.hh:116
unary< type_t::complement, Label, Weight > complement
Definition: fwd.hh:126
variadic< type_t::prod, Label, Weight > prod
Definition: fwd.hh:142
Main namespace of Awali.
Definition: ato.hh:22