ipa: libipa: vector: Add compound assignment operators

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

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
This commit is contained in:
Laurent Pinchart 2024-11-16 21:02:52 +02:00
parent f0d73c8758
commit 69544f5b7b
2 changed files with 115 additions and 0 deletions

View file

@ -120,6 +120,62 @@ namespace ipa {
* \return The element-wise division of this vector by \a scalar * \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 * \fn Vector::dot(const Vector<T, Rows> &other) const
* \brief Compute the dot product * \brief Compute the dot product

View file

@ -108,6 +108,46 @@ public:
return apply(*this, scalar, std::divides<>{}); 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 constexpr T dot(const Vector<T, Rows> &other) const
{ {
T ret = 0; T ret = 0;
@ -202,6 +242,25 @@ private:
return result; 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_; std::array<T, Rows> data_;
}; };