Vaucanson  1.4.1
alphabets_base.hxx
1 // alphabets_base.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, 2006, 2007, 2008 The
6 // Vaucanson Group.
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
12 //
13 // The complete GNU General Public Licence Notice can be found as the
14 // `COPYING' file in the root directory.
15 //
16 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
17 //
18 #ifndef VCSN_ALGEBRA_CONCEPT_ALPHABETS_BASE_HXX
19 # define VCSN_ALGEBRA_CONCEPT_ALPHABETS_BASE_HXX
20 
22 # include <vaucanson/misc/random.hh>
24 # include <cstddef>
25 
26 namespace vcsn
27 {
28  namespace algebra
29  {
30  /*-------------------.
31  | AlphabetSetBase<S> |
32  `-------------------*/
33 
34  template<typename S>
36  {}
37 
38  template<typename S>
40  {}
41 
42  } // algebra
43 
44  /*-----------------------------------.
45  | MetaElement<AlphabetSetBase<S>, T> |
46  `-----------------------------------*/
47  // Meta-information about element formed from an AlphabetSetBase
48  // structuring element.
49 
50  template<typename S, typename T>
51  size_t
53  {
54  return op_size(this->structure(), this->value());
55  }
56 
57  template<typename S, typename T>
58  size_t
60  {
61  return op_max_size(this->structure(), this->value());
62  }
63 
64  template<typename S, typename T>
65  bool
66  MetaElement<algebra::AlphabetSetBase<S>, T>::contains(const letter_t& l) const
67  {
68  return op_contains_e(this->structure(), this->value(), l);
69  }
70 
71  template<typename S, typename T>
72  bool
74  {
75  return op_is_finite(this->structure(), this->value());
76  }
77 
78  template<typename S, typename T>
79  typename MetaElement<algebra::AlphabetSetBase<S>, T>::iterator
81  {
82  return op_begin(this->structure(), this->value());
83  }
84 
85  template<typename S, typename T>
86  typename MetaElement<algebra::AlphabetSetBase<S>, T>::const_iterator
88  {
89  return op_begin_const(this->structure(), this->value());
90  }
91 
92  template<typename S, typename T>
93  typename MetaElement<algebra::AlphabetSetBase<S>, T>::iterator
95  {
96  return op_end(this->structure(), this->value());
97  }
98 
99  template<typename S, typename T>
100  typename MetaElement<algebra::AlphabetSetBase<S>, T>::const_iterator
102  {
103  return op_end_const(this->structure(), this->value());
104  }
105 
106  template<typename S, typename T>
107  void
109  {
110  op_insert(this->structure(), this->value(), l);
111  }
112 
113  template<typename S, typename T>
114  void
115  MetaElement<algebra::AlphabetSetBase<S>, T>::insert(const std::string& lit)
116  {
117  op_insert(this->structure(), this->value(), algebra::letter_traits<letter_t>::literal_to_letter(lit).second);
118  }
119 
120  template<typename S, typename T>
121  bool
123  letter_t rhs) const
124  {
125  return op_letter_equality(this->structure(), this->value(), lhs, rhs);
126  }
127 
128  template<typename S, typename T>
131  {
132  // FIXME: recommendation(overload this operator)
133  precondition (is_finite());
134  precondition (size() > 0);
135 
136  int nr = misc::random::generate<int>(0, size() - 1);
137 
138  const_iterator it = begin();
139  for (int k = 0; k < nr; ++k)
140  ++it;
141 
142  return *it;
143  }
144 
145  template <class S, typename T>
147  MetaElement<algebra::AlphabetSetBase<S>, T>::random_letter() const
148  {
151  }
152 
153  template<typename S, typename T>
155  {}
156 
157  template<typename S, typename T>
159  MetaElement<Structure<S>, T>(other)
160  {}
161 
162  namespace algebra
163  {
164 
165  template <typename S, typename L>
167  const std::string&,
168  size_t&)
169  {
170  static_error(no_op_parse_operator_available);
171  return 0;
172  }
173 
174  template <typename S, typename T, typename L>
176  const T& a,
177  L lhs,
178  L rhs)
179  {
180  return lhs == rhs;
181  }
182 
183  template<typename S, typename St, typename T>
184  St& op_rout(const algebra::AlphabetSetBase<S>& s, St& st, const T& a)
185  {
186  st << "{ ";
187 
188  if (op_is_finite(s.self(), a))
189  for (typename op_begin_traits<S, T>::const_ret_t i = op_begin_const(s.self(), a);
190  i != op_end_const(s.self(), a);
191  ++i)
192  {
193  if (i != op_begin_const(s.self(), a))
194  st << ", ";
195  st << algebra::letter_traits<typename S::letter_t>::letter_to_literal(*i);
196  }
197  else
198  st << "<many letters>";
199 
200  st << " }";
201 
202  return st;
203  }
204 
205  } // ! algebra
206 
207  template <typename S, typename T>
208  std::pair<bool, typename Element<S, T>::letter_t>
209  parse_letter(const Element<S, T>& alphabet,
210  const std::string& s)
211  {
212  typedef typename Element<S, T>::letter_t letter_t;
213 
214  size_t tmp = 0;
215  std::pair<bool, letter_t> res =
216  op_parse(alphabet.structure(), alphabet.value(), s, tmp);
217 
218  if (tmp != s.size())
219  res.first = false;
220 
221  return res;
222  }
223 
224 } // ! vcsn
225 
226 #endif // ! VCSN_ALGEBRA_CONCEPT_ALPHABETS_BASE_HXX