Vcsn  2.3
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  using const_reverse_iterator = typename values_t::const_reverse_iterator;
135 
136  virtual type_t type() const { return Type; };
137 
138  const_iterator begin() const;
139  const_iterator end() const;
142  size_t size() const;
143 
146  const value_t operator[](size_t n) const;
147 
149  const value_t head() const;
151  const value_t back() const;
152 
154  auto tail() const -> decltype(boost::make_iterator_range(*this, 1, 0));
155 
156  variadic(values_t ns = values_t());
157 
158  template <typename... Vs>
159  variadic(Vs&&... vs)
160  : sub_{std::forward<Vs>(vs)...}
161  {}
162 
163  variadic(const variadic& that)
164  : super_t(that)
165  , sub_(that.sub_)
166  {}
167 
169  values_t subs() const;
170 
171  virtual void accept(typename super_t::const_visitor& v) const;
172 
173  private:
174  values_t sub_;
175  };
176 
177  /*---------.
178  | tuple. |
179  `---------*/
180 
181  template <typename Context, bool Enable>
182  class tuple
183  : public inner<Context>
184  {
185  public:
186  static_assert(Context::is_lat,
187  "tuple: requires a tupleset labelset");
189  using context_t = Context;
190  using value_t = typename super_t::value_t;
191 
193  template <unsigned Tape>
194  using value_t_of
195  = std::shared_ptr<const rat::node<detail::project_context<Tape, context_t>>>;
196 
197  template <typename Sequence>
199 
200  template <size_t... I>
201  struct values_t_impl<detail::index_sequence<I...>>
202  {
203  using type = std::tuple<value_t_of<I>...>;
204  };
207 
208  template <typename... Args>
209  tuple(Args&&... args)
210  : sub_{std::forward<Args>(args)...}
211  {};
212  virtual type_t type() const { return type_t::tuple; };
213 
214  virtual void accept(typename super_t::const_visitor& v) const
215  {
216  v.visit(*this);
217  }
218 
219  const values_t sub() const { return sub_; }
220 
221  private:
223  };
224 
225  template <typename Context>
226  class tuple<Context, false>
227  : public inner<Context>
228  {};
229 
230  /*--------.
231  | unary. |
232  `--------*/
233 
234  template <exp::type_t Type, typename Context>
235  class unary
236  : public inner<Context>
237  {
238  public:
239  static_assert(is_unary(Type), "invalid type");
240 
242  using value_t = typename super_t::value_t;
243 
244  unary(value_t exp);
245  virtual type_t type() const { return Type; };
246 
247  const value_t sub() const;
248 
249  virtual void accept(typename super_t::const_visitor& v) const;
250 
251  private:
253  };
254 
255 
256  /*--------.
257  | weight. |
258  `--------*/
259 
263  template <exp::type_t Type, typename Context>
265  : public inner<Context>
266  {
267  public:
268  static_assert(Type == type_t::lweight
269  || Type == type_t::rweight,
270  "invalid type");
271 
274  using value_t = typename super_t::value_t;
275 
276  virtual type_t type() const { return Type; };
277 
278  const value_t sub() const;
279  const weight_t& weight() const;
280  void set_weight(weight_t w);
281 
282  weight_node(weight_t w, value_t exp);
284  : sub_(that.sub_)
285  , weight_(that.weight_)
286  {}
287 
288  virtual void accept(typename super_t::const_visitor& v) const;
289 
290  private:
293  };
294 
295 
296  /*-------.
297  | leaf. |
298  `-------*/
299 
301  template <typename Context>
302  class leaf
303  : public node<Context>
304  {
305  public:
307  };
308 
309 
310  template <exp::type_t Type, typename Context>
311  class constant
312  : public leaf<Context>
313  {
314  public:
315  static_assert(is_constant(Type), "invalid type");
317  using value_t = typename super_t::value_t;
318  using type_t = typename super_t::type_t;
319 
320  virtual type_t type() const { return Type; };
321 
322  virtual void accept(typename super_t::const_visitor& v) const;
323  };
324 
325 
326  template <typename Context>
327  class atom
328  : public leaf<Context>
329  {
330  public:
333  using type_t = typename super_t::type_t;
334  using value_t = typename super_t::value_t;
335 
336  atom(const label_t& value);
337 
338  virtual type_t type() const { return type_t::atom; };
339 
340  virtual void accept(typename super_t::const_visitor& v) const;
341  const label_t& value() const;
342 
343  private:
345  };
346 
347  } // namespace rat
348 } // namespace vcsn
349 
return v
Definition: multiply.hh:361
value_impl< detail::weight_tag > weight
Definition: fwd.hh:28
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:66
type_t
The possible types of expressions.
Definition: fwd.hh:39
const value_t back() const
The last item of this variadic.
Definition: expression.hxx:105
The abstract parameterized, root for all rational expression types.
Definition: expression.hh:80
virtual void accept(const_visitor &v) const =0
constexpr bool is_unary(type_t t)
Whether star, complement.
Definition: fwd.hh:67
variadic(const variadic &that)
Definition: expression.hh:163
values_t subs() const
Return a copy of children.
Definition: expression.hxx:117
typename super_t::value_t value_t
Definition: expression.hh:125
label_t_of< Context > label_t
Definition: expression.hh:332
typename values_t::const_iterator const_iterator
Definition: expression.hh:128
std::shared_ptr< const node_t > value_t
An expression usable with value semantics.
Definition: expression.hh:88
tuple(Args &&...args)
Definition: expression.hh:209
virtual type_t type() const
The type of this node.
Definition: expression.hh:338
const values_t sub() const
Definition: expression.hh:219
const_iterator begin() const
Definition: expression.hxx:63
virtual type_t type() const
The type of this node.
Definition: expression.hh:245
rat::type_t type_t
The possible types of expressions.
Definition: expression.hh:26
An inner node.
Definition: expression.hh:101
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
vcsn::rat::const_visitor< context_t > const_visitor
Definition: expression.hh:90
const_iterator iterator
Definition: expression.hh:132
An inner node implementing a weight.
Definition: expression.hh:264
The abstract, non-parameterized, root for all rational expression node types.
Definition: expression.hh:20
constexpr bool is_variadic(type_t t)
Whether one of the variadic types.
Definition: fwd.hh:75
weight_node(const weight_node &that)
Definition: expression.hh:283
virtual type_t type() const
The type of this node.
Definition: expression.hh:212
constexpr bool is_constant(type_t t)
Whether is a constant (\z or \e).
Definition: fwd.hh:60
virtual ~exp()=default
weight_t_of< Context > weight_t
Definition: expression.hh:273
const value_t operator[](size_t n) const
Access the n-th element.
Definition: expression.hxx:93
typename values_t::const_reverse_iterator const_reverse_iterator
Definition: expression.hh:133
std::vector< value_t > values_t
Definition: expression.hh:89
Definition: a-star.hh:8
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:195
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:62
void visit(const tuple< context_t > &v)
Definition: visitor.hh:33
const_reverse_iterator rend() const
Definition: expression.hxx:81
typename super_t::value_t value_t
Definition: expression.hh:106
const_iterator end() const
Definition: expression.hxx:69
virtual void accept(typename super_t::const_visitor &v) const
Definition: expression.hxx:123
The root from which to derive the final node types.
Definition: expression.hh:302
typename super_t::values_t values_t
Definition: expression.hh:126
Context context_t
Definition: expression.hh:85
Implementation of nodes of tuple of rational expressions.
Definition: expression.hh:182
virtual type_t type() const
The type of this node.
Definition: expression.hh:276
bool is_leaf() const
Whether a leaf of the expression tree.
Definition: expression.hh:38
virtual type_t type() const
The type of this node.
Definition: expression.hh:136
auto tail() const -> decltype(boost::make_iterator_range(*this, 1, 0))
The non-first items.
Definition: expression.hxx:111
typename labelset_t_of< context_t >::indices_t indices_t
Definition: expression.hh:205
An inner node with multiple children.
Definition: expression.hh:118
virtual type_t type() const =0
The type of this node.
virtual void accept(typename super_t::const_visitor &v) const
Definition: expression.hh:214
size_t size() const
Definition: expression.hxx:87
const_reverse_iterator reverse_iterator
Definition: expression.hh:134
const_reverse_iterator rbegin() const
Definition: expression.hxx:75
const value_t head() const
The first item of this variadic.
Definition: expression.hxx:99
virtual type_t type() const
The type of this node.
Definition: expression.hh:320
bool is_unary() const
Whether star, complement.
Definition: expression.hh:32