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/misc/static.hh>
00031 # include <vaucanson/misc/usual_macros.hh>
00032 # include <vaucanson/automata/implementation/geometry.hh>
00033 
00034 
00035 namespace vcsn
00036 {
00037 
00038 
00039   typedef htransition_t hedge_t;
00040   namespace delta_kind
00041   {
00042     typedef transitions edges;
00043   }
00044 
00046   template<typename EdgeLabel>
00047   struct edge_value
00048   {
00049       edge_value(hstate_t, hstate_t, const EdgeLabel& v = EdgeLabel());
00050 
00051       operator const EdgeLabel& () const
00052       { return label; }
00053       operator EdgeLabel& ()
00054       { return label; }
00055 
00056       EdgeLabel label;
00057       hstate_t  from;
00058       hstate_t  to;
00059   };
00060 
00062   struct state_value
00063   {
00064       typedef std::set<hedge_t> edges_t;
00065       state_value();
00066 
00067       edges_t output_edges;
00068       edges_t input_edges;
00069   };
00070 
00072   typedef misc::SparseInterval<hstate_t, std::set<hstate_t> >
00073   StateContainer;
00074 
00075   typedef misc::SparseInterval<hedge_t, std::set<hedge_t> >
00076   EdgeContainer;
00077 
00078 
00080   template <class K, class WordValue, class WeightValue,
00081             class SeriesValue, class Letter, class Tag, class Geometry>
00082   class Graph
00083   {
00085     public:
00086       typedef Graph<K, WordValue, WeightValue, SeriesValue,
00087                     Letter, Tag, Geometry>                      self_t;
00088 
00090     public:
00091       typedef typename LabelOf<K, WordValue, WeightValue, SeriesValue, Letter>
00092       ::ret label_t;
00093 
00094       typedef state_value                       state_value_t;
00095       typedef edge_value<label_t>               edge_value_t;
00096 
00097       typedef std::vector<state_value_t>        state_data_t;
00098       typedef std::vector<edge_value_t>         edge_data_t;
00099 
00100       typedef StateContainer            states_t;
00101       typedef EdgeContainer             edges_t;
00102 
00103       typedef SeriesValue                       series_set_elt_value_t;
00104 
00105       typedef std::map<hstate_t, series_set_elt_value_t> initial_t;
00106       typedef std::map<hstate_t, series_set_elt_value_t> final_t;
00107 
00108       typedef misc::Support<initial_t>  initial_support_t;
00109       typedef misc::Support<final_t>    final_support_t;
00110 
00111     public:
00112       // we guarantee that the handlers of state are indexed from 0 to
00113       // initial_number_of_state - 1 when using this constructor.
00114       Graph();
00115       Graph(unsigned initial_number_of_state,
00116             unsigned number_of_edge_initially_allocated);
00117 
00119       states_t                  states() const;
00120 
00122       edges_t                   edges() const;
00123 
00125       initial_support_t         initial() const;
00126       final_support_t           final() const;
00127 
00130     public:
00131       bool                      has_state(hstate_t n) const;
00132 
00133     public:
00134       hstate_t                  add_state();
00135 
00139       void                      del_state(hstate_t n);
00140 
00141     public:
00142       void                      set_initial(hstate_t,
00143                                             const series_set_elt_value_t&,
00144                                             const series_set_elt_value_t&);
00145       const series_set_elt_value_t& get_initial(hstate_t,
00146                                                 const
00147                                                 series_set_elt_value_t&) const;
00148       void                      clear_initial();
00149       void                      set_final(hstate_t,
00150                                           const series_set_elt_value_t&,
00151                                           const series_set_elt_value_t&);
00152       const series_set_elt_value_t& get_final(hstate_t,
00153                                               const
00154                                               series_set_elt_value_t&) const;
00155       void                      clear_final();
00161     public:
00162       bool                      has_edge(hedge_t n) const;
00163 
00164     public:
00165       hedge_t                   add_edge(hstate_t h1, hstate_t h2,
00166                                          const label_t& v);
00167       void                      del_edge(hedge_t e);
00168 
00169     public:
00170       hstate_t                  src_of(hedge_t e1) const;
00171       hstate_t                  dst_of(hedge_t e2) const;
00172 
00173     public:
00174       const label_t&            label_of(hedge_t n) const;
00175       void                      update(hedge_t, label_t);
00180     public:
00181       template <class S>
00182       bool                      exists(const AutomataBase<S>& s) const;
00183 
00184     public:
00185 
00187 # define DECLARE_DELTA_FUNCTION(DeltaName, DKind)                       \
00188       template <class OutputIterator, class Query>                      \
00189       void                      DeltaName(OutputIterator res,           \
00190                                           hstate_t from,                \
00191                                           const Query& q,               \
00192                                           delta_kind::DKind) const
00193       DECLARE_DELTA_FUNCTION (delta, states);
00194       DECLARE_DELTA_FUNCTION (delta, edges);
00195       DECLARE_DELTA_FUNCTION (rdelta, states);
00196       DECLARE_DELTA_FUNCTION (rdelta, edges);
00197 # undef DECLARE_DELTA_FUNCTION
00198 
00199 # define DECLARE_DELTAF_BOOL_FUNCTION(DeltaName, DKind, IsBool)         \
00200       template <class Functor, class Query>                             \
00201       void                      DeltaName(Functor& fun,                 \
00202                                           hstate_t from,                \
00203                                           const Query& q,               \
00204                                           delta_kind::DKind,            \
00205                                           misc::IsBool ## _t) const
00206       DECLARE_DELTAF_BOOL_FUNCTION (deltaf, states, true);
00207       DECLARE_DELTAF_BOOL_FUNCTION (deltaf, states, false);
00208       DECLARE_DELTAF_BOOL_FUNCTION (deltaf, edges, true);
00209       DECLARE_DELTAF_BOOL_FUNCTION (deltaf, edges, false);
00210       DECLARE_DELTAF_BOOL_FUNCTION (rdeltaf, states, true);
00211       DECLARE_DELTAF_BOOL_FUNCTION (rdeltaf, states, false);
00212       DECLARE_DELTAF_BOOL_FUNCTION (rdeltaf, edges, true);
00213       DECLARE_DELTAF_BOOL_FUNCTION (rdeltaf, edges, false);
00214 # undef DECLARE_DELTAF_BOOL_FUNCTION
00215 
00216 # define DECLARE_DELTAF_FUNCTION(DeltaName)                             \
00217       template <class Functor, class Query, typename DKind>             \
00218       void                      DeltaName(Functor& fun,                 \
00219                                           hstate_t from,                \
00220                                           const Query& q,               \
00221                                           delta_kind::kind<DKind>) const
00222       DECLARE_DELTAF_FUNCTION (deltaf);
00223       DECLARE_DELTAF_FUNCTION (rdeltaf);
00224 
00225 # undef DECLARE_DELTAF_FUNCTION
00226 
00229       // FIXME: Not implemented.
00230     public:
00231       self_t&                   clone() const;
00232 
00235     public:
00236       typedef Tag tag_t;
00237       tag_t& tag();
00238       const tag_t& tag() const;
00243     public:
00244       typedef Geometry geometry_t;
00245       geometry_t&               geometry();
00246       const geometry_t& geometry() const;
00249     private:
00250       geometry_t                geometry_;
00251 
00252     public:
00253       state_data_t              states_;
00254       edge_data_t                       edges_;
00255       std::set<hstate_t>                removed_states_;
00256       std::set<hedge_t>         removed_edges_;
00257       tag_t                     tag_;
00258       final_t                   final_;
00259       initial_t                 initial_;
00260   };
00261 
00262 
00263 # define TParam                                                         \
00264   template <class S, class WordValue, class WeightValue, class SeriesValue, \
00265             class Letter, class Tag, class Geometry>
00266 
00267   TParam
00268   ADAPT_ADD_LETTER_TRANSITION_TO_SERIES_LABEL(Graph<labels_are_series,
00269                                               WordValue, WeightValue,
00270                                               SeriesValue, Letter, Tag, Geometry>);
00271 
00272 
00273   TParam
00274   ADAPT_LETTER_OF_TO_SERIES_LABEL(Graph<labels_are_series,
00275                                   WordValue, WeightValue,
00276                                   SeriesValue, Letter, Tag, Geometry>);
00277 
00278   TParam
00279   ADAPT_WORD_OF_TO_SERIES_LABEL(Graph<labels_are_series,
00280                                 WordValue, WeightValue,
00281                                 SeriesValue, Letter, Tag, Geometry>);
00282 
00283   TParam
00284   ADAPT_WORD_OF_TO_LETTERS_LABEL(Graph<labels_are_letters,
00285                                  WordValue, WeightValue,
00286                                  SeriesValue, Letter, Tag, Geometry>);
00287 
00288   TParam
00289   ADAPT_SERIE_OF_TO_LETTERS_LABEL(Graph<labels_are_letters,
00290                                   WordValue, WeightValue,
00291                                   SeriesValue, Letter, Tag, Geometry>);
00292 
00293   TParam
00294   ADAPT_ADD_SERIE_TRANSITION_TO_LETTERS_LABEL(Graph<labels_are_letters,
00295                                               WordValue, WeightValue,
00296                                               SeriesValue, Letter, Tag, Geometry>);
00297 
00298   template <class Kind, class WordValue, class WeightValue, class SerieValue,
00299             class Letter, class Tag, class Geometry, class I>
00300   Tag& op_tag(const AutomataBase<I>&,
00301               Graph<Kind, WordValue, WeightValue,
00302               SerieValue, Letter, Tag, Geometry>&);
00303 
00304   template <class Kind, class WordValue, class WeightValue, class SerieValue,
00305             class Letter, class Tag, class Geometry, class I>
00306   const Tag& op_tag(const AutomataBase<I>&,
00307                     const Graph<Kind, WordValue, WeightValue,
00308                     SerieValue, Letter, Tag, Geometry>&);
00309 
00310   template <class Kind, class WordValue, class WeightValue, class SerieValue,
00311             class Letter, class Tag, class Geometry, class I>
00312   Geometry&
00313   op_geometry(const AutomataBase<I>&,
00314               Graph<Kind, WordValue, WeightValue,
00315               SerieValue, Letter, Tag, Geometry>&);
00316 
00317   template <class Kind, class WordValue, class WeightValue, class SerieValue,
00318             class Letter, class Tag, class Geometry, class I>
00319   const Geometry&
00320   op_geometry(const AutomataBase<I>&,
00321               const Graph<Kind, WordValue, WeightValue,
00322               SerieValue, Letter, Tag, Geometry>&);
00323 
00324 
00325 
00326 # undef TParam
00327 
00328   // This implementation can be used as an implementation of automaton.
00329   template <class Kind,
00330             class WordValue,
00331             class WeightValue,
00332             class SeriesValue,
00333             class Letter,
00334             class Tag,
00335             class Geometry>
00336   struct automaton_traits<Graph<Kind,
00337                                 WordValue,
00338                                 WeightValue,
00339                                 SeriesValue,
00340                                 Letter,
00341                                 Tag,
00342                                 Geometry>  >
00343   {
00344       typedef SeriesValue                                       series_set_elt_value_t;
00345       typedef WordValue                                 word_value_t;
00346       typedef WordValue                                 monoid_elt_value_t;
00347       typedef WeightValue                                       semiring_elt_value_t;
00348       typedef Letter                                    letter_t;
00349       typedef typename LabelOf<Kind, WordValue, WeightValue, SeriesValue, Letter>
00350       ::ret                                             label_t;
00351       typedef Tag                                               tag_t;
00352       typedef edge_value<label_t>                               transition_value_t;
00353       typedef state_value                                       state_value_t;
00354       typedef std::vector<state_value_t>                        state_data_t;
00355       typedef std::vector<transition_value_t>           transition_data_t;
00356 
00357       typedef StateContainer                            states_t;
00358       typedef EdgeContainer                             transitions_t;
00359 
00360       typedef typename states_t::iterator                       state_iterator;
00361       typedef typename transitions_t::iterator          transition_iterator;
00362 
00363       typedef std::map<hstate_t, series_set_elt_value_t>        initial_t;
00364       typedef std::map<hstate_t, series_set_elt_value_t>        final_t;
00365       typedef misc::Support<initial_t>                  initial_support_t;
00366       typedef misc::Support<final_t>                    final_support_t;
00367       typedef typename initial_support_t::iterator      initial_iterator;
00368       typedef typename final_support_t::iterator                final_iterator;
00369 
00370       typedef Geometry                                  geometry_t;
00371   };
00372 
00373   // This implementation can be used as a transducer one.
00374   template <class Kind,
00375             class WordValue,
00376             class WeightValue,
00377             class SeriesValue,
00378             class Letter,
00379             class Tag,
00380             class Geometry>
00381   struct transducer_traits<Graph<Kind,
00382                                  WordValue,
00383                                  WeightValue,
00384                                  SeriesValue,
00385                                  Letter,
00386                                  Tag,
00387                                  Geometry>  >
00388   {
00389       typedef WordValue                 input_monoid_elt_value_t;
00390       typedef typename algebra::series_traits<WeightValue>::monoid_elt_value_t
00391       output_monoid_elt_value_t;
00392       typedef typename algebra::series_traits<WeightValue>::semiring_elt_value_t
00393       output_semiring_elt_value_t;
00394   };
00395 
00396   // Explain how to project type of transducer into input automaton type.
00397   template <class S,
00398             class Kind,
00399             class WordValue,
00400             class WeightValue,
00401             class SeriesValue,
00402             class Letter,
00403             class Tag,
00404             class Geometry>
00405   struct projection_traits<S, Graph<Kind,
00406                                     WordValue,
00407                                     WeightValue,
00408                                     SeriesValue,
00409                                     Letter,
00410                                     Tag,
00411                                     Geometry>  >
00412   {
00413       typedef Graph<Kind, WordValue, WeightValue, SeriesValue,
00414                     Letter, Tag, Geometry>                      self_t;
00415       typedef typename transducer_traits<self_t>::output_semiring_elt_value_t
00416       semiring_elt_value_t;
00417       typedef typename transducer_traits<self_t>::input_monoid_elt_value_t
00418       monoid_elt_value_t;
00419       typedef typename algebra::mute_series_impl<SeriesValue,
00420                                                  semiring_elt_value_t,
00421                                                  monoid_elt_value_t>
00422       ::ret series_set_elt_value_t;
00423 
00424       typedef
00425       Graph<Kind,
00426             monoid_elt_value_t,
00427             semiring_elt_value_t,
00428             series_set_elt_value_t,
00429             Letter,
00430             Tag,
00431             Geometry>
00432       ret;
00433   };
00434 
00435   template <class Kind,
00436             class WordValue,
00437             class WeightValue,
00438             class SeriesValue,
00439             class Letter,
00440             class Tag,
00441             class Geometry>
00442   struct output_projection_traits<Graph<Kind,
00443                                         WordValue,
00444                                         WeightValue,
00445                                         SeriesValue,
00446                                         Letter,
00447                                         Tag, Geometry>  >
00448   {
00449       typedef Graph<Kind, WordValue, WeightValue, SeriesValue,
00450                     Letter, Tag, Geometry>                      self_t;
00451 
00452       typedef typename automaton_traits<self_t>::semiring_elt_value_t
00453       series_set_elt_value_t;
00454 
00455       typedef typename
00456       algebra::series_traits<series_set_elt_value_t>::monoid_elt_value_t
00457       monoid_elt_value_t;
00458 
00459       typedef typename
00460       algebra::series_traits<series_set_elt_value_t>::semiring_elt_value_t
00461       semiring_elt_value_t;
00462 
00463       typedef
00464       Graph<Kind,
00465             monoid_elt_value_t,
00466             semiring_elt_value_t,
00467             series_set_elt_value_t,
00468             Letter,
00469             Tag,
00470             Geometry>
00471       ret;
00472   };
00473 
00474   // Explain how to extend an input automaton into a transducer.
00475   template <class Kind,
00476             class WordValue,
00477             class WeightValue,
00478             class SeriesValue,
00479             class Letter,
00480             class Tag,
00481             class Geometry>
00482   struct extension_traits<Graph<Kind,
00483                                 WordValue,
00484                                 WeightValue,
00485                                 SeriesValue,
00486                                 Letter,
00487                                 Tag,
00488                                 Geometry>  >
00489   {
00490       typedef Graph<Kind, WordValue, WeightValue,
00491                     SeriesValue, Letter, Tag, Geometry>         self_t;
00492       typedef typename automaton_traits<self_t>::monoid_elt_value_t
00493       monoid_elt_value_t;
00494       typedef typename algebra::mute_series_impl<SeriesValue, SeriesValue, monoid_elt_value_t>
00495       ::ret series_set_elt_value_t;
00496 
00497       typedef
00498       Graph<Kind,
00499             monoid_elt_value_t,
00500             SeriesValue,
00501             series_set_elt_value_t,
00502             Letter,
00503             Tag,
00504             Geometry>
00505       ret;
00506   };
00507 
00508 }
00509 
00510 
00511 # ifndef VCSN_USE_INTERFACE_ONLY
00512 #  include <vaucanson/automata/implementation/graph.hxx>
00513 # endif // VCSN_USE_INTERFACE_ONLY
00514 
00515 #endif // ! VCSN_AUTOMATA_IMPLEMENTATION_GRAPH_HH

Generated on Sat Jul 29 17:13:00 2006 for Vaucanson by  doxygen 1.4.6