Vaucanson  1.4.1
partial_rat_exp_constant_term.hxx
1 // partial_rat_exp_constant_term.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_ALGORITHMS_INTERNAL_PARTIAL_RAT_EXP_CONSTANT_TERM_HXX
18 # define VCSN_ALGORITHMS_INTERNAL_PARTIAL_RAT_EXP_CONSTANT_TERM_HXX
19 
20 # include <utility>
23 
24 namespace vcsn {
25 
26  template <class Series, class T>
27  std::pair<typename Element<Series, T>::semiring_elt_t, bool>
28  constant_term(const PartialExp<Series, T>& exp)
29  {
30  typedef typename PartialExp<Series, T>::const_iterator const_iterator;
31  typedef typename PartialExp<Series, T>::semiring_elt_t semiring_elt_t;
32  typedef typename PartialExp<Series, T>::series_set_elt_value_t
33  series_set_elt_value_t;
34  typedef std::pair<typename Element<Series, T>::semiring_elt_t, bool>
35  result_t;
36 
37  const_iterator i = exp.begin();
38  semiring_elt_t res = i.semiring_elt();
39  bool defined = true;
40 
41  for (i++; i != exp.end() && defined; ++i)
42  {
43  if (i.on_node())
44  {
45  result_t tmp = constant_term(
46  Element<Series, T>(exp.exp_structure(),
47  series_set_elt_value_t (i.node())));
48  defined = tmp.second;
49  res *= tmp.first;
50  }
51  else
52  res *= i.semiring_elt();
53  }
54  return std::make_pair(res, defined);
55  }
56 
57 } // vcsn
58 
59 #endif // ! VCSN_ALGORITHMS_INTERNAL_PARTIAL_RAT_EXP_CONSTANT_TERM_HXX