@@ -111,6 +111,13 @@ public:
return apply(*this, scalar, std::divides<>{});
}
+ 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; });
+ }
+
Vector &operator+=(const Vector &other)
{
return apply(other, [](T a, T b) { return a + b; });
@@ -151,6 +158,13 @@ public:
return apply(scalar, [](T a, T b) { return a / b; });
}
+ 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; });
+ }
+
constexpr Vector min(const Vector &other) const
{
return apply(*this, other, [](T a, T b) { return std::min(a, b); });
@@ -260,8 +274,8 @@ private:
return result;
}
- template<class BinaryOp>
- static constexpr Vector apply(const Vector &lhs, T rhs, BinaryOp op)
+ template<class U, class BinaryOp>
+ static constexpr Vector apply(const Vector &lhs, U rhs, BinaryOp op)
{
Vector result;
std::transform(lhs.data_.begin(), lhs.data_.end(),
@@ -281,8 +295,8 @@ private:
return *this;
}
- template<class BinaryOp>
- Vector &apply(T scalar, BinaryOp op)
+ template<class U, class BinaryOp>
+ Vector &apply(U scalar, BinaryOp op)
{
std::for_each(data_.begin(), data_.end(),
[&op, scalar](T &v) { v = op(v, scalar); });
@@ -126,6 +126,13 @@ LOG_DEFINE_CATEGORY(Vector)
* \return The element-wise division of this vector by \a scalar
*/
+/**
+ * \fn Vector::operator>>(unsigned int shift) const
+ * \brief Right-shift each element of this vector by \a shift bits
+ * \param[in] shift The shift amount
+ * \return A new vector with each element right-shifted by \a shift
+ */
+
/**
* \fn Vector::operator+=(Vector const &other)
* \brief Add \a other element-wise to this vector
@@ -182,6 +189,13 @@ LOG_DEFINE_CATEGORY(Vector)
* \return This vector
*/
+/**
+ * \fn Vector::operator>>=(unsigned int shift)
+ * \brief Right-shift each element of this vector by \a shift bits in place
+ * \param[in] shift The shift amount
+ * \return This vector
+ */
+
/**
* \fn Vector::min(const Vector &other) const
* \brief Calculate the minimum of this vector and \a other element-wise
@@ -93,6 +93,11 @@ protected:
v2 /= 4.0;
ASSERT_EQ(v2, (Vector<double, 3>{{ 1.0, 4.0, 8.0 }}));
+ Vector<int, 3> vi{{ 8, 16, 32 }};
+ ASSERT_EQ(vi >> 2, (Vector<int, 3>{{ 2, 4, 8 }}));
+ vi >>= 1;
+ ASSERT_EQ(vi, (Vector<int, 3>{{ 4, 8, 16 }}));
+
return TestPass;
}
};