00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VCSN_ALGORITHMS_NORMALIZED_HXX
00019 # define VCSN_ALGORITHMS_NORMALIZED_HXX
00020
00021 # include <vaucanson/algorithms/normalized.hh>
00022 # include <vaucanson/algorithms/internal/has_neighbour.hh>
00023
00024 # include <vaucanson/automata/concept/automata_base.hh>
00025 # include <vaucanson/misc/usual_macros.hh>
00026 # include <vaucanson/algorithms/sum.hh>
00027
00028 # include <stack>
00029
00030 namespace vcsn {
00031
00032
00033
00034
00035 template <class A_, typename Auto_>
00036 void
00037 do_normalize_here(const AutomataBase<A_>&,
00038 Auto_& a)
00039 {
00040 TIMER_SCOPED("normalize");
00041 AUTOMATON_TYPES(Auto_);
00042
00043 hstate_t h = a.add_state();
00044
00045 for_all_const_initial_states(i, a)
00046 a.add_series_transition(h, *i, a.get_initial(*i));
00047
00048 a.clear_initial();
00049 a.set_initial(h);
00050
00051 h = a.add_state();
00052
00053 for_all_const_final_states(i, a)
00054 a.add_series_transition(*i, h, a.get_final(*i));
00055
00056 a.clear_final();
00057 a.set_final(h);
00058 }
00059
00060 template <typename A, typename T>
00061 Element<A, T>
00062 normalize(const Element<A, T>& a)
00063 {
00064 Element<A, T> result(a);
00065 do_normalize_here(result.structure(), result);
00066 return result;
00067 }
00068
00069 template<typename A, typename T>
00070 void
00071 normalize_here(Element<A, T>& a)
00072 {
00073 do_normalize_here(a.structure(), a);
00074 }
00075
00076
00077
00078
00079
00080
00081 template <typename A, typename lhs_t, typename rhs_t>
00082 void
00083 do_union_of_normalized_here(const AutomataBase<A>&,
00084 lhs_t& lhs,
00085 const rhs_t& rhs)
00086 {
00087 TIMER_SCOPED("union_of_normalized");
00088 AUTOMATON_TYPES(lhs_t);
00089 monoid_elt_t monoid_identity =
00090 lhs.series().monoid().identity(SELECT(monoid_elt_value_t));
00091 hstate_t new_i = lhs.add_state();
00092 hstate_t new_f = lhs.add_state();
00093
00094 sum_here(lhs, rhs);
00095
00096 for_all_const_initial_states(i, lhs)
00097 lhs.add_spontaneous(new_i, *i, lhs.get_initial(*i).get(monoid_identity));
00098 lhs.clear_initial();
00099
00100 for_all_const_final_states(f, lhs)
00101 lhs.add_spontaneous(*f, new_f, lhs.get_final(*f).get(monoid_identity));
00102 lhs.clear_final();
00103
00104 lhs.set_final(new_f);
00105 lhs.set_initial(new_i);
00106 }
00107
00108 template<typename A, typename T, typename U>
00109 void
00110 union_of_normalized_here(Element<A, T>& lhs,
00111 const Element<A, U>& rhs)
00112 {
00113
00114 do_union_of_normalized_here(lhs.structure(), lhs, rhs);
00115 }
00116
00117 template<typename A, typename T, typename U>
00118 Element<A, T>
00119 union_of_normalized(const Element<A, T>& lhs,
00120 const Element<A, U>& rhs)
00121 {
00122
00123 Element<A, T> ret(lhs);
00124 do_union_of_normalized_here(ret.structure(), ret, rhs);
00125 return ret;
00126 }
00127
00128
00129
00130
00131 template <typename A, typename auto_t>
00132 bool
00133 do_is_normalized(const AutomataBase<A>&,
00134 const auto_t& a)
00135 {
00136 TIMER_SCOPED("is_normalized");
00137 typedef typename auto_t::series_set_elt_value_t series_set_elt_value_t;
00138 typedef typename auto_t::series_set_elt_t series_set_elt_t;
00139
00140 series_set_elt_t series_identity =
00141 a.series().identity(SELECT(series_set_elt_value_t));
00142
00143 if (a.initial().size() != 1
00144 || a.final().size() != 1
00145 || a.get_initial(*a.initial().begin()) != series_identity
00146 || a.get_final(*a.final().begin()) != series_identity)
00147 return false;
00148
00149
00150
00151 return !has_successors(a, *a.final().begin())
00152 && !has_predecessors(a, *a.initial().begin());
00153 }
00154
00155 template<typename A, typename T>
00156 bool
00157 is_normalized(const Element<A, T>& a)
00158 {
00159 return do_is_normalized(a.structure(), a);
00160 }
00161
00162
00163
00164
00165 template <typename A, typename lhs_t, typename rhs_t>
00166 void
00167 do_concatenate_of_normalized_here(const AutomataBase<A>&,
00168 lhs_t& lhs,
00169 const rhs_t& rhs)
00170 {
00171 TIMER_SCOPED("concatenate_of_normalized");
00172 AUTOMATON_TYPES(rhs_t);
00173 typedef std::map<hstate_t, hstate_t> map_lhs_rhs_t;
00174 typedef std::list<htransition_t> delta_ret_t;
00175
00176 hstate_t glue_state = *lhs.final().begin();
00177
00178
00179
00180
00181 map_lhs_rhs_t map_h;
00182
00183
00184
00185 bool merge_lhs_final_and_rhs_initial =
00186 (lhs.get_final(*lhs.final().begin()).value() ==
00187 lhs.series().identity(SELECT(series_set_elt_value_t)) &&
00188 rhs.get_initial(*rhs.initial().begin()).value() ==
00189 rhs.series().identity(SELECT(series_set_elt_value_t)));
00190
00191 for_all_const_states(s, rhs)
00192 {
00193 hstate_t new_state;
00194
00195 if (merge_lhs_final_and_rhs_initial)
00196 {
00197 if (rhs.is_initial(*s))
00198 new_state = glue_state;
00199 else
00200 new_state = lhs.add_state();
00201 }
00202 else
00203 new_state = lhs.add_state();
00204 map_h[*s] = new_state;
00205 lhs.set_final(new_state, rhs.get_final(*s));
00206 }
00207
00208
00209
00210
00211 for_all_const_transitions(t, rhs)
00212 lhs.add_transition(map_h[rhs.src_of(*t)],
00213 map_h[rhs.dst_of(*t)],
00214 rhs.label_of(*t));
00215
00216
00217
00218
00219 if (!merge_lhs_final_and_rhs_initial)
00220 {
00221 monoid_elt_t monoid_identity =
00222 rhs.series().monoid().identity(SELECT(monoid_elt_value_t));
00223 hstate_t i = *rhs.initial().begin();
00224 hstate_t f = *lhs.final().begin();
00225
00226 lhs.add_spontaneous(f, map_h[i],
00227 lhs.get_final(f).get(monoid_identity) *
00228 rhs.get_initial(i).get(monoid_identity));
00229 lhs.unset_final(f);
00230 lhs.unset_initial(map_h[i]);
00231 }
00232 }
00233
00234 template<typename A, typename T, typename U>
00235 void
00236 concatenate_of_normalized_here(Element<A, T>& lhs, const Element<A, U>& rhs)
00237 {
00238
00239 do_concatenate_of_normalized_here(lhs.structure(), lhs, rhs);
00240 }
00241
00242 template<typename A, typename T, typename U>
00243 Element<A, T>
00244 concatenate_of_normalized(const Element<A, T>& lhs, const Element<A, U>& rhs)
00245 {
00246
00247 Element<A, T> ret(lhs);
00248 do_concatenate_of_normalized_here(ret.structure(), ret, rhs);
00249 return ret;
00250 }
00251
00252
00253
00254
00255 template <typename A, typename auto_t>
00256 void
00257 do_star_of_normalized_here(const AutomataBase<A>&, auto_t& a)
00258 {
00259 TIMER_SCOPED("star_of_normalized");
00260 AUTOMATON_TYPES(auto_t);
00261 monoid_elt_t monoid_identity =
00262 a.series().monoid().identity(SELECT(monoid_elt_value_t));
00263 hstate_t old_i = *a.initial().begin();
00264 hstate_t old_f = *a.final().begin();
00265 hstate_t new_i = a.add_state();
00266 hstate_t new_f = a.add_state();
00267
00268 a.add_spontaneous(old_f, old_i, a.get_initial(old_i).get(monoid_identity));
00269 a.add_spontaneous(new_i, old_i, a.get_initial(old_i).get(monoid_identity));
00270 a.add_spontaneous(old_f, new_f, a.get_final(old_f).get(monoid_identity));
00271 a.add_spontaneous(new_i, new_f);
00272 a.set_final(new_f);
00273 a.unset_final(old_f);
00274 a.set_initial(new_i);
00275 a.unset_initial(old_i);
00276 }
00277
00278 template<typename A, typename T>
00279 void
00280 star_of_normalized_here(Element<A, T>& a)
00281 {
00282 do_star_of_normalized_here(a.structure(), a);
00283 }
00284
00285 template<typename A, typename T>
00286 Element<A, T>
00287 star_of_normalized(const Element<A, T>& a)
00288 {
00289
00290 Element<A, T> ret(a);
00291 do_star_of_normalized_here(ret.structure(), ret);
00292 return ret;
00293 }
00294
00295 }
00296
00297 #endif // ! VCSN_ALGORITHMS_NORMALIZED_HXX