- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

# 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 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       delete constant_term_;
    00084     }
    00085
    00086     template<typename M_, typename W_>
    00087     W_* &               Node<M_,W_>::c()
    00088     {
    00089       return constant_term_;
    00090     }
    00091
    00092     template<typename M_, typename W_>  W_ const * &
    00093     Node<M_,W_>::c() const
    00094     {
    00095       return constant_term_;
    00096     }
    00097
    00098     template<typename M_, typename W_>
    00099     Node<M_,W_>::Node()
    00100       : constant_term_(0)
    00101     {}
    00102
    00103     /*-----.
    00104     | Zero |
    00105     `-----*/
    00106     template <class M_, class W_>
    00107     Zero<M_,W_>::Zero()
    00108     {}
    00109
    00110     template <class M_, class W_>
    00111     typename Node<M_, W_>::type
    00112     Zero<M_,W_>::what() const
    00113     {
    00114       return Node<M_, W_>::zero;
    00115     }
    00116
    00117     template <class M_, class W_>
    00118     Node<M_, W_>*
    00119     Zero<M_,W_>::clone() const
    00120     {
    00121       Node<M_, W_>* p = new Zero<M_, W_>;
    00122       if (this->constant_term_)
    00123         p->constant_term_ = new W_ (*(this->constant_term_));
    00124       return p;
    00125     }
    00126
    00127     template <class M_, class W_>
    00128     void
    00129     Zero<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
    00130     {
    00131       v.zero();
    00132     }
    00133
    00134     template <class M_, class W_>
    00135     bool
    00136     Zero<M_,W_>::operator!=(const Node<M_, W_>& other) const
    00137     {
    00138       return (dynamic_cast<const Zero<M_, W_>*>(&other) == 0);
    00139     }
    00140
    00141     template <class M_, class W_>
    00142     bool
    00143     Zero<M_,W_>::operator<(const Node<M_, W_>& other) const
    00144     {
    00145       return what() < other.what();
    00146     }
    00147
    00148     template <class M_, class W_>
    00149
    00150     Zero<M_,W_>::~Zero()
    00151     {}
    00152
    00153     /*----.
    00154     | One |
    00155     `----*/
    00156
    00157     template<typename M_, typename W_>
    00158     One<M_,W_>::One()
    00159     {}
    00160
    00161     template<typename M_, typename W_>
    00162     typename Node<M_, W_>::type
    00163     One<M_,W_>::what() const
    00164     {
    00165       return Node<M_, W_>::one;
    00166     }
    00167
    00168     template<typename M_, typename W_>
    00169     Node<M_, W_>*
    00170     One<M_,W_>::clone() const
    00171     {
    00172       Node<M_, W_>* p = new One<M_, W_>;
    00173       if (this->constant_term_)
    00174         p->constant_term_ = new W_ (*(this->constant_term_));
    00175       return p;
    00176     }
    00177
    00178     template<typename M_, typename W_>
    00179     void
    00180     One<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
    00181     {
    00182       v.one();
    00183     }
    00184
    00185     template<typename M_, typename W_>
    00186     bool
    00187     One<M_,W_>::operator!=(const Node<M_, W_>& other) const
    00188     {
    00189       return (dynamic_cast<const One<M_, W_>*>(&other) == 0);
    00190     }
    00191
    00192     template<typename M_, typename W_>
    00193     bool
    00194     One<M_,W_>::operator<(const Node<M_, W_>& other) const
    00195     {
    00196       return what() < other.what();
    00197     }
    00198
    00199     template<typename M_, typename W_>
    00200
    00201     One<M_,W_>::~One()
    00202     {}
    00203
    00204     /*---------.
    00205     | Constant |
    00206     `---------*/
    00207
    00208     template<typename M_, typename W_>
    00209     Constant<M_,W_>::Constant(const M_ &v) : value_(v)
    00210     {}
    00211
    00212     template<typename M_, typename W_>
    00213     typename Node<M_, W_>::type
    00214     Constant<M_,W_>::what() const
    00215     {
    00216       return Node<M_, W_>::constant;
    00217     }
    00218
    00219     template<typename M_, typename W_>
    00220     Node<M_, W_>*
    00221     Constant<M_,W_>::clone() const
    00222     {
    00223       Node<M_, W_>* p = new Constant<M_, W_>(value_);
    00224       if (this->constant_term_)
    00225         p->constant_term_ = new W_ (*(this->constant_term_));
    00226       return p;
    00227     }
    00228
    00229     template<typename M_, typename W_>
    00230     void
    00231     Constant<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
    00232     {
    00233       v.constant(value_);
    00234     }
    00235
    00236     template<typename M_, typename W_>
    00237     bool
    00238     Constant<M_,W_>::operator!=(const Node<M_, W_>& other) const
    00239     {
    00240       const Constant<M_, W_>* otherp =
    00241         dynamic_cast<const Constant<M_, W_>*>(&other);
    00242       if(!otherp)
    00243         return true;
    00244       return (value_ != otherp->value_);
    00245     }
    00246
    00247     template<typename M_, typename W_>
    00248     bool
    00249     Constant<M_,W_>::operator<(const Node<M_, W_>& other) const
    00250     {
    00251       const Constant<M_, W_>* otherp =
    00252         dynamic_cast<const Constant<M_, W_>*>(&other);
    00253       if (otherp)
    00254         return value_ < otherp->value_;
    00255       else
    00256         return what() < other.what();
    00257     }
    00258
    00259     template<typename M_, typename W_>
    00260
    00261     Constant<M_,W_>::~Constant()
    00262     {};
    00263
    00264     /*-------------.
    00265     | LeftWeighted |
    00266     `-------------*/
    00267     template<typename M_, typename W_>
    00268     LeftWeighted<M_,W_>::LeftWeighted(const W_& w, const Node<M_, W_>& c)
    00269       : weight_(w), child_(c.clone())
    00270     {}
    00271
    00272     template<typename M_, typename W_>
    00273     LeftWeighted<M_,W_>::LeftWeighted(const W_& w, Node<M_, W_>* c)
    00274       : weight_(w), child_(c)
    00275     {}
    00276
    00277     template<typename M_, typename W_>
    00278     LeftWeighted<M_,W_>::LeftWeighted(const W_& w)
    00279       : weight_(w),
    00280         child_(new One<M_, W_>)
    00281     {}
    00282
    00283     template<typename M_, typename W_>
    00284     typename Node<M_, W_>::type
    00285     LeftWeighted<M_,W_>::what() const
    00286     {
    00287       return Node<M_, W_>::lweight;
    00288     }
    00289
    00290     template<typename M_, typename W_>
    00291     Node<M_, W_>*
    00292     LeftWeighted<M_,W_>::clone() const
    00293     {
    00294       Node<M_, W_>* p = new LeftWeighted<M_, W_>(weight_, *child_);
    00295       if (this->constant_term_)
    00296         p->constant_term_ = new W_ (*(this->constant_term_));
    00297       return p;
    00298     }
    00299
    00300     template<typename M_, typename W_>
    00301     void
    00302     LeftWeighted<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
    00303     {
    00304       v.left_weight(weight_, child_);
    00305     }
    00306
    00307     template<typename M_, typename W_>
    00308     bool
    00309     LeftWeighted<M_,W_>::operator!=(const Node<M_, W_>& other) const
    00310     {
    00311       const LeftWeighted<M_, W_>* otherp =
    00312         dynamic_cast<const LeftWeighted<M_, W_>*>(&other);
    00313       if(!otherp || (weight_ != otherp->weight_))
    00314         return true;
    00315       return (*child_ != *otherp->child_);
    00316     }
    00317
    00318     template<typename M_, typename W_>
    00319     bool
    00320     LeftWeighted<M_,W_>::operator<(const Node<M_, W_>& other) const
    00321     {
    00322       const LeftWeighted<M_, W_>* otherp =
    00323         dynamic_cast<const LeftWeighted<M_, W_>*>(&other);
    00324       if (otherp)
    00325       {
    00326         if (weight_ == otherp->weight_)
    00327           return *child_ < *otherp->child_;
    00328         else
    00329           return weight_ < otherp->weight_;
    00330       }
    00331       else
    00332         return what() < other.what();
    00333     }
    00334
    00335     template<typename M_, typename W_>
    00336
    00337     LeftWeighted<M_,W_>::~LeftWeighted()
    00338     {
    00339       delete child_;
    00340     }
    00341
    00342     /*--------------.
    00343     | RightWeighted |
    00344     `--------------*/
    00345     template<typename M_, typename W_>
    00346     RightWeighted<M_,W_>::RightWeighted(const W_& w, const Node<M_, W_>& c)
    00347       : weight_(w), child_(c.clone())
    00348     {}
    00349
    00350     template<typename M_, typename W_>
    00351     RightWeighted<M_,W_>::RightWeighted(const W_& w, Node<M_, W_>* c)
    00352       : weight_(w), child_(c)
    00353     {}
    00354
    00355     template<typename M_, typename W_>
    00356     RightWeighted<M_,W_>::RightWeighted(const W_& w)
    00357       : weight_(w),
    00358         child_(new One<M_, W_>)
    00359     {}
    00360
    00361     template<typename M_, typename W_>
    00362     typename Node<M_, W_>::type
    00363     RightWeighted<M_,W_>::what() const
    00364     {
    00365       return Node<M_, W_>::rweight;
    00366     }
    00367
    00368     template<typename M_, typename W_>
    00369     Node<M_, W_>*
    00370     RightWeighted<M_,W_>::clone() const
    00371     {
    00372       Node<M_, W_>* p = new RightWeighted<M_, W_>(weight_, *child_);
    00373       if (this->constant_term_)
    00374         p->constant_term_ = new W_(*(this->constant_term_));
    00375       return p;
    00376     }
    00377
    00378     template<typename M_, typename W_>
    00379     void
    00380     RightWeighted<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
    00381     {
    00382       v.right_weight(weight_, child_);
    00383     }
    00384
    00385     template<typename M_, typename W_>
    00386     bool
    00387     RightWeighted<M_,W_>::operator!=(const Node<M_, W_>& other) const
    00388     {
    00389       const RightWeighted<M_, W_>* otherp =
    00390         dynamic_cast<const RightWeighted<M_, W_>*>(&other);
    00391       if(!otherp || (weight_ != otherp->weight_))
    00392         return true;
    00393       return (*child_ != *otherp->child_);
    00394     }
    00395
    00396     template<typename M_, typename W_>
    00397     bool
    00398     RightWeighted<M_,W_>::operator<(const Node<M_, W_>& other) const
    00399     {
    00400       const RightWeighted<M_, W_>* otherp =
    00401         dynamic_cast<const RightWeighted<M_, W_>*>(&other);
    00402       if (otherp)
    00403       {
    00404         if (weight_ == otherp->weight_)
    00405           return *child_ < *otherp->child_;
    00406         else
    00407           return weight_ < otherp->weight_;
    00408       }
    00409       else
    00410         return what() < other.what();
    00411     }
    00412
    00413     template<typename M_, typename W_>
    00414
    00415     RightWeighted<M_,W_>::~RightWeighted()
    00416     {
    00417       delete child_;
    00418     }
    00419
    00420     /*-----.
    00421     | Star |
    00422     `-----*/
    00423     template <class M_,class W_>
    00424     Star<M_,W_>::Star(const Node<M_, W_>& other)
    00425       : child_(other.clone())
    00426     {}
    00427
    00428     template <class M_,class W_>
    00429     Star<M_,W_>::Star(Node<M_, W_>* other)
    00430       : child_(other)
    00431     {}
    00432
    00433     template <class M_,class W_>
    00434     typename Node<M_, W_>::type
    00435     Star<M_,W_>::what() const
    00436     {
    00437       return Node<M_, W_>::star;
    00438     }
    00439
    00440     template <class M_,class W_>
    00441     Node<M_, W_>*
    00442     Star<M_,W_>::clone() const
    00443     {
    00444       Node<M_, W_>* p = new Star<M_, W_>(*child_);
    00445       if (this->constant_term_)
    00446         p->constant_term_ = new W_(*(this->constant_term_));
    00447       return p;
    00448     }
    00449
    00450     template <class M_,class W_>
    00451     void
    00452     Star<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
    00453     {
    00454       v.star(child_);
    00455     }
    00456
    00457     template <class M_,class W_>
    00458     bool
    00459     Star<M_,W_>::operator!=(const Node<M_, W_>& other) const
    00460     {
    00461       const Star<M_, W_>* otherp =
    00462         dynamic_cast<const Star<M_, W_>*>(&other);
    00463       if(!otherp)
    00464         return true;
    00465       return (*child_ != *otherp->child_);
    00466     }
    00467
    00468     template <class M_,class W_>
    00469     bool
    00470     Star<M_,W_>::operator<(const Node<M_, W_>& other) const
    00471     {
    00472       const Star<M_, W_>* otherp =
    00473         dynamic_cast<const Star<M_, W_>*>(&other);
    00474       if (otherp)
    00475         return *child_ < *otherp->child_;
    00476       else
    00477         return what() < other.what();
    00478     }
    00479
    00480     template <class M_,class W_>
    00481
    00482     Star<M_,W_>::~Star()
    00483     {
    00484       delete child_;
    00485     }
    00486
    00487     /*--------.
    00488     | Product |
    00489     `--------*/
    00490     template <class M_,class W_>
    00491     Product<M_,W_>::Product(const Node<M_, W_>& left,
    00492                             const Node<M_, W_>& right)
    00493       : left_(left.clone()), right_(right.clone())
    00494     {}
    00495
    00496     template <class M_,class W_>
    00497     Product<M_,W_>::Product(Node<M_, W_>* left, Node<M_, W_>* right)
    00498       : left_(left), right_(right)
    00499     {}
    00500
    00501     template <class M_,class W_>
    00502     typename Node<M_, W_>::type
    00503     Product<M_,W_>::what() const
    00504     {
    00505       return Node<M_, W_>::prod;
    00506     }
    00507
    00508     template <class M_,class W_>
    00509     Node<M_, W_>*
    00510     Product<M_,W_>::clone() const
    00511     {
    00512       Node<M_, W_>* p = new Product<M_, W_>(*left_, *right_);
    00513       if (this->constant_term_)
    00514         p->constant_term_ = new W_(*(this->constant_term_));
    00515       return p;
    00516     }
    00517
    00518     template <class M_,class W_>
    00519     void
    00520     Product<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
    00521     {
    00522       return v.product(left_, right_);
    00523     }
    00524
    00525     template <class M_,class W_>
    00526     bool
    00527     Product<M_,W_>::operator!=(const Node<M_, W_>& other) const
    00528     {
    00529       const Product<M_, W_>* otherp =
    00530         dynamic_cast<const Product<M_, W_>*>(&other);
    00531       if(!otherp || (*left_ != *otherp->left_))
    00532         return true;
    00533       return (*right_ != *otherp->right_);
    00534     }
    00535
    00536     template <class M_,class W_>
    00537     bool
    00538     Product<M_,W_>::operator<(const Node<M_, W_>& other) const
    00539     {
    00540       const Product<M_, W_>* otherp =
    00541         dynamic_cast<const Product<M_, W_>*>(&other);
    00542       if (otherp)
    00543       {
    00544         if (*left_ != *otherp->left_)
    00545           return *left_ < *otherp->left_;
    00546         else
    00547           return *right_ < *otherp->right_;
    00548       }
    00549       else
    00550         return what() < other.what();
    00551     }
    00552
    00553     template <class M_,class W_>
    00554
    00555     Product<M_,W_>::~Product()
    00556     {
    00557       delete right_;
    00558       delete left_;
    00559     }
    00560
    00561     /*----.
    00562     | Sum |
    00563     `----*/
    00564     template<typename M_, typename W_>
    00565     Sum<M_,W_>::Sum(const Node<M_, W_>& left, const Node<M_, W_>& right)
    00566       : left_(left.clone()), right_(right.clone())
    00567     {}
    00568
    00569     template<typename M_, typename W_>
    00570     Sum<M_,W_>::Sum(Node<M_, W_>* left, Node<M_, W_>* right)
    00571       : left_(left), right_(right)
    00572     {}
    00573
    00574     template<typename M_, typename W_>
    00575     void
    00576     Sum<M_,W_>::accept(ConstNodeVisitor<M_, W_>& v) const
    00577     {
    00578       return v.sum(left_, right_);
    00579     }
    00580
    00581     template<typename M_, typename W_>
    00582     typename Node<M_, W_>::type
    00583     Sum<M_,W_>::what() const
    00584     {
    00585       return Node<M_, W_>::sum;
    00586     }
    00587
    00588     template<typename M_, typename W_>
    00589     Node<M_, W_>*
    00590     Sum<M_,W_>::clone() const
    00591     {
    00592       Node<M_, W_>* p = new Sum<M_, W_>(*left_, *right_);
    00593       if (this->constant_term_)
    00594         p->constant_term_ = new W_(*(this->constant_term_));
    00595       return p;
    00596     }
    00597
    00598     template<typename M_, typename W_>
    00599     bool
    00600     Sum<M_,W_>::operator!=(const Node<M_, W_>& other) const
    00601     {
    00602       const Sum<M_, W_>* otherp =
    00603         dynamic_cast<const Sum<M_, W_>*>(&other);
    00604       if(!otherp)
    00605         return true;
    00606       // X + Y and Y + X are NOT equal
    00607       // Indeed : if   b + X < a + c   and   a + c = c + a
    00608       // then   b + X > a + c   !
    00609       return (*left_ != *otherp->left_) || (*right_ != *otherp->right_);
    00610     }
    00611
    00612     template<typename M_, typename W_>
    00613     bool
    00614     Sum<M_,W_>::operator<(const Node<M_, W_>& other) const
    00615     {
    00616       const Sum<M_, W_>* otherp =
    00617         dynamic_cast<const Sum<M_, W_>*>(&other);
    00618       if (otherp)
    00619       {
    00620         if (*left_ != *otherp->left_)
    00621           return *left_ < *otherp->left_;
    00622         else
    00623           return *right_ < *otherp->right_;
    00624       }
    00625       else
    00626         return what() < other.what();
    00627     }
    00628
    00629     template<typename M_, typename W_>
    00630
    00631     Sum<M_,W_>::~Sum()
    00632     {
    00633       delete right_;
    00634       delete left_;
    00635     }
    00636
    00637   } // End of namespace rat.
    00638
    00639 } // End of namespace vcsn.
    00640
    00641 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_NODES_HXX