Nexus Engine  v0.0.1
Loading...
Searching...
No Matches
Vec.inl
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5namespace Nexus {
6
7 template <Numeric T, usize N>
8 template <typename... Args>
9 requires(sizeof...(Args) == N) && (std::convertible_to<Args, T> && ...)
10 constexpr Vec<T, N>::Vec(Args&&... args) : m_data{static_cast<T>(std::forward<Args>(args))...} {}
11
12 template <Numeric T, usize N>
13 constexpr T& Vec<T, N>::operator[](size_type index) {
14 return m_data[index];
15 }
16
17 template <Numeric T, usize N>
18 constexpr const T& Vec<T, N>::operator[](size_type index) const {
19 return m_data[index];
20 }
21
22 template <Numeric T, usize N>
23 constexpr T* Vec<T, N>::Data() {
24 return m_data.data();
25 }
26
27 template <Numeric T, usize N>
28 constexpr const T* Vec<T, N>::Data() const {
29 return m_data.data();
30 }
31
32 template <Numeric T, usize N>
33 constexpr usize Vec<T, N>::Size() {
34 return N;
35 }
36
37 template <Numeric T, usize N>
38 template <Numeric U>
39 constexpr bool Vec<T, N>::operator==(const Vec<U, N>& other) const {
40 for (size_type i = 0; i < N; ++i) {
41 if ((*this)[i] != other[i]) {
42 return false;
43 }
44 }
45
46 return true;
47 }
48
49 template <Numeric T, usize N>
50 template <Numeric U>
51 constexpr Vec<T, N>& Vec<T, N>::operator+=(const Vec<U, N>& other) {
52 for (size_type i = 0; i < N; ++i) {
53 (*this)[i] += other[i];
54 }
55
56 return *this;
57 }
58
59 template <Numeric T, usize N>
60 template <Numeric U>
61 constexpr Vec<T, N>& Vec<T, N>::operator-=(const Vec<U, N>& other) {
62 for (size_type i = 0; i < N; ++i) {
63 (*this)[i] -= other[i];
64 }
65
66 return *this;
67 }
68
69 template <Numeric T, usize N>
70 template <Numeric U>
71 constexpr Vec<T, N>& Vec<T, N>::operator*=(const Vec<U, N>& other) {
72 for (size_type i = 0; i < N; ++i) {
73 (*this)[i] *= other[i];
74 }
75
76 return *this;
77 }
78
79 template <Numeric T, usize N>
80 template <Numeric U>
81 constexpr Vec<T, N>& Vec<T, N>::operator/=(const Vec<U, N>& other) {
82 for (size_type i = 0; i < N; ++i) {
83 (*this)[i] /= other[i];
84 }
85
86 return *this;
87 }
88
89 template <Numeric T, usize N>
90 template <Numeric U>
91 constexpr Vec<T, N>& Vec<T, N>::operator*=(U scalar) {
92 for (auto& value : m_data) {
93 value *= scalar;
94 }
95
96 return *this;
97 }
98
99 template <Numeric T, usize N>
100 template <Numeric U>
101 constexpr Vec<T, N>& Vec<T, N>::operator/=(U scalar) {
102 for (auto& value : m_data) {
103 value /= scalar;
104 }
105
106 return *this;
107 }
108
109 template <Numeric T, usize N>
110 constexpr Vec<T, N> Vec<T, N>::operator+() const {
111 return *this;
112 }
113
114 template <Numeric T, usize N>
115 constexpr Vec<T, N> Vec<T, N>::operator-() const {
116 Vec result;
117
118 for (size_type i = 0; i < N; ++i) {
119 result[i] = -(*this)[i];
120 }
121
122 return result;
123 }
124
125 template <Numeric T, Numeric U, usize N>
126 constexpr auto operator+(const Vec<T, N>& a, const Vec<U, N>& b) -> VecCommon<T, U, N> {
127 using R = std::common_type_t<T, U>;
128
129 VecCommon<T, U, N> result;
130
131 for (usize i = 0; i < N; ++i) {
132 result[i] = static_cast<R>(a[i]) + static_cast<R>(b[i]);
133 }
134
135 return result;
136 }
137
138 template <Numeric T, Numeric U, usize N>
139 constexpr auto operator-(const Vec<T, N>& a, const Vec<U, N>& b) -> VecCommon<T, U, N> {
140 using R = std::common_type_t<T, U>;
141
142 VecCommon<T, U, N> result;
143
144 for (usize i = 0; i < N; ++i) {
145 result[i] = static_cast<R>(a[i]) - static_cast<R>(b[i]);
146 }
147
148 return result;
149 }
150
151 template <Numeric T, Numeric U, usize N>
152 constexpr auto operator*(const Vec<T, N>& a, const Vec<U, N>& b) -> VecCommon<T, U, N> {
153 using R = std::common_type_t<T, U>;
154
155 VecCommon<T, U, N> result;
156
157 for (usize i = 0; i < N; ++i) {
158 result[i] = static_cast<R>(a[i]) * static_cast<R>(b[i]);
159 }
160
161 return result;
162 }
163
164 template <Numeric T, Numeric U, usize N>
165 constexpr auto operator/(const Vec<T, N>& a, const Vec<U, N>& b) -> VecCommon<T, U, N> {
166 using R = std::common_type_t<T, U>;
167
168 VecCommon<T, U, N> result;
169
170 for (usize i = 0; i < N; ++i) {
171 result[i] = static_cast<R>(a[i]) / static_cast<R>(b[i]);
172 }
173
174 return result;
175 }
176
177 template <Numeric T, Numeric U, usize N>
178 constexpr auto operator*(const Vec<T, N>& v, U scalar) -> VecCommon<T, U, N> {
179 using R = std::common_type_t<T, U>;
180
181 VecCommon<T, U, N> result;
182
183 for (usize i = 0; i < N; ++i) {
184 result[i] = static_cast<R>(v[i]) * static_cast<R>(scalar);
185 }
186
187 return result;
188 }
189
190 template <Numeric T, Numeric U, usize N>
191 constexpr auto operator*(U scalar, const Vec<T, N>& v) -> VecCommon<T, U, N> {
192 return v * scalar;
193 }
194
195 template <Numeric T, Numeric U, usize N>
196 constexpr auto operator/(const Vec<T, N>& v, U scalar) -> VecCommon<T, U, N> {
197 using R = std::common_type_t<T, U>;
198
199 VecCommon<T, U, N> result;
200
201 for (usize i = 0; i < N; ++i) {
202 result[i] = static_cast<R>(v[i]) / static_cast<R>(scalar);
203 }
204
205 return result;
206 }
207
208} // namespace Nexus
209
210template <Nexus::Numeric T, Nexus::usize N>
211struct std::formatter<Nexus::Vec<T, N>> {
212 std::formatter<T> underlying;
213
214 constexpr auto parse(std::format_parse_context& ctx) {
215 return underlying.parse(ctx);
216 }
217
218 auto format(const Nexus::Vec<T, N>& obj, std::format_context& ctx) const {
219 auto out = ctx.out();
220 out = std::format_to(out, "(");
221
222 for (Nexus::usize i = 0; i < N; ++i) {
223 if (i != 0) {
224 out = std::format_to(out, ", ");
225 }
226 out = underlying.format(obj[i], ctx);
227 }
228
229 return std::format_to(out, ")");
230 }
231};
Definition Vec.cppm:17
constexpr Vec operator+() const
Definition Vec.inl:110
static constexpr size_type Size()
Definition Vec.inl:33
constexpr Vec operator-() const
Definition Vec.inl:115
constexpr T * Data()
Definition Vec.inl:23
constexpr Vec & operator+=(const Vec< U, N > &other)
constexpr T & operator[](size_type index)
Definition Vec.inl:13
usize size_type
Definition Vec.cppm:20
constexpr Vec & operator-=(const Vec< U, N > &other)
constexpr Vec & operator*=(const Vec< U, N > &other)
constexpr Vec & operator/=(const Vec< U, N > &other)
constexpr bool operator==(const Vec< U, N > &other) const
Definition Vec.inl:39
constexpr Vec()=default
Definition Config.cppm:11
constexpr auto operator+(const Mat< T, RowCount, ColCount, Layout > &a, const Mat< U, RowCount, ColCount, Layout > &b) -> MatCommon< T, U, RowCount, ColCount, Layout >
Definition Mat.inl:289
std::size_t usize
Definition Types.cppm:25
constexpr auto operator*(const Mat< T, RowCount, ColCount, Layout > &mat, U scalar) -> MatCommon< T, U, RowCount, ColCount, Layout >
Definition Mat.inl:321
constexpr auto operator/(const Mat< T, RowCount, ColCount, Layout > &mat, U scalar) -> MatCommon< T, U, RowCount, ColCount, Layout >
Definition Mat.inl:343
constexpr auto operator-(const Mat< T, RowCount, ColCount, Layout > &a, const Mat< U, RowCount, ColCount, Layout > &b) -> MatCommon< T, U, RowCount, ColCount, Layout >
Definition Mat.inl:305
Vec< std::common_type_t< T, U >, N > VecCommon
Definition Vec.cppm:66
constexpr auto parse(std::format_parse_context &ctx)
Definition Vec.inl:214
std::formatter< T > underlying
Definition Vec.inl:212
auto format(const Nexus::Vec< T, N > &obj, std::format_context &ctx) const
Definition Vec.inl:218