Vaucanson  1.4.1
semiring_base.hxx
1 // semiring_base.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, 2007 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_CONCEPT_SEMIRING_BASE_HXX
18 # define VCSN_ALGEBRA_CONCEPT_SEMIRING_BASE_HXX
19 
20 # include <vaucanson/algebra/concept/semiring_base.hh>
22 
23 # include <sstream>
24 # include <string>
25 # include <iostream>
26 
27 namespace vcsn {
28 
29  namespace algebra {
30 
31  /*-------------.
32  | SemiringBase |
33  `-------------*/
34 
35  template <class Self>
37  {}
38 
39  template <class Self>
41  MonoidBase<Self>(other)
42  {}
43 
44  template <class Self>
45  template <class T>
46  bool
48  {
49  return op_can_choose_non_starable(this->self(), SELECT(T));
50  }
51 
52  template <class Self>
53  template <class T>
56  {
57  return op_choose_starable(this->self(), SELECT(T));
58  }
59 
60 
61  template <class Self>
62  template <class T>
65  {
66  return op_choose_non_starable(this->self(), SELECT(T));
67  }
68 
69  } // algebra
70 
71  /*---------------------.
72  | MetaElement<Self, T> |
73  `---------------------*/
74 
75  template <class Self, class T>
78  {
79  op_in_star(this->structure(), this->value());
80  return this->self();
81  }
82 
83  template <class Self, class T>
84  bool
86  {
87  return op_starable(this->structure(), this->value());
88  }
89 
90  template <class Self, class T>
92  {}
93 
94  template <class Self, class T>
96  (const MetaElement& other) :
98  {}
99 
100  /*-----.
101  | star |
102  `-----*/
103 
104  template <typename S, typename T>
105  typename op_star_traits<S, T>::ret_t
106  star(const Element<S, T>& e)
107  {
108  typename op_star_traits<S, T>::ret_t res(e);
109  return res.star();
110  }
111 
112  template <typename S, typename T>
113  bool starable(const Element<S, T>& elt)
114  {
115  return op_starable(elt.structure(), elt.value());
116  }
117 
118  template <typename S, typename T>
119  bool
120  parse_weight(Element<S, T>& w, const std::string&s ,
121  typename std::string::const_iterator& i)
122  {
123  return op_parse(w.structure(), w.value(), s, i);
124  }
125 
126 
127  namespace algebra {
128 
129  // default implementations:
130 
131  template <typename S, typename T>
132  bool
134  SELECTOR(T))
135  {
136  return false;
137  }
138 
139 
140  template <typename S, typename T>
143  {
144  std::cerr << "WARNING: default implementation of op_choose_starable "
145  "called." << std::endl;
146  std::cerr << "RESULT IS NOT RANDOM." << std::endl;
147  // Zero is always starable.
148  return T();
149  }
150 
151  template <typename S, typename T>
154  {
155  pure_service_call ("default implementation of op_choose_non_starable()");
156  return T();
157  }
158 
159  template <typename S, typename T>
160  bool
162  T& w,
163  const std::string& s,
164  typename std::string::const_iterator& i)
165  {
166  if (*i != '-' && (*i < '0' || *i > '9'))
167  return false;
168  T res;
169  std::stringstream ret;
170  ret << std::string(i, s.end());
171  int init = ret.tellg();
172  ret >> std::dec >> res;
173  if (ret.eof())
174  {
175  i = s.end();
176  }
177  else
178  {
179  int cur = ret.tellg();
180  if (cur < 0)
181  return false;
182  i += cur - init;
183  }
184  w = res;
185  return true;
186  }
187 
188  template <typename Self, typename T>
189  bool op_starable(const algebra::SemiringBase<Self>& s, const T& v)
190  {
191  return op_eq(SELECT(Self), v, zero_value(SELECT(Self), SELECT(T)));
192  }
193 
194  template <typename Self, typename T>
195  void op_in_star(const algebra::SemiringBase<Self>&, T& v)
196  {
197  assertion(op_eq(SELECT(Self), v, zero_value(SELECT(Self), SELECT(T))));
198  v = identity_value(SELECT(Self), SELECT(T));
199  }
200 
201  template <typename Self, typename T>
202  T op_default(SELECTOR(algebra::SemiringBase<Self>), SELECTOR(T))
203  {
204  return zero_value(SELECT(Self), SELECT(T));
205  }
206 
207  } // algebra
208 
209 } // vcsn
210 
211 #endif // ! VCSN_ALGEBRA_CONCEPT_SEMIRING_BASE_HXX