[RFC PATCH v2 09/12] ipa: libipa: vector: Add sum() function
Milan Zamazal
mzamazal at redhat.com
Mon Nov 18 14:55:17 CET 2024
Laurent Pinchart <laurent.pinchart at ideasonboard.com> writes:
> Add a function to calculate the sum of a vector. It will be useful for
> algorithms.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
Reviewed-by: Milan Zamazal <mzamazal at redhat.com>
> ---
> Changes since v1:
>
> - Drop Vector::normalize()
> ---
> src/ipa/libipa/vector.cpp | 12 ++++++++++++
> src/ipa/libipa/vector.h | 7 +++++++
> 2 files changed, 19 insertions(+)
>
> diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
> index 14816cdb5d09..19e7537f8a5a 100644
> --- a/src/ipa/libipa/vector.cpp
> +++ b/src/ipa/libipa/vector.cpp
> @@ -296,6 +296,18 @@ namespace ipa {
> * \return The length of the vector
> */
>
> +/**
> + * \fn Vector::sum() const
> + * \brief Calculate the sum of all the vector elements
> + * \tparam R The type of the sum
> + *
> + * The type R of the sum defaults to the type T of the elements, but can be set
> + * explicitly to use a different type in case the type T would risk
> + * overflowing.
> + *
> + * \return The sum of all the vector elements
> + */
> +
> /**
> * \fn Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols> &v)
> * \brief Multiply a matrix by a vector
> diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
> index 949dc4650aa4..f55da6476103 100644
> --- a/src/ipa/libipa/vector.h
> +++ b/src/ipa/libipa/vector.h
> @@ -9,6 +9,7 @@
> #include <algorithm>
> #include <array>
> #include <cmath>
> +#include <numeric>
> #include <optional>
> #include <ostream>
>
> @@ -245,6 +246,12 @@ public:
> return std::sqrt(length2());
> }
>
> + template<typename R = T>
> + constexpr R sum() const
> + {
> + return std::accumulate(data_.begin(), data_.end(), R{});
> + }
> +
> private:
> static constexpr Vector apply(const Vector &lhs, const Vector &rhs, std::function<T(T, T)> func)
> {
More information about the libcamera-devel
mailing list