Vaucanson  1.4.1
series_rep_base.hxx
1 // series_rep_base.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2008 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 
18 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SERIES_SERIES_REP_BASE_HXX
19 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_SERIES_REP_BASE_HXX
20 
21 # include <vaucanson/algebra/implementation/series/series_rep_base.hh>
22 
23 namespace vcsn
24 {
25  namespace algebra
26  {
27  template <template <typename, typename> class B, typename S, typename M>
28  SeriesRepBase<B, S, M>::SeriesRepBase()
29  {
30  self_t* self = static_cast<self_t*>(this);
31 
32  // Sane defaults.
33  self->maybe_zero.push_back("0");
34  self->maybe_zero.push_back("z");
35 
36  // Trying with more than one char.
37  self->maybe_zero.push_back("_z");
38  self->maybe_zero.push_back("zero");
39 
40  self->zero = *(self->maybe_zero.begin());
41  self->open_par = "(";
42  self->close_par = ")";
43  self->plus = "+";
44  self->times = ".";
45  self->star = "*";
46  self->open_weight = "{";
47  self->close_weight = "}";
48  self->spaces.push_back(" ");
49  }
50 
51  // FIXME: maybe we can factor even more code with MonoidRepBase
52  // (maybe some sort of RepresentationBase?)
53  template <template <typename, typename> class B, typename S, typename M>
54  void
55  SeriesRepBase<B, S, M>::disambiguate(const monoid_t& monoid,
56  pointer_t& orig)
57  {
58  // Type helpers.
59  typedef typename monoid_t::alphabet_t alphabet_t;
60  typedef typename alphabet_t::letter_t letter_t;
61 
62  self_t* self = static_cast<self_t*>(this);
63 
64  // Type helpers.
65  typedef std::vector<std::string>::const_iterator iterator_t;
66 
67  self_t new_rep = *self;
68 
69  for (iterator_t zero_it = new_rep.maybe_zero.begin();
70  zero_it != new_rep.maybe_zero.end();
71  ++zero_it)
72  {
73  bool found = true;
74 
75  for_all_const_(alphabet_t, i, monoid.alphabet())
76  {
77  if (letter_traits<letter_t>::letter_to_literal(*i) == *zero_it)
78  {
79  found = false;
80  break;
81  }
82  }
83 
84  // Best match.
85  if (found)
86  {
87  // Copy on write.
88  if (new_rep.zero != *zero_it)
89  {
90  new_rep.zero = *zero_it;
91  orig = pointer_t(new self_t(new_rep));
92  }
93  break;
94  }
95  }
96  }
97 
98  template <template <typename, typename> class B, typename S, typename M>
99  bool
100  operator==(boost::shared_ptr<SeriesRepBase<B, S, M> > lhs,
101  boost::shared_ptr<SeriesRepBase<B, S, M> > rhs)
102  {
103  return (lhs->open_par == rhs->open_par &&
104  lhs->close_par == rhs->close_par &&
105  lhs->plus == rhs->plus &&
106  lhs->times == rhs->times &&
107  lhs->star == rhs->star &&
108  lhs->zero == rhs->zero &&
109  lhs->open_weight == rhs->open_weight &&
110  lhs->close_weight == rhs->close_weight &&
111  lhs->spaces == rhs->spaces &&
112  lhs->maybe_zero == rhs->maybe_zero);
113  }
114 
115  } // ! algebra
116 
117 } // ! vcsn
118 
119 #endif // !VCSN_ALGEBRA_IMPLEMENTATION_SERIES_SERIES_REP_BASE_HXX