Vaucanson  1.4.1
krat_conversion.hxx
1 // krat_conversion.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 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_KRAT_CONVERSION_HXX
18 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_CONVERSION_HXX
19 
20 # include <vaucanson/algebra/implementation/series/krat_conversion.hh>
21 
22 namespace vcsn {
23 
24  namespace algebra {
25 
26  /*------------------.
27  | ExpConvertVisitor |
28  `------------------*/
29 
30  template<typename Tm, typename Tw, typename oTm, typename oTw,
31  typename Series_>
32  ExpConvertVisitor<Tm, Tw, oTm, oTw, Series_>::ExpConvertVisitor(const Series_& s)
33  : exp_(), series_(s)
34  {}
35 
36 
37  template<typename Tm, typename Tw, typename oTm, typename oTw,
38  typename Series_>
39  ExpConvertVisitor<Tm, Tw, oTm, oTw, Series_>::~ExpConvertVisitor()
40  {}
41 
42  template<typename Tm, typename Tw, typename oTm, typename oTw,
43  typename Series_>
44  void
45  ExpConvertVisitor<Tm, Tw, oTm, oTw, Series_>::product(const node_t* left_,
46  const node_t* right_)
47  {
48  right_->accept(*this);
49  exp_t tmp = exp_;
50  left_->accept(*this);
51  op_in_mul(series_, exp_, tmp);
52  }
53 
54  template<typename Tm, typename Tw, typename oTm, typename oTw,
55  typename Series_>
56  void
57  ExpConvertVisitor<Tm, Tw, oTm, oTw, Series_>::sum(const node_t* left_,
58  const node_t* right_)
59  {
60  right_->accept(*this);
61  exp_t tmp = exp_;
62  left_->accept(*this);
63  op_in_add(series_, exp_, tmp);
64  }
65 
66  template<typename Tm, typename Tw, typename oTm, typename oTw,
67  typename Series_>
68  void
70  {
71  node_->accept(*this);
72  op_in_star(series_, exp_);
73  }
74 
75  template<typename Tm, typename Tw, typename oTm, typename oTw,
76  typename Series_>
77  void
78  ExpConvertVisitor<Tm, Tw, oTm, oTw, Series_>::left_weight(const oTw& w,
79  const node_t* node_)
80  {
81  node_->accept(*this);
82  exp_ = op_mul(series_.semiring(), series_,
83  op_convert(SELECT(typename Series_::semiring_t),
84  SELECT(Tw),
85  w),
86  exp_);
87  }
88 
89  template<typename Tm, typename Tw, typename oTm, typename oTw,
90  typename Series_>
91  void
92  ExpConvertVisitor<Tm, Tw, oTm, oTw, Series_>::right_weight(const oTw& w,
93  const node_t*
94  node_)
95  {
96  node_->accept(*this);
97  op_in_mul(series_, series_.semiring(), exp_,
98  op_convert(SELECT(typename Series_::semiring_t),
99  SELECT(Tw),
100  w));
101  }
102 
103  template<typename Tm, typename Tw, typename oTm, typename oTw,
104  typename Series_>
105  void
106  ExpConvertVisitor<Tm, Tw, oTm, oTw, Series_>::constant(const oTm& m_)
107  {
108  Tm m = op_convert(SELECT(typename Series_::monoid_t),
109  SELECT(Tm),
110  m_);
111  if (m == identity_value(SELECT(typename Series_::monoid_t),
112  SELECT(Tm)))
113  exp_ = exp_t::one();
114  else
115  exp_ = exp_t::constant(m);
116  }
117 
118  template<typename Tm, typename Tw, typename oTm, typename oTw,
119  typename Series_>
120  void
121  ExpConvertVisitor<Tm, Tw, oTm, oTw, Series_>::one()
122  {
123  exp_ = exp_t::one();
124  }
125 
126  template<typename Tm, typename Tw, typename oTm, typename oTw,
127  typename Series_>
128  void
129  ExpConvertVisitor<Tm, Tw, oTm, oTw, Series_>::zero()
130  {
131  exp_ = exp_t::zero();
132  }
133 
134  template<typename Tm, typename Tw, typename oTm, typename oTw,
135  typename Series_>
136  const typename ExpConvertVisitor<Tm,Tw,oTm,oTw,Series_>::exp_t&
138  {
139  return exp_;
140  }
141 
142  template<typename Tm, typename Tw, typename W, typename M,
143  typename oTm, typename oTw>
144  rat::exp<Tm, Tw>
145  convert_exp(SELECTOR2(rat::exp<Tm, Tw>),
146  const Series<W, M>& s,
147  const rat::exp<oTm, oTw>& arg)
148  {
149  ExpConvertVisitor<Tm, Tw, oTm, oTw, Series<W, M> > v(s);
150  arg.accept(v);
151  return v.get();
152  }
153 
154  template<typename W, typename M, typename Tm, typename Tw,
155  typename oTm, typename oTw>
156  Element<Series<W, M>, rat::exp<Tm, Tw> >
157  convert_exp(SELECTOR4(Element<Series<W, M>, rat::exp<Tm, Tw> >),
158  const Element<Series<W, M>, rat::exp<oTm, oTw> >& arg)
159  {
160  return Element<Series<W, M>, rat::exp<Tm, Tw> >
161  (arg.structure(),
162  convert_exp(SELECT2(rat::exp<Tm, Tw>),
163  arg.structure(),
164  arg.value()));
165  }
166 
167  } // algebra
168 
169 } // vcsn
170 
171 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_CONVERSION_HXX