[2/4] libcamera: vector: Swap order of arguments to apply()
diff mbox series

Message ID 20260621002305.3763752-3-laurent.pinchart@ideasonboard.com
State New
Headers show
Series
  • libcamera: vector: Add clamp() function
Related show

Commit Message

Laurent Pinchart June 21, 2026, 12:23 a.m. UTC
The Vector class is lacking a clamp() function, which will take two
scalar parameters. To support that, the apply() function will need to be
converted to variable number of parameters. This will require passing
the scalar parameter last, to make automatic template arguments
deduction possible.

In preparation for that change, swap the arguments to apply() to pass
the operation before the scalar parameter last. Only one variant of
apply() strictly requires this change, arguments to the three other
functions are also swapped for consistency. Also for consistency, swap
the template arguments as well.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/internal/vector.h | 56 ++++++++++++++---------------
 1 file changed, 28 insertions(+), 28 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/vector.h b/include/libcamera/internal/vector.h
index 04ba5d847b7d..256de1d1c53a 100644
--- a/include/libcamera/internal/vector.h
+++ b/include/libcamera/internal/vector.h
@@ -73,116 +73,116 @@  public:
 
 	constexpr Vector operator+(const Vector &other) const
 	{
-		return apply(*this, other, std::plus<>{});
+		return apply(*this, std::plus<>{}, other);
 	}
 
 	constexpr Vector operator+(T scalar) const
 	{
-		return apply(*this, scalar, std::plus<>{});
+		return apply(*this, std::plus<>{}, scalar);
 	}
 
 	constexpr Vector operator-(const Vector &other) const
 	{
-		return apply(*this, other, std::minus<>{});
+		return apply(*this, std::minus<>{}, other);
 	}
 
 	constexpr Vector operator-(T scalar) const
 	{
-		return apply(*this, scalar, std::minus<>{});
+		return apply(*this, std::minus<>{}, scalar);
 	}
 
 	constexpr Vector operator*(const Vector &other) const
 	{
-		return apply(*this, other, std::multiplies<>{});
+		return apply(*this, std::multiplies<>{}, other);
 	}
 
 	constexpr Vector operator*(T scalar) const
 	{
-		return apply(*this, scalar, std::multiplies<>{});
+		return apply(*this, std::multiplies<>{}, scalar);
 	}
 
 	constexpr Vector operator/(const Vector &other) const
 	{
-		return apply(*this, other, std::divides<>{});
+		return apply(*this, std::divides<>{}, other);
 	}
 
 	constexpr Vector operator/(T scalar) const
 	{
-		return apply(*this, scalar, std::divides<>{});
+		return apply(*this, std::divides<>{}, scalar);
 	}
 
 	constexpr Vector operator>>(unsigned int shift) const
 	{
 		static_assert(std::is_integral_v<T>,
 			      "Vector::operator>> requires an integer element type");
-		return apply(*this, shift, [](T a, unsigned int b) { return a >> b; });
+		return apply(*this, [](T a, unsigned int b) { return a >> b; }, shift);
 	}
 
 	Vector &operator+=(const Vector &other)
 	{
-		return apply(other, [](T a, T b) { return a + b; });
+		return apply([](T a, T b) { return a + b; }, other);
 	}
 
 	Vector &operator+=(T scalar)
 	{
-		return apply(scalar, [](T a, T b) { return a + b; });
+		return apply([](T a, T b) { return a + b; }, scalar);
 	}
 
 	Vector &operator-=(const Vector &other)
 	{
-		return apply(other, [](T a, T b) { return a - b; });
+		return apply([](T a, T b) { return a - b; }, other);
 	}
 
 	Vector &operator-=(T scalar)
 	{
-		return apply(scalar, [](T a, T b) { return a - b; });
+		return apply([](T a, T b) { return a - b; }, scalar);
 	}
 
 	Vector &operator*=(const Vector &other)
 	{
-		return apply(other, [](T a, T b) { return a * b; });
+		return apply([](T a, T b) { return a * b; }, other);
 	}
 
 	Vector &operator*=(T scalar)
 	{
-		return apply(scalar, [](T a, T b) { return a * b; });
+		return apply([](T a, T b) { return a * b; }, scalar);
 	}
 
 	Vector &operator/=(const Vector &other)
 	{
-		return apply(other, [](T a, T b) { return a / b; });
+		return apply([](T a, T b) { return a / b; }, other);
 	}
 
 	Vector &operator/=(T scalar)
 	{
-		return apply(scalar, [](T a, T b) { return a / b; });
+		return apply([](T a, T b) { return a / b; }, scalar);
 	}
 
 	Vector &operator>>=(unsigned int shift)
 	{
 		static_assert(std::is_integral_v<T>,
 			      "Vector::operator>>= requires an integer element type");
-		return apply(shift, [](T a, unsigned int b) { return a >> b; });
+		return apply([](T a, unsigned int b) { return a >> b; }, shift);
 	}
 
 	constexpr Vector min(const Vector &other) const
 	{
-		return apply(*this, other, [](T a, T b) { return std::min(a, b); });
+		return apply(*this, [](T a, T b) { return std::min(a, b); }, other);
 	}
 
 	constexpr Vector min(T scalar) const
 	{
-		return apply(*this, scalar, [](T a, T b) { return std::min(a, b); });
+		return apply(*this, [](T a, T b) { return std::min(a, b); }, scalar);
 	}
 
 	constexpr Vector max(const Vector &other) const
 	{
-		return apply(*this, other, [](T a, T b) { return std::max(a, b); });
+		return apply(*this, [](T a, T b) { return std::max(a, b); }, other);
 	}
 
 	constexpr Vector max(T scalar) const
 	{
-		return apply(*this, scalar, [](T a, T b) -> T { return std::max(a, b); });
+		return apply(*this, [](T a, T b) -> T { return std::max(a, b); }, scalar);
 	}
 
 	constexpr T dot(const Vector<T, Rows> &other) const
@@ -264,7 +264,7 @@  public:
 
 private:
 	template<typename BinaryOp>
-	static constexpr Vector apply(const Vector &lhs, const Vector &rhs, BinaryOp op)
+	static constexpr Vector apply(const Vector &lhs, BinaryOp op, const Vector &rhs)
 	{
 		Vector result;
 		std::transform(lhs.data_.begin(), lhs.data_.end(),
@@ -274,8 +274,8 @@  private:
 		return result;
 	}
 
-	template<typename U, typename BinaryOp>
-	static constexpr Vector apply(const Vector &lhs, U rhs, BinaryOp op)
+	template<typename BinaryOp, typename U>
+	static constexpr Vector apply(const Vector &lhs, BinaryOp op, U rhs)
 	{
 		Vector result;
 		std::transform(lhs.data_.begin(), lhs.data_.end(),
@@ -286,7 +286,7 @@  private:
 	}
 
 	template<typename BinaryOp>
-	Vector &apply(const Vector &other, BinaryOp op)
+	Vector &apply(BinaryOp op, const Vector &other)
 	{
 		auto itOther = other.data_.begin();
 		std::for_each(data_.begin(), data_.end(),
@@ -295,8 +295,8 @@  private:
 		return *this;
 	}
 
-	template<typename U, typename BinaryOp>
-	Vector &apply(U scalar, BinaryOp op)
+	template<typename BinaryOp, typename U>
+	Vector &apply(BinaryOp op, U scalar)
 	{
 		std::for_each(data_.begin(), data_.end(),
 			      [&op, scalar](T &v) { v = op(v, scalar); });