random_visitor.hxx

00001 // random_visitor.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 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_RANDOM_VISITOR_HXX
00018 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_RANDOM_VISITOR_HXX
00019 
00020 # include <algorithm>
00021 
00022 # include <vaucanson/misc/contract.hh>
00023 # include <vaucanson/algebra/implementation/series/rat/random_visitor.hh>
00024 # include <vaucanson/algebra/implementation/series/rat/nodes.hh>
00025 
00026 namespace vcsn {
00027 
00028   namespace rat {
00029 
00030     // FIXME : adapt this code to multiplicity.
00031     template <typename M_, typename W_>
00032     const
00033     unsigned RandomVisitor<M_, W_>::nb_star_max_default = 5;
00034 
00035     template <typename M_, typename W_>
00036     RandomVisitor<M_, W_>::RandomVisitor(unsigned nb_star_max) :
00037       not_empty(false),
00038       nb_star_max_(nb_star_max)
00039     {}
00040 
00041     template <typename M_, typename W_>
00042     RandomVisitor<M_, W_>::RandomVisitor() :
00043       not_empty(false),
00044       nb_star_max_(nb_star_max_default)
00045     {}
00046 
00047     template<typename M_, typename W_>
00048     void
00049     RandomVisitor<M_,W_>::product(const Node<M_, W_>* left_,
00050                                   const Node<M_, W_>* right_)
00051     {
00052       M_ tmp;
00053       right_->accept(*this);
00054       tmp = w_;
00055       left_->accept(*this);
00056       // FIXME : M_ += M_ assumed here.
00057       w_ += tmp;
00058     }
00059 
00060     template <class M_, class W_>
00061     void
00062     RandomVisitor<M_,W_>::left_weight(const W_&, const Node<M_, W_>* node)
00063     {
00064       node->accept(*this);
00065     }
00066 
00067     template <class M_, class W_>
00068     void
00069     RandomVisitor<M_,W_>::right_weight(const W_&, const Node<M_, W_>* node)
00070     {
00071       node->accept(*this);
00072     }
00073 
00074     template<typename M_, typename W_>
00075     void
00076     RandomVisitor<M_,W_>::sum(const Node<M_, W_>* left_,
00077                               const Node<M_, W_>* right_)
00078     {
00079       unsigned r = rand() * 2 / RAND_MAX;
00080       if (r < 1)
00081         left_->accept(*this);
00082       else
00083         right_->accept(*this);
00084     }
00085 
00086     template<typename M_, typename W_>
00087     void
00088     RandomVisitor<M_,W_>::star(const Node<M_, W_>* node)
00089     {
00090       not_empty = true;
00091       unsigned n = rand() * nb_star_max_ / RAND_MAX;
00092       M_       tmp;
00093 
00094       for (unsigned i = 0; i < n; ++i)
00095         {
00096           node->accept(*this);
00097           // FIXME: M_ += M_ assumed here.
00098           tmp += w_;
00099         }
00100       w_ = tmp;
00101     }
00102 
00103     template<typename M_, typename W_>
00104     void
00105     RandomVisitor<M_,W_>::constant(const M_& m)
00106     {
00107       not_empty = true;
00108       w_ = m;
00109     }
00110 
00111     template <class M_, class W_>
00112     void
00113     RandomVisitor<M_,W_>::one()
00114     {
00115       w_ = M_();
00116       not_empty = true;
00117     }
00118 
00119     template <class M_, class W_>
00120     void
00121     RandomVisitor<M_,W_>::zero()
00122     {
00123     }
00124 
00125     template<typename M_, typename W_>
00126     M_ RandomVisitor<M_,W_>::get() const
00127     {
00128       assertion(not_empty);
00129       return w_;
00130     }
00131 
00132   } // rat
00133 
00134 } // vcsn
00135 
00136 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_RANDOM_VISITOR_HXX

Generated on Fri Jul 28 12:18:51 2006 for Vaucanson by  doxygen 1.4.6