matrix: use begin/end to define most operators

This commit is contained in:
Danny Robson 2016-08-15 17:45:45 +10:00
parent 18915b6610
commit 980018656b
2 changed files with 14 additions and 32 deletions

View File

@ -190,14 +190,10 @@ matrix<S,T>::operator* (const point<S,T> &rhs) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
matrix<S,T> matrix<S,T>
matrix<S,T>::operator* (T f) const matrix<S,T>::operator* (T t) const
{ {
matrix<S,T> out; matrix<S,T> out;
std::transform (cbegin (), cend (), std::begin (out), [t] (auto x) { return x * t; });
for (size_t i = 0; i < S; ++i)
for (size_t j = 0; j < S; ++j)
out.values[i][j] = values[i][j] * f;
return out; return out;
} }
@ -205,12 +201,9 @@ matrix<S,T>::operator* (T f) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
matrix<S,T>& matrix<S,T>&
matrix<S,T>::operator*= (T f) matrix<S,T>::operator*= (T t)
{ {
for (size_t i = 0; i < S; ++i) std::transform (cbegin (), cend (), begin (), [t] (auto x) { return x * t; });
for (size_t j = 0; j < S; ++j)
values[i][j] *= f;
return *this; return *this;
} }
@ -220,12 +213,8 @@ template <size_t S, typename T>
util::matrix<S,T> util::matrix<S,T>
util::matrix<S,T>::operator/ (T t) const util::matrix<S,T>::operator/ (T t) const
{ {
auto out = *this; matrix<S,T> out;
std::transform (cbegin (), cend (), std::begin (out), [t] (auto x) { return x / t; });
for (auto &i: out.values)
for (auto &j: i)
j /= t;
return out; return out;
} }
@ -233,12 +222,9 @@ util::matrix<S,T>::operator/ (T t) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
matrix<S,T>& matrix<S,T>&
matrix<S,T>::operator/= (T s) matrix<S,T>::operator/= (T t)
{ {
for (size_t r = 0; r < rows; ++r) std::transform (cbegin (), cend (), begin (), [t] (auto x) { return x / t; });
for (size_t c = 0; c < cols; ++c)
values[r][c] /= s;
return *this; return *this;
} }
@ -248,11 +234,8 @@ template <size_t S, typename T>
bool bool
matrix<S,T>::operator== (const matrix<S,T> &rhs) const matrix<S,T>::operator== (const matrix<S,T> &rhs) const
{ {
for (size_t r = 0; r < rows; ++r) constexpr bool (*comparator)(const T&,const T&) = util::almost_equal;
for (size_t c = 0; c < cols; ++c) return std::equal (cbegin (), cend (), std::cbegin (rhs), comparator);
if (!almost_equal (rhs.values[r][c], values[r][c]))
return false;
return true;
} }

View File

@ -101,15 +101,14 @@ util::matrix<S,U>
util::matrix<S,T>::cast (void) const util::matrix<S,T>::cast (void) const
{ {
util::matrix<S,U> out; util::matrix<S,U> out;
std::copy (cbegin (), cend (), std::begin (out));
for (size_t i = 0; i < rows; ++i)
for (size_t j = 0; j < cols; ++j)
out.values[i][j] = values[i][j];
return out; return out;
} }
}
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
//template <size_t S, typename T> //template <size_t S, typename T>
//T //T