Vcsn  2.3
Be Rational
project.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 
6 
7 namespace vcsn
8 {
9  namespace rat
10  {
15  template <typename InExpSet, size_t Tape>
17  : public InExpSet::const_visitor
18  {
19  public:
20  using in_expressionset_t = InExpSet;
23  = typename in_expressionset_t::template project_t<Tape>;
24  using super_t = typename in_expressionset_t::const_visitor;
26 
27  using in_expression_t = typename in_expressionset_t::value_t;
28  using out_expression_t = typename out_expressionset_t::value_t;
29  using node_t = typename super_t::node_t;
30  using inner_t = typename super_t::inner_t;
31  template <type_t Type>
32  using unary_t = typename super_t::template unary_t<Type>;
33  template <type_t Type>
34  using variadic_t = typename super_t::template variadic_t<Type>;
35  using leaf_t = typename super_t::leaf_t;
36 
38  : in_rs_(in_rs)
39  , out_rs_(::vcsn::detail::project<Tape>(in_rs_.context()))
40  {}
41 
45  {
46  return project(v);
47  }
48 
49  private:
52  {
53  v->accept(*this);
54  return res_;
55  }
56 
58  template <exp::type_t Type>
59  void
60  project_(const unary_t<Type>& v)
61  {
62  using out_unary_t
63  = typename out_expressionset_t::template unary_t<Type>;
64  res_ = std::make_shared<out_unary_t>(project(v.sub()));
65  }
66 
68  template <exp::type_t Type, typename Fun>
69  void
70  project_(const variadic_t<Type>& v, Fun&& fun)
71  {
72  // Be sure to apply the identities: merely projecting on 0 in
73  // `a|b+a|b` would result in `a+a`.
74  auto res = project(v.head());
75  for (const auto& c: v.tail())
76  // FIXME: C++17: invoke.
77  res = (out_rs_.*fun)(res, project(c));
78  res_ = std::move(res);
79  }
80 
85  VCSN_RAT_VISIT(one,) { res_ = out_rs_.one(); }
86  // I do not understand why I need the cast just for this one,
87  // but GCC and Clang agree.
88  using bin_t =
92  static_cast<bin_t>(&out_expressionset_t::mul)); }
97  VCSN_RAT_VISIT(zero,) { res_ = out_rs_.zero(); }
98 
100  {
101  res_ = out_rs_.atom(std::get<Tape>(v.value()));
102  }
103 
105  {
106  res_ = out_rs_.lweight(v.weight(), project(v.sub()));
107  }
108 
110  {
111  res_ = out_rs_.rweight(project(v.sub()), v.weight());
112  }
113 
114  /*---------.
115  | tuple. |
116  `---------*/
117 
118  using tuple_t = typename super_t::tuple_t;
119 
120  template <bool = in_context_t::is_lat,
121  typename Dummy = void>
122  struct visit_tuple
123  {
126  {
127  return std::get<Tape>(v.sub());
128  }
130  };
131 
132  template <typename Dummy>
133  struct visit_tuple<false, Dummy>
134  {
136  {
138  }
140  };
141 
142  void visit(const tuple_t& v, std::true_type) override
143  {
144  res_ = visit_tuple<>{*this}(v);
145  }
146 
153  };
154 
155 
160  template <size_t Tape, typename InExpSet>
161  auto
162  project(const InExpSet& in_rs, const typename InExpSet::value_t& v)
164  {
165  auto p = project_impl<InExpSet, Tape>{in_rs};
166  return p(v);
167  }
168  } // namespace rat
169 } // namespace vcsn
return v
Definition: multiply.hh:361
typename out_expressionset_t::value_t out_expression_t
Definition: project.hh:28
VCSN_RAT_VISIT(infiltrate, v)
Definition: project.hh:83
VCSN_RAT_VISIT(ldivide, v)
Definition: project.hh:84
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
out_expression_t operator()(const in_expression_t &v)
Entry point: print v.
Definition: project.hh:44
VCSN_RAT_VISIT(complement, v)
Definition: project.hh:81
typename super_t::leaf_t leaf_t
Definition: project.hh:35
typename in_expressionset_t::value_t in_expression_t
Definition: project.hh:27
out_expressionset_t out_rs_
expressionset to build the output value.
Definition: project.hh:150
Functor to project a rational expression.
Definition: project.hh:16
VCSN_RAT_VISIT(rweight, v)
Definition: project.hh:109
void project_(const unary_t< Type > &v)
Factor the handling of unary operations.
Definition: project.hh:60
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
typename super_t::tuple_t tuple_t
Definition: project.hh:118
typename super_t::template unary_t< Type > unary_t
Definition: project.hh:32
out_expression_t project(const in_expression_t &v)
Easy recursion.
Definition: project.hh:51
An inner node implementing a weight.
Definition: expression.hh:264
VCSN_RAT_VISIT(atom, v)
Definition: project.hh:99
return res
Definition: multiply.hh:398
VCSN_RAT_VISIT(lweight, v)
Definition: project.hh:104
out_expression_t(out_expressionset_t::*)(const out_expression_t &, const out_expression_t &) const bin_t
Definition: project.hh:90
out_expression_t operator()(const tuple_t &v)
Entry point.
Definition: project.hh:125
typename super_t::inner_t inner_t
Definition: project.hh:30
Definition: a-star.hh:8
typename super_t::template variadic_t< Type > variadic_t
Definition: project.hh:34
VCSN_RAT_VISIT(shuffle, v)
Definition: project.hh:93
const in_expressionset_t & in_rs_
expressionset to decode the input value.
Definition: project.hh:148
project_impl(const in_expressionset_t &in_rs)
Definition: project.hh:37
InExpSet in_expressionset_t
Definition: project.hh:20
VCSN_RAT_VISIT(transposition, v)
Definition: project.hh:96
void project_(const variadic_t< Type > &v, Fun &&fun)
Factor the handling of n-ary operations.
Definition: project.hh:70
auto project(const InExpSet &in_rs, const typename InExpSet::value_t &v) -> typename project_impl< InExpSet, Tape >::out_expression_t
Project a rational expression.
Definition: project.hh:162
typename super_t::node_t node_t
Definition: project.hh:29
VCSN_RAT_VISIT(conjunction, v)
Definition: project.hh:82
out_expression_t res_
Output value, under construction.
Definition: project.hh:152
typename in_expressionset_t::template project_t< Tape > out_expressionset_t
Definition: project.hh:23
An inner node with multiple children.
Definition: expression.hh:118
context_t_of< in_expressionset_t > in_context_t
Definition: project.hh:21
VCSN_RAT_VISIT(star, v)
Definition: project.hh:94
out_expression_t operator()(const tuple_t &)
Definition: project.hh:135
typename in_expressionset_t::const_visitor super_t
Definition: project.hh:24
void visit(const tuple_t &v, std::true_type) override
Definition: project.hh:142