listg_graph_impl.hh

00001 // listg_graph_impl.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, 2007, 2008 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_LISTG_GRAPH_IMPL_HH
00018 # define VCSN_AUTOMATA_IMPLEMENTATION_LISTG_GRAPH_IMPL_HH
00019 
00020 # include <set>
00021 # include <map>
00022 # include <vector>
00023 
00024 # include <vaucanson/misc/static.hh>
00025 # include <vaucanson/misc/usual_macros.hh>
00026 
00027 # include <vaucanson/misc/support.hh>
00028 # include <vaucanson/algebra/implementation/series/rat/exp.hh>
00029 # include <vaucanson/automata/concept/automata_base.hh>
00030 # include <vaucanson/automata/concept/automata.hh>
00031 # include <vaucanson/automata/concept/transducer_base.hh>
00032 # include <vaucanson/automata/concept/automata_kind.hh>
00033 # include <vaucanson/automata/concept/tags.hh>
00034 # include <vaucanson/automata/implementation/kind_adapter.hh>
00035 # include <vaucanson/automata/implementation/geometry.hh>
00036 # include <vaucanson/automata/implementation/listg/iterator.hh>
00037 # include <vaucanson/automata/implementation/listg/listg_handlers.hh>
00038 # include <vaucanson/automata/implementation/listg/listg_sparse_interval.hh>
00039 
00040 namespace vcsn
00041 {
00042   namespace listg
00043   {
00045     template <class K, class WordValue, class WeightValue,
00046               class SeriesValue, class Letter, class Tag, class GeometryCoords>
00047     class Graph
00048     {
00050       public:
00051         typedef Graph<K, WordValue, WeightValue, SeriesValue,
00052                       Letter, Tag, GeometryCoords>      self_t;
00053 
00054         typedef WeightValue                             semiring_elt_value_t;
00055         typedef WordValue                               monoid_elt_value_t;
00056         typedef WordValue                               word_value_t;
00057         typedef SeriesValue                             series_set_elt_value_t;
00058         typedef Letter                                  letter_t;
00059         typedef Tag                                     tag_t;
00060 
00062         typedef typename LabelOf<K, WordValue, WeightValue, SeriesValue, Letter>
00063           ::ret                                         label_t;
00064         typedef unsigned                                hstate_value_t;
00065         typedef unsigned                                hedge_value_t;
00066         typedef handler<state_h, hstate_value_t>        hstate_t;
00067         typedef handler<transition_h, hedge_value_t>    htransition_t;
00068         typedef htransition_t                           hedge_t;
00069 
00071         template<typename EdgeLabel>
00072         struct edge_value
00073         {
00074           inline edge_value(const hstate_t& h1, const hstate_t& h2,
00075               const EdgeLabel& l = EdgeLabel())
00076             : label(l),
00077             from(h1),
00078             to(h2)
00079           {}
00080 
00081           inline operator const EdgeLabel& () const
00082           { return label; }
00083 
00084           inline operator EdgeLabel& ()
00085           { return label; }
00086 
00087           EdgeLabel     label;
00088           hstate_t      from;
00089           hstate_t      to;
00090         };
00091 
00093         struct state_value
00094         {
00095           typedef std::set<hedge_t> edges_t;
00096           inline state_value() {}
00097 
00098           edges_t output_edges;
00099           edges_t input_edges;
00100         };
00101 
00104         typedef misc::SparseInterval<hstate_t, std::set<hstate_t> >
00105                                                         StateContainer;
00106         typedef misc::SparseInterval<hedge_t, std::set<hedge_t> >
00107                                                         EdgeContainer;
00108 
00109         typedef state_value                             state_value_t;
00110         typedef edge_value<label_t>                     edge_value_t;
00111 
00112         typedef std::vector<state_value_t>              state_data_t;
00113         typedef std::vector<edge_value_t>               edge_data_t;
00114 
00115         typedef StateContainer                          states_t;
00116         typedef EdgeContainer                           edges_t;
00117 
00118         typedef std::map<hstate_t, series_set_elt_value_t>
00119                                                         initial_t;
00120         typedef std::map<hstate_t, series_set_elt_value_t>
00121                                                         final_t;
00122 
00123         typedef misc::Support<initial_t>                initial_support_t;
00124         typedef misc::Support<final_t>                  final_support_t;
00125 
00126         typedef ::vcsn::listg::DeltaConstIterator<self_t, hstate_t, ::vcsn::listg::forward_iterator>
00127                                                           delta_state_iterator;
00128         typedef ::vcsn::listg::DeltaConstIterator<self_t, htransition_t, ::vcsn::listg::forward_iterator>
00129                                                           delta_transition_iterator;
00130 
00131         typedef ::vcsn::listg::DeltaConstIterator<self_t, hstate_t, ::vcsn::listg::backward_iterator>
00132                                                           rdelta_state_iterator;
00133         typedef ::vcsn::listg::DeltaConstIterator<self_t, htransition_t, ::vcsn::listg::backward_iterator>
00134                                                           rdelta_transition_iterator;
00135         // we guarantee that the handlers of state are indexed from 0 to
00136         // initial_number_of_state - 1 when using this constructor.
00137         Graph();
00138         Graph(unsigned initial_number_of_state,
00139               unsigned number_of_edge_initially_allocated);
00140 
00142         states_t            states() const;
00143 
00145         edges_t             edges() const;
00146 
00148         initial_support_t   initial() const;
00149         final_support_t     final() const;
00150 
00153         hstate_t            get_state(int n) const;
00154         bool                has_state(const hstate_t& n) const;
00155         hstate_t            add_state();
00156 
00160         void                del_state(const hstate_t& n);
00161 
00177         void                set_initial(const hstate_t& s,
00178                                         const series_set_elt_value_t& v,
00179                                         const series_set_elt_value_t& z);
00180         const series_set_elt_value_t&
00181                             get_initial(const hstate_t&,
00182                                         const series_set_elt_value_t&) const;
00183         bool                is_initial(const hstate_t& s,
00184                                        const series_set_elt_value_t&) const;
00185 
00186         void                clear_initial();
00187 
00194         void                set_final(const hstate_t&,
00195                                       const series_set_elt_value_t&,
00196                                       const series_set_elt_value_t&);
00197         const series_set_elt_value_t&
00198                             get_final(const hstate_t&,
00199                                       const series_set_elt_value_t&) const;
00200         bool                is_final(const hstate_t& s,
00201                                      const series_set_elt_value_t&) const;
00202 
00203         void                clear_final();
00209         bool                has_edge(const hedge_t& n) const;
00210 
00211         hedge_t             add_edge(const hstate_t& h1,
00212                                      const hstate_t& h2,
00213                                      const label_t& v);
00214         void                del_edge(const hedge_t& e);
00215 
00216         hstate_t            src_of(const hedge_t& e1) const;
00217         hstate_t            dst_of(const hedge_t& e2) const;
00218 
00219         const label_t&      label_of(const hedge_t& n) const;
00220         void                update(const hedge_t&, label_t);
00225         template <class S>
00226         bool                exists(const AutomataBase<S>& s) const;
00227 
00229 # define DECLARE_DELTA_FUNCTION(DeltaName, DKind)                       \
00230         template <class OutputIterator, typename Query>                 \
00231         void DeltaName(OutputIterator res, const hstate_t& from,        \
00232                        const Query& q, ::vcsn::delta_kind::DKind) const
00233         DECLARE_DELTA_FUNCTION (delta, states);
00234         DECLARE_DELTA_FUNCTION (delta, transitions);
00235         DECLARE_DELTA_FUNCTION (rdelta, states);
00236         DECLARE_DELTA_FUNCTION (rdelta, transitions);
00237 # undef DECLARE_DELTA_FUNCTION
00238 
00239 # define DECLARE_DELTAF_BOOL_FUNCTION(DeltaName, DKind, IsBool)         \
00240         template <class Functor, typename Query>                        \
00241         void DeltaName(Functor& fun, const hstate_t& from,              \
00242                        const Query& q, ::vcsn::delta_kind::DKind,       \
00243                        misc::IsBool ## _t) const
00244         DECLARE_DELTAF_BOOL_FUNCTION (deltaf, states, true);
00245         DECLARE_DELTAF_BOOL_FUNCTION (deltaf, states, false);
00246         DECLARE_DELTAF_BOOL_FUNCTION (deltaf, transitions, true);
00247         DECLARE_DELTAF_BOOL_FUNCTION (deltaf, transitions, false);
00248         DECLARE_DELTAF_BOOL_FUNCTION (rdeltaf, states, true);
00249         DECLARE_DELTAF_BOOL_FUNCTION (rdeltaf, states, false);
00250         DECLARE_DELTAF_BOOL_FUNCTION (rdeltaf, transitions, true);
00251         DECLARE_DELTAF_BOOL_FUNCTION (rdeltaf, transitions, false);
00252 # undef DECLARE_DELTAF_BOOL_FUNCTION
00253 
00254 # define DECLARE_DELTAF_FUNCTION(DeltaName)                             \
00255         template <class Functor, typename Query, typename DKind>        \
00256         void DeltaName(Functor& fun, const hstate_t& from,              \
00257                        const Query& q, ::vcsn::delta_kind::kind<DKind>) const
00258         DECLARE_DELTAF_FUNCTION (deltaf);
00259         DECLARE_DELTAF_FUNCTION (rdeltaf);
00260 
00261 # undef DECLARE_DELTAF_FUNCTION
00262 
00265 
00266         self_t&             clone() const;
00267 
00270         tag_t&              tag();
00271         const tag_t&        tag() const;
00276         typedef vcsn::geometry<hstate_t, hedge_t, GeometryCoords>
00277                             geometry_t;
00278         geometry_t&         geometry();
00279         const geometry_t&   geometry() const;
00282         geometry_t          geometry_;
00283 
00284       public:
00285         state_data_t        states_;
00286         edge_data_t         edges_;
00287         std::set<hstate_t>  removed_states_;
00288         std::set<hedge_t>   removed_edges_;
00289         tag_t               tag_;
00290         final_t             final_;
00291         initial_t           initial_;
00292     };
00293 
00294 
00295 # define TParam                                                         \
00296     template <class S, class WordValue, class WeightValue, class SeriesValue, \
00297               class Letter, class Tag, class GeometryCoords>
00298 # define GRAPH \
00299     Graph<Kind, WordValue, WeightValue, SerieValue, \
00300           Letter, Tag, GeometryCoords>
00301 
00302 
00303 
00304     TParam
00305     ADAPT_ADD_LETTER_TRANSITION_TO_SERIES_LABEL(Graph<labels_are_series,
00306                                                 WordValue, WeightValue,
00307                                                 SeriesValue, Letter, Tag,
00308                                                 GeometryCoords>);
00309 
00310     TParam
00311     ADAPT_LETTER_OF_TO_SERIES_LABEL(Graph<labels_are_series,
00312                                     WordValue, WeightValue,
00313                                     SeriesValue, Letter, Tag,
00314                                     GeometryCoords>);
00315 
00316     TParam
00317     ADAPT_WORD_OF_TO_SERIES_LABEL(Graph<labels_are_series,
00318                                   WordValue, WeightValue,
00319                                   SeriesValue, Letter, Tag,
00320                                   GeometryCoords>);
00321 
00322     TParam
00323     ADAPT_WORD_OF_TO_LETTERS_LABEL(Graph<labels_are_letters,
00324                                    WordValue, WeightValue,
00325                                    SeriesValue, Letter, Tag,
00326                                    GeometryCoords>);
00327 
00328     TParam
00329     ADAPT_SERIE_OF_TO_LETTERS_LABEL(Graph<labels_are_letters,
00330                                     WordValue, WeightValue,
00331                                     SeriesValue, Letter, Tag,
00332                                     GeometryCoords>);
00333 
00334     TParam
00335     ADAPT_ADD_SERIE_TRANSITION_TO_LETTERS_LABEL(Graph<labels_are_letters,
00336                                                 WordValue, WeightValue,
00337                                                 SeriesValue, Letter, Tag,
00338                                                 GeometryCoords>);
00339 
00340     template <class Kind, class WordValue, class WeightValue, class SerieValue,
00341               class Letter, class Tag, class GeometryCoords, class I>
00342     Tag&                    op_tag(const AutomataBase<I>&,
00343                                    Graph<Kind, WordValue, WeightValue,
00344                                    SerieValue, Letter, Tag,
00345                                    GeometryCoords>&);
00346 
00347     template <class Kind, class WordValue, class WeightValue, class SerieValue,
00348               class Letter, class Tag, class GeometryCoords, class I>
00349     const Tag&              op_tag(const AutomataBase<I>&,
00350                                    const Graph<Kind, WordValue, WeightValue,
00351                                    SerieValue, Letter, Tag, GeometryCoords>&);
00352 
00353     template <class Kind, class WordValue, class WeightValue, class SerieValue,
00354               class Letter, class Tag, class GeometryCoords, class I>
00355     typename GRAPH::geometry_t&
00356                             op_geometry(const AutomataBase<I>&,
00357                                         Graph<Kind, WordValue, WeightValue,
00358                                         SerieValue, Letter, Tag, GeometryCoords>&);
00359 
00360     template <class Kind, class WordValue, class WeightValue, class SerieValue,
00361               class Letter, class Tag, class GeometryCoords, class I>
00362     const typename GRAPH::geometry_t&
00363                             op_geometry(const AutomataBase<I>&,
00364                                         const Graph<Kind, WordValue,
00365                                         WeightValue, SerieValue, Letter, Tag,
00366                                         GeometryCoords>&);
00367 # undef TParam
00368 # undef GRAPH
00369 
00370   } // End of namespace listg
00371 
00372 
00373   // This implementation can be used as an implementation of automaton.
00374   VCSN_MAKE_AUTOMATON_TRAITS(listg::Graph);
00375   VCSN_MAKE_GENERALIZED_AUTOMATON_TRAITS(listg::Graph);
00376   VCSN_MAKE_MUTE_GRAPH_IMPL_TRAITS(listg::Graph);
00377   VCSN_MAKE_MUTE_GRAPH_IMPL_MONOID_TRAITS(listg::Graph);
00378 
00379   // This implementation can be used as a transducer one.
00380   template <class Kind,
00381             class WordValue,
00382             class WeightValue,
00383             class SeriesValue,
00384             class Letter,
00385             class Tag,
00386             class GeometryCoords>
00387   struct transducer_traits<listg::Graph<Kind,
00388                                          WordValue,
00389                                          WeightValue,
00390                                          SeriesValue,
00391                                          Letter,
00392                                          Tag,
00393                                          GeometryCoords> >
00394   {
00395     typedef WordValue         input_monoid_elt_value_t;
00396     typedef typename algebra::series_traits<WeightValue>::monoid_elt_value_t
00397                               output_monoid_elt_value_t;
00398     typedef typename algebra::series_traits<WeightValue>::semiring_elt_value_t
00399                               output_semiring_elt_value_t;
00400   };
00401 
00402   // Explain how to project type of transducer into input automaton type.
00403   template <class Kind,
00404             class WordValue,
00405             class WeightValue,
00406             class SeriesValue,
00407             class Letter,
00408             class Tag,
00409             class GeometryCoords>
00410   struct input_projection_traits<listg::Graph<Kind,
00411                                               WordValue,
00412                                               WeightValue,
00413                                               SeriesValue,
00414                                               Letter,
00415                                               Tag,
00416                                               GeometryCoords> >
00417   {
00418     typedef listg::Graph<Kind, WordValue, WeightValue, SeriesValue,
00419                           Letter, Tag, GeometryCoords>
00420                               self_t;
00421     typedef typename transducer_traits<self_t>::output_semiring_elt_value_t
00422                               semiring_elt_value_t;
00423     typedef typename transducer_traits<self_t>::input_monoid_elt_value_t
00424                               monoid_elt_value_t;
00425     typedef typename algebra::mute_series_impl<SeriesValue,
00426                                                semiring_elt_value_t,
00427                                                monoid_elt_value_t>::ret
00428                               series_set_elt_value_t;
00429 
00430     typedef
00431     listg::Graph<Kind,
00432                   monoid_elt_value_t,
00433                   semiring_elt_value_t,
00434                   series_set_elt_value_t,
00435                   Letter,
00436                   Tag,
00437                   GeometryCoords>
00438                               ret;
00439   };
00440 
00441   // Input projection for FMP transducers.
00442   template <class Kind,
00443             class WordValue,
00444             class WeightValue,
00445             class SeriesValue,
00446             class Letter,
00447             class Tag,
00448             class GeometryCoords>
00449   struct fmp_input_projection_traits<listg::Graph<Kind,
00450                                                   WordValue,
00451                                                   WeightValue,
00452                                                   SeriesValue,
00453                                                   Letter,
00454                                                   Tag,
00455                                                   GeometryCoords> >
00456   {
00457     typedef listg::Graph<Kind, WordValue, WeightValue, SeriesValue,
00458                          Letter, Tag, GeometryCoords>
00459                                   self_t;
00460 
00461     typedef typename automaton_traits<self_t>::semiring_elt_value_t
00462                                   semiring_elt_value_t;
00463 
00464     typedef typename WordValue::first_type monoid_elt_value_t;
00465     typedef typename monoid_elt_value_t::value_type letter_t;
00466 
00467     typedef typename algebra::mute_series_impl<SeriesValue,
00468                                                semiring_elt_value_t,
00469                                                monoid_elt_value_t>::ret
00470                                   series_set_elt_value_t;
00471 
00472     typedef
00473     listg::Graph<Kind,
00474           monoid_elt_value_t,
00475           semiring_elt_value_t,
00476           series_set_elt_value_t,
00477           letter_t,
00478           Tag,
00479           GeometryCoords>
00480                                   ret;
00481   };
00482 
00483   template <class Kind,
00484             class WordValue,
00485             class WeightValue,
00486             class SeriesValue,
00487             class Letter,
00488             class Tag,
00489             class GeometryCoords>
00490   struct output_projection_traits<listg::Graph<Kind,
00491                                                 WordValue,
00492                                                 WeightValue,
00493                                                 SeriesValue,
00494                                                 Letter,
00495                                                 Tag, GeometryCoords> >
00496   {
00497     typedef listg::Graph<Kind, WordValue, WeightValue, SeriesValue,
00498                           Letter, Tag, GeometryCoords>
00499                               self_t;
00500 
00501     typedef typename automaton_traits<self_t>::semiring_elt_value_t
00502                               series_set_elt_value_t;
00503 
00504     typedef typename
00505     algebra::series_traits<series_set_elt_value_t>::monoid_elt_value_t
00506                               monoid_elt_value_t;
00507 
00508     typedef typename
00509     algebra::series_traits<series_set_elt_value_t>::semiring_elt_value_t
00510                               semiring_elt_value_t;
00511 
00512     typedef
00513     listg::Graph<Kind,
00514                   monoid_elt_value_t,
00515                   semiring_elt_value_t,
00516                   series_set_elt_value_t,
00517                   Letter,
00518                   Tag,
00519                   GeometryCoords>
00520                               ret;
00521   };
00522 
00523   // Output projection for FMP transducers.
00524   template <class Kind,
00525             class WordValue,
00526             class WeightValue,
00527             class SeriesValue,
00528             class Letter,
00529             class Tag,
00530             class GeometryCoords>
00531   struct fmp_output_projection_traits<listg::Graph<Kind,
00532                                         WordValue,
00533                                         WeightValue,
00534                                         SeriesValue,
00535                                         Letter,
00536                                         Tag,
00537                                         GeometryCoords> >
00538   {
00539     typedef listg::Graph<Kind, WordValue, WeightValue, SeriesValue,
00540                          Letter, Tag, GeometryCoords>
00541                                   self_t;
00542 
00543     typedef typename WordValue::second_type monoid_elt_value_t;
00544     typedef typename monoid_elt_value_t::value_type letter_t;
00545 
00546     typedef typename automaton_traits<self_t>::semiring_elt_value_t
00547                                   semiring_elt_value_t;
00548 
00549     typedef typename algebra::mute_series_impl<SeriesValue,
00550             semiring_elt_value_t,
00551             monoid_elt_value_t>::ret series_set_elt_value_t;
00552 
00553     typedef
00554     listg::Graph<Kind,
00555           monoid_elt_value_t,
00556           semiring_elt_value_t,
00557           series_set_elt_value_t,
00558           letter_t,
00559           Tag,
00560           GeometryCoords>
00561                                   ret;
00562   };
00563 
00564   // Explain how to extend an input automaton into a transducer.
00565   template <class Kind,
00566             class WordValue,
00567             class WeightValue,
00568             class SeriesValue,
00569             class Letter,
00570             class Tag,
00571             class GeometryCoords>
00572   struct extension_traits<listg::Graph<Kind,
00573                                         WordValue,
00574                                         WeightValue,
00575                                         SeriesValue,
00576                                         Letter,
00577                                         Tag,
00578                                         GeometryCoords> >
00579   {
00580     typedef listg::Graph<Kind, WordValue, WeightValue,
00581                           SeriesValue, Letter, Tag, GeometryCoords>
00582                               self_t;
00583     typedef typename automaton_traits<self_t>::monoid_elt_value_t
00584                               monoid_elt_value_t;
00585     typedef typename algebra::mute_series_impl<SeriesValue,
00586                                                SeriesValue,
00587                                                monoid_elt_value_t>::ret
00588                               series_set_elt_value_t;
00589 
00590     typedef
00591     listg::Graph<Kind,
00592                   monoid_elt_value_t,
00593                   SeriesValue,
00594                   series_set_elt_value_t,
00595                   Letter,
00596                   Tag,
00597                   GeometryCoords>
00598                               ret;
00599   };
00600 } // Enf of namespace vcsn
00601 
00602 
00603 # if !defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB
00604 #  include <vaucanson/automata/implementation/listg_graph_impl.hxx>
00605 # endif // VCSN_USE_INTERFACE_ONLY
00606 
00607 #endif // ! VCSN_AUTOMATA_IMPLEMENTATION_LISTG_GRAPH_IMPL_HH

Generated on Thu Oct 9 20:22:39 2008 for Vaucanson by  doxygen 1.5.1