Vcsn  2.4
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 
13 namespace vcsn
14 {
15  namespace rat
16  {
17 
20  class exp
21  {
22  public:
23  virtual ~exp() = default;
24 
27 
29  virtual type_t type() const = 0;
30 
32  bool is_unary() const
33  {
34  return vcsn::rat::is_unary(type());
35  }
36 
38  bool is_leaf() const
39  {
40  type_t t = type();
41  return (vcsn::rat::is_constant(t)
42  || t == type_t::atom);
43  }
44  };
45 
46 
47  /*-------.
48  | node. |
49  `-------*/
50 
79  template <typename Context>
80  class node
81  : public std::enable_shared_from_this<node<Context>>
82  , public exp
83  {
84  public:
85  using context_t = Context;
88  using value_t = std::shared_ptr<const node_t>;
89  using values_t = std::vector<value_t>;
91 
92  virtual void accept(const_visitor& v) const = 0;
93  };
94 
95  /*--------.
96  | inner. |
97  `--------*/
98 
100  template <typename Context>
101  class inner
102  : public node<Context>
103  {
104  public:
106  using value_t = typename super_t::value_t;
107  };
108 
109 
110  /*-----------.
111  | variadic. |
112  `-----------*/
113 
117  template <exp::type_t Type, typename Context>
118  class variadic
119  : public inner<Context>
120  {
121  public:
122  static_assert(vcsn::rat::is_variadic(Type), "invalid type");
123 
125  using value_t = typename super_t::value_t;
126  using values_t = typename super_t::values_t;
127 
128  using const_iterator = typename values_t::const_iterator;
129  // Needed by boost::make_iterator_range, but since we iterate
130  // over const value (well, shared_ptr to const values), make it
131  // a const_iterator anyway. Otherwise, clang won't compile.
133 
134  virtual type_t type() const { return Type; };
135 
136  const_iterator begin() const;
137  const_iterator end() const;
138  size_t size() const;
139 
142  const value_t operator[](size_t n) const;
143 
145  const value_t head() const;
147  const value_t back() const;
148 
150  auto tail() const -> decltype(boost::make_iterator_range(*this, 1, 0));
151 
152  variadic(values_t ns = values_t());
153 
154  template <typename... Vs>
155  variadic(Vs&&... vs)
156  : sub_{std::forward<Vs>(vs)...}
157  {}
158 
160  values_t subs() const;
161 
162  virtual void accept(typename super_t::const_visitor& v) const;
163 
164  private:
165  values_t sub_;
166  };
167 
168  /*---------.
169  | tuple. |
170  `---------*/
171 
172  template <typename Context, bool Enable>
173  class tuple
174  : public inner<Context>
175  {
176  public:
177  static_assert(Context::is_lat,
178  "tuple: requires a tupleset labelset");
180  using context_t = Context;
181  using value_t = typename super_t::value_t;
182 
184  template <unsigned Tape>
185  using value_t_of
186  = std::shared_ptr<const rat::node<detail::project_context<Tape, context_t>>>;
187 
188  template <typename Sequence>
190 
191  template <size_t... I>
192  struct values_t_impl<detail::index_sequence<I...>>
193  {
194  using type = std::tuple<value_t_of<I>...>;
195  };
198 
199  template <typename... Args>
200  tuple(Args&&... args)
201  : sub_{std::forward<Args>(args)...}
202  {};
203  virtual type_t type() const { return type_t::tuple; };
204 
205  virtual void accept(typename super_t::const_visitor& v) const
206  {
207  v.visit(*this);
208  }
209 
210  const values_t sub() const { return sub_; }
211 
212  private:
214  };
215 
216  template <typename Context>
217  class tuple<Context, false>
218  : public inner<Context>
219  {};
220 
221  /*--------.
222  | unary. |
223  `--------*/
224 
225  template <exp::type_t Type, typename Context>
226  class unary
227  : public inner<Context>
228  {
229  public:
230  static_assert(is_unary(Type), "invalid type");
231 
233  using value_t = typename super_t::value_t;
234 
235  unary(value_t exp);
236  virtual type_t type() const { return Type; };
237 
238  const value_t sub() const;
239 
240  virtual void accept(typename super_t::const_visitor& v) const;
241 
242  private:
244  };
245 
246 
247  /*--------.
248  | weight. |
249  `--------*/
250 
254  template <exp::type_t Type, typename Context>
256  : public inner<Context>
257  {
258  public:
259  static_assert(Type == type_t::lweight
260  || Type == type_t::rweight,
261  "invalid type");
262 
265  using value_t = typename super_t::value_t;
266 
267  virtual type_t type() const { return Type; };
268 
269  const value_t sub() const;
270  const weight_t& weight() const;
271  void set_weight(weight_t w);
272 
273  weight_node(weight_t w, value_t exp);
274 
275  virtual void accept(typename super_t::const_visitor& v) const;
276 
277  private:
280  };
281 
282 
283  /*-------.
284  | leaf. |
285  `-------*/
286 
288  template <typename Context>
289  class leaf
290  : public node<Context>
291  {
292  public:
294  };
295 
296 
297  template <exp::type_t Type, typename Context>
298  class constant
299  : public leaf<Context>
300  {
301  public:
302  static_assert(is_constant(Type), "invalid type");
304  using value_t = typename super_t::value_t;
305  using type_t = typename super_t::type_t;
306 
307  virtual type_t type() const { return Type; };
308 
309  virtual void accept(typename super_t::const_visitor& v) const;
310  };
311 
312 
313  template <typename Context>
314  class atom
315  : public leaf<Context>
316  {
317  public:
320  using type_t = typename super_t::type_t;
321  using value_t = typename super_t::value_t;
322 
323  atom(const label_t& value);
324 
325  virtual type_t type() const { return type_t::atom; };
326 
327  virtual void accept(typename super_t::const_visitor& v) const;
328  const label_t& value() const;
329 
330  private:
332  };
333 
334  } // namespace rat
335 } // namespace vcsn
336 
bool is_unary() const
Whether star, complement.
Definition: expression.hh:32
bool is_leaf() const
Whether a leaf of the expression tree.
Definition: expression.hh:38
const value_t operator[](size_t n) const
Access the n-th element.
Definition: expression.hxx:81
Implementation of nodes of tuple of rational expressions.
Definition: expression.hh:173
std::shared_ptr< const node_t > value_t
An expression usable with value semantics.
Definition: expression.hh:88
std::vector< value_t > values_t
Definition: expression.hh:89
label_t_of< Context > label_t
Definition: expression.hh:319
value_impl< detail::weight_tag > weight
Definition: fwd.hh:28
const_iterator begin() const
Definition: expression.hxx:63
An inner node implementing a weight.
Definition: expression.hh:255
virtual type_t type() const
The type of this node.
Definition: expression.hh:134
virtual type_t type() const
The type of this node.
Definition: expression.hh:325
virtual type_t type() const
The type of this node.
Definition: expression.hh:203
virtual void accept(typename super_t::const_visitor &v) const
Definition: expression.hxx:111
Definition: filter.hh:56
virtual void accept(typename super_t::const_visitor &v) const
Definition: expression.hh:205
rat::type_t type_t
The possible types of expressions.
Definition: expression.hh:26
typename labelset_t_of< context_t >::indices_t indices_t
Definition: expression.hh:196
type_t
The possible types of expressions.
Definition: fwd.hh:39
virtual type_t type() const
The type of this node.
Definition: expression.hh:267
Context context_t
Definition: expression.hh:85
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:62
vcsn::rat::const_visitor< context_t > const_visitor
Definition: expression.hh:90
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
virtual ~exp()=default
Definition: a-star.hh:8
typename super_t::value_t value_t
Definition: expression.hh:106
virtual type_t type() const
The type of this node.
Definition: expression.hh:236
const value_t back() const
The last item of this variadic.
Definition: expression.hxx:93
tuple(Args &&...args)
Definition: expression.hh:200
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:186
virtual void accept(const_visitor &v) const =0
const value_t head() const
The first item of this variadic.
Definition: expression.hxx:87
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:66
void visit(const tuple< context_t > &v)
Definition: visitor.hh:33
size_t size() const
Definition: expression.hxx:75
virtual type_t type() const =0
The type of this node.
An inner node with multiple children.
Definition: expression.hh:118
return v
Definition: multiply.hh:361
const_iterator iterator
Definition: expression.hh:132
constexpr bool is_unary(type_t t)
Whether star, complement.
Definition: fwd.hh:67
typename super_t::values_t values_t
Definition: expression.hh:126
The root from which to derive the final node types.
Definition: expression.hh:289
values_t subs() const
Return a copy of children.
Definition: expression.hxx:105
const values_t sub() const
Definition: expression.hh:210
auto tail() const -> decltype(boost::make_iterator_range(*this, 1, 0))
The non-first items.
Definition: expression.hxx:99
An inner node.
Definition: expression.hh:101
constexpr bool is_constant(type_t t)
Whether is a constant (\\z or \\e).
Definition: fwd.hh:60
typename super_t::value_t value_t
Definition: expression.hh:125
typename values_t::const_iterator const_iterator
Definition: expression.hh:128
weight_t_of< Context > weight_t
Definition: expression.hh:264
The abstract parameterized, root for all rational expression types.
Definition: expression.hh:80
constexpr bool is_variadic(type_t t)
Whether one of the variadic types.
Definition: fwd.hh:75
virtual type_t type() const
The type of this node.
Definition: expression.hh:307
const_iterator end() const
Definition: expression.hxx:69
The abstract, non-parameterized, root for all rational expression node types.
Definition: expression.hh:20