1
/* boost random/detail/signed_unsigned_compare.hpp header file
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,
9
* Jens Maurer makes no representations about the suitability of this
10
* software for any purpose. It is provided "as is" without express or
13
* See http://www.boost.org for most recent version including documentation.
19
#ifndef BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE
20
#define BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE
22
#include <boost/limits.hpp>
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.
33
* With most compilers, the straightforward implementation produces a
34
* bunch of (legitimate) warnings. Some template magic helps, though.
38
template<bool signed1, bool signed2>
43
struct do_compare<false, false>
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; }
53
struct do_compare<true, true> : do_compare<false, false>
57
struct do_compare<true, false>
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; }
66
struct do_compare<false, true>
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); }
77
template<class T1, class T2>
78
int equal_signed_unsigned(T1 x, T2 y)
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);
85
template<class T1, class T2>
86
int lessthan_signed_unsigned(T1 x, T2 y)
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);
96
#endif // BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE