Skip to content

Commit

Permalink
AK: Add min() and max() methods to Array<T, N>
Browse files Browse the repository at this point in the history
...That are only defined when min() and max() are defined on the
elements.
  • Loading branch information
alimpfard authored and awesomekling committed May 5, 2021
1 parent 11306d7 commit b361793
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions AK/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};

Expand Down

0 comments on commit b361793

Please sign in to comment.