summaryrefslogtreecommitdiffstats
path: root/vector
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--vector/vector.cpp24
1 files changed, 11 insertions, 13 deletions
diff --git a/vector/vector.cpp b/vector/vector.cpp
index 8057cdd..260f08f 100644
--- a/vector/vector.cpp
+++ b/vector/vector.cpp
@@ -5,6 +5,7 @@
#include <array>
#include <algorithm>
+#include <numeric>
#include <initializer_list>
@@ -61,10 +62,12 @@ T basic_vec<T, d>::length() const {
template<typename T, std::size_t d>
basic_vec<T, d> operator+(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs) {
basic_vec<T, d> out;
- auto rit = rhs.cbegin(), lit = lhs.cbegin();
- std::generate(out.begin(), out.end(), [rit, lit] () mutable {
- return *(rit++) + *(lit++);
- });
+
+ std::transform(rhs.begin(), rhs.end(), lhs.begin(), out.begin(),
+ [](const T& r, const T& l) -> T {
+ return r + l;
+ }
+ );
return out;
}
@@ -72,10 +75,10 @@ basic_vec<T, d> operator+(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs
template<typename T, std::size_t d>
basic_vec<T, d> operator*(const T& rhs, const basic_vec<T, d>& lhs) {
basic_vec<T, d> out;
- auto lit = lhs.cbegin();
- std::generate(out.begin(), out.end(), [rhs, lit] () mutable {
- return rhs * *(lit++);
+ std::transform(lhs.begin(), lhs.end(), out.begin(),
+ [rhs](const T& t) -> T {
+ return t * rhs;
});
return out;
@@ -88,12 +91,7 @@ basic_vec<T, d> operator-(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs
template<typename T, std::size_t d>
T operator*(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs) {
- T res = basic_vec<T, d>::null_element;
- for (std::size_t i = 0; i < d; i++) {
- res += rhs[i] * lhs[i];
- }
-
- return res;
+ return std::inner_product(rhs.begin(), rhs.end(), lhs.begin(), 0);
}
template<typename T, std::size_t d>