From b88a6c9de904d479dd00b8093176803197338186 Mon Sep 17 00:00:00 2001 From: Max Lyon Date: Mon, 9 Mar 2020 18:43:09 +0100 Subject: [PATCH 1/3] add arg min and arg max methods to smart ranges --- src/OpenMesh/Core/Mesh/SmartRange.hh | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/OpenMesh/Core/Mesh/SmartRange.hh b/src/OpenMesh/Core/Mesh/SmartRange.hh index 06e5192b..b827137a 100644 --- a/src/OpenMesh/Core/Mesh/SmartRange.hh +++ b/src/OpenMesh/Core/Mesh/SmartRange.hh @@ -250,6 +250,37 @@ struct SmartRangeT 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 + auto argmin(Functor&& f) -> HandleT + { + auto range = static_cast(this); + auto it = range->begin(); + auto min_it = it; + auto end = range->end(); + assert(it != end); + + typename std::decay()))>::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. * * Computes the maximum of all objects returned by functor \p f. @@ -275,6 +306,38 @@ struct SmartRangeT 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 + auto argmax(Functor&& f) -> HandleT + { + auto range = static_cast(this); + auto it = range->begin(); + auto max_it = it; + auto end = range->end(); + assert(it != end); + + typename std::decay()))>::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. * * Computes the minimum and maximum of all objects returned by functor \p f. Result is returned as std::pair From 227b1cd5c7a3d4ca37d7fa64bc153649108cc43a Mon Sep 17 00:00:00 2001 From: Max Lyon Date: Mon, 9 Mar 2020 18:43:21 +0100 Subject: [PATCH 2/3] add count_if to smart ranges --- src/OpenMesh/Core/Mesh/SmartRange.hh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/OpenMesh/Core/Mesh/SmartRange.hh b/src/OpenMesh/Core/Mesh/SmartRange.hh index b827137a..5518878e 100644 --- a/src/OpenMesh/Core/Mesh/SmartRange.hh +++ b/src/OpenMesh/Core/Mesh/SmartRange.hh @@ -353,7 +353,16 @@ struct SmartRangeT } - + template + auto count_if(Functor&& f) -> int + { + int count = 0; + auto range = static_cast(this); + for (const auto& e : *range) + if (f(e)) + ++count; + return count; + } }; From e2b9871164bdd3526b5888690f817168e673cb90 Mon Sep 17 00:00:00 2001 From: Max Lyon Date: Thu, 12 Mar 2020 09:38:20 +0100 Subject: [PATCH 3/3] add documentation to count_if method --- src/OpenMesh/Core/Mesh/SmartRange.hh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/OpenMesh/Core/Mesh/SmartRange.hh b/src/OpenMesh/Core/Mesh/SmartRange.hh index 5518878e..0c2ceaad 100644 --- a/src/OpenMesh/Core/Mesh/SmartRange.hh +++ b/src/OpenMesh/Core/Mesh/SmartRange.hh @@ -353,6 +353,12 @@ 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 auto count_if(Functor&& f) -> int {