Vaucanson  1.4.1
random_visitor.hxx
1 // random_visitor.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_RANDOM_VISITOR_HXX
18 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_RANDOM_VISITOR_HXX
19 
20 # include <algorithm>
21 
23 # include <vaucanson/algebra/implementation/series/rat/random_visitor.hh>
24 # include <vaucanson/algebra/implementation/series/rat/nodes.hh>
25 
26 namespace vcsn {
27 
28  namespace rat {
29 
30  template <typename M_, typename W_>
31  RandomVisitor<M_, W_>::RandomVisitor(unsigned nb_star_max) :
32  not_empty(false),
33  nb_star_max_(nb_star_max)
34  {}
35 
36  template <typename M_, typename W_>
37  RandomVisitor<M_, W_>::RandomVisitor() :
38  not_empty(false),
39  nb_star_max_(nb_star_max_default)
40  {}
41 
42  template<typename M_, typename W_>
43  void
44  RandomVisitor<M_,W_>::product(const Node<M_, W_>* left_,
45  const Node<M_, W_>* right_)
46  {
47  M_ tmp;
48  right_->accept(*this);
49  tmp = w_;
50  left_->accept(*this);
51  // FIXME : M_ += M_ assumed here.
52  w_ += tmp;
53  }
54 
55  template <class M_, class W_>
56  void
57  RandomVisitor<M_,W_>::left_weight(const W_&, const Node<M_, W_>* node)
58  {
59  node->accept(*this);
60  }
61 
62  template <class M_, class W_>
63  void
64  RandomVisitor<M_,W_>::right_weight(const W_&, const Node<M_, W_>* node)
65  {
66  node->accept(*this);
67  }
68 
69  template<typename M_, typename W_>
70  void
71  RandomVisitor<M_,W_>::sum(const Node<M_, W_>* left_,
72  const Node<M_, W_>* right_)
73  {
74  unsigned r = rand() * 2 / RAND_MAX;
75  if (r < 1)
76  left_->accept(*this);
77  else
78  right_->accept(*this);
79  }
80 
81  template<typename M_, typename W_>
82  void
83  RandomVisitor<M_,W_>::star(const Node<M_, W_>* node)
84  {
85  not_empty = true;
86  unsigned n = rand() * nb_star_max_ / RAND_MAX;
87  M_ tmp;
88 
89  for (unsigned i = 0; i < n; ++i)
90  {
91  node->accept(*this);
92  // FIXME: M_ += M_ assumed here.
93  tmp += w_;
94  }
95  w_ = tmp;
96  }
97 
98  template<typename M_, typename W_>
99  void
100  RandomVisitor<M_,W_>::constant(const M_& m)
101  {
102  not_empty = true;
103  w_ = m;
104  }
105 
106  template <class M_, class W_>
107  void
108  RandomVisitor<M_,W_>::one()
109  {
110  w_ = M_();
111  not_empty = true;
112  }
113 
114  template <class M_, class W_>
115  void
116  RandomVisitor<M_,W_>::zero()
117  {
118  }
119 
120  template<typename M_, typename W_>
121  M_ RandomVisitor<M_,W_>::get() const
122  {
123  assertion(not_empty);
124  return w_;
125  }
126 
127  } // rat
128 
129 } // vcsn
130 
131 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_RANDOM_VISITOR_HXX