summaryrefslogtreecommitdiff
path: root/src/ipa/libipa
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-11-16 21:02:52 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-11-26 19:05:19 +0200
commit49e961ca3598922f62ec9381dc1d742b4c1c0b31 (patch)
treed7decdde22ab6a778176e2cd370f9767c9b3d8e4 /src/ipa/libipa
parentdd624b3fff1154334cef3f596bfecd29e4a5daaf (diff)
ipa: libipa: vector: Generalize arithmetic operators
Instead of hand-coding all arithmetic operators, implement them based on a generic apply() function that takes an operator-specific binary operators. This will simplify adding missing arithmetic operators. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
Diffstat (limited to 'src/ipa/libipa')
-rw-r--r--src/ipa/libipa/vector.h52
1 files changed, 32 insertions, 20 deletions
diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
index b8315dd0..5545da12 100644
--- a/src/ipa/libipa/vector.h
+++ b/src/ipa/libipa/vector.h
@@ -6,8 +6,10 @@
*/
#pragma once
+#include <algorithm>
#include <array>
#include <cmath>
+#include <functional>
#include <optional>
#include <ostream>
@@ -66,36 +68,24 @@ public:
return ret;
}
- constexpr Vector<T, Rows> operator-(const Vector<T, Rows> &other) const
+ constexpr Vector operator-(const Vector &other) const
{
- Vector<T, Rows> ret;
- for (unsigned int i = 0; i < Rows; i++)
- ret[i] = data_[i] - other[i];
- return ret;
+ return apply(*this, other, std::minus<>{});
}
- constexpr Vector<T, Rows> operator+(const Vector<T, Rows> &other) const
+ constexpr Vector operator+(const Vector &other) const
{
- Vector<T, Rows> ret;
- for (unsigned int i = 0; i < Rows; i++)
- ret[i] = data_[i] + other[i];
- return ret;
+ return apply(*this, other, std::plus<>{});
}
- constexpr Vector<T, Rows> operator*(T factor) const
+ constexpr Vector operator*(T factor) const
{
- Vector<T, Rows> ret;
- for (unsigned int i = 0; i < Rows; i++)
- ret[i] = data_[i] * factor;
- return ret;
+ return apply(*this, factor, std::multiplies<>{});
}
- constexpr Vector<T, Rows> operator/(T factor) const
+ constexpr Vector operator/(T factor) const
{
- Vector<T, Rows> ret;
- for (unsigned int i = 0; i < Rows; i++)
- ret[i] = data_[i] / factor;
- return ret;
+ return apply(*this, factor, std::divides<>{});
}
constexpr T dot(const Vector<T, Rows> &other) const
@@ -170,6 +160,28 @@ public:
}
private:
+ template<class BinaryOp>
+ static constexpr Vector apply(const Vector &lhs, const Vector &rhs, BinaryOp op)
+ {
+ Vector result;
+ std::transform(lhs.data_.begin(), lhs.data_.end(),
+ rhs.data_.begin(), result.data_.begin(),
+ op);
+
+ return result;
+ }
+
+ template<class BinaryOp>
+ static constexpr Vector apply(const Vector &lhs, T rhs, BinaryOp op)
+ {
+ Vector result;
+ std::transform(lhs.data_.begin(), lhs.data_.end(),
+ result.data_.begin(),
+ [&op, rhs](T v) { return op(v, rhs); });
+
+ return result;
+ }
+
std::array<T, Rows> data_;
};