From af72a33e68a662dbcc932c659a61c9cb57febde8 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Fri, 12 Jan 2024 15:26:11 +0800 Subject: [PATCH] feat add clamp --- src/ulib/utils/utils.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/ulib/utils/utils.h diff --git a/src/ulib/utils/utils.h b/src/ulib/utils/utils.h new file mode 100644 index 0000000..0d5ff4e --- /dev/null +++ b/src/ulib/utils/utils.h @@ -0,0 +1,22 @@ +#ifndef ULIB_SRC_ULIB_UTILS_UTILS_H_ +#define ULIB_SRC_ULIB_UTILS_UTILS_H_ + +#include + +namespace ulib { +template +const T & +Clamp(const T &value, const T &low, const T &high) +{ + return Clamp(value, low, high, std::less{}); +} + +template +const T & +Clamp(const T &value, const T &low, const T &high, Comp comp) +{ + return comp(value, low) ? low : (comp(high, value) ? high : value); +} + +}// namespace ulib +#endif// ULIB_SRC_ULIB_UTILS_UTILS_H_