From 740f10084309e435e6cec589c6c736988d015e82 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Wed, 13 Mar 2024 18:18:03 +0200 Subject: [PATCH] Add clamp function to --- include_std/algorithm | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include_std/algorithm b/include_std/algorithm index 85eb61b..918bc20 100644 --- a/include_std/algorithm +++ b/include_std/algorithm @@ -52,6 +52,19 @@ namespace std return (a > b) ? a : b; } + template + constexpr const T &clamp(const T &v, const T &lo, const T &hi) + { + return std::max(lo, std::min(v, hi)); + } + + template + constexpr const T &clamp(const T &v, const T &lo, const T &hi, Compare comp) + { + return comp(v, lo) ? lo : comp(hi, v) ? hi + : v; + } + template constexpr InputIt find(InputIt first, InputIt last, const T &value) {