Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
constant-term.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_CONSTANT_TERM_HH
2 # define VCSN_ALGOS_CONSTANT_TERM_HH
3 
4 # include <set>
5 
6 # include <vcsn/ctx/fwd.hh>
7 # include <vcsn/ctx/traits.hh>
8 # include <vcsn/core/rat/visitor.hh>
9 # include <vcsn/dyn/ratexp.hh>
10 # include <vcsn/dyn/weight.hh>
11 
12 namespace vcsn
13 {
14 
15  namespace rat
16  {
17 
18  /*------------------------.
19  | constant_term(ratexp). |
20  `------------------------*/
21 
23  template <typename RatExpSet>
25  : public RatExpSet::const_visitor
26  {
27  public:
28  using ratexpset_t = RatExpSet;
30  using ratexp_t = typename ratexpset_t::value_t;
33 
34  using super_t = typename ratexpset_t::const_visitor;
35 
36  constexpr static const char* me() { return "constant_term"; }
37 
39  : ws_(*rs.weightset())
40  {}
41 
42  weight_t
43  operator()(const ratexp_t& v)
44  {
45  v->accept(*this);
46  return std::move(res_);
47  }
48 
51  {
52  v->accept(*this);
53  return std::move(res_);
54  }
55 
57  {
58  res_ = ws_.zero();
59  }
60 
62  {
63  res_ = ws_.one();
64  }
65 
67  {
68  res_ = ws_.zero();
69  }
70 
72  {
73  weight_t res = ws_.zero();
74  for (auto c: v)
75  res = ws_.add(res, constant_term(c));
76  res_ = std::move(res);
77  }
78 
80  {
81  weight_t res = ws_.one();
82  for (auto c: v)
83  res = ws_.mul(res, constant_term(c));
84  res_ = std::move(res);
85  }
86 
89 
91  {
92  // FIXME: Code duplication with prod_t.
93  weight_t res = ws_.one();
94  for (auto c: v)
95  res = ws_.mul(res, constant_term(c));
96  res_ = std::move(res);
97  }
98 
100  {
101  // FIXME: Code duplication with prod_t.
102  weight_t res = ws_.one();
103  for (auto c: v)
104  res = ws_.mul(res, constant_term(c));
105  res_ = std::move(res);
106  }
107 
109  {
110  res_ = ws_.star(constant_term(v.sub()));
111  }
112 
114  {
115  v.sub()->accept(*this);
116  res_ = ws_.mul(v.weight(), constant_term(v.sub()));
117  }
118 
120  {
121  v.sub()->accept(*this);
122  res_ = ws_.mul(constant_term(v.sub()), v.weight());
123  }
124 
126  {
127  res_
128  = ws_.is_zero(constant_term(v.sub()))
129  ? ws_.one()
130  : ws_.zero();
131  }
132 
133  private:
134  //ratexpset_t ws_;
137  };
138 
139  } // rat::
140 
141  template <typename RatExpSet>
143  constant_term(const RatExpSet& rs, const typename RatExpSet::value_t& e)
144  {
146  return constant_term(e);
147  }
148 
149  namespace dyn
150  {
151  namespace detail
152  {
153  /*--------------------------.
154  | dyn::constant_term(exp). |
155  `--------------------------*/
156  template <typename RatExpSet>
157  weight
158  constant_term(const ratexp& exp)
159  {
160  const auto& e = exp->as<RatExpSet>();
161  return make_weight(*e.ratexpset().weightset(),
162  constant_term<RatExpSet>(e.ratexpset(),
163  e.ratexp()));
164  }
165 
167  }
168  }
169 
170 } // vcsn::
171 
172 #endif // !VCSN_ALGOS_CONSTANT_TERM_HH
typename ratexpset_t::const_visitor super_t
weight_t_of< ratexpset_t > weight_t
An inner node with multiple children.
Definition: fwd.hh:123
#define VCSN_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:54
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
weight constant_term(const ratexp &exp)
std::shared_ptr< const detail::weight_base > weight
Definition: fwd.hh:82
std::shared_ptr< detail::ratexp_base > ratexp
Definition: fwd.hh:64
weight_t constant_term(const ratexp_t &v)
Easy recursion.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:32
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
context_t_of< ratexpset_t > context_t
weight make_weight(const WeightSet &ws, const typename WeightSet::value_t &w)
Definition: weight.hh:82
An inner node implementing a weight.
Definition: fwd.hh:145
weight_t_of< RatExpSet > constant_term(const RatExpSet &rs, const typename RatExpSet::value_t &e)
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:37
typename ratexpset_t::value_t ratexp_t
static constexpr const char * me()
weight_t operator()(const ratexp_t &v)
weightset_t_of< ratexpset_t > weightset_t
constant_term_visitor(const ratexpset_t &rs)