Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
sarray_soa_impl.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <cstddef>
5#include <new>
6#include <tuple>
7#include <type_traits>
8#include <utility>
9
10#include "gaia/core/iterator.h"
11#include "gaia/core/utility.h"
12#include "gaia/mem/data_layout_policy.h"
13#include "gaia/mem/mem_utils.h"
14#include "gaia/mem/raw_data_holder.h"
15
16namespace gaia {
17 namespace cnt {
18 namespace sarr_soa_detail {
19 using difference_type = uint32_t;
20 using size_type = uint32_t;
21 } // namespace sarr_soa_detail
22
23 template <typename T>
25 using value_type = T;
26 // using pointer = T*; not supported
27 // using reference = T&; not supported
30
33
34 private:
35 uint8_t* m_ptr;
36 uint32_t m_cnt;
37 uint32_t m_idx;
38
39 public:
40 sarr_soa_iterator(uint8_t* ptr, uint32_t cnt, uint32_t idx): m_ptr(ptr), m_cnt(cnt), m_idx(idx) {}
41
42 T operator*() const {
43 return mem::data_view_policy<T::gaia_Data_Layout, T>::get({m_ptr, m_cnt}, m_idx);
44 }
45 T operator->() const {
46 return mem::data_view_policy<T::gaia_Data_Layout, T>::get({m_ptr, m_cnt}, m_idx);
47 }
48 iterator operator[](size_type offset) const {
49 return iterator(m_ptr, m_cnt, m_idx + offset);
50 }
51
52 iterator& operator+=(size_type diff) {
53 m_idx += diff;
54 return *this;
55 }
56 iterator& operator-=(size_type diff) {
57 m_idx -= diff;
58 return *this;
59 }
60 iterator& operator++() {
61 ++m_idx;
62 return *this;
63 }
64 iterator operator++(int) {
65 iterator temp(*this);
66 ++*this;
67 return temp;
68 }
69 iterator& operator--() {
70 --m_idx;
71 return *this;
72 }
73 iterator operator--(int) {
74 iterator temp(*this);
75 --*this;
76 return temp;
77 }
78
79 iterator operator+(size_type offset) const {
80 return iterator(m_ptr, m_cnt, m_idx + offset);
81 }
82 iterator operator-(size_type offset) const {
83 return iterator(m_ptr, m_cnt, m_idx + offset);
84 }
85 difference_type operator-(const iterator& other) const {
86 GAIA_ASSERT(m_ptr == other.m_ptr);
87 return (difference_type)(m_idx - other.m_idx);
88 }
89
90 GAIA_NODISCARD bool operator==(const iterator& other) const {
91 GAIA_ASSERT(m_ptr == other.m_ptr);
92 return m_idx == other.m_idx;
93 }
94 GAIA_NODISCARD bool operator!=(const iterator& other) const {
95 GAIA_ASSERT(m_ptr == other.m_ptr);
96 return m_idx != other.m_idx;
97 }
98 GAIA_NODISCARD bool operator>(const iterator& other) const {
99 GAIA_ASSERT(m_ptr == other.m_ptr);
100 return m_idx > other.m_idx;
101 }
102 GAIA_NODISCARD bool operator>=(const iterator& other) const {
103 GAIA_ASSERT(m_ptr == other.m_ptr);
104 return m_idx >= other.m_idx;
105 }
106 GAIA_NODISCARD bool operator<(const iterator& other) const {
107 GAIA_ASSERT(m_ptr == other.m_ptr);
108 return m_idx < other.m_idx;
109 }
110 GAIA_NODISCARD bool operator<=(const iterator& other) const {
111 GAIA_ASSERT(m_ptr == other.m_ptr);
112 return m_idx <= other.m_idx;
113 }
114 };
115
116 template <typename T>
118 using value_type = T;
119 // using pointer = T*; not supported
120 // using reference = T&; not supported
123
126
127 private:
128 const uint8_t* m_ptr;
129 uint32_t m_cnt;
130 uint32_t m_idx;
131
132 public:
133 const_sarr_soa_iterator(const uint8_t* ptr, uint32_t cnt, uint32_t idx): m_ptr(ptr), m_cnt(cnt), m_idx(idx) {}
134
135 T operator*() const {
136 return mem::data_view_policy<T::gaia_Data_Layout, T>::get({m_ptr, m_cnt}, m_idx);
137 }
138 T operator->() const {
139 return mem::data_view_policy<T::gaia_Data_Layout, T>::get({m_ptr, m_cnt}, m_idx);
140 }
141 iterator operator[](size_type offset) const {
142 return iterator(m_ptr, m_cnt, m_idx + offset);
143 }
144
145 iterator& operator+=(size_type diff) {
146 m_idx += diff;
147 return *this;
148 }
149 iterator& operator-=(size_type diff) {
150 m_idx -= diff;
151 return *this;
152 }
153 iterator& operator++() {
154 ++m_idx;
155 return *this;
156 }
157 iterator operator++(int) {
158 iterator temp(*this);
159 ++*this;
160 return temp;
161 }
162 iterator& operator--() {
163 --m_idx;
164 return *this;
165 }
166 iterator operator--(int) {
167 iterator temp(*this);
168 --*this;
169 return temp;
170 }
171
172 iterator operator+(size_type offset) const {
173 return iterator(m_ptr, m_cnt, m_idx + offset);
174 }
175 iterator operator-(size_type offset) const {
176 return iterator(m_ptr, m_cnt, m_idx + offset);
177 }
178 difference_type operator-(const iterator& other) const {
179 GAIA_ASSERT(m_ptr == other.m_ptr);
180 return (difference_type)(m_idx - other.m_idx);
181 }
182
183 GAIA_NODISCARD bool operator==(const iterator& other) const {
184 GAIA_ASSERT(m_ptr == other.m_ptr);
185 return m_idx == other.m_idx;
186 }
187 GAIA_NODISCARD bool operator!=(const iterator& other) const {
188 GAIA_ASSERT(m_ptr == other.m_ptr);
189 return m_idx != other.m_idx;
190 }
191 GAIA_NODISCARD bool operator>(const iterator& other) const {
192 GAIA_ASSERT(m_ptr == other.m_ptr);
193 return m_idx > other.m_idx;
194 }
195 GAIA_NODISCARD bool operator>=(const iterator& other) const {
196 GAIA_ASSERT(m_ptr == other.m_ptr);
197 return m_idx >= other.m_idx;
198 }
199 GAIA_NODISCARD bool operator<(const iterator& other) const {
200 GAIA_ASSERT(m_ptr == other.m_ptr);
201 return m_idx < other.m_idx;
202 }
203 GAIA_NODISCARD bool operator<=(const iterator& other) const {
204 GAIA_ASSERT(m_ptr == other.m_ptr);
205 return m_idx <= other.m_idx;
206 }
207 };
208
213 template <typename T, sarr_soa_detail::size_type N>
214 class sarr_soa {
215 static_assert(mem::is_soa_layout_v<T>, "sarr_soa can be used only with soa types");
216
217 public:
218 static_assert(N > 0);
219
220 using value_type = T;
221 using reference = T&;
222 using const_reference = const T&;
223 using pointer = T*;
224 using const_pointer = T*;
227 using size_type = sarr_soa_detail::size_type;
228
232
233 static constexpr size_type extent = N;
234 static constexpr uint32_t allocated_bytes = view_policy::get_min_byte_size(0, N);
235
237
238 sarr_soa() noexcept = default;
239
243 // explicit zeroing
244 for (auto i = (size_type)0; i < extent; ++i)
245 operator[](i) = {};
246 }
247
248 ~sarr_soa() = default;
249
250 template <typename InputIt>
251 sarr_soa(InputIt first, InputIt last) noexcept {
252 const auto count = (size_type)core::distance(first, last);
253
254 if constexpr (std::is_pointer_v<InputIt>) {
255 for (size_type i = 0; i < count; ++i)
256 operator[](i) = first[i];
257 } else if constexpr (std::is_same_v<typename InputIt::iterator_category, core::random_access_iterator_tag>) {
258 for (size_type i = 0; i < count; ++i)
259 operator[](i) = *(first[i]);
260 } else {
261 size_type i = 0;
262 for (auto it = first; it != last; ++it)
263 operator[](++i) = *it;
264 }
265 }
266
267 sarr_soa(std::initializer_list<T> il): sarr_soa(il.begin(), il.end()) {}
268
269 sarr_soa(const sarr_soa& other): sarr_soa(other.begin(), other.end()) {}
270
271 sarr_soa(sarr_soa&& other) noexcept {
272 GAIA_ASSERT(core::addressof(other) != this);
273
274 mem::move_elements<T, true>((uint8_t*)m_data, (uint8_t*)other.m_data, other.size(), 0, extent, other.extent);
275 }
276
277 sarr_soa& operator=(std::initializer_list<T> il) {
278 *this = sarr_soa(il.begin(), il.end());
279 return *this;
280 }
281
282 sarr_soa& operator=(const sarr_soa& other) {
283 GAIA_ASSERT(core::addressof(other) != this);
284
285 mem::copy_elements<T, true>(
286 GAIA_ACC((uint8_t*)&m_data[0]), GAIA_ACC((const uint8_t*)&other.m_data[0]), other.size(), 0, extent,
287 other.extent);
288
289 return *this;
290 }
291
292 sarr_soa& operator=(sarr_soa&& other) noexcept {
293 GAIA_ASSERT(core::addressof(other) != this);
294
295 mem::move_elements<T, true>(
296 GAIA_ACC((uint8_t*)&m_data[0]), GAIA_ACC((uint8_t*)&other.m_data[0]), other.size(), 0, extent,
297 other.extent);
298
299 return *this;
300 }
301
302 GAIA_CLANG_WARNING_PUSH()
303 // Memory is aligned so we can silence this warning
304 GAIA_CLANG_WARNING_DISABLE("-Wcast-align")
305
306 GAIA_NODISCARD pointer data() noexcept {
307 return GAIA_ACC((pointer)&m_data[0]);
308 }
309
310 GAIA_NODISCARD const_pointer data() const noexcept {
311 return GAIA_ACC((const_pointer)&m_data[0]);
312 }
313
314 GAIA_NODISCARD decltype(auto) operator[](size_type pos) noexcept {
315 GAIA_ASSERT(pos < size());
316 return view_policy::set({GAIA_ACC((typename view_policy::TargetCastType) & m_data[0]), extent}, pos);
317 }
318
319 GAIA_NODISCARD decltype(auto) operator[](size_type pos) const noexcept {
320 GAIA_ASSERT(pos < size());
321 return view_policy::get({GAIA_ACC((typename view_policy::TargetCastType) & m_data[0]), extent}, pos);
322 }
323
324 GAIA_CLANG_WARNING_POP()
325
326 GAIA_NODISCARD constexpr size_type size() const noexcept {
327 return N;
328 }
329
330 GAIA_NODISCARD constexpr bool empty() const noexcept {
331 return false;
332 }
333
334 GAIA_NODISCARD constexpr size_type capacity() const noexcept {
335 return N;
336 }
337
338 GAIA_NODISCARD constexpr size_type max_size() const noexcept {
339 return N;
340 }
341
342 GAIA_NODISCARD decltype(auto) front() noexcept {
343 return *begin();
344 }
345
346 GAIA_NODISCARD decltype(auto) front() const noexcept {
347 return *begin();
348 }
349
350 GAIA_NODISCARD decltype(auto) back() noexcept {
351 return (operator[])(N - 1);
352 }
353
354 GAIA_NODISCARD decltype(auto) back() const noexcept {
355 return operator[](N - 1);
356 }
357
358 GAIA_NODISCARD auto begin() noexcept {
359 return iterator(GAIA_ACC(&m_data[0]), extent, 0);
360 }
361
362 GAIA_NODISCARD auto begin() const noexcept {
363 return const_iterator(GAIA_ACC(&m_data[0]), extent, 0);
364 }
365
366 GAIA_NODISCARD auto cbegin() const noexcept {
367 return const_iterator(GAIA_ACC(&m_data[0]), extent, 0);
368 }
369
370 GAIA_NODISCARD auto rbegin() noexcept {
371 return iterator(GAIA_ACC(&m_data[0]), extent, size() - 1);
372 }
373
374 GAIA_NODISCARD auto rbegin() const noexcept {
375 return const_iterator(m_data, extent, size() - 1);
376 }
377
378 GAIA_NODISCARD auto crbegin() const noexcept {
379 return const_iterator(m_data, extent, size() - 1);
380 }
381
382 GAIA_NODISCARD auto end() noexcept {
383 return iterator(GAIA_ACC(&m_data[0]), extent, size());
384 }
385
386 GAIA_NODISCARD auto end() const noexcept {
387 return const_iterator(GAIA_ACC(&m_data[0]), extent, size());
388 }
389
390 GAIA_NODISCARD auto cend() const noexcept {
391 return const_iterator(GAIA_ACC(&m_data[0]), extent, size());
392 }
393
394 GAIA_NODISCARD auto rend() noexcept {
395 return iterator(GAIA_ACC(&m_data[0]), extent, -1);
396 }
397
398 GAIA_NODISCARD auto rend() const noexcept {
399 return const_iterator(GAIA_ACC(&m_data[0]), extent, -1);
400 }
401
402 GAIA_NODISCARD auto crend() const noexcept {
403 return const_iterator(GAIA_ACC(&m_data[0]), extent, -1);
404 }
405
406 GAIA_NODISCARD bool operator==(const sarr_soa& other) const {
407 for (size_type i = 0; i < N; ++i)
408 if (!(operator[](i) == other[i]))
409 return false;
410 return true;
411 }
412
413 GAIA_NODISCARD bool operator!=(const sarr_soa& other) const {
414 return !operator==(other);
415 }
416
417 template <size_t Item>
418 auto view_mut() noexcept {
419 return mem::data_view_policy<T::gaia_Data_Layout, T>::template set<Item>(
420 std::span<uint8_t>{GAIA_ACC((uint8_t*)&m_data[0]), extent});
421 }
422
423 template <size_t Item>
424 auto view() const noexcept {
425 return mem::data_view_policy<T::gaia_Data_Layout, T>::template get<Item>(
426 std::span<const uint8_t>{GAIA_ACC((const uint8_t*)&m_data[0]), extent});
427 }
428 };
429
430 namespace detail {
431 template <typename T, uint32_t N, uint32_t... I>
432 sarr_soa<std::remove_cv_t<T>, N> to_array_impl(T (&a)[N], std::index_sequence<I...> /*no_name*/) {
433 return {{a[I]...}};
434 }
435 } // namespace detail
436
437 template <typename T, uint32_t N>
438 sarr_soa<std::remove_cv_t<T>, N> to_array(T (&a)[N]) {
439 return detail::to_array_impl(a, std::make_index_sequence<N>{});
440 }
441
442 template <typename T, typename... U>
443 sarr_soa(T, U...) -> sarr_soa<T, 1 + (uint32_t)sizeof...(U)>;
444
445 } // namespace cnt
446} // namespace gaia
447
448namespace std {
449 template <typename T, uint32_t N>
450 struct tuple_size<gaia::cnt::sarr_soa<T, N>>: std::integral_constant<uint32_t, N> {};
451
452 template <size_t I, typename T, uint32_t N>
453 struct tuple_element<I, gaia::cnt::sarr_soa<T, N>> {
454 using type = T;
455 };
456} // namespace std
Array with variable size of elements of type.
Definition darray_impl.h:25
Fixed-size stack array with SoA storage. Interface compatiblity with std::array where it matters.
Definition sarray_soa_impl.h:214
sarr_soa(core::zero_t) noexcept
Zero-initialization constructor. Because sarr_soa is not aggretate type, doing: sarr_soa<int,...
Definition sarray_soa_impl.h:242
Definition span_impl.h:99
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition sarray_soa_impl.h:117
Definition sarray_soa_impl.h:24
Tag type used to request zero-initialization in APIs that accept marker objects.
Definition utility.h:128
View policy for accessing and storing data in the SoA way. Good for SIMD processing.
Definition data_layout_policy.h:243
Definition data_layout_policy.h:87
Definition raw_data_holder.h:12