graph.hh

00001 // graph.hh: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2005, 2006 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_AUTOMATA_IMPLEMENTATION_GRAPH_HH
00018 # define VCSN_AUTOMATA_IMPLEMENTATION_GRAPH_HH
00019 
00020 # include <set>
00021 # include <map>
00022 # include <vector>
00023 # include <vaucanson/automata/concept/handlers.hh>
00024 # include <vaucanson/automata/concept/automata_base.hh>
00025 # include <vaucanson/automata/concept/transducer_base.hh>
00026 # include <vaucanson/automata/concept/automata_kind.hh>
00027 # include <vaucanson/automata/concept/tags.hh>
00028 # include <vaucanson/automata/implementation/kind_adapter.hh>
00029 # include <vaucanson/misc/support.hh>
00030 # include <vaucanson/tools/usual_macros.hh>
00031 # include <vaucanson/automata/implementation/geometry.hh>
00032 
00033 
00034 namespace vcsn
00035 {
00036 
00037 
00038   typedef htransition_t hedge_t;
00039   namespace delta_kind
00040   {
00041     typedef transitions edges;
00042   }
00043 
00045   template<typename EdgeLabel>
00046   struct edge_value
00047   {
00048       edge_value(hstate_t, hstate_t, const EdgeLabel& v = EdgeLabel());
00049 
00050       operator const EdgeLabel& () const
00051       { return label; }
00052       operator EdgeLabel& ()
00053       { return label; }
00054 
00055       EdgeLabel label;
00056       hstate_t  from;
00057       hstate_t  to;
00058   };
00059 
00061   struct state_value
00062   {
00063       typedef std::set<hedge_t> edges_t;
00064       state_value();
00065 
00066       edges_t output_edges;
00067       edges_t input_edges;
00068   };
00069 
00071   typedef utility::SparseInterval<hstate_t, std::set<hstate_t> >
00072   StateContainer;
00073 
00074   typedef utility::SparseInterval<hedge_t, std::set<hedge_t> >
00075   EdgeContainer;
00076 
00077 
00079   template <class K, class WordValue, class WeightValue,
00080             class SeriesValue, class Letter, class Tag, class Geometry>
00081   class Graph
00082   {
00084     public:
00085       typedef Graph<K, WordValue, WeightValue, SeriesValue,
00086                     Letter, Tag, Geometry>                      self_t;
00087 
00089     public:
00090       typedef typename LabelOf<K, WordValue, WeightValue, SeriesValue, Letter>
00091       ::ret label_t;
00092 
00093       typedef state_value                       state_value_t;
00094       typedef edge_value<label_t>               edge_value_t;
00095 
00096       typedef std::vector<state_value_t>        state_data_t;
00097       typedef std::vector<edge_value_t>         edge_data_t;
00098 
00099       typedef StateContainer            states_t;
00100       typedef EdgeContainer             edges_t;
00101 
00102       typedef SeriesValue                       series_set_elt_value_t;
00103 
00104       typedef std::map<hstate_t, series_set_elt_value_t> initial_t;
00105       typedef std::map<hstate_t, series_set_elt_value_t> final_t;
00106 
00107       typedef utility::Support<initial_t>       initial_support_t;
00108       typedef utility::Support<final_t> final_support_t;
00109 
00110     public:
00111       // we guarantee that the handlers of state are indexed from 0 to
00112       // initial_number_of_state - 1 when using this constructor.
00113       Graph();
00114       Graph(unsigned initial_number_of_state,
00115             unsigned number_of_edge_initially_allocated);
00116 
00118       states_t                  states() const;
00119 
00121       edges_t                   edges() const;
00122 
00124       initial_support_t         initial() const;
00125       final_support_t           final() const;
00126 
00129     public:
00130       bool                      has_state(hstate_t n) const;
00131 
00132     public:
00133       hstate_t                  add_state();
00134       void                      del_state(hstate_t n);
00135 
00136     public:
00137       void                      set_initial(hstate_t,
00138                                             const series_set_elt_value_t&,
00139                                             const series_set_elt_value_t&);
00140       const series_set_elt_value_t& get_initial(hstate_t,
00141                                                 const
00142                                                 series_set_elt_value_t&) const;
00143       void                      clear_initial();
00144       void                      set_final(hstate_t,
00145                                           const series_set_elt_value_t&,
00146                                           const series_set_elt_value_t&);
00147       const series_set_elt_value_t& get_final(hstate_t,
00148                                               const
00149                                               series_set_elt_value_t&) const;
00150       void                      clear_final();
00156     public:
00157       bool                      has_edge(hedge_t n) const;
00158 
00159     public:
00160       hedge_t                   add_edge(hstate_t h1, hstate_t h2,
00161                                          const label_t& v);
00162       void                      del_edge(hedge_t e);
00163 
00164     public:
00165       hstate_t                  src_of(hedge_t e1) const;
00166       hstate_t                  dst_of(hedge_t e2) const;
00167 
00168     public:
00169       const label_t&            label_of(hedge_t n) const;
00170       void                      update(hedge_t, label_t);
00175     public:
00176       template <class S>
00177       bool                      exists(const AutomataBase<S>& s) const;
00178 
00179     public:
00180       template <class OutputIterator, class Query>
00181       void                      delta(OutputIterator res,
00182                                       hstate_t from,
00183                                       const Query& q,
00184                                       delta_kind::edges) const;
00185       template <class OutputIterator, class Query>
00186       void                      delta(OutputIterator res,
00187                                       hstate_t from,
00188                                       const Query& q,
00189                                       delta_kind::states) const;
00190       template <class OutputIterator, class Query>
00191       void                      rdelta(OutputIterator res,
00192                                        hstate_t from,
00193                                        const Query& q,
00194                                        delta_kind::edges) const;
00195       template <class OutputIterator, class Query>
00196       void                      rdelta(OutputIterator res,
00197                                        hstate_t from,
00198                                        const Query& q,
00199                                        delta_kind::states) const;
00202       // FIXME: Not implemented.
00203     public:
00204       self_t&                   clone() const;
00205 
00208     public:
00209       typedef Tag tag_t;
00210       tag_t& tag();
00211       const tag_t& tag() const;
00216     public:
00217       typedef Geometry geometry_t;
00218       geometry_t&               geometry();
00219       const geometry_t& geometry() const;
00222     private:
00223       geometry_t                geometry_;
00224 
00225     public:
00226       state_data_t              states_;
00227       edge_data_t                       edges_;
00228       std::set<hstate_t>                removed_states_;
00229       std::set<hedge_t>         removed_edges_;
00230       tag_t                     tag_;
00231       final_t                   final_;
00232       initial_t                 initial_;
00233   };
00234 
00235 
00236 # define TParam                                                         \
00237   template <class S, class WordValue, class WeightValue, class SeriesValue, \
00238             class Letter, class Tag, class Geometry>
00239 
00240   TParam
00241   ADAPT_ADD_LETTER_TRANSITION_TO_SERIES_LABEL(Graph<labels_are_series,
00242                                               WordValue, WeightValue,
00243                                               SeriesValue, Letter, Tag, Geometry>);
00244 
00245 
00246   TParam
00247   ADAPT_LETTER_OF_TO_SERIES_LABEL(Graph<labels_are_series,
00248                                   WordValue, WeightValue,
00249                                   SeriesValue, Letter, Tag, Geometry>);
00250 
00251   TParam
00252   ADAPT_WORD_OF_TO_SERIES_LABEL(Graph<labels_are_series,
00253                                 WordValue, WeightValue,
00254                                 SeriesValue, Letter, Tag, Geometry>);
00255 
00256   TParam
00257   ADAPT_WORD_OF_TO_LETTERS_LABEL(Graph<labels_are_letters,
00258                                  WordValue, WeightValue,
00259                                  SeriesValue, Letter, Tag, Geometry>);
00260 
00261   TParam
00262   ADAPT_SERIE_OF_TO_LETTERS_LABEL(Graph<labels_are_letters,
00263                                   WordValue, WeightValue,
00264                                   SeriesValue, Letter, Tag, Geometry>);
00265 
00266   TParam
00267   ADAPT_ADD_SERIE_TRANSITION_TO_LETTERS_LABEL(Graph<labels_are_letters,
00268                                               WordValue, WeightValue,
00269                                               SeriesValue, Letter, Tag, Geometry>);
00270 
00271   template <class S, class WordValue, class WeightValue, class SeriesValue,
00272             class Letter, class Tag, class Geometry,
00273             typename OutputIterator, typename L>
00274   void op_letter_delta(const AutomataBase<S>&,
00275                        const Graph<labels_are_letters,
00276                        WordValue, WeightValue,
00277                        SeriesValue, Letter, Tag, Geometry>&,
00278                        OutputIterator, hstate_t, const L&,
00279                        delta_kind::states);
00280 
00281   template <class Kind, class WordValue, class WeightValue, class SerieValue,
00282             class Letter, class Tag, class Geometry, class I>
00283   Tag& op_tag(const AutomataBase<I>&,
00284               Graph<Kind, WordValue, WeightValue,
00285               SerieValue, Letter, Tag, Geometry>&);
00286 
00287   template <class Kind, class WordValue, class WeightValue, class SerieValue,
00288             class Letter, class Tag, class Geometry, class I>
00289   const Tag& op_tag(const AutomataBase<I>&,
00290                     const Graph<Kind, WordValue, WeightValue,
00291                     SerieValue, Letter, Tag, Geometry>&);
00292 
00293   template <class Kind, class WordValue, class WeightValue, class SerieValue,
00294             class Letter, class Tag, class Geometry, class I>
00295   Geometry&
00296   op_geometry(const AutomataBase<I>&,
00297               Graph<Kind, WordValue, WeightValue,
00298               SerieValue, Letter, Tag, Geometry>&);
00299 
00300   template <class Kind, class WordValue, class WeightValue, class SerieValue,
00301             class Letter, class Tag, class Geometry, class I>
00302   const Geometry&
00303   op_geometry(const AutomataBase<I>&,
00304               const Graph<Kind, WordValue, WeightValue,
00305               SerieValue, Letter, Tag, Geometry>&);
00306 
00307 
00308 
00309 # undef TParam
00310 
00311   // This implementation can be used as an implementation of automaton.
00312   template <class Kind,
00313             class WordValue,
00314             class WeightValue,
00315             class SeriesValue,
00316             class Letter,
00317             class Tag,
00318             class Geometry>
00319   struct automaton_traits<Graph<Kind,
00320                                 WordValue,
00321                                 WeightValue,
00322                                 SeriesValue,
00323                                 Letter,
00324                                 Tag,
00325                                 Geometry>  >
00326   {
00327       typedef SeriesValue                                       series_set_elt_value_t;
00328       typedef WordValue                                 word_value_t;
00329       typedef WordValue                                 monoid_elt_value_t;
00330       typedef WeightValue                                       semiring_elt_value_t;
00331       typedef Letter                                    letter_t;
00332       typedef typename LabelOf<Kind, WordValue, WeightValue, SeriesValue, Letter>
00333       ::ret                                             label_t;
00334       typedef Tag                                               tag_t;
00335       typedef edge_value<label_t>                               transition_value_t;
00336       typedef state_value                                       state_value_t;
00337       typedef std::vector<state_value_t>                        state_data_t;
00338       typedef std::vector<transition_value_t>           transition_data_t;
00339 
00340       typedef StateContainer                            states_t;
00341       typedef EdgeContainer                             transitions_t;
00342 
00343       typedef typename states_t::iterator                       state_iterator;
00344       typedef typename transitions_t::iterator          transition_iterator;
00345 
00346       typedef std::map<hstate_t, series_set_elt_value_t>        initial_t;
00347       typedef std::map<hstate_t, series_set_elt_value_t>        final_t;
00348       typedef utility::Support<initial_t>                       initial_support_t;
00349       typedef utility::Support<final_t>                 final_support_t;
00350       typedef typename initial_support_t::iterator      initial_iterator;
00351       typedef typename final_support_t::iterator                final_iterator;
00352 
00353       typedef Geometry                                  geometry_t;
00354   };
00355 
00356   // This implementation can be used as a transducer one.
00357   template <class Kind,
00358             class WordValue,
00359             class WeightValue,
00360             class SeriesValue,
00361             class Letter,
00362             class Tag,
00363             class Geometry>
00364   struct transducer_traits<Graph<Kind,
00365                                  WordValue,
00366                                  WeightValue,
00367                                  SeriesValue,
00368                                  Letter,
00369                                  Tag,
00370                                  Geometry>  >
00371   {
00372       typedef WordValue                 input_monoid_elt_value_t;
00373       typedef typename algebra::series_traits<WeightValue>::monoid_elt_value_t
00374       output_monoid_elt_value_t;
00375       typedef typename algebra::series_traits<WeightValue>::semiring_elt_value_t
00376       output_semiring_elt_value_t;
00377   };
00378 
00379   // Explain how to project type of transducer into input automaton type.
00380   template <class S,
00381             class Kind,
00382             class WordValue,
00383             class WeightValue,
00384             class SeriesValue,
00385             class Letter,
00386             class Tag,
00387             class Geometry>
00388   struct projection_traits<S, Graph<Kind,
00389                                     WordValue,
00390                                     WeightValue,
00391                                     SeriesValue,
00392                                     Letter,
00393                                     Tag,
00394                                     Geometry>  >
00395   {
00396       typedef Graph<Kind, WordValue, WeightValue, SeriesValue,
00397                     Letter, Tag, Geometry>                      self_t;
00398       typedef typename transducer_traits<self_t>::output_semiring_elt_value_t
00399       semiring_elt_value_t;
00400       typedef typename transducer_traits<self_t>::input_monoid_elt_value_t
00401       monoid_elt_value_t;
00402       typedef typename algebra::mute_series_impl<SeriesValue,
00403                                                  semiring_elt_value_t,
00404                                                  monoid_elt_value_t>
00405       ::ret series_set_elt_value_t;
00406 
00407       typedef
00408       Graph<Kind,
00409             monoid_elt_value_t,
00410             semiring_elt_value_t,
00411             series_set_elt_value_t,
00412             Letter,
00413             Tag,
00414             Geometry>
00415       ret;
00416   };
00417 
00418   template <class Kind,
00419             class WordValue,
00420             class WeightValue,
00421             class SeriesValue,
00422             class Letter,
00423             class Tag,
00424             class Geometry>
00425   struct output_projection_traits<Graph<Kind,
00426                                         WordValue,
00427                                         WeightValue,
00428                                         SeriesValue,
00429                                         Letter,
00430                                         Tag, Geometry>  >
00431   {
00432       typedef Graph<Kind, WordValue, WeightValue, SeriesValue,
00433                     Letter, Tag, Geometry>                      self_t;
00434 
00435       typedef typename automaton_traits<self_t>::semiring_elt_value_t
00436       series_set_elt_value_t;
00437 
00438       typedef typename
00439       algebra::series_traits<series_set_elt_value_t>::monoid_elt_value_t
00440       monoid_elt_value_t;
00441 
00442       typedef typename
00443       algebra::series_traits<series_set_elt_value_t>::semiring_elt_value_t
00444       semiring_elt_value_t;
00445 
00446       typedef
00447       Graph<Kind,
00448             monoid_elt_value_t,
00449             semiring_elt_value_t,
00450             series_set_elt_value_t,
00451             Letter,
00452             Tag,
00453             Geometry>
00454       ret;
00455   };
00456 
00457   // Explain how to extend an input automaton into a transducer.
00458   template <class Kind,
00459             class WordValue,
00460             class WeightValue,
00461             class SeriesValue,
00462             class Letter,
00463             class Tag,
00464             class Geometry>
00465   struct extension_traits<Graph<Kind,
00466                                 WordValue,
00467                                 WeightValue,
00468                                 SeriesValue,
00469                                 Letter,
00470                                 Tag,
00471                                 Geometry>  >
00472   {
00473       typedef Graph<Kind, WordValue, WeightValue,
00474                     SeriesValue, Letter, Tag, Geometry>         self_t;
00475       typedef typename automaton_traits<self_t>::monoid_elt_value_t
00476       monoid_elt_value_t;
00477       typedef typename algebra::mute_series_impl<SeriesValue, SeriesValue, monoid_elt_value_t>
00478       ::ret series_set_elt_value_t;
00479 
00480       typedef
00481       Graph<Kind,
00482             monoid_elt_value_t,
00483             SeriesValue,
00484             series_set_elt_value_t,
00485             Letter,
00486             Tag,
00487             Geometry>
00488       ret;
00489   };
00490 
00491 }
00492 
00493 
00494 # ifndef VCSN_USE_INTERFACE_ONLY
00495 #  include <vaucanson/automata/implementation/graph.hxx>
00496 # endif // VCSN_USE_INTERFACE_ONLY
00497 
00498 #endif // ! VCSN_AUTOMATA_IMPLEMENTATION_GRAPH_HH

Generated on Fri Jul 28 12:18:34 2006 for Vaucanson by  doxygen 1.4.6