From f8c0fb7e5d4cd0139b2b287980149eca688803bd Mon Sep 17 00:00:00 2001 From: Mel Date: Fri, 7 Jul 2023 23:27:20 +0200 Subject: Replace std::function in Vector with lambda templates --- src/Math/Vector.hpp | 16 +++++++++------- 1 file 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 #include #include -#include template struct Vector { @@ -30,7 +29,8 @@ struct Vector { std::copy(vector.elements, vector.elements + S, elements); } - Vector map(std::function f) const { + template + 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 f) const { + template + 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 f) const { + template + 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 { -- cgit 1.4.1