diff options
Diffstat (limited to 'src/Math/Vector.hpp')
| -rw-r--r-- | src/Math/Vector.hpp | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/Math/Vector.hpp b/src/Math/Vector.hpp index 2d87c4b..f6bdd7a 100644 --- a/src/Math/Vector.hpp +++ b/src/Math/Vector.hpp @@ -3,7 +3,6 @@ #include <cstddef> #include <sstream> #include <cmath> -#include <functional> template <size_t S, typename T = float> struct Vector { @@ -30,7 +29,8 @@ struct Vector { std::copy(vector.elements, vector.elements + S, elements); } - Vector map(std::function<T(T)> f) const { + template<typename F> + Vector map(F f) const { Vector result{}; for (int i = 0; i < S; i++) { result[i] = f(elements[i]); @@ -38,7 +38,8 @@ struct Vector { return result; } - Vector map(std::function<T(size_t, T)> f) const { + template<typename F> + Vector map_indexed(F f) const { Vector result{}; for (int i = 0; i < S; i++) { result[i] = f(i, elements[i]); @@ -46,7 +47,8 @@ struct Vector { return result; } - T reduce(std::function<T(T, T)> f) const { + template<typename F> + T reduce(F f) const { T result = elements[0]; for (int i = 1; i < S; i++) { result = f(result, elements[i]); @@ -99,7 +101,7 @@ struct Vector { } Vector operator+(const Vector other) const { - return map([&](auto i, auto x) { return x + other[i]; }); + return map_indexed([&](auto i, auto x) { return x + other[i]; }); } Vector operator+(T scalar) const { @@ -111,11 +113,11 @@ struct Vector { } T operator*(const Vector other) const { - return map([&](auto i, auto x) { return x * other[i]; }).sum(); + return map_indexed([&](auto i, auto x) { return x * other[i]; }).sum(); } Vector operator-(const Vector other) const { - return map([&](auto i, auto x) { return x - other[i]; }); + return map_indexed([&](auto i, auto x) { return x - other[i]; }); } Vector operator-() const { |
