Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
star-normal-form.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_STAR_NORMAL_FORM_HH
2 # define VCSN_ALGOS_STAR_NORMAL_FORM_HH
3 
5 # include <vcsn/core/rat/visitor.hh>
6 # include <vcsn/ctx/fwd.hh>
7 # include <vcsn/dyn/ratexp.hh>
8 # include <vcsn/misc/raise.hh>
9 
10 namespace vcsn
11 {
12 
13  namespace rat
14  {
15 
16  /*---------------------------.
17  | star_normal_form(ratexp). |
18  `---------------------------*/
19 
26  template <typename RatExpSet>
28  : public RatExpSet::const_visitor
29  {
30  public:
31  using ratexpset_t = RatExpSet;
32  using ratexp_t = typename ratexpset_t::value_t;
35  using weight_t = typename weightset_t::value_t;
36 
37  using super_t = typename RatExpSet::const_visitor;
38 
39  constexpr static const char* me() { return "star_normal_form"; }
40 
42  enum operation_t { dot, box };
43 
45  : rs_(rs)
46  {}
47 
48  ratexp_t
49  operator()(const ratexp_t& v)
50  {
51  operation_ = dot;
52  v->accept(*this);
53  return std::move(res_);
54  }
55 
57  {
58  res_ = rs_.zero();
59  }
60 
62  {
63  res_ = operation_ == box ? rs_.zero() : rs_.one();
64  }
65 
67  {
68  res_ = rs_.atom(v.value());
69  }
70 
71  // Plain traversal for sums.
73  {
74  v.head()->accept(*this);
75  ratexp_t res = res_;
76  for (auto c: v.tail())
77  {
78  c->accept(*this);
79  res = rs_.add(res, res_);
80  }
81  res_ = std::move(res);
82  }
83 
89 
91  {
92  if (operation_ == box)
93  box_of(v);
94  else
95  dot_of(v);
96  }
97 
99  void box_of(const prod_t& v)
100  {
101  if (std::any_of(std::begin(v), std::end(v),
102  [this](const ratexp_t& n)
103  {
104  return ws_.is_zero(constant_term(rs_, n));
105  }))
106  {
107  // Some factor has a null constant-term.
108  operation_ = dot;
109  dot_of(v);
110  operation_ = box;
111  }
112  else
113  {
114  // All the factors have a non null constant-term.
115  v.head()->accept(*this);
116  ratexp_t res = res_;
117  for (auto c: v.tail())
118  {
119  c->accept(*this);
120  res = rs_.add(res, res_);
121  }
122  res_ = std::move(res);
123  }
124  }
125 
127  void dot_of(const prod_t& v)
128  {
129  v.head()->accept(*this);
130  ratexp_t res = res_;
131  for (auto c: v.tail())
132  {
133  c->accept(*this);
134  res = rs_.mul(res, res_);
135  }
136  res_ = std::move(res);
137  }
138 
140  {
141  if (operation_ == dot)
142  {
143  operation_ = box;
144  v.sub()->accept(*this);
145  res_ = rs_.star(res_);
146  res_ = rs_.lmul(ws_.star(constant_term(rs_, v.sub())), res_);
147  operation_ = dot;
148  }
149  else
150  {
151  v.sub()->accept(*this);
152  }
153  }
154 
156  {
157  v.sub()->accept(*this);
158  res_ = rs_.lmul(v.weight(), std::move(res_));
159  }
160 
162  {
163  v.sub()->accept(*this);
164  res_ = rs_.rmul(std::move(res_), v.weight());
165  }
166 
167  private:
170  weightset_t ws_ = *rs_.weightset();
175  };
176 
177  } // rat::
178 
180  template <typename RatExpSet>
181  typename RatExpSet::value_t
182  star_normal_form(const RatExpSet& rs, const typename RatExpSet::value_t& e)
183  {
185  return star_normal_form(e);
186  }
187 
188  namespace dyn
189  {
190  namespace detail
191  {
193  template <typename RatExpSet>
194  ratexp
196  {
197  const auto& e = exp->as<RatExpSet>();
198  return make_ratexp(e.ratexpset(),
199  ::vcsn::star_normal_form(e.ratexpset(), e.ratexp()));
200  }
201 
203  }
204  }
205 
206 } // vcsn::
207 
208 #endif // !VCSN_ALGOS_STAR_NORMAL_FORM_HH
ratexp make_ratexp(const RatExpSet &rs, const typename RatExpSet::value_t &ratexp)
Definition: ratexp.hh:85
An inner node with multiple children.
Definition: fwd.hh:123
#define VCSN_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:54
weightset_t ws_
Shorthand to the weightset.
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
typename RatExpSet::const_visitor super_t
static constexpr const char * me()
RatExpSet::value_t star_normal_form(const RatExpSet &rs, const typename RatExpSet::value_t &e)
Star_Normal_Forming a typed ratexp shared_ptr.
std::shared_ptr< detail::ratexp_base > ratexp
Definition: fwd.hh:64
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:32
void box_of(const prod_t &v)
Handling of a product by the box operator.
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
context_t_of< ratexpset_t > context_t
ratexp star_normal_form(const ratexp &exp)
Bridge.
operation_t operation_
The current operation.
star_normal_form_visitor(const ratexpset_t &rs)
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 weightset_t::value_t weight_t
operation_t
The type of the operator.
typename ratexpset_t::value_t ratexp_t
weightset_t_of< context_t > weightset_t
void dot_of(const prod_t &v)
Handling of a product by the dot operator.
ratexp_t operator()(const ratexp_t &v)