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));
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     template <typename S, typename T, typename L>
00165     bool op_letter_equality(const algebra::AlphabetSetBase<S>& s,
00166                             const T& a,
00167                             L lhs,
00168                             L rhs)
00169     {
00170       return lhs == rhs;
00171     }
00172 
00173     template<typename S, typename St, typename T>
00174     St& op_rout(const algebra::AlphabetSetBase<S>& s, St& st, const T& a)
00175     {
00176       st << "{ ";
00177       if (op_is_finite(s.self(), a))
00178         for (typename op_begin_traits<S, T>::const_ret_t i = op_begin_const(s.self(), a);
00179              i != op_end_const(s.self(), a);
00180              ++i)
00181           {
00182             if (i != op_begin_const(s.self(), a))
00183               st << ", ";
00184             st << *i;
00185           }
00186       else
00187         st << "<many letters>";
00188       st << " }";
00189       return st;
00190     }
00191 
00192   } // algebra
00193 
00194 } // vcsn
00195 
00196 #endif // ! VCSN_ALGEBRA_CONCEPT_ALPHABETS_BASE_HXX

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