From 3f48271ef9b14e3bd2313c9f86b81fc34d8008ed Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 4 Apr 2021 11:32:28 +0100 Subject: [PATCH] Added etl::accumulate & etl::clamp --- include/etl/algorithm.h | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/include/etl/algorithm.h b/include/etl/algorithm.h index f61fe6f0..7b93d45b 100644 --- a/include/etl/algorithm.h +++ b/include/etl/algorithm.h @@ -2578,6 +2578,74 @@ namespace etl std::stable_sort(first, last); } #endif + +#if ETL_NOT_USING_STL + //*************************************************************************** + /// Accumulates values. + ///\ingroup algorithm + //*************************************************************************** + template + ETL_CONSTEXPR14 T accumulate(TIterator first, TIterator last, T sum) + { + while (first != last) + { + sum = etl::move(sum) + *first++; + } + + return sum; + } + + //*************************************************************************** + /// Accumulates values. + ///\ingroup algorithm + //*************************************************************************** + template + ETL_CONSTEXPR14 T accumulate(TIterator first, TIterator last, T sum, TBinaryOperation operation) + { + while (first != last) + { + sum = operation(etl::move(sum), *first++); + } + + return sum; + } +#else + //*************************************************************************** + /// Accumulates values. + ///\ingroup algorithm + //*************************************************************************** + template + ETL_CONSTEXPR14 T accumulate(TIterator first, TIterator last, T sum) + { + return std::accumulate(first, last, sum); + } + + //*************************************************************************** + /// Accumulates values. + ///\ingroup algorithm + //*************************************************************************** + template + ETL_CONSTEXPR14 T accumulate(TIterator first, TIterator last, T sum, TBinaryOperation operation) + { + return std::accumulate(first, last, sum, operation); + } +#endif + + //*************************************************************************** + /// Clamp values. + ///\ingroup algorithm + //*************************************************************************** + template + ETL_CONSTEXPR const T& clamp(const T& value, const T& low, const T& high, TCompare compare) + { + return compare(value, low) ? low : compare(high, value) ? high : value; + } + + template + ETL_CONSTEXPR const T& clamp(const T& value, const T& low, const T& high ) + { + return clamp(value, low, high, etl::less()); + } } //*****************************************************************************