Vaucanson 1.4
monoid_rep_base.hxx
00001 // monoid_rep_base.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 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 
00018 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_MONOID_MONOID_REP_BASE_HXX
00019 # define VCSN_ALGEBRA_IMPLEMENTATION_MONOID_MONOID_REP_BASE_HXX
00020 
00021 # include <vaucanson/algebra/implementation/monoid/monoid_rep_base.hh>
00022 
00023 namespace vcsn
00024 {
00025   namespace algebra
00026   {
00027     template <template <typename> class S, typename T>
00028     MonoidRepBase<S, T>::MonoidRepBase()
00029     {
00030       self_t* self = static_cast<self_t*>(this);
00031 
00032       // Sane defaults.
00033       self->maybe_epsilon.push_back("1");
00034       self->maybe_epsilon.push_back("e");
00035 
00036       // Trying with more than one char.
00037       self->maybe_epsilon.push_back("_e");
00038       self->maybe_epsilon.push_back("eps");
00039 
00040       self->empty = *(self->maybe_epsilon.begin());
00041       self->concat = "";
00042     }
00043 
00044     template <template <typename> class S, typename T>
00045     void
00046     MonoidRepBase<S, T>::disambiguate(const monoid_t& monoid, pointer_t& orig)
00047     {
00048       // Type helpers.
00049       typedef typename monoid_t::alphabet_t     alphabet_t;
00050       typedef typename alphabet_t::letter_t     letter_t;
00051 
00052       self_t* self = static_cast<self_t*>(this);
00053 
00054       // Type helpers.
00055       typedef std::vector<std::string>::const_iterator iterator_t;
00056 
00057       self_t new_rep = *self;
00058 
00059       for (iterator_t empty_it = new_rep.maybe_epsilon.begin();
00060            empty_it != new_rep.maybe_epsilon.end();
00061            ++empty_it)
00062       {
00063         bool found = true;
00064 
00065         for_all_const_(alphabet_t, i, monoid.alphabet())
00066         {
00067           if (letter_traits<letter_t>::letter_to_literal(*i) == *empty_it)
00068           {
00069             found = false;
00070             break;
00071           }
00072         }
00073 
00074         // Best match.
00075         if (found)
00076         {
00077           // Copy on write.
00078           if (new_rep.empty != *empty_it)
00079           {
00080             new_rep.empty = *empty_it;
00081             orig = pointer_t(new self_t(new_rep));
00082           }
00083           break;
00084         }
00085       }
00086     }
00087 
00088     template <template <typename> class S, typename T>
00089     bool
00090     operator==(boost::shared_ptr<MonoidRepBase<S, T> > lhs,
00091                boost::shared_ptr<MonoidRepBase<S, T> > rhs)
00092     {
00093       return (lhs->empty == rhs->empty &&
00094               lhs->concat == rhs->concat &&
00095               lhs->maybe_epsilon == rhs->maybe_epsilon);
00096     }
00097 
00098   } // ! algebra
00099 
00100 } // ! vcsn
00101 
00102 #endif // !VCSN_ALGEBRA_IMPLEMENTATION_MONOID_MONOID_REP_BASE_HXX