File: min.hpp

package info (click to toggle)
primecount 7.16%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,724 kB
  • sloc: cpp: 18,769; ansic: 102; makefile: 89; sh: 86
file content (76 lines) | stat: -rw-r--r-- 1,834 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
///
/// @file  min.hpp
/// @brief Template min and max functions that allow comparing
///        different types if both types are integral
///        and sizeof(A) >= sizeof(B).
///
/// Copyright (C) 2024 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#ifndef MIN_HPP
#define MIN_HPP

#include <int128_t.hpp>
#include <macros.hpp>

#include <algorithm>

namespace {

template <typename A, typename B>
struct is_comparable
{
  enum {
    value = pstd::is_same<A, B>::value || (
            pstd::is_integral<A>::value &&
            pstd::is_integral<B>::value &&
            sizeof(A) >= sizeof(B))
  };
};

template <typename A, typename B>
ALWAYS_INLINE B min(A a, B b)
{
  static_assert(is_comparable<A, B>::value,
                "min(A, B): Cannot compare types A and B");

#if defined(ENABLE_ASSERT)
  if (pstd::is_unsigned<A>::value && pstd::is_signed<B>::value) ASSERT(b >= 0);
  if (pstd::is_unsigned<B>::value && pstd::is_signed<A>::value) ASSERT(a >= 0 && b <= pstd::numeric_limits<A>::max());
#endif

  return (B) std::min(a, (A) b);
}

template <typename A, typename B>
ALWAYS_INLINE A max(A a, B b)
{
  static_assert(is_comparable<A, B>::value,
                "max(A, B): Cannot compare types A and B");

#if defined(ENABLE_ASSERT)
  if (pstd::is_unsigned<A>::value && pstd::is_signed<B>::value) ASSERT(b >= 0);
  if (pstd::is_unsigned<B>::value && pstd::is_signed<A>::value) ASSERT(b <= pstd::numeric_limits<A>::max());
#endif

  return std::max(a, (A) b);
}

template <typename A, typename B, typename C>
ALWAYS_INLINE C min3(A a, B b, C c)
{
  return min(a, min(b, c));
}

template <typename A, typename B, typename C>
ALWAYS_INLINE A max3(A a, B b, C c)
{
  return max(a, max(b, c));
}

} // namespace

#endif