Vcsn  2.8
Be Rational
expression.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include <boost/range.hpp> // make_iterator_range
6 
7 #include <vcsn/core/rat/fwd.hh>
9 #include <vcsn/ctx/context.hh>
10 #include <vcsn/ctx/traits.hh>
12 #include <vcsn/misc/symbol.hh>
13 
14 namespace vcsn
15 {
16  namespace rat
17  {
18 
21  class exp
22  {
23  public:
24  virtual ~exp() = default;
25 
28 
30  virtual type_t type() const = 0;
31 
33  bool is_unary() const
34  {
35  return vcsn::rat::is_unary(type());
36  }
37 
39  bool is_leaf() const
40  {
41  type_t t = type();
42  return (vcsn::rat::is_constant(t)
43  || t == type_t::atom);
44  }
45  };
46 
47 
48  /*-------.
49  | node. |
50  `-------*/
51 
80  template <typename Context>
81  class node
82  : public std::enable_shared_from_this<node<Context>>
83  , public exp
84  {
85  public:
86  using context_t = Context;
89  using value_t = std::shared_ptr<const node_t>;
90  using values_t = std::vector<value_t>;
92 
93  virtual void accept(const_visitor& v) const = 0;
94  };
95 
96  /*--------.
97  | inner. |
98  `--------*/
99 
101  template <typename Context>
102  class inner
103  : public node<Context>
104  {
105  public:
107  using value_t = typename super_t::value_t;
108  };
109 
110 
111  /*-----------.
112  | variadic. |
113  `-----------*/
114 
118  template <exp::type_t Type, typename Context>
119  class variadic
120  : public inner<Context>
121  {
122  public:
123  static_assert(vcsn::rat::is_variadic(Type), "invalid type");
124 
126  using value_t = typename super_t::value_t;
127  using values_t = typename super_t::values_t;
128 
129  using const_iterator = typename values_t::const_iterator;
130  // Needed by boost::make_iterator_range, but since we iterate
131  // over const value (well, shared_ptr to const values), make it
132  // a const_iterator anyway. Otherwise, clang won't compile.
134 
135  virtual type_t type() const { return Type; };
136 
137  const_iterator begin() const;
138  const_iterator end() const;
139  size_t size() const;
140 
143  const value_t operator[](size_t n) const;
144 
146  const value_t head() const;
148  const value_t back() const;
149 
151  auto tail() const -> decltype(boost::make_iterator_range(*this, 1, 0));
152 
153  variadic(values_t ns = values_t());
154 
155  template <typename... Vs>
156  variadic(Vs&&... vs)
157  : sub_{std::forward<Vs>(vs)...}
158  {}
159 
161  values_t subs() const;
162 
163  virtual void accept(typename super_t::const_visitor& v) const;
164 
165  private:
167  };
168 
169  /*---------.
170  | tuple. |
171  `---------*/
172 
173  template <typename Context, bool Enable>
174  class tuple
175  : public inner<Context>
176  {
177  public:
178  static_assert(Context::is_lat,
179  "tuple: requires a tupleset labelset");
181  using context_t = Context;
182  using value_t = typename super_t::value_t;
183 
185  template <unsigned Tape>
186  using value_t_of
187  = std::shared_ptr<const rat::node<detail::project_context<Tape, context_t>>>;
188 
189  template <typename Sequence>
191 
192  template <size_t... I>
193  struct values_t_impl<detail::index_sequence<I...>>
194  {
195  using type = std::tuple<value_t_of<I>...>;
196  };
199 
200  template <typename... Args>
201  tuple(Args&&... args)
202  : sub_{std::forward<Args>(args)...}
203  {};
204  virtual type_t type() const { return type_t::tuple; };
205 
206  virtual void accept(typename super_t::const_visitor& v) const
207  {
208  v.visit(*this);
209  }
210 
211  const values_t sub() const { return sub_; }
212 
213  private:
215  };
216 
217  template <typename Context>
218  class tuple<Context, false>
219  : public inner<Context>
220  {};
221 
222  /*--------.
223  | unary. |
224  `--------*/
225 
226  template <exp::type_t Type, typename Context>
227  class unary
228  : public inner<Context>
229  {
230  public:
231  static_assert(is_unary(Type), "invalid type");
232 
234  using value_t = typename super_t::value_t;
235 
236  unary(value_t exp);
237  virtual type_t type() const { return Type; };
238 
239  const value_t sub() const;
240 
241  virtual void accept(typename super_t::const_visitor& v) const;
242 
243  private:
245  };
246 
247 
248  /*--------.
249  | weight. |
250  `--------*/
251 
255  template <exp::type_t Type, typename Context>
257  : public inner<Context>
258  {
259  public:
260  static_assert(Type == type_t::lweight
261  || Type == type_t::rweight,
262  "invalid type");
263 
266  using value_t = typename super_t::value_t;
267 
268  virtual type_t type() const { return Type; };
269 
270  const value_t sub() const;
271  const weight_t& weight() const;
272  void set_weight(weight_t w);
273 
275 
276  virtual void accept(typename super_t::const_visitor& v) const;
277 
278  private:
281  };
282 
283 
284  /*------.
285  | name. |
286  `------*/
287 
289  template <typename Context>
290  class name
291  : public inner<Context>
292  {
293  public:
295  using value_t = typename super_t::value_t;
296 
298 
299  const value_t sub() const;
300  symbol name_get() const;
301 
302  virtual type_t type() const { return type_t::name; };
303  virtual void accept(typename super_t::const_visitor& v) const;
304 
305  private:
308  };
309 
310 
311  /*-------.
312  | leaf. |
313  `-------*/
314 
316  template <typename Context>
317  class leaf
318  : public node<Context>
319  {
320  public:
322  };
323 
324 
325  template <exp::type_t Type, typename Context>
326  class constant
327  : public leaf<Context>
328  {
329  public:
330  static_assert(is_constant(Type), "invalid type");
332  using value_t = typename super_t::value_t;
333  using type_t = typename super_t::type_t;
334 
335  virtual type_t type() const { return Type; };
336 
337  virtual void accept(typename super_t::const_visitor& v) const;
338  };
339 
340 
341  template <typename Context>
342  class atom
343  : public leaf<Context>
344  {
345  public:
348  using type_t = typename super_t::type_t;
349  using value_t = typename super_t::value_t;
350 
351  atom(const label_t& value);
352 
353  virtual type_t type() const { return type_t::atom; };
354 
355  virtual void accept(typename super_t::const_visitor& v) const;
356  const label_t& value() const;
357 
358  private:
360  };
361 
362  } // namespace rat
363 } // namespace vcsn
364 
void visit(const tuple< context_t > &v)
Definition: visitor.hh:33
virtual type_t type() const
The type of this node.
Definition: expression.hh:204
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
constexpr bool is_constant(type_t t)
Whether is a constant (\\z or \\e).
Definition: fwd.hh:67
virtual type_t type() const
The type of this node.
Definition: expression.hh:237
std::vector< value_t > values_t
Definition: expression.hh:90
Context context_t
Definition: expression.hh:86
An inner node with multiple children.
Definition: expression.hh:119
typename labelset_t_of< context_t >::indices_t indices_t
Definition: expression.hh:197
The abstract, non-parameterized, root for all rational expression node types.
Definition: expression.hh:21
typename super_t::value_t value_t
Definition: expression.hh:107
virtual type_t type() const
The type of this node.
Definition: expression.hh:135
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:21
bool is_leaf() const
Whether a leaf of the expression tree.
Definition: expression.hh:39
An inner node to name the subexpression.
Definition: expression.hh:290
Container::value_type back(const Container &container)
The last member of this Container.
Definition: algorithm.hh:37
label_t_of< Context > label_t
Definition: expression.hh:347
std::shared_ptr< const rat::node< detail::project_context< Tape, context_t > >> value_t_of
Given a tape, its corresponding expression type.
Definition: expression.hh:187
constexpr bool is_unary(type_t t)
Whether star, complement.
Definition: fwd.hh:74
const values_t sub() const
Definition: expression.hh:211
bool is_unary() const
Whether star, complement.
Definition: expression.hh:33
An inner node.
Definition: expression.hh:102
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
Definition: traits.hh:63
type_t
The possible types of expressions.
Definition: fwd.hh:43
Definition: a-star.hh:8
virtual type_t type() const
The type of this node.
Definition: expression.hh:268
constexpr bool is_variadic(type_t t)
Whether one of the variadic types.
Definition: fwd.hh:82
std::shared_ptr< const node_t > value_t
An expression usable with value semantics.
Definition: expression.hh:89
Implementation of nodes of tuple of rational expressions.
Definition: expression.hh:174
typename detail::label_t_of_impl< base_t< ValueSet > >::type label_t_of
Definition: traits.hh:62
The abstract parameterized, root for all rational expression types.
Definition: expression.hh:81
virtual ~exp()=default
Naming an expression.
An inner node implementing a weight.
Definition: expression.hh:256
virtual type_t type() const
The type of this node.
Definition: expression.hh:302
weight_t_of< Context > weight_t
Definition: expression.hh:265
virtual void accept(typename super_t::const_visitor &v) const
Definition: expression.hh:206
return v
Definition: multiply.hh:362
virtual type_t type() const =0
The type of this node.
const_iterator iterator
Definition: expression.hh:133
The root from which to derive the final node types.
Definition: expression.hh:317
tuple(Args &&... args)
Definition: expression.hh:201
virtual type_t type() const
The type of this node.
Definition: expression.hh:335
typename detail::weight_t_of_impl< base_t< ValueSet > >::type weight_t_of
Definition: traits.hh:66
typename values_t::const_iterator const_iterator
Definition: expression.hh:129
variadic(Vs &&... vs)
Definition: expression.hh:156
value_impl< detail::weight_tag > weight
Definition: fwd.hh:34
virtual type_t type() const
The type of this node.
Definition: expression.hh:353