Vaucanson 1.4
alphabet_set.hxx
00001 // alphabet_set.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 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_ALGEBRA_IMPLEMENTATION_ALPHABETS_ALPHABET_SET_HXX
00018 # define VCSN_ALGEBRA_IMPLEMENTATION_ALPHABETS_ALPHABET_SET_HXX
00019 
00020 # include <limits>
00021 
00022 # include <vaucanson/algebra/concept/letter.hh>
00023 # include <vaucanson/algebra/implementation/alphabets/alphabet_set.hh>
00024 
00025 namespace vcsn
00026 {
00027   namespace algebra
00028   {
00029     /*----------------------------------------.
00030     | Projections for types that support them |
00031     `----------------------------------------*/
00032 
00033 # define ALPHABET_TRAITS \
00034     alphabet_traits<AlphabetSet<L>, std::set<L> >
00035 
00036     template <typename L>
00037     inline typename ALPHABET_TRAITS::first_projection_t
00038     ALPHABET_TRAITS::first_projection(const ALPHABET_TRAITS::alphabet_t& A)
00039     {
00040       // We can not project if the type does not support it.
00041       static_assertion_(not (misc::static_eq<first_projection_t,
00042                              undefined_type>::value), need_first_projection);
00043 
00044       first_projection_t R;
00045 
00046       for_all_const_(alphabet_t, i, A)
00047       {
00048         // We assume we can access the first projection with "first".
00049         R.insert((*i).first);
00050       }
00051 
00052       return R;
00053     }
00054 
00055     template <typename L>
00056     inline typename ALPHABET_TRAITS::second_projection_t
00057     ALPHABET_TRAITS::second_projection(const ALPHABET_TRAITS::alphabet_t& A)
00058     {
00059       // We can not project if the type does not support it.
00060       static_assertion_(not (misc::static_eq<second_projection_t,
00061                              undefined_type>::value), need_second_projection);
00062 
00063       second_projection_t R;
00064 
00065       for_all_const_(alphabet_t, i, A)
00066       {
00067         // We assume we can access the second projection with "second".
00068         R.insert((*i).second);
00069       }
00070 
00071       return R;
00072     }
00073 
00074 # undef ALPHABET_TRAITS
00075 
00076     /*-----------------------------------------------------------.
00077     | Definition of an alphabet implementation based on std::set |
00078     `-----------------------------------------------------------*/
00079 
00080     template <typename L>
00081     size_t
00082     op_max_size(const algebra::AlphabetSet<L>&, const std::set<L>&)
00083     {
00084       return algebra::letter_traits<L>::cardinal;
00085     }
00086 
00087     template<typename L>
00088     bool op_contains(const algebra::AlphabetSet<L>&, const std::set<L>&)
00089     {
00090       return true;
00091     }
00092 
00093     template<typename L>
00094     bool op_is_finite(const algebra::AlphabetSet<L>&, const std::set<L>&)
00095     {
00096       return true;
00097     }
00098 
00099     template<typename L>
00100     bool op_contains_e(const algebra::AlphabetSet<L>&, const std::set<L>& a,
00101                        const L& v)
00102     {
00103       return a.find(v) != a.end();
00104     }
00105 
00106     template <typename L>
00107     std::pair<bool, L>
00108     op_parse(const AlphabetSet<L>& s,
00109              const std::set<L>& v,
00110              const std::string& in,
00111              size_t& pos)
00112     {
00113       // Type helpers.
00114       typedef std::string::const_iterator iter_t;
00115 
00116       // Return values.
00117       bool ret = false;
00118       L ret_letter;
00119       size_t ret_pos = pos;
00120 
00121       // Temporaries.
00122       size_t current_pos = pos;
00123       std::string current_letter_rep = "";
00124 
00125       for (iter_t i = in.begin() + pos; i != in.end(); ++i)
00126       {
00127         std::pair<bool, L> tmp;
00128         current_letter_rep += *i;
00129         ++current_pos;
00130         tmp = letter_traits<L>::literal_to_letter(current_letter_rep);
00131         if (tmp.first && op_contains_e(s, v, tmp.second))
00132         {
00133           ret = true;
00134           ret_letter = tmp.second;
00135           ret_pos = current_pos;
00136         }
00137       }
00138 
00139       pos = ret_pos;
00140       return std::make_pair(ret, ret_letter);
00141     }
00142 
00143   } // ! algebra
00144 
00145 } // ! vcsn
00146 
00147 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_ALPHABETS_ALPHABET_SET_HXX