couple_letter.hxx

00001 // couple_letter.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 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_COUPLE_LETTER_HXX
00018 # define VCSN_ALGEBRA_IMPLEMENTATION_LETTER_COUPLE_LETTER_HXX
00019 
00020 # include <sstream>
00021 
00022 # include <vaucanson/algebra/implementation/letter/couple_letter.hh>
00023 
00024 namespace vcsn {
00025 
00026   // Specialization for pairs.
00027   template <typename S, typename U, typename V>
00028   bool op_parse (const algebra::FreeMonoidBase<S>& set,
00029                  std::basic_string< std::pair<U, V> >& v,
00030                  const std::string& s,
00031                  typename std::string::const_iterator& i,
00032                  const std::list<char>&)
00033   {
00034     typename std::string::const_iterator j = i;
00035 
00036     while (i != s.end ()) {
00037       if (*i != '(')
00038         break ;
00039       std::string sub (i, s.end ());
00040       std::istringstream is (sub);
00041       std::pair<U,V> p;
00042       is >> p;
00043       if (!set.alphabet ().contains (p))
00044         break ;
00045       int inc = sub.size () - is.str ().size ();
00046       for (int k = 0; k < inc; k++, i++) ;
00047       v += p;
00048     }
00049     return (i != j);
00050   }
00051 
00052   namespace misc {
00053 
00054     template <typename U, typename V>
00055     std::ostream& operator<< (std::ostream& o, std::pair<U, V> p)
00056     {
00057       return o << " (" << p.first << "," << p.second << ")";
00058     }
00059 
00060     template <typename U, typename V, class Traits, class Allocator>
00061     std::ostream& operator<< (std::ostream& o,
00062                               std::basic_string<std::pair<U, V>, Traits, Allocator> s)
00063     {
00064       typename
00065         std::basic_string<std::pair<U, V>, Traits, Allocator>::const_iterator i;
00066       for (i = s.begin (); i != s.end (); ++i)
00067         o << " (" << i->first << "," << i->second << ")";
00068       return o;
00069     }
00070 
00071     template <typename U, typename V>
00072     std::istream& operator>> (std::istream& i, std::pair<U, V>& p)
00073     {
00074       char c = i.get ();
00075       if (c != '(')
00076         i.unget ();
00077       i >> p.first;
00078       c = i.get ();
00079       if (c != ',')
00080         i.unget ();
00081       i >> p.second;
00082       c = i.get ();
00083       if (c != ')')
00084         i.unget ();
00085       return i;
00086     }
00087 
00088   } // misc
00089 } // vcsn
00090 
00091 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_LETTER_COUPLE_LETTER_HXX

Generated on Wed Jun 13 17:00:21 2007 for Vaucanson by  doxygen 1.5.1