00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_MONOID_STR_WORDS_HXX
00019 # define VCSN_ALGEBRA_IMPLEMENTATION_MONOID_STR_WORDS_HXX
00020
00021 # include <vaucanson/algebra/implementation/monoid/str_words.hh>
00022
00023 # include <vaucanson/misc/char_traits.hh>
00024
00025 namespace vcsn {
00026
00027 namespace algebra {
00028
00029 template <typename A>
00030 bool
00031 op_parse(const FreeMonoid<A>& s,
00032 std::basic_string<typename A::letter_t>& v,
00033 const std::string& in)
00034 {
00035 int last_op = 0;
00036
00037 for (size_t i = 0; i < in.size();)
00038 if (!in.compare(i, s.representation()->empty.size(), s.representation()->empty))
00039 {
00040 last_op = 1;
00041 i += s.representation()->empty.size();
00042 }
00043 else
00044 if (!s.representation()->concat.empty() &&
00045 !in.compare(i, s.representation()->concat.size(), s.representation()->concat))
00046 {
00047 if (last_op == 0 || last_op == 2)
00048 return false;
00049 last_op = 2;
00050 i += s.representation()->concat.size();
00051 }
00052 else
00053 {
00054 last_op = 3;
00055 std::pair<bool, typename A::letter_t> letter = op_parse(s.alphabet().structure(), s.alphabet().value(), in, i);
00056 if (!letter.first)
00057 return (letter.first);
00058 v.push_back(letter.second);
00059 }
00060 return (last_op != 2);
00061 }
00062
00063 template<typename A>
00064 void
00065 op_in_mul(const algebra::FreeMonoid<A>&,
00066 std::basic_string<typename A::letter_t>& dst,
00067 const std::basic_string<typename A::letter_t>& src)
00068 {
00069 dst += src;
00070 }
00071
00072 template<typename A>
00073 std::basic_string<typename A::letter_t>
00074 op_mul(const algebra::FreeMonoid<A>&,
00075 const std::basic_string<typename A::letter_t>& a,
00076 const std::basic_string<typename A::letter_t>& b)
00077 {
00078 return a + b;
00079 }
00080
00081 template<typename A, typename St>
00082 St&
00083 op_rout(const FreeMonoid<A>& s,
00084 St& st,
00085 const std::basic_string<typename A::letter_t>& v)
00086 {
00087 if (v.empty())
00088 st << s.representation()->empty;
00089 else
00090 {
00091
00092 typedef typename A::letter_t letter_t;
00093 typedef algebra::letter_traits<letter_t> letter_traits_t;
00094 typedef typename std::basic_string<letter_t>::const_iterator iter_t;
00095
00096 iter_t i = v.begin();
00097
00098
00099 st << letter_traits_t::letter_to_literal(*i);
00100 ++i;
00101
00102
00103 while (i != v.end())
00104 {
00105 st << s.representation()->concat
00106 << letter_traits_t::letter_to_literal(*i);
00107 ++i;
00108 }
00109 }
00110
00111 return st;
00112 }
00113
00114 template <typename A>
00115 bool
00116 op_xeq(const algebra::FreeMonoid<A>& s,
00117 const std::basic_string<typename A::letter_t>& a,
00118 const std::basic_string<typename A::letter_t>& b)
00119 {
00120 typename std::basic_string<typename A::letter_t>::const_iterator
00121 m = b.begin();
00122 typename std::basic_string<typename A::letter_t>::const_iterator l;
00123 for (l = a.begin(); m != b.end() && l != a.end(); ++l)
00124 {
00125 if (! s.alphabet().letter_equality(*l, *m))
00126 return false;
00127 ++m;
00128 }
00129 return (m == b.end() && l == a.end());
00130 }
00131
00132 template<typename A>
00133 const std::basic_string<typename A::letter_t>&
00134 identity_value(SELECTOR(algebra::FreeMonoid<A>),
00135 SELECTOR(std::basic_string<typename A::letter_t>))
00136 {
00137 static const std::basic_string<typename A::letter_t> instance;
00138 return instance;
00139 }
00140
00141 template<typename A>
00142 const std::basic_string<typename A::letter_t,
00143 misc::char_traits<typename A::letter_t> >&
00144 identity_value(SELECTOR(algebra::FreeMonoid<A>),
00145 SELECTOR2(std::basic_string<typename A::letter_t,
00146 misc::char_traits<typename A::letter_t> >))
00147 {
00148 static const std::basic_string<typename A::letter_t,
00149 misc::char_traits<typename A::letter_t> > instance;
00150 return instance;
00151 }
00152
00153 template<typename A>
00154 std::basic_string<typename A::letter_t>
00155 op_convert(SELECTOR(algebra::FreeMonoid<A>),
00156 SELECTOR(std::basic_string<typename A::letter_t>),
00157 const typename A::letter_t& c)
00158 {
00159 std::basic_string<typename A::letter_t> str;
00160 str = c;
00161 return str;
00162 }
00163
00164 template<typename A>
00165 std::basic_string<typename A::letter_t>
00166 op_convert(SELECTOR(algebra::FreeMonoid<A>) mon,
00167 SELECTOR(std::basic_string<typename A::letter_t>),
00168 const std::string& str)
00169 {
00170 Element<algebra::FreeMonoid<A>,
00171 std::basic_string<typename A::letter_t> > x(mon);
00172 parse_word(x, str);
00173 return x.value();
00174 }
00175
00176 template<typename A>
00177 std::basic_string<typename A::letter_t>
00178 op_convert(SELECTOR(algebra::FreeMonoid<A>) M,
00179 SELECTOR(std::basic_string<typename A::letter_t>) bs,
00180 const char* str)
00181 {
00182 return op_convert(M, bs, std::string(str));
00183 }
00184
00185 template <class A>
00186 Element<algebra::FreeMonoid<A>, std::basic_string<typename A::letter_t> >
00187 op_choose(const algebra::FreeMonoid<A>& s,
00188 SELECTOR(std::basic_string<typename A::letter_t>))
00189 {
00190 unsigned length =
00191 misc::random::generate<unsigned>(0, op_choose_max_word_length);
00192 std::basic_string<typename A::letter_t> r;
00193 for (unsigned i = 0; i < length; ++i)
00194 r = r + s.alphabet().choose();
00195 return Element<algebra::FreeMonoid<A>,
00196 std::basic_string<typename A::letter_t> >(s, r);
00197 }
00198
00199 # define WORD_TRAITS \
00200 word_traits<FreeMonoid<A>, std::basic_string<typename A::letter_t> >
00201
00202 template <typename A>
00203 inline typename WORD_TRAITS::first_projection_value_t
00204 WORD_TRAITS::first_projection(const WORD_TRAITS::word_value_t& str)
00205 {
00206
00207 static_assertion_(not (misc::static_eq<first_projection_t,
00208 undefined_type>::value), need_first_projection)
00209
00210 first_projection_value_t R;
00211
00212
00213 for_all_const_(word_value_t, i, str)
00214 R += (*i).first;
00215
00216 return R;
00217 }
00218
00219 template <typename A>
00220 inline typename WORD_TRAITS::first_projection_t
00221 WORD_TRAITS::first_projection(const WORD_TRAITS::first_monoid_t& mon,
00222 const WORD_TRAITS::word_t& word)
00223 {
00224
00225 static_assertion_(not (misc::static_eq<first_projection_t,
00226 undefined_type>::value), need_first_projection)
00227
00228 first_projection_t R(mon);
00229
00230 R.value() = first_projection(word.value());
00231
00232 return R;
00233 }
00234
00235 template <typename A>
00236 inline typename WORD_TRAITS::second_projection_value_t
00237 WORD_TRAITS::second_projection(const WORD_TRAITS::word_value_t& str)
00238 {
00239
00240 static_assertion_(not (misc::static_eq<second_projection_t,
00241 undefined_type>::value), need_second_projection)
00242
00243 second_projection_value_t R;
00244
00245
00246 for_all_const_(word_value_t, i, str)
00247 R += (*i).second;
00248
00249 return R;
00250 }
00251
00252 template <typename A>
00253 inline typename WORD_TRAITS::second_projection_t
00254 WORD_TRAITS::second_projection(const WORD_TRAITS::second_monoid_t& mon,
00255 const WORD_TRAITS::word_t& word)
00256 {
00257
00258 static_assertion_(not (misc::static_eq<second_projection_t,
00259 undefined_type>::value), need_second_projection)
00260
00261 second_projection_t R(mon);
00262
00263 R.value() = second_projection(word.value());
00264
00265 return R;
00266 }
00267
00268 # undef WORD_TRAITS
00269
00270 }
00271
00272 }
00273
00274 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_MONOID_STR_WORDS_HXX