Vaucanson  1.4.1
nodes.hxx
1 // nodes.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2008 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_NODES_HXX
18 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_NODES_HXX
19 
20 # include <vaucanson/algebra/implementation/series/rat/nodes.hh>
21 
22 namespace vcsn {
23 
24  namespace rat {
25 
26  template<class M_, class W_>
27  void
28  DefaultMutableNodeVisitor<M_,W_>::product(Node<M_, W_>* lhs,
29  Node<M_, W_>* rhs)
30  {
31  lhs->accept(*this);
32  rhs->accept(*this);
33  }
34 
35  template<class M_, class W_>
36  void DefaultMutableNodeVisitor<M_,W_>::sum(Node<M_, W_>* lhs,
37  Node<M_, W_>* rhs)
38  {
39  lhs->accept(*this);
40  rhs->accept(*this);
41  }
42 
43  template<class M_, class W_>
44  void DefaultMutableNodeVisitor<M_,W_>::star(Node<M_, W_>* n)
45  {
46  n->accept(*this);
47  }
48 
49  template<class M_, class W_>
50  void DefaultMutableNodeVisitor<M_,W_>::left_weight(W_&,
51  Node<M_, W_>* n)
52  {
53  n->accept(*this);
54  }
55 
56  template<class M_, class W_>
57  void DefaultMutableNodeVisitor<M_,W_>::right_weight(W_&,
58  Node<M_, W_>*n)
59  {
60  n->accept(*this);
61  }
62 
63  template<class M_, class W_>
64  void DefaultMutableNodeVisitor<M_,W_>::constant( M_&)
65  {}
66 
67  template<class M_, class W_>
68  void DefaultMutableNodeVisitor<M_,W_>::zero()
69  {}
70 
71  template<class M_, class W_>
72  void DefaultMutableNodeVisitor<M_,W_>::one()
73  {}
74 
75  /*-----.
76  | Node |
77  `-----*/
78 
79  // Defined methods
80  template<typename M_, typename W_>
81  Node<M_,W_>::~Node()
82  {
83  }
84 
85  template<typename M_, typename W_>
86  Node<M_,W_>::Node()
87  {}
88 
89  /*-----.
90  | Zero |
91  `-----*/
92  template <class M_, class W_>
93  Zero<M_,W_>::Zero()
94  {}
95 
96  template <class M_, class W_>
97  typename Node<M_, W_>::type
98  Zero<M_,W_>::what() const
99  {
100  return Node<M_, W_>::zero;
101  }
102 
103  template <class M_, class W_>
104  Node<M_, W_>*
105  Zero<M_,W_>::clone() const
106  {
107  Node<M_, W_>* p = new Zero<M_, W_>;
108  return p;
109  }
110 
111  template <class M_, class W_>
112  void
113  Zero<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
114  {
115  v.zero();
116  }
117 
118  template <class M_, class W_>
119  bool
120  Zero<M_,W_>::operator!=(const Node<M_, W_>& other) const
121  {
122  return (dynamic_cast<const Zero<M_, W_>*>(&other) == 0);
123  }
124 
125  template <class M_, class W_>
126  bool
127  Zero<M_,W_>::operator<(const Node<M_, W_>& other) const
128  {
129  return what() < other.what();
130  }
131 
132  template <class M_, class W_>
133 
134  Zero<M_,W_>::~Zero()
135  {}
136 
137  /*----.
138  | One |
139  `----*/
140 
141  template<typename M_, typename W_>
142  One<M_,W_>::One()
143  {}
144 
145  template<typename M_, typename W_>
146  typename Node<M_, W_>::type
147  One<M_,W_>::what() const
148  {
149  return Node<M_, W_>::one;
150  }
151 
152  template<typename M_, typename W_>
153  Node<M_, W_>*
154  One<M_,W_>::clone() const
155  {
156  Node<M_, W_>* p = new One<M_, W_>;
157  return p;
158  }
159 
160  template<typename M_, typename W_>
161  void
162  One<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
163  {
164  v.one();
165  }
166 
167  template<typename M_, typename W_>
168  bool
169  One<M_,W_>::operator!=(const Node<M_, W_>& other) const
170  {
171  return (dynamic_cast<const One<M_, W_>*>(&other) == 0);
172  }
173 
174  template<typename M_, typename W_>
175  bool
176  One<M_,W_>::operator<(const Node<M_, W_>& other) const
177  {
178  return what() < other.what();
179  }
180 
181  template<typename M_, typename W_>
182 
183  One<M_,W_>::~One()
184  {}
185 
186  /*---------.
187  | Constant |
188  `---------*/
189 
190  template<typename M_, typename W_>
191  Constant<M_,W_>::Constant(const M_ &v) : value_(v)
192  {}
193 
194  template<typename M_, typename W_>
195  typename Node<M_, W_>::type
196  Constant<M_,W_>::what() const
197  {
198  return Node<M_, W_>::constant;
199  }
200 
201  template<typename M_, typename W_>
202  Node<M_, W_>*
203  Constant<M_,W_>::clone() const
204  {
205  Node<M_, W_>* p = new Constant<M_, W_>(value_);
206  return p;
207  }
208 
209  template<typename M_, typename W_>
210  void
211  Constant<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
212  {
213  v.constant(value_);
214  }
215 
216  template<typename M_, typename W_>
217  bool
218  Constant<M_,W_>::operator!=(const Node<M_, W_>& other) const
219  {
220  const Constant<M_, W_>* otherp =
221  dynamic_cast<const Constant<M_, W_>*>(&other);
222  if(!otherp)
223  return true;
224  return (value_ != otherp->value_);
225  }
226 
227  template<typename M_, typename W_>
228  bool
229  Constant<M_,W_>::operator<(const Node<M_, W_>& other) const
230  {
231  const Constant<M_, W_>* otherp =
232  dynamic_cast<const Constant<M_, W_>*>(&other);
233  if (otherp)
234  return value_ < otherp->value_;
235  else
236  return what() < other.what();
237  }
238 
239  template<typename M_, typename W_>
240 
241  Constant<M_,W_>::~Constant()
242  {};
243 
244  /*-------------.
245  | LeftWeighted |
246  `-------------*/
247  template<typename M_, typename W_>
248  LeftWeighted<M_,W_>::LeftWeighted(const W_& w, const Node<M_, W_>& c)
249  : weight_(w), child_(c.clone())
250  {}
251 
252  template<typename M_, typename W_>
253  LeftWeighted<M_,W_>::LeftWeighted(const W_& w, Node<M_, W_>* c)
254  : weight_(w), child_(c)
255  {}
256 
257  template<typename M_, typename W_>
258  LeftWeighted<M_,W_>::LeftWeighted(const W_& w)
259  : weight_(w),
260  child_(new One<M_, W_>)
261  {}
262 
263  template<typename M_, typename W_>
264  typename Node<M_, W_>::type
265  LeftWeighted<M_,W_>::what() const
266  {
267  return Node<M_, W_>::lweight;
268  }
269 
270  template<typename M_, typename W_>
271  Node<M_, W_>*
272  LeftWeighted<M_,W_>::clone() const
273  {
274  Node<M_, W_>* p = new LeftWeighted<M_, W_>(weight_, *child_);
275  return p;
276  }
277 
278  template<typename M_, typename W_>
279  void
280  LeftWeighted<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
281  {
282  v.left_weight(weight_, child_);
283  }
284 
285  template<typename M_, typename W_>
286  bool
287  LeftWeighted<M_,W_>::operator!=(const Node<M_, W_>& other) const
288  {
289  const LeftWeighted<M_, W_>* otherp =
290  dynamic_cast<const LeftWeighted<M_, W_>*>(&other);
291  if(!otherp || (weight_ != otherp->weight_))
292  return true;
293  return (*child_ != *otherp->child_);
294  }
295 
296  template<typename M_, typename W_>
297  bool
298  LeftWeighted<M_,W_>::operator<(const Node<M_, W_>& other) const
299  {
300  const LeftWeighted<M_, W_>* otherp =
301  dynamic_cast<const LeftWeighted<M_, W_>*>(&other);
302  if (otherp)
303  {
304  if (weight_ == otherp->weight_)
305  return *child_ < *otherp->child_;
306  else
307  return weight_ < otherp->weight_;
308  }
309  else
310  return what() < other.what();
311  }
312 
313  template<typename M_, typename W_>
314 
315  LeftWeighted<M_,W_>::~LeftWeighted()
316  {
317  delete child_;
318  }
319 
320  /*--------------.
321  | RightWeighted |
322  `--------------*/
323  template<typename M_, typename W_>
324  RightWeighted<M_,W_>::RightWeighted(const W_& w, const Node<M_, W_>& c)
325  : weight_(w), child_(c.clone())
326  {}
327 
328  template<typename M_, typename W_>
329  RightWeighted<M_,W_>::RightWeighted(const W_& w, Node<M_, W_>* c)
330  : weight_(w), child_(c)
331  {}
332 
333  template<typename M_, typename W_>
334  RightWeighted<M_,W_>::RightWeighted(const W_& w)
335  : weight_(w),
336  child_(new One<M_, W_>)
337  {}
338 
339  template<typename M_, typename W_>
340  typename Node<M_, W_>::type
341  RightWeighted<M_,W_>::what() const
342  {
343  return Node<M_, W_>::rweight;
344  }
345 
346  template<typename M_, typename W_>
347  Node<M_, W_>*
348  RightWeighted<M_,W_>::clone() const
349  {
350  Node<M_, W_>* p = new RightWeighted<M_, W_>(weight_, *child_);
351  return p;
352  }
353 
354  template<typename M_, typename W_>
355  void
356  RightWeighted<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
357  {
358  v.right_weight(weight_, child_);
359  }
360 
361  template<typename M_, typename W_>
362  bool
363  RightWeighted<M_,W_>::operator!=(const Node<M_, W_>& other) const
364  {
365  const RightWeighted<M_, W_>* otherp =
366  dynamic_cast<const RightWeighted<M_, W_>*>(&other);
367  if(!otherp || (weight_ != otherp->weight_))
368  return true;
369  return (*child_ != *otherp->child_);
370  }
371 
372  template<typename M_, typename W_>
373  bool
374  RightWeighted<M_,W_>::operator<(const Node<M_, W_>& other) const
375  {
376  const RightWeighted<M_, W_>* otherp =
377  dynamic_cast<const RightWeighted<M_, W_>*>(&other);
378  if (otherp)
379  {
380  if (weight_ == otherp->weight_)
381  return *child_ < *otherp->child_;
382  else
383  return weight_ < otherp->weight_;
384  }
385  else
386  return what() < other.what();
387  }
388 
389  template<typename M_, typename W_>
390 
391  RightWeighted<M_,W_>::~RightWeighted()
392  {
393  delete child_;
394  }
395 
396  /*-----.
397  | Star |
398  `-----*/
399  template <class M_,class W_>
400  Star<M_,W_>::Star(const Node<M_, W_>& other)
401  : child_(other.clone())
402  {}
403 
404  template <class M_,class W_>
405  Star<M_,W_>::Star(Node<M_, W_>* other)
406  : child_(other)
407  {}
408 
409  template <class M_,class W_>
410  typename Node<M_, W_>::type
411  Star<M_,W_>::what() const
412  {
413  return Node<M_, W_>::star;
414  }
415 
416  template <class M_,class W_>
417  Node<M_, W_>*
418  Star<M_,W_>::clone() const
419  {
420  Node<M_, W_>* p = new Star<M_, W_>(*child_);
421  return p;
422  }
423 
424  template <class M_,class W_>
425  void
426  Star<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
427  {
428  v.star(child_);
429  }
430 
431  template <class M_,class W_>
432  bool
433  Star<M_,W_>::operator!=(const Node<M_, W_>& other) const
434  {
435  const Star<M_, W_>* otherp =
436  dynamic_cast<const Star<M_, W_>*>(&other);
437  if(!otherp)
438  return true;
439  return (*child_ != *otherp->child_);
440  }
441 
442  template <class M_,class W_>
443  bool
444  Star<M_,W_>::operator<(const Node<M_, W_>& other) const
445  {
446  const Star<M_, W_>* otherp =
447  dynamic_cast<const Star<M_, W_>*>(&other);
448  if (otherp)
449  return *child_ < *otherp->child_;
450  else
451  return what() < other.what();
452  }
453 
454  template <class M_,class W_>
455 
456  Star<M_,W_>::~Star()
457  {
458  delete child_;
459  }
460 
461  /*--------.
462  | Product |
463  `--------*/
464  template <class M_,class W_>
465  Product<M_,W_>::Product(const Node<M_, W_>& left,
466  const Node<M_, W_>& right)
467  : left_(left.clone()), right_(right.clone())
468  {}
469 
470  template <class M_,class W_>
471  Product<M_,W_>::Product(Node<M_, W_>* left, Node<M_, W_>* right)
472  : left_(left), right_(right)
473  {}
474 
475  template <class M_,class W_>
476  typename Node<M_, W_>::type
477  Product<M_,W_>::what() const
478  {
479  return Node<M_, W_>::prod;
480  }
481 
482  template <class M_,class W_>
483  Node<M_, W_>*
484  Product<M_,W_>::clone() const
485  {
486  Node<M_, W_>* p = new Product<M_, W_>(*left_, *right_);
487  return p;
488  }
489 
490  template <class M_,class W_>
491  void
492  Product<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
493  {
494  return v.product(left_, right_);
495  }
496 
497  template <class M_,class W_>
498  bool
499  Product<M_,W_>::operator!=(const Node<M_, W_>& other) const
500  {
501  const Product<M_, W_>* otherp =
502  dynamic_cast<const Product<M_, W_>*>(&other);
503  if(!otherp || (*left_ != *otherp->left_))
504  return true;
505  return (*right_ != *otherp->right_);
506  }
507 
508  template <class M_,class W_>
509  bool
510  Product<M_,W_>::operator<(const Node<M_, W_>& other) const
511  {
512  const Product<M_, W_>* otherp =
513  dynamic_cast<const Product<M_, W_>*>(&other);
514  if (otherp)
515  {
516  if (*left_ != *otherp->left_)
517  return *left_ < *otherp->left_;
518  else
519  return *right_ < *otherp->right_;
520  }
521  else
522  return what() < other.what();
523  }
524 
525  template <class M_,class W_>
526 
527  Product<M_,W_>::~Product()
528  {
529  delete right_;
530  delete left_;
531  }
532 
533  /*----.
534  | Sum |
535  `----*/
536  template<typename M_, typename W_>
537  Sum<M_,W_>::Sum(const Node<M_, W_>& left, const Node<M_, W_>& right)
538  : left_(left.clone()), right_(right.clone())
539  {}
540 
541  template<typename M_, typename W_>
542  Sum<M_,W_>::Sum(Node<M_, W_>* left, Node<M_, W_>* right)
543  : left_(left), right_(right)
544  {}
545 
546  template<typename M_, typename W_>
547  void
548  Sum<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
549  {
550  return v.sum(left_, right_);
551  }
552 
553  template<typename M_, typename W_>
554  typename Node<M_, W_>::type
555  Sum<M_,W_>::what() const
556  {
557  return Node<M_, W_>::sum;
558  }
559 
560  template<typename M_, typename W_>
561  Node<M_, W_>*
562  Sum<M_,W_>::clone() const
563  {
564  Node<M_, W_>* p = new Sum<M_, W_>(*left_, *right_);
565  return p;
566  }
567 
568  template<typename M_, typename W_>
569  bool
570  Sum<M_,W_>::operator!=(const Node<M_, W_>& other) const
571  {
572  const Sum<M_, W_>* otherp =
573  dynamic_cast<const Sum<M_, W_>*>(&other);
574  if(!otherp)
575  return true;
576  // X + Y and Y + X are NOT equal
577  // Indeed : if b + X < a + c and a + c = c + a
578  // then b + X > a + c !
579  return (*left_ != *otherp->left_) || (*right_ != *otherp->right_);
580  }
581 
582  template<typename M_, typename W_>
583  bool
584  Sum<M_,W_>::operator<(const Node<M_, W_>& other) const
585  {
586  const Sum<M_, W_>* otherp =
587  dynamic_cast<const Sum<M_, W_>*>(&other);
588  if (otherp)
589  {
590  if (*left_ != *otherp->left_)
591  return *left_ < *otherp->left_;
592  else
593  return *right_ < *otherp->right_;
594  }
595  else
596  return what() < other.what();
597  }
598 
599  template<typename M_, typename W_>
600 
601  Sum<M_,W_>::~Sum()
602  {
603  delete right_;
604  delete left_;
605  }
606 
607  } // End of namespace rat.
608 
609 } // End of namespace vcsn.
610 
611 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_NODES_HXX