Vaucanson 1.4
nodes.hxx
00001 // nodes.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2008 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_NODES_HXX
00018 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_NODES_HXX
00019 
00020 # include <vaucanson/algebra/implementation/series/rat/nodes.hh>
00021 
00022 namespace vcsn {
00023 
00024   namespace rat {
00025 
00026     template<class M_, class W_>
00027     void
00028     DefaultMutableNodeVisitor<M_,W_>::product(Node<M_, W_>* lhs,
00029                                               Node<M_, W_>* rhs)
00030     {
00031       lhs->accept(*this);
00032       rhs->accept(*this);
00033     }
00034 
00035     template<class M_, class W_>
00036     void DefaultMutableNodeVisitor<M_,W_>::sum(Node<M_, W_>* lhs,
00037                                                Node<M_, W_>* rhs)
00038     {
00039       lhs->accept(*this);
00040       rhs->accept(*this);
00041     }
00042 
00043     template<class M_, class W_>
00044     void DefaultMutableNodeVisitor<M_,W_>::star(Node<M_, W_>* n)
00045     {
00046       n->accept(*this);
00047     }
00048 
00049     template<class M_, class W_>
00050     void DefaultMutableNodeVisitor<M_,W_>::left_weight(W_&,
00051                                                        Node<M_, W_>* n)
00052     {
00053       n->accept(*this);
00054     }
00055 
00056     template<class M_, class W_>
00057     void DefaultMutableNodeVisitor<M_,W_>::right_weight(W_&,
00058                                                         Node<M_, W_>*n)
00059     {
00060       n->accept(*this);
00061     }
00062 
00063     template<class M_, class W_>
00064     void DefaultMutableNodeVisitor<M_,W_>::constant( M_&)
00065     {}
00066 
00067     template<class M_, class W_>
00068     void DefaultMutableNodeVisitor<M_,W_>::zero()
00069     {}
00070 
00071     template<class M_, class W_>
00072     void DefaultMutableNodeVisitor<M_,W_>::one()
00073     {}
00074 
00075     /*-----.
00076     | Node |
00077     `-----*/
00078 
00079     // Defined methods
00080     template<typename M_, typename W_>
00081     Node<M_,W_>::~Node()
00082     {
00083     }
00084 
00085     template<typename M_, typename W_>
00086     Node<M_,W_>::Node()
00087     {}
00088 
00089     /*-----.
00090     | Zero |
00091     `-----*/
00092     template <class M_, class W_>
00093     Zero<M_,W_>::Zero()
00094     {}
00095 
00096     template <class M_, class W_>
00097     typename Node<M_, W_>::type
00098     Zero<M_,W_>::what() const
00099     {
00100       return Node<M_, W_>::zero;
00101     }
00102 
00103     template <class M_, class W_>
00104     Node<M_, W_>*
00105     Zero<M_,W_>::clone() const
00106     {
00107       Node<M_, W_>* p = new Zero<M_, W_>;
00108       return p;
00109     }
00110 
00111     template <class M_, class W_>
00112     void
00113     Zero<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
00114     {
00115       v.zero();
00116     }
00117 
00118     template <class M_, class W_>
00119     bool
00120     Zero<M_,W_>::operator!=(const Node<M_, W_>& other) const
00121     {
00122       return (dynamic_cast<const Zero<M_, W_>*>(&other) == 0);
00123     }
00124 
00125     template <class M_, class W_>
00126     bool
00127     Zero<M_,W_>::operator<(const Node<M_, W_>& other) const
00128     {
00129       return what() < other.what();
00130     }
00131 
00132     template <class M_, class W_>
00133 
00134     Zero<M_,W_>::~Zero()
00135     {}
00136 
00137     /*----.
00138     | One |
00139     `----*/
00140 
00141     template<typename M_, typename W_>
00142     One<M_,W_>::One()
00143     {}
00144 
00145     template<typename M_, typename W_>
00146     typename Node<M_, W_>::type
00147     One<M_,W_>::what() const
00148     {
00149       return Node<M_, W_>::one;
00150     }
00151 
00152     template<typename M_, typename W_>
00153     Node<M_, W_>*
00154     One<M_,W_>::clone() const
00155     {
00156       Node<M_, W_>* p = new One<M_, W_>;
00157       return p;
00158     }
00159 
00160     template<typename M_, typename W_>
00161     void
00162     One<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
00163     {
00164       v.one();
00165     }
00166 
00167     template<typename M_, typename W_>
00168     bool
00169     One<M_,W_>::operator!=(const Node<M_, W_>& other) const
00170     {
00171       return (dynamic_cast<const One<M_, W_>*>(&other) == 0);
00172     }
00173 
00174     template<typename M_, typename W_>
00175     bool
00176     One<M_,W_>::operator<(const Node<M_, W_>& other) const
00177     {
00178       return what() < other.what();
00179     }
00180 
00181     template<typename M_, typename W_>
00182 
00183     One<M_,W_>::~One()
00184     {}
00185 
00186     /*---------.
00187     | Constant |
00188     `---------*/
00189 
00190     template<typename M_, typename W_>
00191     Constant<M_,W_>::Constant(const M_ &v) : value_(v)
00192     {}
00193 
00194     template<typename M_, typename W_>
00195     typename Node<M_, W_>::type
00196     Constant<M_,W_>::what() const
00197     {
00198       return Node<M_, W_>::constant;
00199     }
00200 
00201     template<typename M_, typename W_>
00202     Node<M_, W_>*
00203     Constant<M_,W_>::clone() const
00204     {
00205       Node<M_, W_>* p = new Constant<M_, W_>(value_);
00206       return p;
00207     }
00208 
00209     template<typename M_, typename W_>
00210     void
00211     Constant<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
00212     {
00213       v.constant(value_);
00214     }
00215 
00216     template<typename M_, typename W_>
00217     bool
00218     Constant<M_,W_>::operator!=(const Node<M_, W_>& other) const
00219     {
00220       const Constant<M_, W_>* otherp =
00221         dynamic_cast<const Constant<M_, W_>*>(&other);
00222       if(!otherp)
00223         return true;
00224       return (value_ != otherp->value_);
00225     }
00226 
00227     template<typename M_, typename W_>
00228     bool
00229     Constant<M_,W_>::operator<(const Node<M_, W_>& other) const
00230     {
00231       const Constant<M_, W_>* otherp =
00232         dynamic_cast<const Constant<M_, W_>*>(&other);
00233       if (otherp)
00234         return value_ < otherp->value_;
00235       else
00236         return what() < other.what();
00237     }
00238 
00239     template<typename M_, typename W_>
00240 
00241     Constant<M_,W_>::~Constant()
00242     {};
00243 
00244     /*-------------.
00245     | LeftWeighted |
00246     `-------------*/
00247     template<typename M_, typename W_>
00248     LeftWeighted<M_,W_>::LeftWeighted(const W_& w, const Node<M_, W_>& c)
00249       : weight_(w), child_(c.clone())
00250     {}
00251 
00252     template<typename M_, typename W_>
00253     LeftWeighted<M_,W_>::LeftWeighted(const W_& w, Node<M_, W_>* c)
00254       : weight_(w), child_(c)
00255     {}
00256 
00257     template<typename M_, typename W_>
00258     LeftWeighted<M_,W_>::LeftWeighted(const W_& w)
00259       : weight_(w),
00260         child_(new One<M_, W_>)
00261     {}
00262 
00263     template<typename M_, typename W_>
00264     typename Node<M_, W_>::type
00265     LeftWeighted<M_,W_>::what() const
00266     {
00267       return Node<M_, W_>::lweight;
00268     }
00269 
00270     template<typename M_, typename W_>
00271     Node<M_, W_>*
00272     LeftWeighted<M_,W_>::clone() const
00273     {
00274       Node<M_, W_>* p = new LeftWeighted<M_, W_>(weight_, *child_);
00275       return p;
00276     }
00277 
00278     template<typename M_, typename W_>
00279     void
00280     LeftWeighted<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
00281     {
00282       v.left_weight(weight_, child_);
00283     }
00284 
00285     template<typename M_, typename W_>
00286     bool
00287     LeftWeighted<M_,W_>::operator!=(const Node<M_, W_>& other) const
00288     {
00289       const LeftWeighted<M_, W_>* otherp =
00290         dynamic_cast<const LeftWeighted<M_, W_>*>(&other);
00291       if(!otherp || (weight_ != otherp->weight_))
00292         return true;
00293       return (*child_ != *otherp->child_);
00294     }
00295 
00296     template<typename M_, typename W_>
00297     bool
00298     LeftWeighted<M_,W_>::operator<(const Node<M_, W_>& other) const
00299     {
00300       const LeftWeighted<M_, W_>* otherp =
00301         dynamic_cast<const LeftWeighted<M_, W_>*>(&other);
00302       if (otherp)
00303       {
00304         if (weight_ == otherp->weight_)
00305           return *child_ < *otherp->child_;
00306         else
00307           return weight_ < otherp->weight_;
00308       }
00309       else
00310         return what() < other.what();
00311     }
00312 
00313     template<typename M_, typename W_>
00314 
00315     LeftWeighted<M_,W_>::~LeftWeighted()
00316     {
00317       delete child_;
00318     }
00319 
00320     /*--------------.
00321     | RightWeighted |
00322     `--------------*/
00323     template<typename M_, typename W_>
00324     RightWeighted<M_,W_>::RightWeighted(const W_& w, const Node<M_, W_>& c)
00325       : weight_(w), child_(c.clone())
00326     {}
00327 
00328     template<typename M_, typename W_>
00329     RightWeighted<M_,W_>::RightWeighted(const W_& w, Node<M_, W_>* c)
00330       : weight_(w), child_(c)
00331     {}
00332 
00333     template<typename M_, typename W_>
00334     RightWeighted<M_,W_>::RightWeighted(const W_& w)
00335       : weight_(w),
00336         child_(new One<M_, W_>)
00337     {}
00338 
00339     template<typename M_, typename W_>
00340     typename Node<M_, W_>::type
00341     RightWeighted<M_,W_>::what() const
00342     {
00343       return Node<M_, W_>::rweight;
00344     }
00345 
00346     template<typename M_, typename W_>
00347     Node<M_, W_>*
00348     RightWeighted<M_,W_>::clone() const
00349     {
00350       Node<M_, W_>* p = new RightWeighted<M_, W_>(weight_, *child_);
00351       return p;
00352     }
00353 
00354     template<typename M_, typename W_>
00355     void
00356     RightWeighted<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
00357     {
00358       v.right_weight(weight_, child_);
00359     }
00360 
00361     template<typename M_, typename W_>
00362     bool
00363     RightWeighted<M_,W_>::operator!=(const Node<M_, W_>& other) const
00364     {
00365       const RightWeighted<M_, W_>* otherp =
00366         dynamic_cast<const RightWeighted<M_, W_>*>(&other);
00367       if(!otherp || (weight_ != otherp->weight_))
00368         return true;
00369       return (*child_ != *otherp->child_);
00370     }
00371 
00372     template<typename M_, typename W_>
00373     bool
00374     RightWeighted<M_,W_>::operator<(const Node<M_, W_>& other) const
00375     {
00376       const RightWeighted<M_, W_>* otherp =
00377         dynamic_cast<const RightWeighted<M_, W_>*>(&other);
00378       if (otherp)
00379       {
00380         if (weight_ == otherp->weight_)
00381           return *child_ < *otherp->child_;
00382         else
00383           return weight_ < otherp->weight_;
00384       }
00385       else
00386         return what() < other.what();
00387     }
00388 
00389     template<typename M_, typename W_>
00390 
00391     RightWeighted<M_,W_>::~RightWeighted()
00392     {
00393       delete child_;
00394     }
00395 
00396     /*-----.
00397     | Star |
00398     `-----*/
00399     template <class M_,class W_>
00400     Star<M_,W_>::Star(const Node<M_, W_>& other)
00401       : child_(other.clone())
00402     {}
00403 
00404     template <class M_,class W_>
00405     Star<M_,W_>::Star(Node<M_, W_>* other)
00406       : child_(other)
00407     {}
00408 
00409     template <class M_,class W_>
00410     typename Node<M_, W_>::type
00411     Star<M_,W_>::what() const
00412     {
00413       return Node<M_, W_>::star;
00414     }
00415 
00416     template <class M_,class W_>
00417     Node<M_, W_>*
00418     Star<M_,W_>::clone() const
00419     {
00420       Node<M_, W_>* p = new Star<M_, W_>(*child_);
00421       return p;
00422     }
00423 
00424     template <class M_,class W_>
00425     void
00426     Star<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
00427     {
00428       v.star(child_);
00429     }
00430 
00431     template <class M_,class W_>
00432     bool
00433     Star<M_,W_>::operator!=(const Node<M_, W_>& other) const
00434     {
00435       const Star<M_, W_>* otherp =
00436         dynamic_cast<const Star<M_, W_>*>(&other);
00437       if(!otherp)
00438         return true;
00439       return (*child_ != *otherp->child_);
00440     }
00441 
00442     template <class M_,class W_>
00443     bool
00444     Star<M_,W_>::operator<(const Node<M_, W_>& other) const
00445     {
00446       const Star<M_, W_>* otherp =
00447         dynamic_cast<const Star<M_, W_>*>(&other);
00448       if (otherp)
00449         return *child_ < *otherp->child_;
00450       else
00451         return what() < other.what();
00452     }
00453 
00454     template <class M_,class W_>
00455 
00456     Star<M_,W_>::~Star()
00457     {
00458       delete child_;
00459     }
00460 
00461     /*--------.
00462     | Product |
00463     `--------*/
00464     template <class M_,class W_>
00465     Product<M_,W_>::Product(const Node<M_, W_>& left,
00466                             const Node<M_, W_>& right)
00467       : left_(left.clone()), right_(right.clone())
00468     {}
00469 
00470     template <class M_,class W_>
00471     Product<M_,W_>::Product(Node<M_, W_>* left, Node<M_, W_>* right)
00472       : left_(left), right_(right)
00473     {}
00474 
00475     template <class M_,class W_>
00476     typename Node<M_, W_>::type
00477     Product<M_,W_>::what() const
00478     {
00479       return Node<M_, W_>::prod;
00480     }
00481 
00482     template <class M_,class W_>
00483     Node<M_, W_>*
00484     Product<M_,W_>::clone() const
00485     {
00486       Node<M_, W_>* p = new Product<M_, W_>(*left_, *right_);
00487       return p;
00488     }
00489 
00490     template <class M_,class W_>
00491     void
00492     Product<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
00493     {
00494       return v.product(left_, right_);
00495     }
00496 
00497     template <class M_,class W_>
00498     bool
00499     Product<M_,W_>::operator!=(const Node<M_, W_>& other) const
00500     {
00501       const Product<M_, W_>* otherp =
00502         dynamic_cast<const Product<M_, W_>*>(&other);
00503       if(!otherp || (*left_ != *otherp->left_))
00504         return true;
00505       return (*right_ != *otherp->right_);
00506     }
00507 
00508     template <class M_,class W_>
00509     bool
00510     Product<M_,W_>::operator<(const Node<M_, W_>& other) const
00511     {
00512       const Product<M_, W_>* otherp =
00513         dynamic_cast<const Product<M_, W_>*>(&other);
00514       if (otherp)
00515       {
00516         if (*left_ != *otherp->left_)
00517           return *left_ < *otherp->left_;
00518         else
00519           return *right_ < *otherp->right_;
00520       }
00521       else
00522         return what() < other.what();
00523     }
00524 
00525     template <class M_,class W_>
00526 
00527     Product<M_,W_>::~Product()
00528     {
00529       delete right_;
00530       delete left_;
00531     }
00532 
00533     /*----.
00534     | Sum |
00535     `----*/
00536     template<typename M_, typename W_>
00537     Sum<M_,W_>::Sum(const Node<M_, W_>& left, const Node<M_, W_>& right)
00538       : left_(left.clone()), right_(right.clone())
00539     {}
00540 
00541     template<typename M_, typename W_>
00542     Sum<M_,W_>::Sum(Node<M_, W_>* left, Node<M_, W_>* right)
00543       : left_(left), right_(right)
00544     {}
00545 
00546     template<typename M_, typename W_>
00547     void
00548     Sum<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
00549     {
00550       return v.sum(left_, right_);
00551     }
00552 
00553     template<typename M_, typename W_>
00554     typename Node<M_, W_>::type
00555     Sum<M_,W_>::what() const
00556     {
00557       return Node<M_, W_>::sum;
00558     }
00559 
00560     template<typename M_, typename W_>
00561     Node<M_, W_>*
00562     Sum<M_,W_>::clone() const
00563     {
00564       Node<M_, W_>* p = new Sum<M_, W_>(*left_, *right_);
00565       return p;
00566     }
00567 
00568     template<typename M_, typename W_>
00569     bool
00570     Sum<M_,W_>::operator!=(const Node<M_, W_>& other) const
00571     {
00572       const Sum<M_, W_>* otherp =
00573         dynamic_cast<const Sum<M_, W_>*>(&other);
00574       if(!otherp)
00575         return true;
00576       // X + Y and Y + X are NOT equal
00577       // Indeed : if   b + X < a + c   and   a + c = c + a
00578       // then   b + X > a + c   !
00579       return (*left_ != *otherp->left_) || (*right_ != *otherp->right_);
00580     }
00581 
00582     template<typename M_, typename W_>
00583     bool
00584     Sum<M_,W_>::operator<(const Node<M_, W_>& other) const
00585     {
00586       const Sum<M_, W_>* otherp =
00587         dynamic_cast<const Sum<M_, W_>*>(&other);
00588       if (otherp)
00589       {
00590         if (*left_ != *otherp->left_)
00591           return *left_ < *otherp->left_;
00592         else
00593           return *right_ < *otherp->right_;
00594       }
00595       else
00596         return what() < other.what();
00597     }
00598 
00599     template<typename M_, typename W_>
00600 
00601     Sum<M_,W_>::~Sum()
00602     {
00603       delete right_;
00604       delete left_;
00605     }
00606 
00607   } // End of namespace rat.
00608 
00609 } // End of namespace vcsn.
00610 
00611 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_NODES_HXX