Vcsn  2.3a
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 <bool = in_context_t::is_lat,
139  typename Dummy = void>
140  struct visit_tuple
141  {
143  template <size_t I>
144  auto work_(const tuple_t& v)
145  {
146  return rat::partial_identity(detail::project<I>(visitor_.in_rs_),
147  detail::project<I>(visitor_.out_rs_),
148  std::get<I>(v.sub()));
149  }
150 
152  template <size_t... I>
154  {
155  return visitor_.out_rs_.tuple(work_<I>(v)...);
156  }
157 
160  {
162  }
164  };
165 
166  template <typename Dummy>
167  struct visit_tuple<false, Dummy>
168  {
170  {
172  }
174  };
175 
176  void visit(const tuple_t& v, std::true_type) override
177  {
178  res_ = visit_tuple<>{*this}(v);
179  }
180 
187  };
188 
193  template <typename InExpSet, typename OutExpSet>
194  typename OutExpSet::value_t
195  partial_identity(const InExpSet& in_rs, const OutExpSet& out_rs,
196  const typename InExpSet::value_t& v)
197  {
198  auto pid = partial_identity_impl<InExpSet, OutExpSet>{in_rs, out_rs};
199  return pid(v);
200  }
201  } // namespace rat
202 } // namespace vcsn
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.
return v
Definition: multiply.hh:361
Definition: a-star.hh:8
Functor to partial_identity/convert a rational expression.
typename super_t::inner_t inner_t
typename in_expressionset_t::const_visitor super_t
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.
An inner node implementing a weight.
Definition: expression.hh:264
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.
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.
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)
auto work_(const tuple_t &v)
Partial_Identity one tape.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
typename out_expressionset_t::value_t out_expression_t
An inner node with multiple children.
Definition: expression.hh:118
const in_expressionset_t & in_rs_
expressionset to decode the input value.
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
out_value_t operator()(const tuple_t &v)
Entry point.
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
out_value_t work_(const tuple_t &v, detail::index_sequence< I... >)
Partial_Identity all the tapes.