Commit 25b20d15 authored by Julius Nehring-Wirxel's avatar Julius Nehring-Wirxel
Browse files

Add `max_index` and `min_index`

parent e76242f6
......@@ -10,6 +10,8 @@
*
* - min_element
* - max_element
* - max_index
* - min_index
* - average (same as arithmetic_mean)
* - mean (same as arithmetic_mean)
* - arithmetic_mean
......@@ -39,11 +41,11 @@ template <class T = void, class RangeT, class TransformF, class ReduceF>
using U = std::decay_t<decltype(f(t(R(*it)), t(R(*it))))>;
auto const e = tg::end(values);
U r = t(R(*it));
it++;
++it;
while (it != e)
{
r = f(r, t(R(*it)));
it++;
++it;
}
return r;
}
......@@ -111,6 +113,63 @@ template <class RangeT, class TransformT = identity_fun>
return detail::fold_right(values, transform, [](auto&& a, auto&& b) { return max(a, b); });
}
/// returns the index of the max element
template <class RangeT, class TransformT = identity_fun>
[[nodiscard]] constexpr size_t max_index(RangeT const& values, TransformT&& transform = {})
{
TG_CONTRACT(tg::begin(values) != tg::end(values) && "values must not be empty");
size_t curr_idx = 0;
auto it = tg::begin(values);
auto const end = tg::end(values);
auto max_v = transform(*it);
size_t max_idx = curr_idx;
++it;
++curr_idx;
while (it != end)
{
auto v = transform(*it);
if (v > max_v)
{
max_v = v;
max_idx = curr_idx;
}
++it;
++curr_idx;
}
return max_idx;
}
/// returns the index of the min element
template <class RangeT, class TransformT = identity_fun>
[[nodiscard]] constexpr size_t min_index(RangeT const& values, TransformT&& transform = {})
{
TG_CONTRACT(tg::begin(values) != tg::end(values) && "values must not be empty");
size_t curr_idx = 0;
auto it = tg::begin(values);
auto const end = tg::end(values);
auto min_v = transform(*it);
size_t min_idx = curr_idx;
++it;
++curr_idx;
while (it != end)
{
auto v = transform(*it);
if (v < min_v)
{
min_v = v;
min_idx = curr_idx;
}
++it;
++curr_idx;
}
return min_idx;
}
template <class T = void, class RangeT = void, class TransformT = identity_fun>
[[nodiscard]] constexpr auto sum(RangeT const& values, TransformT&& transform = {})
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment