Vaucanson  1.4.1
freemonoid_base.hxx
1 // freemonoid_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 Vaucanson
6 // 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_FREEMONOID_BASE_HXX
19 # define VCSN_ALGEBRA_CONCEPT_FREEMONOID_BASE_HXX
20 
21 # include <vaucanson/algebra/concept/freemonoid_base.hh>
22 # include <vaucanson/misc/escaper.hh>
23 
24 namespace vcsn {
25 
26  namespace algebra {
27 
28  /*---------------------.
29  | FreeMonoidBase<Self> |
30  `---------------------*/
31 
32  template <class Self>
33  typename FreeMonoidBase<Self>::alphabet_t&
35  {
36  return this->self().alphabet();
37  }
38 
39  template <class Self>
40  const typename FreeMonoidBase<Self>::alphabet_t&
42  {
43  return this->self().alphabet();
44  }
45 
46  template <class Self>
48  {}
49 
50  template <class Self>
52  MonoidBase<Self>(m)
53  {}
54 
55  } // ! algebra
56 
57  /*-------------------------------------.
58  | MetaElement<FreeMonoidBase<Self>, T> |
59  `-------------------------------------*/
60 
61  template <class Self, typename T>
62  size_t
64  {
65  return op_size(this->structure(), this->value());
66  }
67 
68  template <class Self, typename T>
69  void
71  {
72  op_in_mirror(this->structure(), this->value());
73  }
74 
75  template <class Self, typename T>
76  typename MetaElement<algebra::FreeMonoidBase<Self>, T>::iterator
78  {
79  return op_begin(this->structure(), this->value());
80  }
81 
82  template <class Self, typename T>
83  typename MetaElement<algebra::FreeMonoidBase<Self>, T>::const_iterator
85  {
86  return op_begin_const(this->structure(), this->value());
87  }
88 
89  template <class Self, typename T>
90  typename MetaElement<algebra::FreeMonoidBase<Self>, T>::reverse_iterator
92  {
93  return op_rbegin(this->structure(), this->value());
94  }
95 
96  template <class Self, typename T>
97  typename MetaElement<algebra::FreeMonoidBase<Self>, T>::const_reverse_iterator
99  {
100  return op_rbegin_const(this->structure(), this->value());
101  }
102 
103  template <class Self, typename T>
104  typename MetaElement<algebra::FreeMonoidBase<Self>, T>::iterator
106  {
107  return op_end(this->structure(), this->value());
108  }
109 
110  template <class Self, typename T>
111  typename MetaElement<algebra::FreeMonoidBase<Self>, T>::const_iterator
113  {
114  return op_end_const(this->structure(), this->value());
115  }
116 
117  template <class Self, typename T>
118  typename MetaElement<algebra::FreeMonoidBase<Self>, T>::reverse_iterator
120  {
121  return op_rend(this->structure(), this->value());
122  }
123 
124  template <class Self, typename T>
125  typename MetaElement<algebra::FreeMonoidBase<Self>, T>::const_reverse_iterator
127  {
128  return op_rend_const(this->structure(), this->value());
129  }
130 
131  template <class Self, typename T>
133  MetaElement<algebra::MonoidBase<Self>, T>()
134  {}
135 
136  template <class Self, typename T>
138  MetaElement<algebra::MonoidBase<Self>, T>(o)
139  {}
140 
141  /*-------------------.
142  | External functions |
143  `-------------------*/
144 
145  template <typename S, typename T>
148  {
149  Element<S, T> ret(e);
150  ret.mirror();
151  return ret;
152  }
153 
154  template <typename S, typename T>
155  std::pair<bool, int>
157  const std::string& s)
158  {
159  return op_parse(dest.structure(), dest.value(), s);
160  }
161 
162  template<typename S, typename T>
163  const std::basic_string<T>&
164  op_convert(const algebra::FreeMonoidBase<S>& s,
165  SELECTOR(std::basic_string<T>),
166  const std::basic_string<T>& from_data)
167  {
168 # ifndef VCSN_NDEBUG
169  for (typename std::basic_string<T>::const_iterator it = from_data.begin();
170  it != from_data.end();
171  ++it)
172  precondition_ (s.alphabet().contains(*it),
173  "The letter " + vcsn::algebra::letter_traits<T>::letter_to_literal(*it) +
174  " is not in the given alphabet");
175 # else // ! VCSN_NDEBUG
176  (void) s;
177 # endif // ! VCSN_NDEBUG
178  return from_data;
179  }
180 
181  namespace algebra {
182 
183  /*------------------.
184  | Default Operators |
185  `------------------*/
186 
187  template <typename S, typename T>
188  std::pair<bool, int>
189  op_parse(const algebra::FreeMonoidBase<S>&,
190  T& v,
191  const std::string& s)
192  {
193  static_error(no_op_parse_operator_available);
194  return std::make_pair(false, 0);
195  }
196 
197  template <typename Self, typename T>
198  void
199  op_in_mirror(const algebra::FreeMonoidBase<Self>& s, T& v)
200  {
201  const T new_v(v);
202  typename MetaElement<algebra::FreeMonoidBase<Self>, T>::iterator it
203  = op_begin(s.self(), v);
204 
205  for (typename MetaElement<algebra::FreeMonoidBase<Self>, T>::
206  const_reverse_iterator i = op_rbegin_const(s.self(), new_v);
207  i != op_rend_const(s.self(), new_v);
208  ++i)
209  *it++ = *i;
210  }
211 
212  template <typename Self, typename T>
213  bool
214  op_contains(const algebra::FreeMonoidBase<Self>& s, const T& v)
215  {
216  typedef typename op_begin_traits<Self, T>::const_ret_t const_iterator;
217 
218  for (const_iterator i = op_begin_const(s.self(), v);
219  i != op_end_const(s.self(), v);
220  ++i)
221  if (! s.alphabet().contains(*i))
222  return false;
223  return true;
224  }
225 
226  template <typename Self, typename St, typename T>
227  St&
228  op_rout(const algebra::FreeMonoidBase<Self>& s,
229  St& st,
230  const T& v)
231  {
232  return op_rout(s.self(), st, v);
233  }
234 
235  } // ! algebra
236 
237 } // ! vcsn
238 
239 #endif // ! VCSN_ALGEBRA_CONCEPT_FREEMONOID_BASE_HXX