Awali
Another Weighted Automata library
star_height.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_STAR_HEIGHT_HH
18 # define AWALI_ALGOS_STAR_HEIGHT_HH
19 
21 
22 namespace awali { namespace sttc {
23 
24  namespace internal
25  {
26  template <typename RatExpSet>
28  : public RatExpSet::const_visitor
29  {
30  public:
31  using ratexpset_t = RatExpSet;
32  using super_type = typename ratexpset_t::const_visitor;
33  using node_t = typename super_type::node_t;
34  template <rat::type_t Type>
35  using variadic_t = typename super_type::template variadic_t<Type>;
36 
38  unsigned
39  operator()(const node_t& v)
40  {
41  height_ = 0;
42  v.accept(*this);
43  return height_;
44  }
45 
47  unsigned
48  operator()(const std::shared_ptr<const node_t>& v)
49  {
50  return operator()(*v);
51  }
52 
53  private:
54 
55 # define DEFINE(Type) \
56  using Type ## _t = typename super_type::Type ## _t; \
57  virtual void visit(const Type ## _t& v) override
58 
59  DEFINE(atom) { (void) v; }
60  DEFINE(complement) { v.sub()->accept(*this); }
61  DEFINE(conjunction) { visit_variadic(v); }
62  DEFINE(ldiv) { visit_variadic(v); }
63  DEFINE(lweight) { v.sub()->accept(*this); }
64  DEFINE(one) { (void) v; }
65  DEFINE(prod) { visit_variadic(v); }
66  DEFINE(rweight) { v.sub()->accept(*this); }
67  DEFINE(shuffle) { visit_variadic(v); }
68  DEFINE(star) { ++height_; v.sub()->accept(*this); }
69  DEFINE(sum) { visit_variadic(v); }
70  DEFINE(transposition){ v.sub()->accept(*this); }
71  DEFINE(zero) { (void) v; }
72 
73 # undef DEFINE
74 
76  template <rat::type_t Type>
77  void
78  visit_variadic(const variadic_t<Type>& n)
79  {
80  /* The height of an n-ary is the max of the n heights. */
81  auto max = height_;
82  auto initial = height_;
83  for (auto child : n)
84  {
85  height_ = initial;
86  child->accept(*this);
87  if (max < height_)
88  max = height_;
89  }
90  height_ = max;
91  }
92 
93  unsigned height_;
94  };
95  } // namespace internal
96 
97 
99  template <typename RatExpSet>
100  inline
101  unsigned
102  star_height(const typename RatExpSet::ratexp_t& e)
103  {
105  return s(e);
106  }
107 
108 }}//end of ns awali::stc
109 
110 #endif // !AWALI_ALGOS_STAR_HEIGHT_HH
Definition: star_height.hh:29
typename ratexpset_t::const_visitor super_type
Definition: star_height.hh:32
typename super_type::node_t node_t
Definition: star_height.hh:33
unsigned operator()(const node_t &v)
Entry point: return the size of v.
Definition: star_height.hh:39
unsigned operator()(const std::shared_ptr< const node_t > &v)
Entry point: return the size of v.
Definition: star_height.hh:48
typename super_type::template variadic_t< Type > variadic_t
Definition: star_height.hh:35
RatExpSet ratexpset_t
Definition: star_height.hh:31
unsigned star_height(const typename RatExpSet::ratexp_t &e)
Star height of a ratexp.
Definition: star_height.hh:102
ATTRIBUTE_CONST int max(int a, int b)
Definition: arith.hh:54
Main namespace of Awali.
Definition: ato.hh:22
#define DEFINE(Type)
Definition: star_height.hh:55