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