Vaucanson  1.4.1
couple_letter.hxx
1 // couple_letter.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008 The Vaucanson
6 // Group.
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
12 //
13 // The complete GNU General Public Licence Notice can be found as the
14 // `COPYING' file in the root directory.
15 //
16 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
17 //
18 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_LETTER_COUPLE_LETTER_HXX
19 # define VCSN_ALGEBRA_IMPLEMENTATION_LETTER_COUPLE_LETTER_HXX
20 
21 # include <string>
22 # include <sstream>
23 # include <utility>
24 # include <climits>
25 # include <vector>
26 
27 # include <vaucanson/algebra/implementation/letter/couple_letter.hh>
28 
29 namespace vcsn
30 {
31  namespace algebra
32  {
33  template <typename U, typename V>
34  struct letter_traits< std::pair<U, V> >
35  {
36  // we only consider letters of the form (u, v)
37  typedef misc::true_t is_char_letter;
38 
39  enum
40  {
41  /*
42  * Theoretically cardinal should be the product of
43  * letter_traits<U>::cardinal and letter_traits<V>::cardinal.
44  * But to avoid overflows and for
45  * practical reasons, it is better to consider it infinite.
46  *
47  * FIXME: Maybe doing this is not a good idea?
48  */
49  cardinal = INT_MAX
50  };
51 
52  // A pair letter has two projections available: U and V.
53  typedef U first_projection_t;
54  typedef V second_projection_t;
55 
56  static
57  std::pair<bool, std::pair<U, V> >
58  literal_to_letter(const std::string& str)
59  {
60  // Check for a basic well formed pair: (x,y).
61  if (str.size() < 5 || str[0] != '(' || *(str.end() - 1) != ')')
62  return std::make_pair(false, std::make_pair(0, 0));
63 
64  // Split the string on commas.
65  typedef std::vector<std::string> tokens_t;
66  typedef tokens_t::const_iterator tokens_iter_t;
67  std::string delim = ",";
68  std::string buff(str.begin() + 1, str.end() - 1);
69 
70  std::string::size_type last_pos = buff.find_first_not_of(delim, 0);
71  std::string::size_type pos = buff.find_first_of(delim, last_pos);
72 
73  tokens_t tokens;
74 
75  while (std::string::npos != pos || std::string::npos != last_pos)
76  {
77  // Push new token.
78  tokens.push_back(buff.substr(last_pos, pos - last_pos));
79  // Update positions.
80  last_pos = buff.find_first_not_of(delim, pos);
81  pos = buff.find_first_of(delim, last_pos);
82  }
83 
84  if (tokens.size() != 2)
85  return std::make_pair(false, std::make_pair(0, 0));
86 
87  std::pair<bool, U> fc = algebra::letter_traits<U>::literal_to_letter(tokens[0]);
88  std::pair<bool, V> sc = algebra::letter_traits<V>::literal_to_letter(tokens[1]);
89 
90  if (!(fc.first && sc.first))
91  return std::make_pair(false, std::make_pair(0, 0));
92 
93  return std::make_pair(true, std::make_pair(fc.second, sc.second));
94  }
95 
96  static
97  std::string
98  letter_to_literal(const std::pair<U, V>& c)
99  {
100  std::stringstream sstr;
101  sstr << '(' << c.first << ',' << c.second << ')';
102  return sstr.str();
103  }
104 
105  // A pair is a "tuple" with dimension 2.
106  static std::string kind() { return "tuple"; }
107  static int dim() { return 2; }
108 
109  };
110 
111  } // ! algebra
112 
113 } // ! vcsn
114 
115 namespace std
116 {
117  template <typename U, typename V>
118  std::ostream& operator<<(std::ostream& s, const std::pair<U, V>& letter)
119  {
120  s << vcsn::algebra::letter_traits<std::pair<U, V> >::letter_to_literal(letter);
121  return s;
122  }
123 
124 } // ! std
125 
126 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_LETTER_COUPLE_LETTER_HXX