summaryrefslogtreecommitdiff
path: root/kernel/lib/libcxx/include/__algorithm/minmax.h
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/lib/libcxx/include/__algorithm/minmax.h')
-rw-r--r--kernel/lib/libcxx/include/__algorithm/minmax.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/kernel/lib/libcxx/include/__algorithm/minmax.h b/kernel/lib/libcxx/include/__algorithm/minmax.h
new file mode 100644
index 0000000..b28d66c
--- /dev/null
+++ b/kernel/lib/libcxx/include/__algorithm/minmax.h
@@ -0,0 +1,76 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the BastionOS freestanding C++ standard library.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBBASTION_ALGORITHM_MINMAX_H
+#define _LIBBASTION_ALGORITHM_MINMAX_H
+
+#include <__config>
+#include <initializer_list>
+
+_LIBBASTION_BEGIN_NAMESPACE_STD
+
+// ── min ─────────────────────────────────────────────────────────────────────
+
+template<class _Tp>
+_LIBBASTION_NODISCARD constexpr const _Tp& min(const _Tp& __a, const _Tp& __b) {
+ return (__b < __a) ? __b : __a;
+}
+
+template<class _Tp, class _Compare>
+_LIBBASTION_NODISCARD constexpr const _Tp& min(const _Tp& __a, const _Tp& __b, _Compare __comp) {
+ return __comp(__b, __a) ? __b : __a;
+}
+
+template<class _Tp>
+_LIBBASTION_NODISCARD constexpr _Tp min(initializer_list<_Tp> __il) {
+ const _Tp* __first = __il.begin();
+ const _Tp* __last = __il.end();
+ const _Tp* __result = __first;
+ for (++__first; __first != __last; ++__first)
+ if (*__first < *__result)
+ __result = __first;
+ return *__result;
+}
+
+// ── max ─────────────────────────────────────────────────────────────────────
+
+template<class _Tp>
+_LIBBASTION_NODISCARD constexpr const _Tp& max(const _Tp& __a, const _Tp& __b) {
+ return (__a < __b) ? __b : __a;
+}
+
+template<class _Tp, class _Compare>
+_LIBBASTION_NODISCARD constexpr const _Tp& max(const _Tp& __a, const _Tp& __b, _Compare __comp) {
+ return __comp(__a, __b) ? __b : __a;
+}
+
+template<class _Tp>
+_LIBBASTION_NODISCARD constexpr _Tp max(initializer_list<_Tp> __il) {
+ const _Tp* __first = __il.begin();
+ const _Tp* __last = __il.end();
+ const _Tp* __result = __first;
+ for (++__first; __first != __last; ++__first)
+ if (*__result < *__first)
+ __result = __first;
+ return *__result;
+}
+
+// ── clamp ───────────────────────────────────────────────────────────────────
+
+template<class _Tp>
+_LIBBASTION_NODISCARD constexpr const _Tp& clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi) {
+ return (__v < __lo) ? __lo : (__hi < __v) ? __hi : __v;
+}
+
+template<class _Tp, class _Compare>
+_LIBBASTION_NODISCARD constexpr const _Tp& clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp) {
+ return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
+}
+
+_LIBBASTION_END_NAMESPACE_STD
+
+#endif // _LIBBASTION_ALGORITHM_MINMAX_H