Vaucanson 1.4
alphabets_base.hxx
00001 // alphabets_base.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, 2006, 2007, 2008 The
00006 // Vaucanson Group.
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License
00010 // as published by the Free Software Foundation; either version 2
00011 // of the License, or (at your option) any later version.
00012 //
00013 // The complete GNU General Public Licence Notice can be found as the
00014 // `COPYING' file in the root directory.
00015 //
00016 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00017 //
00018 #ifndef VCSN_ALGEBRA_CONCEPT_ALPHABETS_BASE_HXX
00019 # define VCSN_ALGEBRA_CONCEPT_ALPHABETS_BASE_HXX
00020 
00021 # include <vaucanson/algebra/concept/alphabets_base.hh>
00022 # include <vaucanson/misc/random.hh>
00023 # include <vaucanson/misc/contract.hh>
00024 # include <cstddef>
00025 
00026 namespace vcsn
00027 {
00028   namespace algebra
00029   {
00030     /*-------------------.
00031     | AlphabetSetBase<S> |
00032     `-------------------*/
00033 
00034     template<typename S>
00035     AlphabetSetBase<S>::AlphabetSetBase()
00036     {}
00037 
00038     template<typename S>
00039     AlphabetSetBase<S>::AlphabetSetBase(const AlphabetSetBase& other)
00040     {}
00041 
00042   } // algebra
00043 
00044   /*-----------------------------------.
00045   | MetaElement<AlphabetSetBase<S>, T> |
00046   `-----------------------------------*/
00047   // Meta-information about element formed from an AlphabetSetBase
00048   // structuring element.
00049 
00050   template<typename S, typename T>
00051   size_t
00052   MetaElement<algebra::AlphabetSetBase<S>, T>::size() const
00053   {
00054     return op_size(this->structure(), this->value());
00055   }
00056 
00057   template<typename S, typename T>
00058   size_t
00059   MetaElement<algebra::AlphabetSetBase<S>, T>::max_size() const
00060   {
00061     return op_max_size(this->structure(), this->value());
00062   }
00063 
00064   template<typename S, typename T>
00065   bool
00066   MetaElement<algebra::AlphabetSetBase<S>, T>::contains(const letter_t& l) const
00067   {
00068     return op_contains_e(this->structure(), this->value(), l);
00069   }
00070 
00071   template<typename S, typename T>
00072   bool
00073   MetaElement<algebra::AlphabetSetBase<S>, T>::is_finite() const
00074   {
00075     return op_is_finite(this->structure(), this->value());
00076   }
00077 
00078   template<typename S, typename T>
00079   typename MetaElement<algebra::AlphabetSetBase<S>, T>::iterator
00080   MetaElement<algebra::AlphabetSetBase<S>, T>::begin()
00081   {
00082     return op_begin(this->structure(), this->value());
00083   }
00084 
00085   template<typename S, typename T>
00086   typename MetaElement<algebra::AlphabetSetBase<S>, T>::const_iterator
00087   MetaElement<algebra::AlphabetSetBase<S>, T>::begin() const
00088   {
00089     return op_begin_const(this->structure(), this->value());
00090   }
00091 
00092   template<typename S, typename T>
00093   typename MetaElement<algebra::AlphabetSetBase<S>, T>::iterator
00094   MetaElement<algebra::AlphabetSetBase<S>, T>::end()
00095   {
00096     return op_end(this->structure(), this->value());
00097   }
00098 
00099   template<typename S, typename T>
00100   typename MetaElement<algebra::AlphabetSetBase<S>, T>::const_iterator
00101   MetaElement<algebra::AlphabetSetBase<S>, T>::end() const
00102   {
00103     return op_end_const(this->structure(), this->value());
00104   }
00105 
00106   template<typename S, typename T>
00107   void
00108   MetaElement<algebra::AlphabetSetBase<S>, T>::insert(const letter_t& l)
00109   {
00110     op_insert(this->structure(), this->value(), l);
00111   }
00112 
00113   template<typename S, typename T>
00114   void
00115   MetaElement<algebra::AlphabetSetBase<S>, T>::insert(const std::string& lit)
00116   {
00117     op_insert(this->structure(), this->value(), algebra::letter_traits<letter_t>::literal_to_letter(lit).second);
00118   }
00119 
00120   template<typename S, typename T>
00121   bool
00122   MetaElement<algebra::AlphabetSetBase<S>, T>::letter_equality(letter_t lhs,
00123                                                                letter_t rhs) const
00124   {
00125     return op_letter_equality(this->structure(), this->value(), lhs, rhs);
00126   }
00127 
00128   template<typename S, typename T>
00129   typename algebra::alphabet_traits<S, T>::letter_t
00130   MetaElement<algebra::AlphabetSetBase<S>, T>::choose() const
00131   {
00132     // FIXME: recommendation(overload this operator)
00133     precondition (is_finite());
00134     precondition (size() > 0);
00135 
00136     int  nr = misc::random::generate<int>(0, size() - 1);
00137 
00138     const_iterator it = begin();
00139     for (int k = 0; k < nr; ++k)
00140       ++it;
00141 
00142     return *it;
00143   }
00144 
00145   template <class S, typename T>
00146   typename algebra::alphabet_traits<S, T>::letter_t
00147   MetaElement<algebra::AlphabetSetBase<S>, T>::random_letter() const
00148   {
00149     return misc::random::generate
00150       <typename algebra::alphabet_traits<S, T>::letter_t>();
00151   }
00152 
00153   template<typename S, typename T>
00154   MetaElement<algebra::AlphabetSetBase<S>, T>::MetaElement()
00155   {}
00156 
00157   template<typename S, typename T>
00158   MetaElement<algebra::AlphabetSetBase<S>, T>::MetaElement(const MetaElement& other) :
00159     MetaElement<Structure<S>, T>(other)
00160   {}
00161 
00162   namespace algebra
00163   {
00164 
00165     template <typename S, typename L>
00166     L op_parse(const AlphabetSetBase<S>&,
00167                const std::string&,
00168                size_t&)
00169     {
00170       static_error(no_op_parse_operator_available);
00171       return 0;
00172     }
00173 
00174     template <typename S, typename T, typename L>
00175     bool op_letter_equality(const algebra::AlphabetSetBase<S>& s,
00176                             const T& a,
00177                             L lhs,
00178                             L rhs)
00179     {
00180       return lhs == rhs;
00181     }
00182 
00183     template<typename S, typename St, typename T>
00184     St& op_rout(const algebra::AlphabetSetBase<S>& s, St& st, const T& a)
00185     {
00186       st << "{ ";
00187 
00188       if (op_is_finite(s.self(), a))
00189         for (typename op_begin_traits<S, T>::const_ret_t i = op_begin_const(s.self(), a);
00190              i != op_end_const(s.self(), a);
00191              ++i)
00192           {
00193             if (i != op_begin_const(s.self(), a))
00194               st << ", ";
00195             st << algebra::letter_traits<typename S::letter_t>::letter_to_literal(*i);
00196           }
00197       else
00198         st << "<many letters>";
00199 
00200       st << " }";
00201 
00202       return st;
00203     }
00204 
00205   } // ! algebra
00206 
00207   template <typename S, typename T>
00208   std::pair<bool, typename Element<S, T>::letter_t>
00209   parse_letter(const Element<S, T>& alphabet,
00210                const std::string& s)
00211   {
00212     typedef typename Element<S, T>::letter_t letter_t;
00213 
00214     size_t tmp = 0;
00215     std::pair<bool, letter_t> res =
00216         op_parse(alphabet.structure(), alphabet.value(), s, tmp);
00217 
00218     if (tmp != s.size())
00219       res.first = false;
00220 
00221     return res;
00222   }
00223 
00224 } // ! vcsn
00225 
00226 #endif // ! VCSN_ALGEBRA_CONCEPT_ALPHABETS_BASE_HXX