Vaucanson  1.4.1
exp.hxx
1 // exp.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_EXP_HXX
18 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_EXP_HXX
19 
20 # include <vaucanson/algebra/implementation/series/rat/exp.hh>
21 
22 # include <vaucanson/algebra/implementation/series/rat/depth_visitor.hh>
23 # include <vaucanson/algebra/implementation/series/rat/star_height_visitor.hh>
24 # include <vaucanson/algebra/implementation/series/rat/length_visitor.hh>
25 
26 namespace vcsn {
27 
28  namespace rat {
29 
30  template<typename LetterT, typename WeightT>
32  : base_(new n_zero_t)
33  {}
34 
35  template<typename LetterT, typename WeightT>
37  : base_(p)
38  {}
39 
40  template<typename LetterT, typename WeightT>
41  exp<LetterT, WeightT>::exp(const node_t* p)
42  : base_(p->clone())
43  {}
44 
45  template<typename LetterT, typename WeightT>
47  : base_(other.base_->clone())
48  {}
49 
50  template<typename LetterT, typename WeightT>
52  {
53  delete base_;
54  }
55 
56  template<typename LetterT, typename WeightT>
59  {
60  if (other.base_ != base_)
61  {
62  delete base_;
63  base_ = other.base_->clone();
64  }
65  return *this;
66  }
67 
68  template<typename LetterT, typename WeightT>
71  {
72  base_ = new n_sum_t(base_, other.base_->clone());
73  return *this;
74  }
75 
76  template<typename LetterT, typename WeightT>
79  {
80  base_ = new n_prod_t(base_, other.base_->clone());
81  return *this;
82  }
83 
84  template<typename LetterT, typename WeightT>
86  {
87  base_ = new n_star_t(base_);
88  return *this;
89  }
90 
91  template<typename LetterT, typename WeightT>
94  {
95  std::swap(base_, other.base_);
96  return *this;
97  }
98 
99  template<typename LetterT, typename WeightT>
101  accept(ConstNodeVisitor<monoid_elt_value_t, semiring_elt_value_t>& v) const
102  {
103  base_->accept(v);
104  }
105 
106  template<typename LetterT, typename WeightT>
108  {
109  DepthVisitor<monoid_elt_value_t, semiring_elt_value_t> v;
110  accept(v);
111  return v.get();
112  }
113 
114  template<typename LetterT, typename WeightT>
116  {
117  StarHeightVisitor<monoid_elt_value_t, semiring_elt_value_t> v;
118  accept(v);
119  return v.get();
120  }
121 
122  template<typename LetterT, typename WeightT>
124  {
125  LengthVisitor<monoid_elt_value_t, semiring_elt_value_t> v;
126  accept(v);
127  return v.get();
128  }
129 
130  template<typename LetterT, typename WeightT>
131  typename exp<LetterT, WeightT>::node_t* &
133  {
134  return base_;
135  }
136 
137  template<typename LetterT, typename WeightT>
138  typename exp<LetterT, WeightT>::node_t* const &
140  {
141  return base_;
142  }
143 
144  template<typename LetterT, typename WeightT>
145  bool exp<LetterT, WeightT>::operator == (const exp& other) const
146  {
147  return !(*base_ != *other.base_);
148  }
149 
150  template<typename LetterT, typename WeightT>
151  bool exp<LetterT, WeightT>::operator != (const exp& other) const
152  {
153  return *base_ != *other.base_;
154  }
155 
156  template<typename LetterT, typename WeightT>
157  bool exp<LetterT, WeightT>::operator < (const exp& other) const
158  {
159  return *base_ < *other.base_;
160  }
161 
162  template<typename LetterT, typename WeightT>
165  {
166  return exp(base_->clone());
167  }
168 
169  template<typename LetterT, typename WeightT>
172  {
173  return exp(new n_one_t);
174  }
175 
176  template<typename LetterT, typename WeightT>
178  {
179  return exp(new n_zero_t);
180  }
181 
182  template<typename LetterT, typename WeightT>
184  constant(const monoid_elt_value_t& l)
185  {
186  return exp(new n_const_t(l));
187  }
188 
189  template<typename LetterT, typename WeightT>
191  {
192  return true;
193  }
194 
195  template<typename M, typename W>
196  const exp<M, W> operator * (const exp<M, W>& lhs,
197  const exp<M, W>& rhs)
198  {
199  exp<M, W> ret(lhs);
200  ret *= rhs;
201  return ret;
202  }
203 
204  template<typename M, typename W>
205  exp<M, W> operator + (const exp<M, W>& lhs,
206  const exp<M, W>& rhs)
207  {
208  exp<M, W> ret(lhs);
209  ret += rhs;
210  return ret;
211  }
212 
213  template<typename M, typename W>
214  exp<M, W> operator * (const W& lhs,
215  const exp<M, W>& rhs)
216  {
217  exp<M, W> ret(rhs);
218  ret.base() = new LeftWeighted<M, W>(lhs, ret.base());
219  return ret;
220  }
221 
222  template<typename M, typename W>
223  exp<M, W> operator * (const exp<M, W>& lhs,
224  const W& rhs)
225  {
226  exp<M, W> ret(lhs);
227  ret.base() = new RightWeighted<M, W>(rhs, ret.base());
228  return ret;
229  }
230 
231  // FIXME: this is an evil hack, but without it there is an ambiguity
232  // FIXME: in calls to exp * number or number * exp.
233 
234  template<typename M, typename S, typename T>
235  exp<M, Element<S, T> >
236  operator*(const Element<S, T>& lhs,
237  const exp<M, Element<S, T> >& rhs)
238  {
239  exp<M, Element<S, T> > ret(rhs);
240  ret.base()
241  = new LeftWeighted<M, Element<S, T> >(lhs, ret.base());
242  return ret;
243  }
244 
245  template<typename M, typename S, typename T>
246  exp<M, Element<S, T> >
247  operator*(const exp<M, Element<S, T> >& lhs,
248  const Element<S, T>& rhs)
249  {
250  exp<M, Element<S, T> > ret(lhs);
251  ret.base()
252  = new RightWeighted<M, Element<S, T> >(rhs, ret.base());
253  return ret;
254  }
255 
256  template<typename M, typename W>
257  void swap(vcsn::rat::exp<M, W>& lhs,
259  {
260  lhs.swap(rhs);
261  }
262 
263  } // End of namespace rat.
264 
265 } // End of namespace vcsn.
266 
267 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_RAT_EXP_HXX