AK: Add min() and max() methods to Array<T, N>

...That are only defined when min() and max() are defined on the
elements.
This commit is contained in:
Ali Mohammad Pur 2021-04-26 01:32:02 +04:30 committed by Andreas Kling
parent 11306d7121
commit b361793ad8
Notes: sideshowbarker 2024-07-18 18:41:19 +09:00

View file

@ -66,6 +66,26 @@ struct Array {
return Size;
}
constexpr T max() requires(requires(T x, T y) { x < y; })
{
static_assert(Size > 0, "No values to max() over");
T value = __data[0];
for (size_t i = 1; i < Size; ++i)
value = AK::max(__data[i], value);
return value;
}
constexpr T min() requires(requires(T x, T y) { x > y; })
{
static_assert(Size > 0, "No values to min() over");
T value = __data[0];
for (size_t i = 1; i < Size; ++i)
value = AK::min(__data[i], value);
return value;
}
T __data[Size];
};