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 = uint32_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
55
57 static constexpr size_t value_size = sizeof(T);
59 static constexpr size_type extent = N;
60
63
64 constexpr sarr() noexcept = default;
65
69 constexpr sarr(core::zero_t) noexcept {
70 for (auto i = (size_type)0; i < extent; ++i)
71 m_data[i] = {};
72 }
73
74 GAIA_CONSTEXPR_DTOR ~sarr() = default;
75
80 template <typename InputIt>
81 constexpr sarr(InputIt first, InputIt last) noexcept: sarr() {
82 const auto count = (size_type)core::distance(first, last);
83
84 if constexpr (std::is_pointer_v<InputIt>) {
85 for (size_type i = 0; i < count; ++i)
86 operator[](i) = first[i];
87 } else if constexpr (std::is_same_v<typename InputIt::iterator_category, core::random_access_iterator_tag>) {
88 for (size_type i = 0; i < count; ++i)
89 operator[](i) = *(first[i]);
90 } else {
91 size_type i = 0;
92 for (auto it = first; it != last; ++it)
93 operator[](++i) = *it;
94 }
95 }
96
99 constexpr sarr(std::initializer_list<T> il): sarr(il.begin(), il.end()) {}
100
101 constexpr sarr(const sarr&) = default;
102
103 constexpr sarr(sarr&&) noexcept = default;
104
109 *this = sarr(il.begin(), il.end());
110 return *this;
111 }
112
116 constexpr sarr& operator=(const sarr& other) {
117 GAIA_ASSERT(core::addressof(other) != this);
118
119 for (size_type i = 0; i < extent; ++i)
120 m_data[i] = other.m_data[i];
121
122 return *this;
123 }
124
128 constexpr sarr& operator=(sarr&& other) noexcept {
129 GAIA_ASSERT(core::addressof(other) != this);
130
131 for (size_type i = 0; i < extent; ++i)
132 m_data[i] = GAIA_MOV(other.m_data[i]);
133
134 return *this;
135 }
136
137 GAIA_CLANG_WARNING_PUSH()
138 // Memory is aligned so we can silence this warning
139 GAIA_CLANG_WARNING_DISABLE("-Wcast-align")
140
143 GAIA_NODISCARD constexpr pointer data() noexcept {
144 return &m_data[0];
145 }
146
149 GAIA_NODISCARD constexpr const_pointer data() const noexcept {
150 return &m_data[0];
151 }
152
156 GAIA_NODISCARD constexpr decltype(auto) operator[](size_type pos) noexcept {
157 GAIA_ASSERT(pos < size());
158 return m_data[pos];
159 }
160
164 GAIA_NODISCARD constexpr decltype(auto) operator[](size_type pos) const noexcept {
165 GAIA_ASSERT(pos < size());
166 return m_data[pos];
167 }
168
169 GAIA_CLANG_WARNING_POP()
170
171
174 return N;
175 }
176
179 GAIA_NODISCARD constexpr bool empty() const noexcept {
180 return false;
181 }
182
185 GAIA_NODISCARD constexpr size_type capacity() const noexcept {
186 return N;
187 }
188
191 GAIA_NODISCARD constexpr size_type max_size() const noexcept {
192 return N;
193 }
194
197 GAIA_NODISCARD constexpr decltype(auto) front() noexcept {
198 return (reference)*begin();
199 }
200
203 GAIA_NODISCARD constexpr decltype(auto) front() const noexcept {
204 return (const_reference)*begin();
205 }
206
209 GAIA_NODISCARD constexpr decltype(auto) back() noexcept {
210 return (reference) operator[](N - 1);
211 }
212
215 GAIA_NODISCARD constexpr decltype(auto) back() const noexcept {
216 return (const_reference) operator[](N - 1);
217 }
218
221 GAIA_NODISCARD constexpr auto begin() noexcept {
222 return iterator(&m_data[0]);
223 }
224
227 GAIA_NODISCARD constexpr auto begin() const noexcept {
228 return const_iterator(&m_data[0]);
229 }
230
233 GAIA_NODISCARD constexpr auto cbegin() const noexcept {
234 return const_iterator(&m_data[0]);
235 }
236
239 GAIA_NODISCARD constexpr auto rbegin() noexcept {
240 return iterator((pointer)&back());
241 }
242
245 GAIA_NODISCARD constexpr auto rbegin() const noexcept {
247 }
248
251 GAIA_NODISCARD constexpr auto crbegin() const noexcept {
253 }
254
257 GAIA_NODISCARD constexpr auto end() noexcept {
258 return iterator(&m_data[0] + size());
259 }
260
263 GAIA_NODISCARD constexpr auto end() const noexcept {
264 return const_iterator(&m_data[0] + size());
265 }
266
269 GAIA_NODISCARD constexpr auto cend() const noexcept {
270 return const_iterator(&m_data[0] + size());
271 }
272
275 GAIA_NODISCARD constexpr auto rend() noexcept {
276 return iterator(&m_data[0] - 1);
277 }
278
281 GAIA_NODISCARD constexpr auto rend() const noexcept {
282 return const_iterator(&m_data[0] - 1);
283 }
284
287 GAIA_NODISCARD constexpr auto crend() const noexcept {
288 return const_iterator(&m_data[0] - 1);
289 }
290
294 GAIA_NODISCARD constexpr bool operator==(const sarr& other) const {
295 for (size_type i = 0; i < N; ++i)
296 if (!(operator[](i) == other[i]))
297 return false;
298 return true;
299 }
300
304 GAIA_NODISCARD constexpr bool operator!=(const sarr& other) const {
305 return !operator==(other);
306 }
307 };
308
310 namespace detail {
311 template <typename T, uint32_t N, uint32_t... I>
312 constexpr sarr<std::remove_cv_t<T>, N> to_array_impl(T (&a)[N], std::index_sequence<I...> /*no_name*/) {
313 return {{a[I]...}};
314 }
315 } // namespace detail
317
323 template <typename T, uint32_t N>
324 constexpr sarr<std::remove_cv_t<T>, N> to_array(T (&a)[N]) {
325 return detail::to_array_impl(a, std::make_index_sequence<N>{});
326 }
327
331 template <typename T, typename... U>
332 sarr(T, U...) -> sarr<T, 1 + (uint32_t)sizeof...(U)>;
333
334 } // namespace cnt
335} // namespace gaia
336
338namespace std {
339 template <typename T, uint32_t N>
340 struct tuple_size<gaia::cnt::sarr<T, N>>: std::integral_constant<uint32_t, N> {};
341
342 template <size_t I, typename T, uint32_t N>
343 struct tuple_element<I, gaia::cnt::sarr<T, N>> {
344 using type = T;
345 };
346} // namespace std
Array with variable size of elements of type.
Definition darray_impl.h:27
darr_detail::size_type size_type
Unsigned type used for sizes and indices.
Definition darray_impl.h:44
GAIA_NODISCARD auto begin() noexcept
Returns an iterator to the first element.
Definition darray_impl.h:556
darr_detail::difference_type difference_type
Type used for iterator differences.
Definition darray_impl.h:42
GAIA_NODISCARD auto end() noexcept
Returns an iterator one past the last element.
Definition darray_impl.h:592
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:209
GAIA_NODISCARD constexpr auto rend() const noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition sarray_impl.h:281
constexpr sarr(std::initializer_list< T > il)
Constructs a container from an initializer list.
Definition sarray_impl.h:99
constexpr sarr & operator=(const sarr &other)
Copy-assigns the container.
Definition sarray_impl.h:116
GAIA_NODISCARD constexpr decltype(auto) operator[](size_type pos) noexcept
Accesses an element without bounds checking in optimized builds.
Definition sarray_impl.h:156
constexpr sarr(InputIt first, InputIt last) noexcept
Constructs a container from an iterator range.
Definition sarray_impl.h:81
GAIA_NODISCARD constexpr pointer data() noexcept
Returns a pointer to the element storage.
Definition sarray_impl.h:143
GAIA_NODISCARD constexpr auto end() noexcept
Returns an iterator one past the last element.
Definition sarray_impl.h:257
GAIA_NODISCARD constexpr auto rbegin() noexcept
Returns a reverse traversal iterator to the last element.
Definition sarray_impl.h:239
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:294
T m_data[N]
Inline storage backing the container elements.
Definition sarray_impl.h:62
GAIA_NODISCARD constexpr auto crend() const noexcept
Returns the read-only reverse traversal sentinel preceding the first element.
Definition sarray_impl.h:287
GAIA_NODISCARD constexpr bool operator!=(const sarr &other) const
Checks whether two containers differ.
Definition sarray_impl.h:304
GAIA_NODISCARD constexpr auto rbegin() const noexcept
Returns a reverse traversal iterator to the last element.
Definition sarray_impl.h:245
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:233
const_pointer const_iterator
Read-only random-access iterator type.
Definition sarray_impl.h:52
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:263
GAIA_NODISCARD constexpr auto crbegin() const noexcept
Returns a read-only reverse traversal iterator to the last element.
Definition sarray_impl.h:251
GAIA_NODISCARD constexpr decltype(auto) front() noexcept
Accesses the first element.
Definition sarray_impl.h:197
GAIA_NODISCARD constexpr auto cend() const noexcept
Returns a read-only iterator one past the last element.
Definition sarray_impl.h:269
GAIA_NODISCARD constexpr auto begin() noexcept
Returns an iterator to the first element.
Definition sarray_impl.h:221
static constexpr size_t value_size
Size of one element in bytes.
Definition sarray_impl.h:57
GAIA_NODISCARD constexpr auto rend() noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition sarray_impl.h:275
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:185
static constexpr size_type extent
Fixed capacity of the container.
Definition sarray_impl.h:59
GAIA_NODISCARD constexpr decltype(auto) back() const noexcept
Accesses the last element.
Definition sarray_impl.h:215
GAIA_NODISCARD constexpr size_type size() const noexcept
Returns the number of elements.
Definition sarray_impl.h:173
GAIA_NODISCARD constexpr size_type max_size() const noexcept
Returns the maximum number of elements supported by this container.
Definition sarray_impl.h:191
GAIA_NODISCARD constexpr const_pointer data() const noexcept
Returns a pointer to the element storage.
Definition sarray_impl.h:149
GAIA_NODISCARD constexpr bool empty() const noexcept
Checks whether the container has no elements.
Definition sarray_impl.h:179
GAIA_NODISCARD constexpr decltype(auto) front() const noexcept
Accesses the first element.
Definition sarray_impl.h:203
constexpr sarr & operator=(sarr &&other) noexcept
Move-assigns the container.
Definition sarray_impl.h:128
GAIA_NODISCARD constexpr auto begin() const noexcept
Returns an iterator to the first element.
Definition sarray_impl.h:227
pointer iterator
Mutable random-access iterator type.
Definition sarray_impl.h:50
View policy for accessing and storing data in the AoS way. Good for random access and when accessing ...
Definition data_layout_policy.h:162