~ubuntu-branches/ubuntu/breezy/aqsis/breezy

« back to all changes in this revision

Viewing changes to boost/boost/random/detail/signed_unsigned_compare.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Will Newton
  • Date: 2004-12-07 20:06:49 UTC
  • Revision ID: james.westby@ubuntu.com-20041207200649-fccswkrvp4oc8lmn
Tags: upstream-0.9.3
ImportĀ upstreamĀ versionĀ 0.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* boost random/detail/signed_unsigned_compare.hpp header file
 
2
 *
 
3
 * Copyright Jens Maurer 2000-2001
 
4
 * Permission to use, copy, modify, sell, and distribute this software
 
5
 * is hereby granted without fee provided that the above copyright notice
 
6
 * appears in all copies and that both that copyright notice and this
 
7
 * permission notice appear in supporting documentation,
 
8
 *
 
9
 * Jens Maurer makes no representations about the suitability of this
 
10
 * software for any purpose. It is provided "as is" without express or
 
11
 * implied warranty.
 
12
 *
 
13
 * See http://www.boost.org for most recent version including documentation.
 
14
 *
 
15
 * Revision history
 
16
 */
 
17
 
 
18
 
 
19
#ifndef BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE
 
20
#define BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE
 
21
 
 
22
#include <boost/limits.hpp>
 
23
 
 
24
namespace boost {
 
25
namespace random {
 
26
 
 
27
/*
 
28
 * Correctly compare two numbers whose types possibly differ in signedness.
 
29
 * See boost::numeric_cast<> for the general idea.
 
30
 * Most "if" statements involve only compile-time constants, so the
 
31
 * optimizing compiler can do its job easily.
 
32
 *
 
33
 * With most compilers, the straightforward implementation produces a
 
34
 * bunch of (legitimate) warnings.  Some template magic helps, though.
 
35
 */
 
36
 
 
37
namespace detail {
 
38
template<bool signed1, bool signed2>
 
39
struct do_compare
 
40
{ };
 
41
 
 
42
template<>
 
43
struct do_compare<false, false>
 
44
{
 
45
  // cast to the larger type is automatic with built-in types
 
46
  template<class T1, class T2>
 
47
  static bool equal(T1 x, T2 y) { return x == y; }
 
48
  template<class T1, class T2>
 
49
  static bool lessthan(T1 x, T2 y) { return x < y; }
 
50
};
 
51
 
 
52
template<>
 
53
struct do_compare<true, true> : do_compare<false, false>
 
54
{ };
 
55
 
 
56
template<>
 
57
struct do_compare<true, false>
 
58
{
 
59
  template<class T1, class T2>
 
60
  static bool equal(T1 x, T2 y) { return x >= 0 && static_cast<T2>(x) == y; }
 
61
  template<class T1, class T2>
 
62
  static bool lessthan(T1 x, T2 y) { return x < 0 || static_cast<T2>(x) < y; }
 
63
};
 
64
 
 
65
template<>
 
66
struct do_compare<false, true>
 
67
{
 
68
  template<class T1, class T2>
 
69
  static bool equal(T1 x, T2 y) { return y >= 0 && x == static_cast<T1>(y); }
 
70
  template<class T1, class T2>
 
71
  static bool lessthan(T1 x, T2 y) { return y >= 0 && x < static_cast<T1>(y); }
 
72
};
 
73
 
 
74
} // namespace detail
 
75
 
 
76
 
 
77
template<class T1, class T2>
 
78
int equal_signed_unsigned(T1 x, T2 y)
 
79
{
 
80
  typedef std::numeric_limits<T1> x_traits;
 
81
  typedef std::numeric_limits<T2> y_traits;
 
82
  return detail::do_compare<x_traits::is_signed, y_traits::is_signed>::equal(x, y);
 
83
}
 
84
 
 
85
template<class T1, class T2>
 
86
int lessthan_signed_unsigned(T1 x, T2 y)
 
87
{
 
88
  typedef std::numeric_limits<T1> x_traits;
 
89
  typedef std::numeric_limits<T2> y_traits;
 
90
  return detail::do_compare<x_traits::is_signed, y_traits::is_signed>::lessthan(x, y);
 
91
}
 
92
 
 
93
} // namespace random
 
94
} // namespace boost
 
95
 
 
96
#endif // BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE