Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ratexp.hh
Go to the documentation of this file.
1 #ifndef VCSN_CORE_RAT_RATEXP_HH
2 # define VCSN_CORE_RAT_RATEXP_HH
3 
4 # include <vector>
5 # include <string>
6 
7 # include <boost/range.hpp> // make_iterator_range
8 
9 # include <vcsn/core/rat/fwd.hh>
10 # include <vcsn/core/rat/visitor.hh>
11 # include <vcsn/ctx/traits.hh>
12 
13 namespace vcsn
14 {
15  namespace rat
16  {
17 
20  class exp
21  {
22  public:
23 #ifndef COVERAGE
24  virtual ~exp() = default;
25 #endif
26 
29 
31  virtual type_t type() const = 0;
32 
34  bool is_unary() const
35  {
36  return vcsn::rat::is_unary(type());
37  }
38 
40  bool is_leaf() const
41  {
42  type_t t = type();
43  return (vcsn::rat::is_constant(t)
44  || t == type_t::atom);
45  }
46  };
47 
48 
49  /*-------.
50  | node. |
51  `-------*/
52 
81  template <typename Context>
82  class node
83  : public std::enable_shared_from_this<node<Context>>
84  , public exp
85  {
86  public:
87  using context_t = Context;
90  using value_t = std::shared_ptr<const node_t>;
91  using values_t = std::vector<value_t>;
93 
94  virtual void accept(const_visitor& v) const = 0;
95  };
96 
97  /*--------.
98  | inner. |
99  `--------*/
100 
102  template <typename Context>
103  class inner
104  : public node<Context>
105  {
106  public:
108  using value_t = typename super_t::value_t;
109  };
110 
111 
112  /*-----------.
113  | variadic. |
114  `-----------*/
115 
119  template <exp::type_t Type, typename Context>
120  class variadic
121  : public inner<Context>
122  {
123  public:
124  static_assert(vcsn::rat::is_variadic(Type), "invalid type");
125 
127  using value_t = typename super_t::value_t;
128  using values_t = typename super_t::values_t;
129 
130  using const_iterator = typename values_t::const_iterator;
131  // Needed by boost::make_iterator_range, but since we iterate
132  // over const value (well, shared_ptr to const values), make it
133  // a const_iterator anyway. Otherwise, clang won't compile.
135  using const_reverse_iterator = typename values_t::const_reverse_iterator;
137 
138  virtual type_t type() const { return Type; };
139 
140  const_iterator begin() const;
141  const_iterator end() const;
144  size_t size() const;
145 
148  const value_t operator[](size_t n) const;
149 
151  const value_t head() const;
153  const value_t back() const;
154 
156  auto tail() const -> decltype(boost::make_iterator_range(*this, 1, 0));
157 
158  variadic(const values_t& ns = values_t());
159  variadic(const variadic& that)
160  : super_t(that)
161  , sub_(that.sub_)
162  {}
163 
165  values_t subs() const;
166 
167  virtual void accept(typename super_t::const_visitor& v) const;
168 
169  private:
171  };
172 
173  /*--------.
174  | unary. |
175  `--------*/
176 
177  template <exp::type_t Type, typename Context>
178  class unary
179  : public inner<Context>
180  {
181  public:
182  static_assert(is_unary(Type), "invalid type");
183 
185  using value_t = typename super_t::value_t;
186 
187  unary(value_t exp);
188  virtual type_t type() const { return Type; };
189 
190  const value_t sub() const;
191 
192  virtual void accept(typename super_t::const_visitor& v) const;
193 
194  private:
196  };
197 
198 
199  /*--------.
200  | weight. |
201  `--------*/
202 
206  template <exp::type_t Type, typename Context>
207  class weight_node
208  : public inner<Context>
209  {
210  public:
211  static_assert(Type == type_t::lweight
212  || Type == type_t::rweight,
213  "invalid type");
214 
217  using value_t = typename super_t::value_t;
218 
219  virtual type_t type() const { return Type; };
220 
221  const value_t sub() const;
222  const weight_t& weight() const;
223  void set_weight(const weight_t& w);
224 
225  weight_node(const weight_t& w, value_t exp);
227  : sub_(that.sub_)
228  , weight_(that.weight_)
229  {}
230 
231  virtual void accept(typename super_t::const_visitor& v) const;
232 
233  private:
236  };
237 
238 
239  /*-------.
240  | leaf. |
241  `-------*/
242 
244  template <typename Context>
245  class leaf
246  : public node<Context>
247  {
248  public:
250  };
251 
252 
253  template <exp::type_t Type, typename Context>
254  class constant
255  : public leaf<Context>
256  {
257  public:
258  static_assert(is_constant(Type), "invalid type");
260  using value_t = typename super_t::value_t;
261  using type_t = typename super_t::type_t;
262 
263  virtual type_t type() const { return Type; };
264 
265  virtual void accept(typename super_t::const_visitor& v) const;
266  };
267 
268 
269  template <typename Context>
270  class atom
271  : public leaf<Context>
272  {
273  public:
276  using type_t = typename super_t::type_t;
277  using value_t = typename super_t::value_t;
278 
279  atom(const label_t& value);
280 
281  virtual type_t type() const { return type_t::atom; };
282 
283  virtual void accept(typename super_t::const_visitor& v) const;
284  const label_t& value() const;
285 
286  private:
288  };
289 
290  } // namespace rat
291 } // namespace vcsn
292 
293 #include <vcsn/core/rat/ratexp.hxx>
294 
295 #endif // !VCSN_CORE_RAT_RATEXP_HH
bool is_leaf() const
Whether a leaf of the ratexp tree.
Definition: ratexp.hh:40
constexpr bool is_variadic(type_t t)
Whether one of the variadic types.
Definition: fwd.hh:64
value_t sub_
Definition: ratexp.hh:195
virtual void accept(typename super_t::const_visitor &v) const
Definition: ratexp.hxx:128
An inner node with multiple children.
Definition: fwd.hh:123
const_iterator iterator
Definition: ratexp.hh:134
The abstract parameterized, root for all rational expression types.
Definition: fwd.hh:80
virtual type_t type() const
The type of this node.
Definition: ratexp.hh:281
const_reverse_iterator reverse_iterator
Definition: ratexp.hh:136
Context context_t
Definition: ratexp.hh:87
typename super_t::values_t values_t
Definition: ratexp.hh:128
typename values_t::const_reverse_iterator const_reverse_iterator
Definition: ratexp.hh:135
const value_t sub() const
Definition: ratexp.hxx:143
atom(const label_t &value)
Definition: ratexp.hxx:27
typename super_t::value_t value_t
Definition: ratexp.hh:127
const_iterator end() const
Definition: ratexp.hxx:74
void set_weight(const weight_t &w)
Definition: ratexp.hxx:155
constexpr bool is_unary(type_t t)
Whether star, complement.
Definition: fwd.hh:56
const_reverse_iterator rbegin() const
Definition: ratexp.hxx:80
typename super_t::value_t value_t
Definition: ratexp.hh:108
The abstract, non-parameterized, root for all rational expression node types.
Definition: ratexp.hh:20
weight_t_of< Context > weight_t
Definition: ratexp.hh:216
typename super_t::value_t value_t
Definition: ratexp.hh:217
virtual type_t type() const
The type of this node.
Definition: ratexp.hh:188
virtual type_t type() const
The type of this node.
Definition: ratexp.hh:138
virtual void accept(typename super_t::const_visitor &v) const
Definition: ratexp.hxx:181
type_t
The possible types of ratexps.
Definition: fwd.hh:31
const value_t back() const
The last item of this variadic.
Definition: ratexp.hxx:110
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:33
size_t size() const
Definition: ratexp.hxx:92
values_t subs() const
Return a copy of children.
Definition: ratexp.hxx:122
const value_t operator[](size_t n) const
Access the n-th element.
Definition: ratexp.hxx:98
unary(value_t exp)
Definition: ratexp.hxx:171
label_t_of< Context > label_t
Definition: ratexp.hh:275
virtual ~exp()=default
const label_t & value() const
Definition: ratexp.hxx:37
typename super_t::value_t value_t
Definition: ratexp.hh:185
rat::type_t type_t
The possible types of ratexps.
Definition: ratexp.hh:28
const_reverse_iterator rend() const
Definition: ratexp.hxx:86
virtual void accept(typename super_t::const_visitor &v) const
Definition: ratexp.hxx:161
weight_node(const weight_t &w, value_t exp)
Definition: ratexp.hxx:138
An inner node implementing a weight.
Definition: fwd.hh:145
label_t value_
Definition: ratexp.hh:287
vcsn::rat::const_visitor< context_t > const_visitor
Definition: ratexp.hh:92
std::vector< value_t > values_t
Definition: ratexp.hh:91
weight_node(const weight_node &that)
Definition: ratexp.hh:226
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:37
virtual type_t type() const =0
The type of this node.
The root from which to derive the final node types.
Definition: fwd.hh:81
An inner node.
Definition: fwd.hh:83
virtual type_t type() const
The type of this node.
Definition: ratexp.hh:219
std::shared_ptr< const node_t > value_t
A ratexp usable with value semantics.
Definition: ratexp.hh:90
const_iterator begin() const
Definition: ratexp.hxx:68
const value_t sub() const
Definition: ratexp.hxx:175
constexpr bool is_constant(type_t t)
Whether is a constant (\z or \e).
Definition: fwd.hh:49
bool is_unary() const
Whether star, complement.
Definition: ratexp.hh:34
virtual void accept(typename super_t::const_visitor &v) const
Definition: ratexp.hxx:191
const value_t head() const
The first item of this variadic.
Definition: ratexp.hxx:104
auto tail() const -> decltype(boost::make_iterator_range(*this, 1, 0))
The non-first items.
Definition: ratexp.hxx:116
typename values_t::const_iterator const_iterator
Definition: ratexp.hh:130
virtual void accept(const_visitor &v) const =0
virtual type_t type() const
The type of this node.
Definition: ratexp.hh:263
const weight_t & weight() const
Definition: ratexp.hxx:149
virtual void accept(typename super_t::const_visitor &v) const
Definition: ratexp.hxx:31