mal-math
C++20 mathematics library.
All Classes Namespaces Files Functions Variables Typedefs Concepts
vecn.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "vec.hpp"
12namespace mal_math
13{
20 template <size_t L, Primitive T>
21 struct vec
22 {
23 static_assert(!std::is_const_v<T>);
25 using type = T;
27 static constexpr size_t size = L;
29 constexpr vec() = default;
36 template <Primitive U>
37 explicit constexpr vec(U value);
44 template <typename... U>
45 explicit constexpr vec(U... data);
46
52 explicit constexpr vec(std::array<T, size> const &arr) : data{ arr } {}
58 explicit constexpr vec(std::array<T, size> &&arr) : data{ std::move(arr) } {}
59
61 constexpr void reset() noexcept;
62
64 template <typename U>
65 constexpr vec<size, T> &operator=(vec<size, U> const &b)
66 {
67 for (int i = 0; i < size; i++)
68 {
69 data[i] = b.data[i];
70 }
71 return *this;
72 }
73
74 template <Primitive U>
75 constexpr vec<L, T> &operator+=(U const value) noexcept;
76 template <Primitive U>
77 constexpr vec<L, T> &operator-=(U const value) noexcept;
78 template <Primitive U>
79 constexpr vec<L, T> &operator*=(U const value) noexcept;
80 template <Primitive U>
81 constexpr vec<L, T> &operator/=(U const value) noexcept;
82 template <Primitive U>
83 constexpr vec<L, T> &operator%=(U const value) noexcept;
84 template <AnyVec U>
85 constexpr vec<L, T> &operator+=(U const &other) noexcept requires(size == U::size);
86 template <AnyVec U>
87 constexpr vec<L, T> &operator-=(U const &other) noexcept requires(size == U::size);
88 template <AnyVec U>
89 constexpr vec<L, T> &operator*=(U const &other) noexcept requires(size == U::size);
90 template <AnyVec U>
91 constexpr vec<L, T> &operator/=(U const &other) noexcept requires(size == U::size);
92 template <AnyVec U>
93 constexpr vec<L, T> &operator%=(U const &other) noexcept requires(size == U::size);
94
96 [[nodiscard]] constexpr T &operator[](size_t i);
98 [[nodiscard]] constexpr T const &operator[](size_t i) const;
99
101 std::array<T, size> data;
102
103 private:
109 template <Primitive _>
110 static constexpr size_t get_parameter_pack_size();
116 template <class V>
117 static constexpr size_t get_parameter_pack_size();
123 template <typename A, typename B, typename... C>
124 static constexpr size_t get_parameter_pack_size();
125
126
133 template <Primitive U>
134 constexpr void unpack_data(size_t offset, U u)
135 {
136 data[offset] = static_cast<T>(u);
137 }
144 template <class V>
145 constexpr void unpack_data(size_t offset, V vec)
146 {
147 for (size_t i = 0; i < V::size; i++)
148 {
149 data[offset + i] = static_cast<T>(vec[i]);
150 }
151 }
160 template <typename A, typename B, typename... C>
161 constexpr void unpack_data(size_t offset, A a, B b, C... c)
162 {
163 unpack_data(offset, a);
164 offset += get_parameter_pack_size<A>();
165 unpack_data(offset, b, c...);
166 }
167 };
168}; // namespace mal_math
169#include "vecn.inl"
Contains mathematical utility functions and classes.
STL namespace.
Definition of the mathematical vector with fixed size L and type T
Definition vecn.hpp:22
constexpr T & operator[](size_t i)
Overloaded subscript operator for non-const rvec.
Definition vecn.inl:133
constexpr vec(std::array< T, size > const &arr)
Constructor from std::array.
Definition vecn.hpp:52
constexpr vec< L, T > & operator%=(U const value) noexcept
Definition vecn.inl:73
std::array< T, size > data
The underlying data of the vector.
Definition vecn.hpp:101
T type
Type of the vector elements.
Definition vecn.hpp:25
static constexpr size_t get_parameter_pack_size()
Determines the size of a parameter pack for primitive types.
Definition vecn.inl:146
constexpr vec< L, T > & operator*=(U const value) noexcept
Definition vecn.inl:53
constexpr void unpack_data(size_t offset, U u)
Unpacks a single primitive value into the vector data.
Definition vecn.hpp:134
static constexpr size_t size
Number of elements in the vector.
Definition vecn.hpp:27
constexpr void unpack_data(size_t offset, V vec)
Unpacks a vector into the matrix data.
Definition vecn.hpp:145
constexpr vec< L, T > & operator/=(U const value) noexcept
Definition vecn.inl:63
static constexpr size_t get_parameter_pack_size()
Determines the size of a parameter pack for vector types.
constexpr vec()=default
Default constructor.
constexpr vec(std::array< T, size > &&arr)
Move constructor from std::array.
Definition vecn.hpp:58
constexpr vec< L, T > & operator+=(U const value) noexcept
Definition vecn.inl:33
constexpr vec< L, T > & operator-=(U const value) noexcept
Definition vecn.inl:43
constexpr void unpack_data(size_t offset, A a, B b, C... c)
Recursively unpacks multiple values into the vector data.
Definition vecn.hpp:161
constexpr void reset() noexcept
Resets the vector to the zero vector.
Definition vecn.inl:23