mal-math
C++20 mathematics library.
All Classes Namespaces Files Functions Variables Typedefs Concepts
vecn.inl
Go to the documentation of this file.
1#pragma once
2#include "vecn.hpp"
3namespace mal_math
4{
5 template <size_t L, Primitive T>
6 template <Primitive U>
7 constexpr vec<L, T>::vec(U value)
8 {
9 for (int i = 0; i < size; i++)
10 {
11 data[i] = static_cast<T>(value);
12 }
13 }
14 template <size_t L, Primitive T>
15 template <typename... U>
16 constexpr vec<L, T>::vec(U... data)
17 {
18 static_assert(get_parameter_pack_size<U...>() == size,
19 "You have provided wrong amount of data");
20 unpack_data(0, data...);
21 }
22 template <size_t L, Primitive T>
23 constexpr void vec<L, T>::reset() noexcept
24 {
25 for (int i = 0; i < size; i++)
26 {
27 data[i] = 0;
28 }
29 }
30
31 template <size_t L, Primitive T>
32 template <Primitive U>
33 constexpr vec<L, T> &vec<L, T>::operator+=(U const value) noexcept
34 {
35 for (int i = 0; i < size; i++)
36 {
37 data[i] = static_cast<T>(data[i] + value);
38 }
39 return *this;
40 }
41 template <size_t L, Primitive T>
42 template <Primitive U>
43 constexpr vec<L, T> &vec<L, T>::operator-=(U const value) noexcept
44 {
45 for (int i = 0; i < size; i++)
46 {
47 data[i] = static_cast<T>(data[i] - value);
48 }
49 return *this;
50 }
51 template <size_t L, Primitive T>
52 template <Primitive U>
53 constexpr vec<L, T> &vec<L, T>::operator*=(U const value) noexcept
54 {
55 for (int i = 0; i < size; i++)
56 {
57 data[i] = static_cast<T>(data[i] * value);
58 }
59 return *this;
60 }
61 template <size_t L, Primitive T>
62 template <Primitive U>
63 constexpr vec<L, T> &vec<L, T>::operator/=(U const value) noexcept
64 {
65 for (int i = 0; i < size; i++)
66 {
67 data[i] = static_cast<T>(data[i] / value);
68 }
69 return *this;
70 }
71 template <size_t L, Primitive T>
72 template <Primitive U>
73 constexpr vec<L, T> &vec<L, T>::operator%=(U const value) noexcept
74 {
75 for (int i = 0; i < size; i++)
76 {
77 data[i] = static_cast<T>(data[i] % value);
78 }
79 return *this;
80 }
81 template <size_t L, Primitive T>
82 template <AnyVec U>
83 constexpr vec<L, T> &vec<L, T>::operator+=(U const &other) noexcept requires(size == U::size)
84 {
85 for (int i = 0; i < size; i++)
86 {
87 data[i] = static_cast<T>(data[i] + other.data[i]);
88 }
89 return *this;
90 }
91 template <size_t L, Primitive T>
92 template <AnyVec U>
93 constexpr vec<L, T> &vec<L, T>::operator-=(U const &other) noexcept requires(size == U::size)
94 {
95 for (int i = 0; i < size; i++)
96 {
97 data[i] = static_cast<T>(data[i] - other.data[i]);
98 }
99 return *this;
100 }
101 template <size_t L, Primitive T>
102 template <AnyVec U>
103 constexpr vec<L, T> &vec<L, T>::operator*=(U const &other) noexcept requires(size == U::size)
104 {
105 for (int i = 0; i < size; i++)
106 {
107 data[i] = static_cast<T>(data[i] * other.data[i]);
108 }
109 return *this;
111 template <size_t L, Primitive T>
112 template <AnyVec U>
113 constexpr vec<L, T> &vec<L, T>::operator/=(U const &other) noexcept requires(size == U::size)
114 {
115 for (int i = 0; i < size; i++)
116 {
117 data[i] = static_cast<T>(data[i] / other.data[i]);
118 }
119 return *this;
120 }
121 template <size_t L, Primitive T>
122 template <AnyVec U>
123 constexpr vec<L, T> &vec<L, T>::operator%=(U const &other) noexcept requires(size == U::size)
125 for (int i = 0; i < size; i++)
126 {
127 data[i] = static_cast<T>(data[i] % other.data[i]);
128 }
129 return *this;
130 }
131
132 template <size_t L, Primitive T>
133 [[nodiscard]] constexpr T &vec<L, T>::operator[](size_t i)
134 {
135 assert(i < size);
136 return data[i];
137 }
138 template <size_t L, Primitive T>
139 [[nodiscard]] constexpr T const &vec<L, T>::operator[](size_t i) const
140 {
141 assert(i < size);
142 return data[i];
143 }
144 template <size_t L, Primitive T>
145 template <Primitive _> // primitives
147 {
148 return 1;
149 }
150 template <size_t L, Primitive T>
151 template <class V> // vectors
152 constexpr size_t vec<L, T>::get_parameter_pack_size()
153 {
154 return V::size;
155 }
156 template <size_t L, Primitive T>
157 template <typename A, typename B, typename... C>
159 {
160 return get_parameter_pack_size<A>() + get_parameter_pack_size<B, C...>();
161 }
162}; // namespace mal_math
Contains mathematical utility functions and classes.
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< L, T > & operator%=(U const value) noexcept
Definition vecn.inl:73
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 vec< L, T > & operator/=(U const value) noexcept
Definition vecn.inl:63
constexpr vec()=default
Default constructor.
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 reset() noexcept
Resets the vector to the zero vector.
Definition vecn.inl:23
Contains the definition of the generic vec<L, T> structure.