Awali
Another Weighted Automata library
ratexp.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_CORE_RAT_RATEXP_HH
18 # define AWALI_CORE_RAT_RATEXP_HH
19 
20 # include <vector>
21 # include <string>
22 
23 //# include <boost/range.hpp> // make_iterator_range
24 #include <awali/sttc/misc/cont_filter.hh> // it_cont
27 
28 namespace awali { namespace sttc
29 {
30  namespace rat
31  {
32 
35  class exp
36  {
37  public:
38 #ifndef COVERAGE
39  virtual ~exp() = default;
40 #endif
41 
44 
46  virtual type_t type() const = 0;
47 
49  bool is_unary() const
50  {
51  return sttc::rat::is_unary(type());
52  }
53 
55  bool is_leaf() const
56  {
57  type_t t = type();
58  return (sttc::rat::is_constant(t)
59  || t == type_t::atom);
60  }
61  };
62 
63 
64  /*-------.
65  | node. |
66  `-------*/
67 
70  template <typename Label, typename Weight>
71  class node
72  : public std::enable_shared_from_this<node<Label, Weight>>
73  , public exp
74  {
75  public:
76  using label_t = Label;
77  using weight_t = Weight;
79  using value_t = std::shared_ptr<const node_t>;
81  using wvalue_t = std::shared_ptr<node_t>;
82  using ratexps_t = std::vector<value_t>;
84 
85  virtual void accept(const_visitor &v) const = 0;
86  };
87 
88  /*--------.
89  | inner. |
90  `--------*/
91 
93  template <typename Label, typename Weight>
94  class inner
95  : public node<Label, Weight>
96  {
97  public:
98  using label_t = Label;
99  using weight_t = Weight;
101  using value_t = typename super_type::value_t;
102  };
103 
104 
105  /*-----------.
106  | variadic. |
107  `-----------*/
108 
112  template <exp::type_t Type, typename Label, typename Weight>
113  class variadic
114  : public inner<Label, Weight>
115  {
116  public:
117  static_assert(sttc::rat::is_variadic(Type), "invalid type");
118 
119  using label_t = Label;
120  using weight_t = Weight;
123  using value_t = typename super_type::value_t;
125 
126  using const_iterator = typename ratexps_t::const_iterator;
127  // Needed by boost::make_iterator_range, but since we iterate
128  // over const value (well, shared_ptr to const values), make it
129  // a const_iterator anyway. Otherwise, clang won't compile.
131  using const_reverse_iterator = typename ratexps_t::const_reverse_iterator;
133 
134  virtual type_t type() const override { return Type; };
135 
136  const_iterator begin() const;
137  const_iterator end() const;
140  size_t size() const;
141 
144  const value_t operator[](size_t n) const;
145 
147  const value_t head() const;
149  const value_t back() const;
150 
152  auto tail() const -> internal::it_cont<const_iterator>;
153 
154  variadic(const ratexps_t& ns = ratexps_t());
155  variadic(const variadic& that)
156  : super_type(that)
157  , sub_(that.sub_)
158  {}
159 
161  ratexps_t subs() const;
162 
163  virtual void accept(typename node_t::const_visitor &v) const override;
164 
165  private:
166  ratexps_t sub_;
167  };
168 
169  /*--------.
170  | unary. |
171  `--------*/
172 
173  template <exp::type_t Type, typename Label, typename Weight>
174  class unary
175  : public inner<Label, Weight>
176  {
177  public:
178  static_assert(is_unary(Type), "invalid type");
179 
180  using label_t = Label;
181  using weight_t = Weight;
184  using value_t = typename node_t::value_t;
185 
186  unary(value_t exp);
187  virtual type_t type() const override { return Type; };
188 
189  const value_t sub() const;
190 
191  virtual void accept(typename node_t::const_visitor &v) const override;
192 
193  private:
194  value_t sub_;
195  };
196 
197 
198  /*--------.
199  | weight. |
200  `--------*/
201 
205  template <exp::type_t Type, typename Label, typename Weight>
207  : public inner<Label, Weight>
208  {
209  public:
210  static_assert(Type == type_t::lweight
211  || Type == type_t::rweight,
212  "invalid type");
213 
214  using label_t = Label;
215  using weight_t = Weight;
218  using value_t = typename super_type::value_t;
219 
220  virtual type_t type() const override { return Type; };
221 
222  const value_t sub() const;
223  const weight_t& weight() const;
224  void set_weight(const weight_t& w);
225 
226  weight_node(const weight_t& w, value_t exp);
228  : sub_(that.sub_)
229  , weight_(that.weight_)
230  {}
231 
232  virtual void accept(typename node_t::const_visitor &v) const override;
233 
234  private:
235  value_t sub_;
236  weight_t weight_;
237  };
238 
239 
240  /*-------.
241  | leaf. |
242  `-------*/
243 
245  template <typename Label, typename Weight>
246  class leaf
247  : public node<Label, Weight>
248  {
249  public:
250  using label_t = Label;
251  using weight_t = Weight;
253  using type_t = typename node_t::type_t;
254  using value_t = typename node_t::value_t;
256  };
257 
258 
259  template <exp::type_t Type, typename Label, typename Weight>
260  class constant
261  : public leaf<Label, Weight>
262  {
263  public:
264  static_assert(is_constant(Type), "invalid type");
265  using label_t = Label;
266  using weight_t = Weight;
269  using value_t = typename node_t::value_t;
270 
271  virtual type_t type() const override { return Type; };
272 
273  virtual void accept(typename node_t::const_visitor &v) const override;
274  };
275 
276 
277  template <typename Label, typename Weight>
278  class atom
279  : public leaf<Label, Weight>
280  {
281  public:
282  using label_t = Label;
283  using weight_t = Weight;
286  using type_t = typename node_t::type_t;
287  using value_t = typename node_t::value_t;
288 
289  atom(const label_t& value);
290 
291  virtual type_t type() const override { return type_t::atom; };
292 
293  virtual void accept(typename node_t::const_visitor &v) const override;
294  const label_t& value() const;
295 
296  private:
297  label_t value_;
298  };
299 
300  } // namespace rat
301 }}//end of ns awali::stc
302 
304 
305 #endif // !AWALI_CORE_RAT_RATEXP_HH
The semiring of Natural numbers.
Definition: n.hh:34
Definition: ratexp.hh:280
Label label_t
Definition: ratexp.hh:282
atom(const label_t &value)
Definition: ratexp.hxx:43
virtual void accept(typename node_t::const_visitor &v) const override
Definition: ratexp.hxx:47
const label_t & value() const
Definition: ratexp.hxx:53
virtual type_t type() const override
The type of this node.
Definition: ratexp.hh:291
Definition: visitor.hh:30
Definition: ratexp.hh:262
virtual type_t type() const override
The type of this node.
Definition: ratexp.hh:271
virtual void accept(typename node_t::const_visitor &v) const override
Definition: ratexp.hxx:208
The abstract, non-parameterized, root for all rational expression node types.
Definition: ratexp.hh:36
bool is_leaf() const
Whether a leaf of the ratexp tree.
Definition: ratexp.hh:55
rat::type_t type_t
The possible types of ratexps.
Definition: ratexp.hh:43
bool is_unary() const
Whether star, complement.
Definition: ratexp.hh:49
virtual type_t type() const =0
The type of this node.
virtual ~exp()=default
An inner node.
Definition: ratexp.hh:96
typename super_type::value_t value_t
Definition: ratexp.hh:101
The root from which to derive the final node types.
Definition: ratexp.hh:248
node< label_t, weight_t > node_t
Definition: ratexp.hh:252
The abstract parameterized, root for all rational expression types.
Definition: ratexp.hh:74
std::shared_ptr< node_t > wvalue_t
Same as value_t, but writable. Use with care.
Definition: ratexp.hh:81
Weight weight_t
Definition: ratexp.hh:77
sttc::rat::const_visitor< label_t, weight_t > const_visitor
Definition: ratexp.hh:83
std::shared_ptr< const node_t > value_t
Definition: ratexp.hh:79
std::vector< value_t > ratexps_t
Definition: ratexp.hh:82
Label label_t
Definition: ratexp.hh:76
virtual void accept(const_visitor &v) const =0
Definition: ratexp.hh:176
virtual void accept(typename node_t::const_visitor &v) const override
Definition: ratexp.hxx:198
const value_t sub() const
Definition: ratexp.hxx:192
typename node_t::value_t value_t
Definition: ratexp.hh:184
virtual type_t type() const override
The type of this node.
Definition: ratexp.hh:187
unary(value_t exp)
Definition: ratexp.hxx:188
An inner node with multiple children.
Definition: ratexp.hh:115
typename super_type::ratexps_t ratexps_t
Definition: ratexp.hh:124
const_iterator end() const
Definition: ratexp.hxx:90
const value_t back() const
The last item of this variadic.
Definition: ratexp.hxx:126
const_iterator begin() const
Definition: ratexp.hxx:84
typename ratexps_t::const_iterator const_iterator
Definition: ratexp.hh:126
typename super_type::value_t value_t
Definition: ratexp.hh:123
typename ratexps_t::const_reverse_iterator const_reverse_iterator
Definition: ratexp.hh:131
virtual type_t type() const override
The type of this node.
Definition: ratexp.hh:134
const_iterator iterator
Definition: ratexp.hh:130
const_reverse_iterator reverse_iterator
Definition: ratexp.hh:132
const value_t operator[](size_t n) const
Access the n-th element.
Definition: ratexp.hxx:114
const value_t head() const
The first item of this variadic.
Definition: ratexp.hxx:120
ratexps_t subs() const
Return a copy of children.
Definition: ratexp.hxx:139
const_reverse_iterator rbegin() const
Definition: ratexp.hxx:96
virtual void accept(typename node_t::const_visitor &v) const override
Definition: ratexp.hxx:145
const_reverse_iterator rend() const
Definition: ratexp.hxx:102
size_t size() const
Definition: ratexp.hxx:108
auto tail() const -> internal::it_cont< const_iterator >
The non-first items.
Definition: ratexp.hxx:132
An inner node implementing a weight.
Definition: ratexp.hh:208
weight_node(const weight_node &that)
Definition: ratexp.hh:227
virtual type_t type() const override
The type of this node.
Definition: ratexp.hh:220
void set_weight(const weight_t &w)
Definition: ratexp.hxx:172
Weight weight_t
Definition: ratexp.hh:215
const value_t sub() const
Definition: ratexp.hxx:160
weight_node(const weight_t &w, value_t exp)
Definition: ratexp.hxx:155
virtual void accept(typename node_t::const_visitor &v) const override
Definition: ratexp.hxx:178
typename super_type::value_t value_t
Definition: ratexp.hh:218
const weight_t & weight() const
Definition: ratexp.hxx:166
constexpr bool is_unary(type_t t)
Whether star, complement.
Definition: fwd.hh:72
constexpr bool is_constant(type_t t)
Whether is a constant (\z or \e).
Definition: fwd.hh:65
type_t
The possible types of ratexps.
Definition: fwd.hh:48
constexpr bool is_variadic(type_t t)
Whether one of the variadic types.
Definition: fwd.hh:80
Main namespace of Awali.
Definition: ato.hh:22