Commit 9567cc62 authored by Philip Trettner's avatar Philip Trettner
Browse files

Merge branch 'jn/develop' into 'develop'

Add `max_index` and `min_index`

See merge request !102
parents e76242f6 25b20d15
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
* *
* - min_element * - min_element
* - max_element * - max_element
* - max_index
* - min_index
* - average (same as arithmetic_mean) * - average (same as arithmetic_mean)
* - mean (same as arithmetic_mean) * - mean (same as arithmetic_mean)
* - arithmetic_mean * - arithmetic_mean
...@@ -39,11 +41,11 @@ template <class T = void, class RangeT, class TransformF, class ReduceF> ...@@ -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))))>; using U = std::decay_t<decltype(f(t(R(*it)), t(R(*it))))>;
auto const e = tg::end(values); auto const e = tg::end(values);
U r = t(R(*it)); U r = t(R(*it));
it++; ++it;
while (it != e) while (it != e)
{ {
r = f(r, t(R(*it))); r = f(r, t(R(*it)));
it++; ++it;
} }
return r; return r;
} }
...@@ -111,6 +113,63 @@ template <class RangeT, class TransformT = identity_fun> ...@@ -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); }); 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> template <class T = void, class RangeT = void, class TransformT = identity_fun>
[[nodiscard]] constexpr auto sum(RangeT const& values, TransformT&& transform = {}) [[nodiscard]] constexpr auto sum(RangeT const& values, TransformT&& transform = {})
{ {
......
Supports Markdown
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