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           auto_->del_state(initial_);
00124           initial_ = lhs_i;
00125           return auto_;
00126         }
00127         END
00128 
00129         MATCH__(Sum, lhs, rhs)
00130         {
00131           AUTOMATON_TYPES(automaton_t);
00132           typedef typename std::list<std::pair<hstate_t, series_set_elt_t> >
00133             list_fin_st_t;
00134 
00135           match(lhs);
00136 
00137           
00138           hstate_t left_i = initial_;
00139 
00140           
00141           list_fin_st_t lhs_tmp;
00142 
00143           for_all_const_final_states(f, *auto_)
00144             lhs_tmp.push_back (std::pair<hstate_t, series_set_elt_t>
00145                                (*f, auto_->get_final(*f)));
00146 
00147           auto_->clear_final();
00148 
00149           match(rhs);
00150 
00151           
00152           for (typename list_fin_st_t::iterator i = lhs_tmp.begin();
00153                i != lhs_tmp.end();
00154                ++i)
00155             auto_->set_final(i->first, i->second);
00156 
00157           union_of_standard_here_common(auto_->structure(),
00158                                         *auto_,
00159                                         left_i,
00160                                         initial_);
00161 
00162           initial_ = left_i;
00163           return auto_;
00164         }
00165         END
00166 
00167         MATCH_(Star, node)
00168         {
00169           match(node);
00170           star_of_standard_here_common(auto_->structure(), *auto_, initial_);
00171           return auto_;
00172         }
00173         END
00174 
00175         MATCH__(LeftWeight, w, node)
00176         {
00177           const semiring_t&     semiring = automata_set_.series().semiring();
00178           const semiring_elt_t  weight (semiring, w);
00179           match(node);
00180 
00181           std::list<htransition_t>      e;
00182           for (typename automaton_t::delta_iterator j(auto_->value(), initial_);
00183                ! j.done();
00184                j.next())
00185             e.push_back(*j);
00186           for (typename std::list<htransition_t>::const_iterator j = e.begin();
00187                j != e.end();
00188                ++j)
00189           {
00190             
00191             
00192             
00193             
00194             
00195             
00196             
00197             typedef typename automaton_t::label_t       label_t;
00198             typedef Element<series_set_t, label_t>      label_elt_t;
00199 
00200             label_elt_t label (automata_set_.series(), auto_->label_of(*j));
00201             label  = weight * label;
00202 
00203             hstate_t dst = auto_->dst_of(*j);
00204             auto_->del_transition(*j);
00205 
00206             if (label != zero_as<label_t>::of(automata_set_.series()))
00207               auto_->add_transition(initial_, dst, label.value());
00208           }
00209 
00210           auto_->set_final(initial_, weight * auto_->get_final(initial_));
00211           return auto_;
00212         }
00213         END
00214 
00215         MATCH__(RightWeight, node, w)
00216         {
00217           const semiring_t&     semiring = automata_set_.series().semiring();
00218           const semiring_elt_t  weight (semiring, w);
00219           match(node);
00220 
00221           for (typename automaton_t::final_iterator f, next = auto_->final().begin();
00222                next != auto_->final().end();)
00223           {
00224             
00225             
00226             
00227             
00228             f = next;
00229             next++;
00230             auto_->set_final(*f, auto_->get_final(*f) * weight);
00231           }
00232 
00233           return auto_;
00234         }
00235         END
00236 
00237         MATCH_(Constant, m)
00238         {
00239           initial_ = auto_->add_state();
00240           hstate_t last = initial_;
00241           hstate_t new_f = initial_;
00242 
00243           for (typename monoid_elt_value_t::const_iterator i = m.begin();
00244                i != m.end(); ++i)
00245           {
00246             new_f = auto_->add_state();
00247             auto_->add_letter_transition(last, new_f, *i);
00248             last = new_f;
00249           }
00250           auto_->set_initial(initial_);
00251           auto_->set_final(new_f);
00252 
00253           return auto_;
00254         }
00255         END
00256 
00257         MATCH(Zero)
00258         {
00259           initial_ = auto_->add_state();
00260           auto_->set_initial(initial_);
00261           return auto_;
00262         }
00263         END
00264 
00265         MATCH(One)
00266         {
00267           initial_ = auto_->add_state();
00268           auto_->set_initial(initial_);
00269           auto_->set_final(initial_);
00270           return auto_;
00271         }
00272         END
00273 
00274       private:
00275         automata_set_t automata_set_;
00276         
00277         hstate_t initial_;
00278         automaton_ptr_t auto_;
00279 
00280         typedef
00281         class
00282         {
00283           public:
00284             inline hstate_t operator[] (hstate_t a)
00285             {
00286               return a;
00287             }
00288         } ident_t;
00289 
00290         ident_t identity_;
00291     };
00292 
00293   }
00294 
00295   template <typename A,
00296             typename Output,
00297             typename Exp>
00298   void
00299   do_standard_of(const AutomataBase<A>&,
00300                  Output& output,
00301                  const Exp& kexp)
00302   {
00303     algebra::Standard_OfVisitor<Exp, Output, algebra::DispatchFunction<Exp> >
00304       m(output);
00305 
00306     m.match(kexp);
00307   }
00308 
00309   template<typename A,
00310            typename T,
00311            typename Exp>
00312   void
00313   standard_of(Element<A, T>& out,
00314               const Exp& kexp)
00315   {
00316     do_standard_of(out.structure(), out, kexp);
00317   }
00318 
00319 } 
00320 
00321 #endif // ! VCSN_ALGORITHMS_STANDARD_OF_HXX