00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef VCSN_ALGORITHMS_STANDARD_OF_HXX
00018 # define VCSN_ALGORITHMS_STANDARD_OF_HXX
00019
00020 # include <vaucanson/algorithms/standard_of.hh>
00021
00022 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hh>
00023 # include <vaucanson/algorithms/standard.hh>
00024
00025 # include <vaucanson/misc/usual_macros.hh>
00026
00027 namespace vcsn {
00028
00029 namespace algebra {
00030
00031
00032
00033
00034 template <class Exp_,
00035 class Auto_,
00036 class Dispatch_>
00037 class Standard_OfVisitor :
00038 public algebra::KRatExpMatcher<
00039 Standard_OfVisitor<Exp_, Auto_, Dispatch_>,
00040 Exp_,
00041 Auto_*,
00042 Dispatch_
00043 >
00044 {
00045 public :
00046 typedef Auto_* automaton_ptr_t;
00047 typedef Auto_ automaton_t;
00048 typedef typename automaton_t::set_t automata_set_t;
00049
00050 typedef typename automaton_t::series_set_t series_set_t;
00051 typedef typename automaton_t::monoid_t monoid_t;
00052 typedef typename automaton_t::semiring_t semiring_t;
00053
00054 typedef typename automaton_t::series_set_elt_t series_set_elt_t;
00055 typedef typename automaton_t::monoid_elt_t monoid_elt_t;
00056 typedef typename automaton_t::semiring_elt_t semiring_elt_t;
00057
00058 typedef typename automaton_t::hstate_t hstate_t;
00059 typedef typename automaton_t::htransition_t htransition_t;
00060
00061 typedef typename Exp_::monoid_elt_value_t monoid_elt_value_t;
00062 typedef typename Exp_::semiring_elt_value_t semiring_elt_value_t;
00063
00064 typedef Standard_OfVisitor<Exp_, Auto_, Dispatch_> this_class;
00065 typedef algebra::KRatExpMatcher<this_class, Exp_, Auto_*, Dispatch_>
00066 parent_class;
00067 typedef typename parent_class::return_type return_type;
00068
00069 public :
00070
00071 Standard_OfVisitor(automaton_t& a) :
00072 automata_set_(a.series()),
00073 auto_(&a)
00074 {
00075 }
00076
00077 INHERIT_CONSTRUCTORS(this_class, Exp_, Auto_*, Dispatch_);
00078
00079
00080
00081 MATCH__(Product, lhs, rhs)
00082 {
00083 AUTOMATON_TYPES(automaton_t);
00084 match(lhs);
00085
00086 hstate_t lhs_i = initial_;
00087
00088
00089 typedef typename std::list<std::pair<hstate_t, series_set_elt_t> >
00090 list_fin_st_t;
00091
00092 list_fin_st_t lhs_tmp;
00093
00094 for_all_final_states(f, *auto_)
00095 lhs_tmp.push_back (std::pair<hstate_t, series_set_elt_t>
00096 (*f, auto_->get_final(*f)));
00097 auto_->clear_final();
00098
00099 match(rhs);
00100
00101
00102 typedef std::list<hstate_t> list_st_t;
00103
00104 list_st_t lhs_finals;
00105
00106 for (typename list_fin_st_t::iterator i = lhs_tmp.begin();
00107 i != lhs_tmp.end();
00108 ++i)
00109 {
00110 auto_->set_final(i->first, i->second);
00111 lhs_finals.push_back (i->first);
00112 }
00113
00114 concat_of_standard_here_common(auto_->structure(),
00115 *auto_,
00116 *auto_,
00117 initial_,
00118 lhs_finals.begin(),
00119 lhs_finals.end(),
00120 identity_);
00121
00122
00123 for (typename automaton_t::delta_iterator i(auto_->value(), initial_);
00124 ! i.done();
00125 i.next())
00126 auto_->del_transition(*i);
00127
00128 auto_->del_state(initial_);
00129 initial_ = lhs_i;
00130 return auto_;
00131 }
00132 END
00133
00134 MATCH__(Sum, lhs, rhs)
00135 {
00136 AUTOMATON_TYPES(automaton_t);
00137 typedef typename std::list<std::pair<hstate_t, series_set_elt_t> >
00138 list_fin_st_t;
00139
00140 match(lhs);
00141
00142
00143 hstate_t left_i = initial_;
00144
00145
00146 list_fin_st_t lhs_tmp;
00147
00148 for_all_const_final_states(f, *auto_)
00149 lhs_tmp.push_back (std::pair<hstate_t, series_set_elt_t>
00150 (*f, auto_->get_final(*f)));
00151
00152 auto_->clear_final();
00153
00154 match(rhs);
00155
00156
00157 for (typename list_fin_st_t::iterator i = lhs_tmp.begin();
00158 i != lhs_tmp.end();
00159 ++i)
00160 auto_->set_final(i->first, i->second);
00161
00162 union_of_standard_here_common(auto_->structure(),
00163 *auto_,
00164 left_i,
00165 initial_);
00166
00167 initial_ = left_i;
00168 return auto_;
00169 }
00170 END
00171
00172 MATCH_(Star, node)
00173 {
00174 match(node);
00175 star_of_standard_here_common(auto_->structure(), *auto_, initial_);
00176 return auto_;
00177 }
00178 END
00179
00180 MATCH__(LeftWeight, w, node)
00181 {
00182 const semiring_t& semiring = automata_set_.series().semiring();
00183 const semiring_elt_t weight (semiring, w);
00184 match(node);
00185
00186 std::list<htransition_t> e;
00187 for (typename automaton_t::delta_iterator j(auto_->value(), initial_);
00188 ! j.done();
00189 j.next())
00190 e.push_back(*j);
00191 for (typename std::list<htransition_t>::const_iterator j = e.begin();
00192 j != e.end();
00193 ++j)
00194 {
00195
00196
00197
00198
00199
00200
00201
00202 typedef typename automaton_t::label_t label_t;
00203 typedef Element<series_set_t, label_t> label_elt_t;
00204
00205 label_elt_t label (automata_set_.series(), auto_->label_of(*j));
00206 label = weight * label;
00207
00208 hstate_t dst = auto_->dst_of(*j);
00209 auto_->del_transition(*j);
00210
00211 if (label != zero_as<label_t>::of(automata_set_.series()))
00212 auto_->add_transition(initial_, dst, label.value());
00213 }
00214
00215 auto_->set_final(initial_, weight * auto_->get_final(initial_));
00216 return auto_;
00217 }
00218 END
00219
00220 MATCH__(RightWeight, node, w)
00221 {
00222 const semiring_t& semiring = automata_set_.series().semiring();
00223 const semiring_elt_t weight (semiring, w);
00224 match(node);
00225
00226 for (typename automaton_t::final_iterator f, next = auto_->final().begin();
00227 next != auto_->final().end();)
00228 {
00229
00230
00231
00232
00233 f = next;
00234 next++;
00235 auto_->set_final(*f, auto_->get_final(*f) * weight);
00236 }
00237
00238 return auto_;
00239 }
00240 END
00241
00242 MATCH_(Constant, m)
00243 {
00244 initial_ = auto_->add_state();
00245 hstate_t last = initial_;
00246 hstate_t new_f = initial_;
00247
00248 for (typename monoid_elt_value_t::const_iterator i = m.begin();
00249 i != m.end(); ++i)
00250 {
00251 new_f = auto_->add_state();
00252 auto_->add_letter_transition(last, new_f, *i);
00253 last = new_f;
00254 }
00255 auto_->set_initial(initial_);
00256 auto_->set_final(new_f);
00257
00258 return auto_;
00259 }
00260 END
00261
00262 MATCH(Zero)
00263 {
00264 initial_ = auto_->add_state();
00265 auto_->set_initial(initial_);
00266 return auto_;
00267 }
00268 END
00269
00270 MATCH(One)
00271 {
00272 initial_ = auto_->add_state();
00273 auto_->set_initial(initial_);
00274 auto_->set_final(initial_);
00275 return auto_;
00276 }
00277 END
00278
00279 private:
00280 automata_set_t automata_set_;
00281
00282 hstate_t initial_;
00283 automaton_ptr_t auto_;
00284
00285 typedef
00286 class
00287 {
00288 public:
00289 inline hstate_t operator[] (hstate_t a)
00290 {
00291 return a;
00292 }
00293 } ident_t;
00294
00295 ident_t identity_;
00296 };
00297
00298 }
00299
00300 template <typename A,
00301 typename Output,
00302 typename Exp>
00303 void
00304 do_standard_of(const AutomataBase<A>&,
00305 Output& output,
00306 const Exp& kexp)
00307 {
00308 algebra::Standard_OfVisitor<Exp, Output, algebra::DispatchFunction<Exp> >
00309 m(output);
00310
00311 m.match(kexp);
00312 }
00313
00314 template<typename A,
00315 typename T,
00316 typename Exp>
00317 void
00318 standard_of(Element<A, T>& out,
00319 const Exp& kexp)
00320 {
00321 do_standard_of(out.structure(), out, kexp);
00322 }
00323
00324 }
00325
00326 #endif // ! VCSN_ALGORITHMS_STANDARD_OF_HXX