Vaucanson  1.4.1
alphabet_set.hxx
1 // alphabet_set.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, 2007, 2008 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_ALPHABETS_ALPHABET_SET_HXX
18 # define VCSN_ALGEBRA_IMPLEMENTATION_ALPHABETS_ALPHABET_SET_HXX
19 
20 # include <limits>
21 
22 # include <vaucanson/algebra/concept/letter.hh>
23 # include <vaucanson/algebra/implementation/alphabets/alphabet_set.hh>
24 
25 namespace vcsn
26 {
27  namespace algebra
28  {
29  /*----------------------------------------.
30  | Projections for types that support them |
31  `----------------------------------------*/
32 
33 # define ALPHABET_TRAITS \
34  alphabet_traits<AlphabetSet<L>, std::set<L> >
35 
36  template <typename L>
37  inline typename ALPHABET_TRAITS::first_projection_t
38  ALPHABET_TRAITS::first_projection(const ALPHABET_TRAITS::alphabet_t& A)
39  {
40  // We can not project if the type does not support it.
41  static_assertion_(not (misc::static_eq<first_projection_t,
42  undefined_type>::value), need_first_projection);
43 
44  first_projection_t R;
45 
46  for_all_const_(alphabet_t, i, A)
47  {
48  // We assume we can access the first projection with "first".
49  R.insert((*i).first);
50  }
51 
52  return R;
53  }
54 
55  template <typename L>
56  inline typename ALPHABET_TRAITS::second_projection_t
57  ALPHABET_TRAITS::second_projection(const ALPHABET_TRAITS::alphabet_t& A)
58  {
59  // We can not project if the type does not support it.
60  static_assertion_(not (misc::static_eq<second_projection_t,
61  undefined_type>::value), need_second_projection);
62 
63  second_projection_t R;
64 
65  for_all_const_(alphabet_t, i, A)
66  {
67  // We assume we can access the second projection with "second".
68  R.insert((*i).second);
69  }
70 
71  return R;
72  }
73 
74 # undef ALPHABET_TRAITS
75 
76  /*-----------------------------------------------------------.
77  | Definition of an alphabet implementation based on std::set |
78  `-----------------------------------------------------------*/
79 
80  template <typename L>
81  size_t
82  op_max_size(const algebra::AlphabetSet<L>&, const std::set<L>&)
83  {
84  return algebra::letter_traits<L>::cardinal;
85  }
86 
87  template<typename L>
88  bool op_contains(const algebra::AlphabetSet<L>&, const std::set<L>&)
89  {
90  return true;
91  }
92 
93  template<typename L>
94  bool op_is_finite(const algebra::AlphabetSet<L>&, const std::set<L>&)
95  {
96  return true;
97  }
98 
99  template<typename L>
100  bool op_contains_e(const algebra::AlphabetSet<L>&, const std::set<L>& a,
101  const L& v)
102  {
103  return a.find(v) != a.end();
104  }
105 
106  template <typename L>
107  std::pair<bool, L>
108  op_parse(const AlphabetSet<L>& s,
109  const std::set<L>& v,
110  const std::string& in,
111  size_t& pos)
112  {
113  // Type helpers.
114  typedef std::string::const_iterator iter_t;
115 
116  // Return values.
117  bool ret = false;
118  L ret_letter;
119  size_t ret_pos = pos;
120 
121  // Temporaries.
122  size_t current_pos = pos;
123  std::string current_letter_rep = "";
124 
125  for (iter_t i = in.begin() + pos; i != in.end(); ++i)
126  {
127  std::pair<bool, L> tmp;
128  current_letter_rep += *i;
129  ++current_pos;
130  tmp = letter_traits<L>::literal_to_letter(current_letter_rep);
131  if (tmp.first && op_contains_e(s, v, tmp.second))
132  {
133  ret = true;
134  ret_letter = tmp.second;
135  ret_pos = current_pos;
136  }
137  }
138 
139  pos = ret_pos;
140  return std::make_pair(ret, ret_letter);
141  }
142 
143  } // ! algebra
144 
145 } // ! vcsn
146 
147 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_ALPHABETS_ALPHABET_SET_HXX