Vaucanson  1.4.1
decorated_alphabet.hxx
1 // decorated_alphabet.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, 2007 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_ALPHABETS_DECORATED_ALPHABET_HXX
18 # define VCSN_ALGEBRA_IMPLEMENTATION_ALPHABETS_DECORATED_ALPHABET_HXX
19 
20 # include <vaucanson/algebra/concept/letter.hh>
21 # include <vaucanson/algebra/implementation/alphabets/decorated_alphabet.hh>
22 
23 namespace vcsn {
24 
25  namespace algebra
26  {
27  template <typename L, typename T>
28  AlphabetDecorator<L, T>::~AlphabetDecorator()
29  {
30  if (alphabet_owner_)
31  delete alphabet_;
32  }
33 
34  template <typename L, typename T>
35  AlphabetDecorator<L, T>::AlphabetDecorator()
36  {
37  alphabet_ = new T();
38  joker_ = letter_traits<L>::default_joker;
39  other_ = letter_traits<L>::default_other;
40  alphabet_owner_ = true;
41  }
42 
43  template <typename L, typename T>
44  AlphabetDecorator<L, T>::AlphabetDecorator(alphabet_impl_t& alphabet) :
45  alphabet_(&alphabet)
46  {
47  alphabet_owner_ = false;
48  if (std::find(alphabet.begin(), alphabet.end(),
49  letter_traits<L>::default_joker())
50  == alphabet.end())
51  joker_ = letter_traits<L>::default_joker();
52  else
53  {
54  joker_ = letter_traits<L>::default_joker();
55  do {
56  ++joker_;
57  if (joker_ == letter_traits<L>::default_joker())
58  {
59  std::cerr << "Did not find a valid 'joker' ! Exiting !"
60  << std::endl;
61  exit(EXIT_FAILURE);
62  }
63  } while (std::find(alphabet.begin(), alphabet.end(),
64  joker_) != alphabet.end());
65  }
66  if (std::find(alphabet.begin(), alphabet.end(),
67  letter_traits<L>::default_other())
68  == alphabet.end())
69  other_ = letter_traits<L>::default_other();
70  else
71  {
72  other_ = letter_traits<L>::default_other();
73  do {
74  ++other_;
75  if (other_ == letter_traits<L>::default_other())
76  {
77  std::cerr << "Did not find a valid 'other' ! Exiting !"
78  << std::endl;
79  exit(EXIT_FAILURE);
80  }
81  } while (std::find(alphabet.begin(), alphabet.end(),
82  other_) != alphabet.end());
83  }
84  }
85 
86  template <typename L, typename T>
87  void
88  AlphabetDecorator<L, T>::insert(L l)
89  {
90  alphabet().insert(l);
91  }
92 
93 
94  template <typename L, typename T>
95  unsigned
96  AlphabetDecorator<L, T>::size() const
97  {
98  return alphabet().size();
99  }
100 
101  template <typename L, typename T>
102  typename AlphabetDecorator<L, T>::iterator
103  AlphabetDecorator<L, T>::begin()
104  {
105  return alphabet().begin();
106  }
107 
108  template <typename L, typename T>
109  typename AlphabetDecorator<L, T>::iterator
110  AlphabetDecorator<L, T>::end()
111  {
112  return alphabet().end();
113  }
114 
115  template <typename L, typename T>
116  typename AlphabetDecorator<L, T>::const_iterator
117  AlphabetDecorator<L, T>::begin() const
118  {
119  return alphabet().begin();
120  }
121 
122  template <typename L, typename T>
123  typename AlphabetDecorator<L, T>::const_iterator
124  AlphabetDecorator<L, T>::end() const
125  {
126  return alphabet().end();
127  }
128 
129  template <typename L, typename T>
130  const T& AlphabetDecorator<L, T>::alphabet() const
131  {
132  return *alphabet_;
133  }
134 
135  template <typename L, typename T>
136  T& AlphabetDecorator<L, T>::alphabet()
137  {
138  return *alphabet_;
139  }
140 
141  template <typename L, typename T>
142  L AlphabetDecorator<L, T>::joker() const
143  {
144  return joker_;
145  }
146 
147  template <typename L, typename T>
148  L AlphabetDecorator<L, T>::other() const
149  {
150  return other_;
151  }
152  }
153 
154  template <typename L, typename T>
155  L MetaElement<algebra::AlphabetSet<L>, algebra::AlphabetDecorator<L, T> >::
156  joker() const
157  {
158  return this->value().joker();
159  }
160 
161  template <typename L, typename T>
163  other() const
164  {
165  return this->value().other();
166  }
167 
168  namespace algebra {
169 
170  /*--------------------------------------------------.
171  | Definition of an alphabet implementation based on |
172  | AlphabetDecorator |
173  `--------------------------------------------------*/
174 
175  template <typename L, typename T>
176  bool op_contains(const algebra::AlphabetSet<L>& s,
178  {
179  return true;
180  }
181 
182  template <typename L, typename T>
183  bool op_is_finite(const algebra::AlphabetSet<L>& s,
184  const algebra::AlphabetDecorator<L, T>& a)
185  {
186  return true;
187  }
188 
189  template <typename L, typename T>
190  bool op_contains_e(const algebra::AlphabetSet<L>& s,
191  const algebra::AlphabetDecorator<L, T>& a,
192  const L& v)
193  {
194  if (v == a.joker())
195  return true;
196  if (v == a.other())
197  return false;
198  return Element<algebra::AlphabetSet<L>, T>(s, a.alphabet()).contains(v);
199  }
200 
201  template <typename T, typename L>
202  bool op_letter_equality(const algebra::AlphabetSet<L>& s,
203  const algebra::AlphabetDecorator<L, T>& a,
204  L lhs,
205  L rhs)
206  {
207  Element<algebra::AlphabetSet<L>,
208  algebra::AlphabetDecorator<L, T> > e(s, a);
209  if (lhs == a.joker())
210  return e.contains(rhs);
211  if (rhs == a.joker())
212  return e.contains(lhs);
213  if (lhs == a.other())
214  return ! e.contains(rhs);
215  if (rhs == a.other())
216  return ! e.contains(lhs);
217  return lhs == rhs;
218  }
219 
220  } // algebra
221 
222 } // vcsn
223 
224 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_ALPHABETS_DECORATED_ALPHABET_HXX