Commit 481175b7 authored by Jan Möbius's avatar Jan Möbius

Merge branch 'SmartRangeImprovement' into 'master'

Smart range improvement

See merge request !251
parents 6ac21207 e2b98711
Pipeline #13769 passed with stages
in 162 minutes and 50 seconds
...@@ -250,6 +250,37 @@ struct SmartRangeT ...@@ -250,6 +250,37 @@ struct SmartRangeT
return res; return res;
} }
/** @brief Compute minimal element.
*
* Computes the element that minimizes \p f.
*
* @param f Functor that is applied to all elements before comparing.
*/
template <typename Functor>
auto argmin(Functor&& f) -> HandleT
{
auto range = static_cast<const RangeT*>(this);
auto it = range->begin();
auto min_it = it;
auto end = range->end();
assert(it != end);
typename std::decay<decltype (f(std::declval<HandleT>()))>::type curr_min = f(*it);
++it;
for (; it != end; ++it)
{
auto val = f(*it);
if (val < curr_min)
{
curr_min = val;
min_it = it;
}
}
return *min_it;
}
/** @brief Compute maximum. /** @brief Compute maximum.
* *
* Computes the maximum of all objects returned by functor \p f. * Computes the maximum of all objects returned by functor \p f.
...@@ -275,6 +306,38 @@ struct SmartRangeT ...@@ -275,6 +306,38 @@ struct SmartRangeT
return res; return res;
} }
/** @brief Compute maximal element.
*
* Computes the element that maximizes \p f.
*
* @param f Functor that is applied to all elements before comparing.
*/
template <typename Functor>
auto argmax(Functor&& f) -> HandleT
{
auto range = static_cast<const RangeT*>(this);
auto it = range->begin();
auto max_it = it;
auto end = range->end();
assert(it != end);
typename std::decay<decltype (f(std::declval<HandleT>()))>::type curr_max = f(*it);
++it;
for (; it != end; ++it)
{
auto val = f(*it);
if (val > curr_max)
{
curr_max = val;
max_it = it;
}
}
return *max_it;
}
/** @brief Computes minimum and maximum. /** @brief Computes minimum and maximum.
* *
* Computes the minimum and maximum of all objects returned by functor \p f. Result is returned as std::pair * Computes the minimum and maximum of all objects returned by functor \p f. Result is returned as std::pair
...@@ -290,7 +353,22 @@ struct SmartRangeT ...@@ -290,7 +353,22 @@ struct SmartRangeT
} }
/** @brief Compute number of elements that satisfy a given predicate.
*
* Computes the numer of elements which satisfy functor \p f.
*
* @param f Predicate that elements have to satisfy in order to be counted.
*/
template <typename Functor>
auto count_if(Functor&& f) -> int
{
int count = 0;
auto range = static_cast<const RangeT*>(this);
for (const auto& e : *range)
if (f(e))
++count;
return count;
}
}; };
......
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