summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2023-07-07 23:27:20 +0200
committerMel <einebeere@gmail.com>2023-07-07 23:27:20 +0200
commitf8c0fb7e5d4cd0139b2b287980149eca688803bd (patch)
treee94032094e91e02fb24b3507755a7737bfc1eaad /src
parentf16a75e299e466073a3886aa3a4d0371b523ad04 (diff)
downloadmeowcraft-f8c0fb7e5d4cd0139b2b287980149eca688803bd.tar.zst
meowcraft-f8c0fb7e5d4cd0139b2b287980149eca688803bd.zip
Replace std::function in Vector with lambda templates
Diffstat (limited to 'src')
-rw-r--r--src/Math/Vector.hpp16
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 {