00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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
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 }
00128
00129 }
00130
00131 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_RANDOM_VISITOR_HXX