summaryrefslogtreecommitdiffstats
path: root/Utilities/std/cm/algorithm
blob: 93fe224c9fd0c760dd99e884d425ec3e6cbed401 (plain)
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
// -*-c++-*-
// vim: set ft=cpp:

/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#pragma once

#include <algorithm> // IWYU pragma: export
#include <cassert>

namespace cm {

#if __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L

using std::clamp;

#else

template <typename T>
T const& clamp(T const& v, T const& lo, T const& hi)
{
  assert(!(hi < lo));
  return (v < lo) ? lo : (hi < v) ? hi : v;
}

template <typename T, typename Comp>
T const& clamp(T const& v, T const& lo, T const& hi, Comp comp)
{
  assert(!comp(hi, lo));
  return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
}

#endif

} // namespace cm