epoc32/include/stdapis/boost/random/detail/signed_unsigned_compare.hpp
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
     1 /* boost random/detail/signed_unsigned_compare.hpp header file
     2  *
     3  * Copyright Jens Maurer 2000-2001
     4  * Distributed under the Boost Software License, Version 1.0. (See
     5  * accompanying file LICENSE_1_0.txt or copy at
     6  * http://www.boost.org/LICENSE_1_0.txt)
     7  *
     8  * See http://www.boost.org for most recent version including documentation.
     9  *
    10  * Revision history
    11  */
    12 
    13 
    14 #ifndef BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE
    15 #define BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE
    16 
    17 #include <boost/limits.hpp>
    18 
    19 namespace boost {
    20 namespace random {
    21 
    22 /*
    23  * Correctly compare two numbers whose types possibly differ in signedness.
    24  * See boost::numeric_cast<> for the general idea.
    25  * Most "if" statements involve only compile-time constants, so the
    26  * optimizing compiler can do its job easily.
    27  *
    28  * With most compilers, the straightforward implementation produces a
    29  * bunch of (legitimate) warnings.  Some template magic helps, though.
    30  */
    31 
    32 namespace detail {
    33 template<bool signed1, bool signed2>
    34 struct do_compare
    35 { };
    36 
    37 template<>
    38 struct do_compare<false, false>
    39 {
    40   // cast to the larger type is automatic with built-in types
    41   template<class T1, class T2>
    42   static bool equal(T1 x, T2 y) { return x == y; }
    43   template<class T1, class T2>
    44   static bool lessthan(T1 x, T2 y) { return x < y; }
    45 };
    46 
    47 template<>
    48 struct do_compare<true, true> : do_compare<false, false>
    49 { };
    50 
    51 template<>
    52 struct do_compare<true, false>
    53 {
    54   template<class T1, class T2>
    55   static bool equal(T1 x, T2 y) { return x >= 0 && static_cast<T2>(x) == y; }
    56   template<class T1, class T2>
    57   static bool lessthan(T1 x, T2 y) { return x < 0 || static_cast<T2>(x) < y; }
    58 };
    59 
    60 template<>
    61 struct do_compare<false, true>
    62 {
    63   template<class T1, class T2>
    64   static bool equal(T1 x, T2 y) { return y >= 0 && x == static_cast<T1>(y); }
    65   template<class T1, class T2>
    66   static bool lessthan(T1 x, T2 y) { return y >= 0 && x < static_cast<T1>(y); }
    67 };
    68 
    69 } // namespace detail
    70 
    71 
    72 template<class T1, class T2>
    73 int equal_signed_unsigned(T1 x, T2 y)
    74 {
    75   typedef std::numeric_limits<T1> x_traits;
    76   typedef std::numeric_limits<T2> y_traits;
    77   return detail::do_compare<x_traits::is_signed, y_traits::is_signed>::equal(x, y);
    78 }
    79 
    80 template<class T1, class T2>
    81 int lessthan_signed_unsigned(T1 x, T2 y)
    82 {
    83   typedef std::numeric_limits<T1> x_traits;
    84   typedef std::numeric_limits<T2> y_traits;
    85   return detail::do_compare<x_traits::is_signed, y_traits::is_signed>::lessthan(x, y);
    86 }
    87 
    88 } // namespace random
    89 } // namespace boost
    90 
    91 #endif // BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE