implement VectorT in-place arithmetic operators based on Scalar in-place arithmetic operators

This commit is contained in:
Janis Born
2015-11-23 12:25:07 +01:00
parent 4e9330a87d
commit cf54f40e3e

View File

@@ -229,20 +229,21 @@ class VectorT {
typename std::enable_if<std::is_convertible< typename std::enable_if<std::is_convertible<
decltype(this->values_[0] * _s), Scalar>::value, decltype(this->values_[0] * _s), Scalar>::value,
VectorT<Scalar, DIM>&>::type { VectorT<Scalar, DIM>&>::type {
std::transform(values_.begin(), values_.end(), values_.begin(), for (auto& e : *this) {
[&_s](const Scalar & c) { return c * _s; }); e *= _s;
}
return *this; return *this;
} }
/** component-wise self-division by scalar /// component-wise self-division by scalar
\attention v *= (1/_s) is much faster than this */
template<typename OtherScalar> template<typename OtherScalar>
auto operator/=(const OtherScalar& _s) -> auto operator/=(const OtherScalar& _s) ->
typename std::enable_if<std::is_convertible< typename std::enable_if<std::is_convertible<
decltype(this->values_[0] / _s), Scalar>::value, decltype(this->values_[0] / _s), Scalar>::value,
VectorT<Scalar, DIM>&>::type { VectorT<Scalar, DIM>&>::type {
std::transform(values_.begin(), values_.end(), values_.begin(), for (auto& e : *this) {
[&_s](const Scalar & c) { return c / _s; }); e /= _s;
}
return *this; return *this;
} }
@@ -274,10 +275,9 @@ class VectorT {
typename std::enable_if< typename std::enable_if<
sizeof(decltype(this->values_[0] * *_rhs.data())) >= 0, sizeof(decltype(this->values_[0] * *_rhs.data())) >= 0,
vector_type&>::type { vector_type&>::type {
for (int i = 0; i < DIM; ++i) {
std::transform(data(), data() + DIM, data()[i] *= _rhs.data()[i];
_rhs.data(), data(), }
[](const Scalar &l, const OtherScalar &r) { return l * r; });
return *this; return *this;
} }
@@ -287,9 +287,9 @@ class VectorT {
typename std::enable_if< typename std::enable_if<
sizeof(decltype(this->values_[0] / *_rhs.data())) >= 0, sizeof(decltype(this->values_[0] / *_rhs.data())) >= 0,
vector_type&>::type { vector_type&>::type {
std::transform(data(), data() + DIM, for (int i = 0; i < DIM; ++i) {
_rhs.data(), data(), data()[i] /= _rhs.data()[i];
[](const Scalar &l, const OtherScalar &r) { return l / r; }); }
return *this; return *this;
} }
@@ -299,9 +299,9 @@ class VectorT {
typename std::enable_if< typename std::enable_if<
sizeof(decltype(this->values_[0] - *_rhs.data())) >= 0, sizeof(decltype(this->values_[0] - *_rhs.data())) >= 0,
vector_type&>::type { vector_type&>::type {
std::transform(data(), data() + DIM, for (int i = 0; i < DIM; ++i) {
_rhs.data(), data(), data()[i] -= _rhs.data()[i];
[](const Scalar &l, const OtherScalar &r) { return l - r; }); }
return *this; return *this;
} }
@@ -311,9 +311,9 @@ class VectorT {
typename std::enable_if< typename std::enable_if<
sizeof(decltype(this->values_[0] + *_rhs.data())) >= 0, sizeof(decltype(this->values_[0] + *_rhs.data())) >= 0,
vector_type&>::type { vector_type&>::type {
std::transform(data(), data() + DIM, for (int i = 0; i < DIM; ++i) {
_rhs.data(), data(), data()[i] += _rhs.data()[i];
[](const Scalar &l, const OtherScalar &r) { return l + r; }); }
return *this; return *this;
} }