int_letter.hxx

00001 // int_letter.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 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_LETTER_INT_LETTER_HXX
00018 # define VCSN_ALGEBRA_IMPLEMENTATION_LETTER_INT_LETTER_HXX
00019 
00020 # include <sstream>
00021 # include <climits>
00022 
00023 # include <vaucanson/algebra/implementation/letter/int_letter.hh>
00024 
00025 namespace vcsn {
00026 
00027   namespace algebra {
00028 
00029     template <>
00030     struct letter_traits<int>
00031     {
00032       // This letter type needs more than one char to be displayed.
00033       typedef misc::false_t is_char_letter;
00034 
00035       enum
00036       {
00037         // Here we use UINT_MAX and not UINT_MAX + 1 because an enum cannot
00038         // hold UINT_MAX + 1.
00039         cardinal = UINT_MAX
00040       };
00041 
00042       // We can not "project" an int letter.
00043       typedef undefined_type first_projection_t;
00044       typedef undefined_type second_projection_t;
00045 
00046       LETTER_DEFAULT(open_par, "(")
00047       LETTER_DEFAULT(close_par, ")")
00048       LETTER_DEFAULT(plus, "+")
00049       LETTER_DEFAULT(times, ".")
00050       LETTER_DEFAULT(star, "*")
00051       LETTER_DEFAULT(epsilon, "e")
00052       LETTER_DEFAULT(zero, "z")
00053       LETTER_DEFAULT(open_weight, "{")
00054       LETTER_DEFAULT(close_weight, "}")
00055       LETTER_DEFAULT(space, " ")
00056 
00057       static
00058       int
00059       literal_to_letter(const std::string& str)
00060       {
00061         std::stringstream sstr(str);
00062         int ret;
00063         sstr >> ret;
00064         return ret;
00065       }
00066 
00067       static
00068       std::string
00069       letter_to_literal(const int& c)
00070       {
00071         std::stringstream sstr;
00072         sstr << c;
00073         return sstr.str();
00074       }
00075 
00076       // An int is "simple" with dimension 1.
00077       static std::string kind() { return "simple"; }
00078       static int dim() { return 1; }
00079 
00080     };
00081 
00082     template <typename S, typename CharContainer>
00083     bool op_parse(const algebra::FreeMonoidBase<S>& set,
00084                   std::basic_string<int>& v,
00085                   const std::string& s,
00086                   typename std::string::const_iterator& i,
00087                   const CharContainer&)
00088     {
00089       typename std::string::const_iterator j = i;
00090       typename std::string::const_iterator back;
00091 
00092       while (i != s.end()) {
00093         std::string out;
00094         back = i;
00095 
00096         while ((i != s.end()) && (((*i >= '0') && (*i <= '9')) || (*i == '#')))
00097           if (*i == '#') {
00098             ++i;
00099             break;
00100           }
00101           else {
00102             out += *i;
00103             ++i;
00104           }
00105 
00106         if (out.empty())
00107           break;
00108 
00109         int value;
00110         std::istringstream is(out);
00111         is >> value;
00112 
00113         if (!set.alphabet().contains(value)) {
00114           i = back;
00115           break ;
00116         }
00117         v.push_back(value);
00118       }
00119       return (i != j);
00120     }
00121 
00122   } // ! algebra
00123 
00124 } // ! vcsn
00125 
00126 namespace std {
00127 
00128   inline
00129   ostream& operator<<(ostream& o, basic_string<int> s)
00130   {
00131     basic_string<int>::const_iterator i;
00132     basic_string<int>::const_iterator j;
00133 
00134     for (i = s.begin(); i != s.end(); ) {
00135       o << *i;
00136       i++;
00137       if (i != s.end())
00138         o << "#";
00139     }
00140 
00141     return o;
00142   }
00143 
00144 } // ! std
00145 
00146 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_LETTER_INT_LETTER_HXX

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