Merge branch 'smart_range_improvement' into 'master'

Smart range improvements

See merge request OpenMesh/OpenMesh!239
This commit is contained in:
Jan Möbius
2019-11-29 13:13:17 +01:00

View File

@@ -78,18 +78,18 @@ struct SmartRangeT
* @param f Functor that is applied to all elements before computing the sum * @param f Functor that is applied to all elements before computing the sum
*/ */
template <typename Functor> template <typename Functor>
auto sum(Functor&& f) -> decltype (f(std::declval<HandleT>())+f(std::declval<HandleT>())) auto sum(Functor&& f) -> typename std::decay<decltype (f(std::declval<HandleT>()))>::type
{ {
auto range = static_cast<const RangeT*>(this); auto range = static_cast<const RangeT*>(this);
auto begin = range->begin(); auto begin = range->begin();
auto end = range->end(); auto end = range->end();
assert(begin != end); assert(begin != end);
decltype (f(*begin) + f(*begin)) sum = f(*begin); typename std::decay<decltype (f(*begin))>::type result = f(*begin);
auto it = begin; auto it = begin;
++it; ++it;
for (; it != end; ++it) for (; it != end; ++it)
sum += f(*it); result += f(*it);
return sum; return result;
} }
/** @brief Computes the average of elements. /** @brief Computes the average of elements.
@@ -99,22 +99,22 @@ struct SmartRangeT
* @param f Functor that is applied to all elements before computing the average. * @param f Functor that is applied to all elements before computing the average.
*/ */
template <typename Functor> template <typename Functor>
auto avg(Functor&& f) -> decltype (1.0 * (f(std::declval<HandleT>())+f(std::declval<HandleT>()))) auto avg(Functor&& f) -> typename std::decay<decltype (f(std::declval<HandleT>()))>::type
{ {
auto range = static_cast<const RangeT*>(this); auto range = static_cast<const RangeT*>(this);
auto begin = range->begin(); auto begin = range->begin();
auto end = range->end(); auto end = range->end();
assert(begin != end); assert(begin != end);
decltype (f(*begin) + f(*begin)) sum = f(*begin); typename std::decay<decltype (f(*begin))>::type result = f(*begin);
auto it = begin; auto it = begin;
++it; ++it;
int n_elements = 1; int n_elements = 1;
for (; it != end; ++it) for (; it != end; ++it)
{ {
sum += f(*it); result += f(*it);
++n_elements; ++n_elements;
} }
return (1.0 / n_elements) * sum; return (1.0 / n_elements) * result;
} }
/** @brief Check if any element fulfils condition. /** @brief Check if any element fulfils condition.
@@ -161,10 +161,10 @@ struct SmartRangeT
* the array will contain the handles. * the array will contain the handles.
*/ */
template <int n, typename Functor = Identity> template <int n, typename Functor = Identity>
auto to_array(Functor&& f = {}) -> std::array<typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type, n> auto to_array(Functor&& f = {}) -> std::array<typename std::decay<decltype (f(std::declval<HandleT>()))>::type, n>
{ {
auto range = static_cast<const RangeT*>(this); auto range = static_cast<const RangeT*>(this);
std::array<typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type, n> res; std::array<typename std::decay<decltype (f(std::declval<HandleT>()))>::type, n> res;
auto it = range->begin(); auto it = range->begin();
auto end = range->end(); auto end = range->end();
int i = 0; int i = 0;
@@ -181,10 +181,10 @@ struct SmartRangeT
* the vector will contain the handles. * the vector will contain the handles.
*/ */
template <typename Functor = Identity> template <typename Functor = Identity>
auto to_vector(Functor&& f = {}) -> std::vector<typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type> auto to_vector(Functor&& f = {}) -> std::vector<typename std::decay<decltype (f(std::declval<HandleT>()))>::type>
{ {
auto range = static_cast<const RangeT*>(this); auto range = static_cast<const RangeT*>(this);
std::vector<typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type> res; std::vector<typename std::decay<decltype (f(std::declval<HandleT>()))>::type> res;
for (const auto& e : *range) for (const auto& e : *range)
res.push_back(f(e)); res.push_back(f(e));
return res; return res;
@@ -198,10 +198,10 @@ struct SmartRangeT
* the set will contain the handles. * the set will contain the handles.
*/ */
template <typename Functor = Identity> template <typename Functor = Identity>
auto to_set(Functor&& f = {}) -> std::set<typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type> auto to_set(Functor&& f = {}) -> std::set<typename std::decay<decltype (f(std::declval<HandleT>()))>::type>
{ {
auto range = static_cast<const RangeT*>(this); auto range = static_cast<const RangeT*>(this);
std::set<typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type> res; std::set<typename std::decay<decltype (f(std::declval<HandleT>()))>::type> res;
for (const auto& e : *range) for (const auto& e : *range)
res.insert(f(e)); res.insert(f(e));
return res; return res;
@@ -232,7 +232,7 @@ struct SmartRangeT
* @param f Functor that is applied to all elements before computing minimum. * @param f Functor that is applied to all elements before computing minimum.
*/ */
template <typename Functor> template <typename Functor>
auto min(Functor&& f) -> typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type auto min(Functor&& f) -> typename std::decay<decltype (f(std::declval<HandleT>()))>::type
{ {
using std::min; using std::min;
@@ -241,7 +241,7 @@ struct SmartRangeT
auto end = range->end(); auto end = range->end();
assert(it != end); assert(it != end);
typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type res = f(*it); typename std::decay<decltype (f(std::declval<HandleT>()))>::type res = f(*it);
++it; ++it;
for (; it != end; ++it) for (; it != end; ++it)
@@ -257,7 +257,7 @@ struct SmartRangeT
* @param f Functor that is applied to all elements before computing maximum. * @param f Functor that is applied to all elements before computing maximum.
*/ */
template <typename Functor> template <typename Functor>
auto max(Functor&& f) -> typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type auto max(Functor&& f) -> typename std::decay<decltype (f(std::declval<HandleT>()))>::type
{ {
using std::max; using std::max;
@@ -266,7 +266,7 @@ struct SmartRangeT
auto end = range->end(); auto end = range->end();
assert(it != end); assert(it != end);
typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type res = f(*it); typename std::decay<decltype (f(std::declval<HandleT>()))>::type res = f(*it);
++it; ++it;
for (; it != end; ++it) for (; it != end; ++it)
@@ -283,8 +283,8 @@ struct SmartRangeT
* @param f Functor that is applied to all elements before computing maximum. * @param f Functor that is applied to all elements before computing maximum.
*/ */
template <typename Functor> template <typename Functor>
auto minmax(Functor&& f) -> std::pair<typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type, auto minmax(Functor&& f) -> std::pair<typename std::decay<decltype (f(std::declval<HandleT>()))>::type,
typename std::remove_reference<decltype (f(std::declval<HandleT>()))>::type> typename std::decay<decltype (f(std::declval<HandleT>()))>::type>
{ {
return std::make_pair(this->min(f), this->max(f)); return std::make_pair(this->min(f), this->max(f));
} }