From 6a05bd3fa6a3f4ecf45c4cc32abc616883998ff9 Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Tue, 24 Sep 2019 07:01:03 +0000 Subject: cm/algorithm: Provide function cm::clamp --- Utilities/std/cm/algorithm | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Utilities/std/cm/algorithm diff --git a/Utilities/std/cm/algorithm b/Utilities/std/cm/algorithm new file mode 100644 index 0000000..8ade99c --- /dev/null +++ b/Utilities/std/cm/algorithm @@ -0,0 +1,38 @@ +// -*-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. */ +#ifndef cm_algorithm +#define cm_algorithm + +#include // IWYU pragma: export +#include + +namespace cm { + +#if __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L + +using std::clamp; + +#else + +template +T const& clamp(T const& v, T const& lo, T const& hi) +{ + assert(!(hi < lo)); + return (v < lo) ? lo : (hi < v) ? hi : v; +} + +template +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 + +#endif -- cgit v0.12