[PATCH v4 08/17] ipa: libipa: vector: Add compound assignment operators

Laurent Pinchart laurent.pinchart at ideasonboard.com
Tue Nov 19 13:19:19 CET 2024


Extend the Vector class with compound assignment operators that match
the binary arithmetic operators.

Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
Reviewed-by: Milan Zamazal <mzamazal at redhat.com>
---
Changes since v3:

- Replace std::function with a templated argument

Changes since v2:

- Use std::plus, std::minus, std::multiplies and std::divides
---
 src/ipa/libipa/vector.cpp | 56 +++++++++++++++++++++++++++++++++++++
 src/ipa/libipa/vector.h   | 59 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 115 insertions(+)

diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
index 13aaacbed20e..3305dcb38e6b 100644
--- a/src/ipa/libipa/vector.cpp
+++ b/src/ipa/libipa/vector.cpp
@@ -133,6 +133,62 @@ namespace ipa {
  * \return The element-wise division of this vector by \a scalar
  */
 
+/**
+ * \fn Vector::operator+=(Vector const &other)
+ * \brief Add \a other element-wise to this vector
+ * \param[in] other The other vector
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator+=(T scalar)
+ * \brief Add \a scalar element-wise to this vector
+ * \param[in] scalar The scalar
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator-=(Vector const &other)
+ * \brief Subtract \a other element-wise from this vector
+ * \param[in] other The other vector
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator-=(T scalar)
+ * \brief Subtract \a scalar element-wise from this vector
+ * \param[in] scalar The scalar
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator*=(const Vector &other)
+ * \brief Multiply this vector by \a other element-wise
+ * \param[in] other The other vector
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator*=(T scalar)
+ * \brief Multiply this vector by \a scalar element-wise
+ * \param[in] scalar The scalar
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator/=(const Vector &other)
+ * \brief Divide this vector by \a other element-wise
+ * \param[in] other The other vector
+ * \return This vector
+ */
+
+/**
+ * \fn Vector::operator/=(T scalar)
+ * \brief Divide this vector by \a scalar element-wise
+ * \param[in] scalar The scalar
+ * \return This vector
+ */
+
 /**
  * \fn Vector::dot(const Vector<T, Rows> &other) const
  * \brief Compute the dot product
diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
index 6e24cdcebec0..683bf9ee2564 100644
--- a/src/ipa/libipa/vector.h
+++ b/src/ipa/libipa/vector.h
@@ -112,6 +112,46 @@ public:
 		return apply(*this, scalar, std::divides<>{});
 	}
 
+	Vector &operator+=(const Vector &other)
+	{
+		return apply(other, [](T a, T b) { return a + b; });
+	}
+
+	Vector &operator+=(T scalar)
+	{
+		return apply(scalar, [](T a, T b) { return a + b; });
+	}
+
+	Vector &operator-=(const Vector &other)
+	{
+		return apply(other, [](T a, T b) { return a - b; });
+	}
+
+	Vector &operator-=(T scalar)
+	{
+		return apply(scalar, [](T a, T b) { return a - b; });
+	}
+
+	Vector &operator*=(const Vector &other)
+	{
+		return apply(other, [](T a, T b) { return a * b; });
+	}
+
+	Vector &operator*=(T scalar)
+	{
+		return apply(scalar, [](T a, T b) { return a * b; });
+	}
+
+	Vector &operator/=(const Vector &other)
+	{
+		return apply(other, [](T a, T b) { return a / b; });
+	}
+
+	Vector &operator/=(T scalar)
+	{
+		return apply(scalar, [](T a, T b) { return a / b; });
+	}
+
 	constexpr T dot(const Vector<T, Rows> &other) const
 	{
 		T ret = 0;
@@ -206,6 +246,25 @@ private:
 		return result;
 	}
 
+	template<class BinaryOp>
+	Vector &apply(const Vector &other, BinaryOp op)
+	{
+		auto itOther = other.data_.begin();
+		std::for_each(data_.begin(), data_.end(),
+			      [&op, &itOther](T &v) { v = op(v, *itOther++); });
+
+		return *this;
+	}
+
+	template<class BinaryOp>
+	Vector &apply(T scalar, BinaryOp op)
+	{
+		std::for_each(data_.begin(), data_.end(),
+			      [&op, scalar](T &v) { v = op(v, scalar); });
+
+		return *this;
+	}
+
 	std::array<T, Rows> data_;
 };
 
-- 
Regards,

Laurent Pinchart



More information about the libcamera-devel mailing list