Vaucanson  1.4.1
range.hxx
1 // range.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 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_LETTER_RANGE_HXX
18 # define VCSN_ALGEBRA_IMPLEMENTATION_LETTER_RANGE_HXX
19 
20 # include <cstdlib>
21 # include <cmath>
22 
23 namespace vcsn
24 {
25 
26  namespace algebra
27  {
28 
29  template <class T, class Interval>
30  static_ranged<T, Interval>::static_ranged()
31  {}
32 
33  template <class T, class Interval>
34  static_ranged<T, Interval>::static_ranged(const T& v)
35  {
36  if (Interval::check(v))
37  value_ = v;
38  else
39  value_ = Interval::from();
40  }
41 
42  template <class T, class Interval>
43  static_ranged<T, Interval>::static_ranged(const static_ranged& o) :
44  value_(o.value_)
45  {}
46 
47  template <class T, class Interval>
48  static_ranged<T, Interval>::operator T() const
49  {
50  return value_;
51  }
52 
53  template <class T, class Interval>
54  bool operator<(const static_ranged<T, Interval>& lhs,
55  const static_ranged<T, Interval>& rhs)
56  {
57  return lhs.value() < rhs.value();
58  }
59 
60  template <class T, class Interval>
61  bool operator==(const static_ranged<T, Interval>& lhs,
62  const static_ranged<T, Interval>& rhs)
63  {
64  return lhs.value() == rhs.value();
65  }
66 
67  template <class T, class Interval>
68  const T& static_ranged<T, Interval>::value() const
69  {
70  return value_;
71  }
72 
73  template <class T, class Interval>
74  T& static_ranged<T, Interval>::value()
75  {
76  return value_;
77  }
78 
79  template <class T, class Interval>
80  static_ranged<T, Interval>
81  static_ranged<T, Interval>::randomized()
82  {
83  value_ = Interval::random();
84  return *this;
85  }
86 
87  template <char From, char To>
88  bool
89  static_char_interval<From, To>::check(char c)
90  {
91  return ((c >= From) && (c <= To));
92  }
93 
94  template <char From, char To>
95  char static_char_interval<From, To>::from()
96  {
97  return From;
98  }
99 
100  template <char From, char To>
101  char static_char_interval<From, To>::to()
102  {
103  return To;
104  }
105 
106  template <char From, char To>
107  char static_char_interval<From, To>::random()
108  {
109  unsigned r = floor((float (rand()) / RAND_MAX) * (To - From + 1));
110  return (char)(From + r);
111  }
112 
113  } // algebra
114 
115 } // vcsn
116 
117 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_LETTER_RANGE_HXX