summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/mm.hpp11
-rw-r--r--include/mm/mmvec.hpp (renamed from include/mmvec.hpp)34
-rw-r--r--ninja/rules.ninja2
3 files changed, 40 insertions, 7 deletions
diff --git a/include/mm.hpp b/include/mm.hpp
new file mode 100644
index 0000000..7e9f02b
--- /dev/null
+++ b/include/mm.hpp
@@ -0,0 +1,11 @@
+#pragma once
+
+/* MiniMath
+ * A mathematical vector library that (ab)uses modern C++ abstraction features.
+ * This header includes the entire library.
+ *
+ * Warning: having (ab)used a lot of templated code it is not recommended to
+ * include everything as it may slow down the compiler.
+ */
+
+#include "mm/mmvec.hpp"
diff --git a/include/mmvec.hpp b/include/mm/mmvec.hpp
index 4bac658..db3c390 100644
--- a/include/mmvec.hpp
+++ b/include/mm/mmvec.hpp
@@ -51,13 +51,22 @@ struct mm::basic_vec : public std::array<T, d> {
basic_vec();
basic_vec(const std::initializer_list<T> l);
+
+ // copyable to a vector of size n <= d
template<std::size_t n> basic_vec(const basic_vec<T, n>& other);
+ // movable to a vector of size n <= d
+ template<std::size_t n> basic_vec(basic_vec<T, n>&& other);
T length() const;
+ // copy operator=
template<std::size_t n>
basic_vec<T, d>& operator=(const mm::basic_vec<T, n>& other);
+ // move operator=
+ template<std::size_t n>
+ basic_vec<T, d>& operator=(mm::basic_vec<T, n>&& other);
+
template<std::size_t n>
basic_vec<T, d>& operator+=(const mm::basic_vec<T, n>& other);
@@ -78,9 +87,6 @@ mm::basic_vec<T, d>::basic_vec() : std::array<T, d>() {
template<typename T, std::size_t d>
mm::basic_vec<T, d>::basic_vec(const std::initializer_list<T> l) {
- // construct with empty values
- basic_vec();
-
// why can't this sh*t be a constexpr with static_assert???
assert(l.size() <= d);
std::copy(l.begin(), l.end(), this->begin());
@@ -89,9 +95,12 @@ mm::basic_vec<T, d>::basic_vec(const std::initializer_list<T> l) {
template<typename T, std::size_t d>
template<std::size_t n>
mm::basic_vec<T, d>::basic_vec(const mm::basic_vec<T, n>& other) {
- // construct with empty values
- basic_vec();
- // uses operator=
+ *this = other;
+}
+
+template<typename T, std::size_t d>
+template<std::size_t n>
+mm::basic_vec<T, d>::basic_vec(basic_vec<T, n>&& other) {
*this = other;
}
@@ -122,6 +131,19 @@ mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator=(const mm::basic_vec<T, n>& o
template<typename T, std::size_t d>
template<std::size_t n>
+mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator=(mm::basic_vec<T, n>&& other) {
+ static_assert(
+ d >= n, "cannot move a higher dimensional vector into a smaller one"
+ );
+
+ std::move(other.begin(), other.end(), this->begin());
+
+ return *this;
+}
+
+
+template<typename T, std::size_t d>
+template<std::size_t n>
mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator+=(const mm::basic_vec<T, n>& other) {
*this = *this + other;
return *this;
diff --git a/ninja/rules.ninja b/ninja/rules.ninja
index 8ff3e02..a17c01a 100644
--- a/ninja/rules.ninja
+++ b/ninja/rules.ninja
@@ -1,4 +1,4 @@
-includes = -I include
+includes = -I include/mm
cflags = -Wall -Werror -pedantic -fPIC -std=c++17 $includes
libs =