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