Gaia-ECS v1.0.0
A simple and powerful entity component system
Loading...
Searching...
No Matches
sarray_impl.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <cstddef>
5#include <tuple>
6#include <type_traits>
7#include <utility>
8
9#include "gaia/core/iterator.h"
10#include "gaia/core/utility.h"
11#include "gaia/mem/data_layout_policy.h"
12
13namespace gaia {
14 namespace cnt {
16 namespace sarr_detail {
17 using difference_type = int32_t;
18 using size_type = uint32_t;
19 } // namespace sarr_detail
21
27 template <typename T, sarr_detail::size_type N>
28 class sarr {
29 public:
30 static_assert(N > 0);
31
33 using value_type = T;
35 using reference = T&;
37 using const_reference = const T&;
39 using pointer = T*;
41 using const_pointer = const T*;
45 using difference_type = sarr_detail::difference_type;
47 using size_type = sarr_detail::size_type;
48
59
61 static constexpr size_t value_size = sizeof(T);
63 static constexpr size_type extent = N;
64
67
68 constexpr sarr() noexcept = default;
69
73 constexpr sarr(core::zero_t) noexcept {
74 for (auto i = (size_type)0; i < extent; ++i)
75 m_data[i] = {};
76 }
77
78 GAIA_CONSTEXPR_DTOR ~sarr() = default;
79
84 template <typename InputIt>
85 constexpr sarr(InputIt first, InputIt last) noexcept: sarr() {
86 const auto count = (size_type)core::distance(first, last);
87
88 if constexpr (std::is_pointer_v<InputIt>) {
89 for (size_type i = 0; i < count; ++i)
90 operator[](i) = first[i];
91 } else if constexpr (std::is_same_v<typename InputIt::iterator_category, core::random_access_iterator_tag>) {
92 for (size_type i = 0; i < count; ++i)
93 operator[](i) = *(first[i]);
94 } else {
95 size_type i = 0;
96 for (auto it = first; it != last; ++it)
97 operator[](i++) = *it;
98 }
99 }
100
103 constexpr sarr(std::initializer_list<T> il): sarr(il.begin(), il.end()) {}
104
105 constexpr sarr(const sarr&) = default;
106
107 constexpr sarr(sarr&&) noexcept = default;
108
113 *this = sarr(il.begin(), il.end());
114 return *this;
115 }
116
120 constexpr sarr& operator=(const sarr& other) {
121 GAIA_ASSERT(core::addressof(other) != this);
122
123 for (size_type i = 0; i < extent; ++i)
124 m_data[i] = other.m_data[i];
125
126 return *this;
127 }
128
132 constexpr sarr& operator=(sarr&& other) noexcept {
133 GAIA_ASSERT(core::addressof(other) != this);
134
135 for (size_type i = 0; i < extent; ++i)
136 m_data[i] = GAIA_MOV(other.m_data[i]);
137
138 return *this;
139 }
140
141 GAIA_CLANG_WARNING_PUSH()
142 // Memory is aligned so we can silence this warning
143 GAIA_CLANG_WARNING_DISABLE("-Wcast-align")
144
147 GAIA_NODISCARD constexpr pointer data() noexcept {
148 return &m_data[0];
149 }
150
153 GAIA_NODISCARD constexpr const_pointer data() const noexcept {
154 return &m_data[0];
155 }
156
160 GAIA_NODISCARD constexpr decltype(auto) operator[](size_type pos) noexcept {
161 GAIA_ASSERT(pos < size());
162 return m_data[pos];
163 }
164
168 GAIA_NODISCARD constexpr decltype(auto) operator[](size_type pos) const noexcept {
169 GAIA_ASSERT(pos < size());
170 return m_data[pos];
171 }
172
173 GAIA_CLANG_WARNING_POP()
174
175
178 return N;
179 }
180
183 GAIA_NODISCARD constexpr bool empty() const noexcept {
184 return false;
185 }
186
189 GAIA_NODISCARD constexpr size_type capacity() const noexcept {
190 return N;
191 }
192
195 GAIA_NODISCARD constexpr size_type max_size() const noexcept {
196 return N;
197 }
198
201 GAIA_NODISCARD constexpr decltype(auto) front() noexcept {
202 return (reference)*begin();
203 }
204
207 GAIA_NODISCARD constexpr decltype(auto) front() const noexcept {
208 return (const_reference)*begin();
209 }
210
213 GAIA_NODISCARD constexpr decltype(auto) back() noexcept {
214 return (reference) operator[](N - 1);
215 }
216
219 GAIA_NODISCARD constexpr decltype(auto) back() const noexcept {
220 return (const_reference) operator[](N - 1);
221 }
222
225 GAIA_NODISCARD constexpr auto begin() noexcept {
226 return iterator(&m_data[0]);
227 }
228
231 GAIA_NODISCARD constexpr auto begin() const noexcept {
232 return const_iterator(&m_data[0]);
233 }
234
237 GAIA_NODISCARD constexpr auto cbegin() const noexcept {
238 return const_iterator(&m_data[0]);
239 }
240
243 GAIA_NODISCARD constexpr auto rbegin() noexcept {
244 return reverse_iterator(end());
245 }
246
249 GAIA_NODISCARD constexpr auto rbegin() const noexcept {
250 return const_reverse_iterator(end());
251 }
252
255 GAIA_NODISCARD constexpr auto crbegin() const noexcept {
257 }
258
261 GAIA_NODISCARD constexpr auto end() noexcept {
262 return iterator(&m_data[0] + size());
263 }
264
267 GAIA_NODISCARD constexpr auto end() const noexcept {
268 return const_iterator(&m_data[0] + size());
269 }
270
273 GAIA_NODISCARD constexpr auto cend() const noexcept {
274 return const_iterator(&m_data[0] + size());
275 }
276
279 GAIA_NODISCARD constexpr auto rend() noexcept {
280 return reverse_iterator(begin());
281 }
282
285 GAIA_NODISCARD constexpr auto rend() const noexcept {
287 }
288
291 GAIA_NODISCARD constexpr auto crend() const noexcept {
293 }
294
298 GAIA_NODISCARD constexpr bool operator==(const sarr& other) const {
299 for (size_type i = 0; i < N; ++i)
300 if (!(operator[](i) == other[i]))
301 return false;
302 return true;
303 }
304
308 GAIA_NODISCARD constexpr bool operator!=(const sarr& other) const {
309 return !operator==(other);
310 }
311 };
312
314 namespace detail {
315 template <typename T, uint32_t N, uint32_t... I>
316 constexpr sarr<std::remove_cv_t<T>, N> to_array_impl(T (&a)[N], std::index_sequence<I...> /*no_name*/) {
317 return {{a[I]...}};
318 }
319 } // namespace detail
321
327 template <typename T, uint32_t N>
328 constexpr sarr<std::remove_cv_t<T>, N> to_array(T (&a)[N]) {
329 return detail::to_array_impl(a, std::make_index_sequence<N>{});
330 }
331
335 template <typename T, typename... U>
336 sarr(T, U...) -> sarr<T, 1 + (uint32_t)sizeof...(U)>;
337
338 } // namespace cnt
339
340 namespace core {
342 template <typename T, cnt::sarr_detail::size_type N>
343 struct fixed_capacity<cnt::sarr<T, N>>: std::integral_constant<uint32_t, (uint32_t)N> {};
344 } // namespace core
345} // namespace gaia
346
348namespace std {
349 template <typename T, uint32_t N>
350 struct tuple_size<gaia::cnt::sarr<T, N>>: std::integral_constant<uint32_t, N> {};
351
352 template <size_t I, typename T, uint32_t N>
353 struct tuple_element<I, gaia::cnt::sarr<T, N>> {
354 using type = T;
355 };
356} // namespace std
Array of elements of type.
Definition darray_ext_impl.h:31
GAIA_NODISCARD auto begin() noexcept
Returns an iterator to the first element.
Definition darray_ext_impl.h:613
GAIA_NODISCARD auto end() noexcept
Returns an iterator one past the last element.
Definition darray_ext_impl.h:649
darr_ext_detail::size_type size_type
Unsigned type used for sizes and indices.
Definition darray_ext_impl.h:50
darr_ext_detail::difference_type difference_type
Type used for iterator differences.
Definition darray_ext_impl.h:48
Fixed-size stack array with AoS storage. Interface compatiblity with std::array where it matters.
Definition sarray_impl.h:28
GAIA_NODISCARD constexpr decltype(auto) back() noexcept
Accesses the last element.
Definition sarray_impl.h:213
GAIA_NODISCARD constexpr auto rend() const noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition sarray_impl.h:285
constexpr sarr(std::initializer_list< T > il)
Constructs a container from an initializer list.
Definition sarray_impl.h:103
constexpr sarr & operator=(const sarr &other)
Copy-assigns the container.
Definition sarray_impl.h:120
GAIA_NODISCARD constexpr decltype(auto) operator[](size_type pos) noexcept
Accesses an element without bounds checking in optimized builds.
Definition sarray_impl.h:160
constexpr sarr(InputIt first, InputIt last) noexcept
Constructs a container from an iterator range.
Definition sarray_impl.h:85
GAIA_NODISCARD constexpr pointer data() noexcept
Returns a pointer to the element storage.
Definition sarray_impl.h:147
GAIA_NODISCARD constexpr auto end() noexcept
Returns an iterator one past the last element.
Definition sarray_impl.h:261
GAIA_NODISCARD constexpr auto rbegin() noexcept
Returns a reverse traversal iterator to the last element.
Definition sarray_impl.h:243
sarr_detail::size_type size_type
Unsigned type used for sizes and indices.
Definition sarray_impl.h:47
GAIA_NODISCARD constexpr bool operator==(const sarr &other) const
Compares two containers element by element.
Definition sarray_impl.h:298
core::reverse_iterator< iterator, difference_type > reverse_iterator
Mutable reverse random-access iterator type.
Definition sarray_impl.h:54
T m_data[N]
Inline storage backing the container elements.
Definition sarray_impl.h:66
GAIA_NODISCARD constexpr auto crend() const noexcept
Returns the read-only reverse traversal sentinel preceding the first element.
Definition sarray_impl.h:291
GAIA_NODISCARD constexpr bool operator!=(const sarr &other) const
Checks whether two containers differ.
Definition sarray_impl.h:308
GAIA_NODISCARD constexpr auto rbegin() const noexcept
Returns a reverse traversal iterator to the last element.
Definition sarray_impl.h:249
T * pointer
Mutable element pointer type.
Definition sarray_impl.h:39
GAIA_NODISCARD constexpr auto cbegin() const noexcept
Returns a read-only iterator to the first element.
Definition sarray_impl.h:237
const_pointer const_iterator
Read-only random-access iterator type.
Definition sarray_impl.h:52
core::reverse_iterator< const_iterator, difference_type > const_reverse_iterator
Read-only reverse random-access iterator type.
Definition sarray_impl.h:56
const T * const_pointer
Read-only element pointer type.
Definition sarray_impl.h:41
GAIA_NODISCARD constexpr auto end() const noexcept
Returns an iterator one past the last element.
Definition sarray_impl.h:267
GAIA_NODISCARD constexpr auto crbegin() const noexcept
Returns a read-only reverse traversal iterator to the last element.
Definition sarray_impl.h:255
GAIA_NODISCARD constexpr decltype(auto) front() noexcept
Accesses the first element.
Definition sarray_impl.h:201
GAIA_NODISCARD constexpr auto cend() const noexcept
Returns a read-only iterator one past the last element.
Definition sarray_impl.h:273
GAIA_NODISCARD constexpr auto begin() noexcept
Returns an iterator to the first element.
Definition sarray_impl.h:225
static constexpr size_t value_size
Size of one element in bytes.
Definition sarray_impl.h:61
GAIA_NODISCARD constexpr auto rend() noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition sarray_impl.h:279
sarr_detail::difference_type difference_type
Type used for iterator differences.
Definition sarray_impl.h:45
GAIA_NODISCARD constexpr size_type capacity() const noexcept
Returns the number of elements that fit without reallocation.
Definition sarray_impl.h:189
static constexpr size_type extent
Fixed capacity of the container.
Definition sarray_impl.h:63
GAIA_NODISCARD constexpr decltype(auto) back() const noexcept
Accesses the last element.
Definition sarray_impl.h:219
GAIA_NODISCARD constexpr size_type size() const noexcept
Returns the number of elements.
Definition sarray_impl.h:177
GAIA_NODISCARD constexpr size_type max_size() const noexcept
Returns the maximum number of elements supported by this container.
Definition sarray_impl.h:195
GAIA_NODISCARD constexpr const_pointer data() const noexcept
Returns a pointer to the element storage.
Definition sarray_impl.h:153
GAIA_NODISCARD constexpr bool empty() const noexcept
Checks whether the container has no elements.
Definition sarray_impl.h:183
GAIA_NODISCARD constexpr decltype(auto) front() const noexcept
Accesses the first element.
Definition sarray_impl.h:207
constexpr sarr & operator=(sarr &&other) noexcept
Move-assigns the container.
Definition sarray_impl.h:132
GAIA_NODISCARD constexpr auto begin() const noexcept
Returns an iterator to the first element.
Definition sarray_impl.h:231
pointer iterator
Mutable random-access iterator type.
Definition sarray_impl.h:50
Random-access iterator that traverses an underlying iterator in reverse. The stored iterator denotes ...
Definition iterator.h:24
Compile-time capacity of a container that can never grow. Zero for containers that can....
Definition utility.h:44
View policy for accessing and storing data in the AoS way. Good for random access and when accessing ...
Definition data_layout_policy.h:160