@@ -74,36 +74,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, [](T a, T b) { return a - b; });
}
- 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, [](T a, T b) { return a + b; });
}
- 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, [](T a, T b) { return a * b; });
}
- 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, [](T a, T b) { return a / b; });
}
constexpr T dot(const Vector<T, Rows> &other) const
@@ -178,6 +166,26 @@ public:
}
private:
+ static constexpr Vector apply(const Vector &lhs, const Vector &rhs, std::function<T(T, T)> func)
+ {
+ Vector result;
+ std::transform(lhs.data_.begin(), lhs.data_.end(),
+ rhs.data_.begin(), result.data_.begin(),
+ func);
+
+ return result;
+ }
+
+ static constexpr Vector apply(const Vector &lhs, T rhs, std::function<T(T, T)> func)
+ {
+ Vector result;
+ std::transform(lhs.data_.begin(), lhs.data_.end(),
+ result.data_.begin(),
+ [&func, rhs](T v) { return func(v, rhs); });
+
+ return result;
+ }
+
std::array<T, Rows> data_;
};
Instead of hand-coding all arithmetic operators, implement them based on a generic apply() function that takes an operator-specific std::function. This will simplify adding missing arithmetic operators. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> --- src/ipa/libipa/vector.h | 48 ++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 20 deletions(-)