Vaucanson  1.4.1
monoid_rep_base.hxx
1 // monoid_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_MONOID_MONOID_REP_BASE_HXX
19 # define VCSN_ALGEBRA_IMPLEMENTATION_MONOID_MONOID_REP_BASE_HXX
20 
21 # include <vaucanson/algebra/implementation/monoid/monoid_rep_base.hh>
22 
23 namespace vcsn
24 {
25  namespace algebra
26  {
27  template <template <typename> class S, typename T>
28  MonoidRepBase<S, T>::MonoidRepBase()
29  {
30  self_t* self = static_cast<self_t*>(this);
31 
32  // Sane defaults.
33  self->maybe_epsilon.push_back("1");
34  self->maybe_epsilon.push_back("e");
35 
36  // Trying with more than one char.
37  self->maybe_epsilon.push_back("_e");
38  self->maybe_epsilon.push_back("eps");
39 
40  self->empty = *(self->maybe_epsilon.begin());
41  self->concat = "";
42  }
43 
44  template <template <typename> class S, typename T>
45  void
46  MonoidRepBase<S, T>::disambiguate(const monoid_t& monoid, pointer_t& orig)
47  {
48  // Type helpers.
49  typedef typename monoid_t::alphabet_t alphabet_t;
50  typedef typename alphabet_t::letter_t letter_t;
51 
52  self_t* self = static_cast<self_t*>(this);
53 
54  // Type helpers.
55  typedef std::vector<std::string>::const_iterator iterator_t;
56 
57  self_t new_rep = *self;
58 
59  for (iterator_t empty_it = new_rep.maybe_epsilon.begin();
60  empty_it != new_rep.maybe_epsilon.end();
61  ++empty_it)
62  {
63  bool found = true;
64 
65  for_all_const_(alphabet_t, i, monoid.alphabet())
66  {
67  if (letter_traits<letter_t>::letter_to_literal(*i) == *empty_it)
68  {
69  found = false;
70  break;
71  }
72  }
73 
74  // Best match.
75  if (found)
76  {
77  // Copy on write.
78  if (new_rep.empty != *empty_it)
79  {
80  new_rep.empty = *empty_it;
81  orig = pointer_t(new self_t(new_rep));
82  }
83  break;
84  }
85  }
86  }
87 
88  template <template <typename> class S, typename T>
89  bool
90  operator==(boost::shared_ptr<MonoidRepBase<S, T> > lhs,
91  boost::shared_ptr<MonoidRepBase<S, T> > rhs)
92  {
93  return (lhs->empty == rhs->empty &&
94  lhs->concat == rhs->concat &&
95  lhs->maybe_epsilon == rhs->maybe_epsilon);
96  }
97 
98  } // ! algebra
99 
100 } // ! vcsn
101 
102 #endif // !VCSN_ALGEBRA_IMPLEMENTATION_MONOID_MONOID_REP_BASE_HXX