Vcsn  2.4
Be Rational
partial-identity.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,
20  typename OutExpSet = InExpSet>
21  typename OutExpSet::value_t
22  partial_identity(const InExpSet& in_rs, const OutExpSet& out_rs,
23  const typename InExpSet::value_t& v);
24 
29  template <typename InExpSet, typename OutExpSet = InExpSet>
31  : public InExpSet::const_visitor
32  {
33  public:
34  using in_expressionset_t = InExpSet;
35  using out_expressionset_t = OutExpSet;
36  using super_t = typename in_expressionset_t::const_visitor;
38 
40  using in_value_t = typename in_expressionset_t::value_t;
41  using out_value_t = typename out_expressionset_t::value_t;
42  using node_t = typename super_t::node_t;
43  using inner_t = typename super_t::inner_t;
44  template <type_t Type>
45  using unary_t = typename super_t::template unary_t<Type>;
46  template <type_t Type>
47  using variadic_t = typename super_t::template variadic_t<Type>;
48  using leaf_t = typename super_t::leaf_t;
49 
50  using out_expression_t = typename out_expressionset_t::value_t;
51 
53  const out_expressionset_t& out_rs)
54  : in_rs_{in_rs}
55  , out_rs_{out_rs}
56  {}
57 
61  {
62  return recurse(v);
63  }
64 
65  private:
68  {
69  v->accept(*this);
70  return res_;
71  }
72 
74  template <exp::type_t Type>
75  void
76  recurse_(const unary_t<Type>& v)
77  {
78  using out_unary_t
79  = typename out_expressionset_t::template unary_t<Type>;
80  res_ = std::make_shared<out_unary_t>(recurse(v.sub()));
81  }
82 
84  template <exp::type_t Type, typename Fun>
85  void
86  recurse_(const variadic_t<Type>& v, Fun&& fun)
87  {
88  using out_variadic_t
89  = typename out_expressionset_t::template variadic_t<Type>;
90  auto sub = typename out_expressionset_t::values_t{};
91  for (const auto& s: v)
92  sub.emplace_back(recurse(s));
93  res_ = std::make_shared<out_variadic_t>(sub);
94  }
95 
102  using bin_t =
106  static_cast<bin_t>(&out_expressionset_t::mul)); }
111  VCSN_RAT_VISIT(zero,) { res_ = out_rs_.zero(); }
112 
114  {
115  res_ = out_rs_.atom(out_rs_.labelset()->tuple(v.value(), v.value()));
116  }
117 
119  {
120  res_ = out_rs_.lweight(out_rs_.weightset()->conv(*in_rs_.weightset(),
121  v.weight()),
122  recurse(v.sub()));
123  }
124 
126  {
127  res_ = out_rs_.rweight(recurse(v.sub()),
128  out_rs_.weightset()->conv(*in_rs_.weightset(),
129  v.weight()));
130  }
131 
132  /*---------.
133  | tuple. |
134  `---------*/
135 
136  using tuple_t = typename super_t::tuple_t;
137 
138  template <typename = void>
139  struct visit_tuple
140  {
142  template <size_t I>
143  auto work_(const tuple_t& v)
144  {
145  return rat::partial_identity(detail::project<I>(visitor_.in_rs_),
146  detail::project<I>(visitor_.out_rs_),
147  std::get<I>(v.sub()));
148  }
149 
151  template <size_t... I>
153  {
154  return visitor_.out_rs_.tuple(work_<I>(v)...);
155  }
156 
159  {
161  }
163  };
164 
165  void visit(const tuple_t& v, std::true_type) override
166  {
167  detail::static_if<in_context_t::is_lat>
168  ([this](auto&& v){ res_ = visit_tuple<decltype(v)>{*this}(v); })
169  (v);
170  }
171 
178  };
179 
184  template <typename InExpSet, typename OutExpSet>
185  typename OutExpSet::value_t
186  partial_identity(const InExpSet& in_rs, const OutExpSet& out_rs,
187  const typename InExpSet::value_t& v)
188  {
189  auto pid = partial_identity_impl<InExpSet, OutExpSet>{in_rs, out_rs};
190  return pid(v);
191  }
192  } // namespace rat
193 } // namespace vcsn
auto work_(const tuple_t &v)
Partial_Identity one tape.
void recurse_(const variadic_t< Type > &v, Fun &&fun)
Factor the handling of n-ary operations.
typename super_t::template unary_t< Type > unary_t
out_value_t operator()(const in_value_t &v)
Entry point: print v.
out_value_t recurse(const in_value_t &v)
Easy recursion.
Functor to partial_identity/convert a rational expression.
An inner node implementing a weight.
Definition: expression.hh:255
typename super_t::inner_t inner_t
typename in_expressionset_t::const_visitor super_t
OutExpSet::value_t partial_identity(const InExpSet &in_rs, const OutExpSet &out_rs, const typename InExpSet::value_t &v)
Partial identity of a rational expression.
out_value_t work_(const tuple_t &v, detail::index_sequence< I... >)
Partial_Identity all the tapes.
void recurse_(const unary_t< Type > &v)
Factor the handling of unary operations.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
typename super_t::template variadic_t< Type > variadic_t
out_value_t res_
Output value, under construction.
out_value_t operator()(const tuple_t &v)
Entry point.
Definition: a-star.hh:8
typename in_expressionset_t::value_t in_value_t
context_t_of< in_expressionset_t > in_context_t
const out_expressionset_t & out_rs_
expressionset to build the output value.
typename super_t::tuple_t tuple_t
typename super_t::leaf_t leaf_t
partial_identity_impl(const in_expressionset_t &in_rs, const out_expressionset_t &out_rs)
typename out_expressionset_t::value_t out_expression_t
An inner node with multiple children.
Definition: expression.hh:118
return v
Definition: multiply.hh:361
const in_expressionset_t & in_rs_
expressionset to decode the input value.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
typename out_expressionset_t::value_t out_value_t
out_expression_t(out_expressionset_t::*)(const out_expression_t &, const out_expression_t &) const bin_t
void visit(const tuple_t &v, std::true_type) override
typename super_t::node_t node_t