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 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_CONCEPT_ALPHABETS_BASE_HXX
00018 # define VCSN_ALGEBRA_CONCEPT_ALPHABETS_BASE_HXX
00019 
00020 # include <vaucanson/algebra/concept/alphabets_base.hh>
00021 # include <vaucanson/misc/random.hh>
00022 # include <vaucanson/misc/contract.hh>
00023 # include <cstddef>
00024 
00025 namespace vcsn {
00026 
00027   namespace algebra {
00028 
00029     /*-------------------.
00030     | AlphabetSetBase<S> |
00031     `-------------------*/
00032 
00033     template<typename S>
00034     AlphabetSetBase<S>::AlphabetSetBase()
00035     {}
00036 
00037     template<typename S>
00038     AlphabetSetBase<S>::AlphabetSetBase(const AlphabetSetBase& other)
00039     {}
00040 
00041   } // algebra
00042 
00043   /*-----------------------------------.
00044   | MetaElement<AlphabetSetBase<S>, T> |
00045   `-----------------------------------*/
00046   // Meta-information about element formed from an AlphabetSetBase
00047   // structuring element.
00048 
00049   template<typename S, typename T>
00050   size_t
00051   MetaElement<algebra::AlphabetSetBase<S>, T>::size() const
00052   {
00053     return op_size(this->structure(), this->value());
00054   }
00055 
00056   template<typename S, typename T>
00057   size_t
00058   MetaElement<algebra::AlphabetSetBase<S>, T>::max_size() const
00059   {
00060     return op_max_size(this->structure(), this->value());
00061   }
00062 
00063   template<typename S, typename T>
00064   bool
00065   MetaElement<algebra::AlphabetSetBase<S>, T>::contains(const letter_t& l) const
00066   {
00067     return op_contains_e(this->structure(), this->value(), l);
00068   }
00069 
00070   template<typename S, typename T>
00071   bool
00072   MetaElement<algebra::AlphabetSetBase<S>, T>::is_finite() const
00073   {
00074     return op_is_finite(this->structure(), this->value());
00075   }
00076 
00077   template<typename S, typename T>
00078   typename MetaElement<algebra::AlphabetSetBase<S>, T>::iterator
00079   MetaElement<algebra::AlphabetSetBase<S>, T>::begin()
00080   {
00081     return op_begin(this->structure(), this->value());
00082   }
00083 
00084   template<typename S, typename T>
00085   typename MetaElement<algebra::AlphabetSetBase<S>, T>::const_iterator
00086   MetaElement<algebra::AlphabetSetBase<S>, T>::begin() const
00087   {
00088     return op_begin_const(this->structure(), this->value());
00089   }
00090 
00091   template<typename S, typename T>
00092   typename MetaElement<algebra::AlphabetSetBase<S>, T>::iterator
00093   MetaElement<algebra::AlphabetSetBase<S>, T>::end()
00094   {
00095     return op_end(this->structure(), this->value());
00096   }
00097 
00098   template<typename S, typename T>
00099   typename MetaElement<algebra::AlphabetSetBase<S>, T>::const_iterator
00100   MetaElement<algebra::AlphabetSetBase<S>, T>::end() const
00101   {
00102     return op_end_const(this->structure(), this->value());
00103   }
00104 
00105   template<typename S, typename T>
00106   void
00107   MetaElement<algebra::AlphabetSetBase<S>, T>::insert(const letter_t& l)
00108   {
00109     op_insert(this->structure(), this->value(), l);
00110   }
00111 
00112   template<typename S, typename T>
00113   bool
00114   MetaElement<algebra::AlphabetSetBase<S>, T>::letter_equality(letter_t lhs,
00115                                                                letter_t rhs) const
00116   {
00117     return op_letter_equality(this->structure(), this->value(), lhs, rhs);
00118   }
00119 
00120   template<typename S, typename T>
00121   typename algebra::alphabet_traits<S, T>::letter_t
00122   MetaElement<algebra::AlphabetSetBase<S>, T>::choose() const
00123   {
00124     // FIXME: recommendation(overload this operator)
00125 
00126     precondition (is_finite() && size() > 0);
00127 
00128     int  nr = ::utility::random::generate<int>(0, size() - 1);
00129 
00130     const_iterator it = begin();
00131     for (int k = 0; k < nr; ++k)
00132       ++it;
00133 
00134     return *it;
00135   }
00136 
00137   template <class Self, typename T>
00138   typename algebra::alphabet_traits<Self, T>::letter_t
00139   MetaElement<algebra::AlphabetSetBase<Self>, T>::random_letter() const
00140   {
00141     return utility::random::generate<typename algebra::alphabet_traits<Self, T>::letter_t>();
00142   }
00143 
00144   template<typename S, typename T>
00145   MetaElement<algebra::AlphabetSetBase<S>, T>::MetaElement()
00146   {}
00147 
00148   template<typename S, typename T>
00149   MetaElement<algebra::AlphabetSetBase<S>, T>::MetaElement(const MetaElement& other) :
00150     MetaElement<Structure<S>, T>(other)
00151   {}
00152 
00153   template <typename S, typename T, typename L>
00154   bool op_letter_equality(const algebra::AlphabetSetBase<S>& s,
00155                           const T& a,
00156                           L lhs,
00157                           L rhs)
00158   {
00159     return lhs == rhs;
00160   }
00161 
00162   template<typename S, typename St, typename T>
00163   St& op_rout(const algebra::AlphabetSetBase<S>& s, St& st, const T& a)
00164   {
00165     st << "{ ";
00166     if (op_is_finite(s.self(), a))
00167       for (typename op_begin_traits<S, T>::const_ret_t i = op_begin_const(s.self(), a);
00168            i != op_end_const(s.self(), a);
00169            ++i)
00170         {
00171           if (i != op_begin_const(s.self(), a))
00172             st << ", ";
00173           st << *i;
00174         }
00175     else
00176       st << "<many letters>";
00177     st << " }";
00178     return st;
00179   }
00180 
00181 } // vcsn
00182 
00183 #endif // ! VCSN_ALGEBRA_CONCEPT_ALPHABETS_BASE_HXX

Generated on Fri Jul 28 12:18:29 2006 for Vaucanson by  doxygen 1.4.6