GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
InplaceArrayList.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_INPLACE_ARRAY_LIST_HPP
5#define GREM_CORE_DATA_INPLACE_ARRAY_LIST_HPP
6
8
10
11#include <algorithm> // std::rotate, std::shift_left, std::equal, std::lexicographical_compare, std::remove, std::remove_if, std::move, std::copy, std::fill_n
12#include <cstddef> // std::size_t, std::ptrdiff_t
13#include <cstdint> // std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t
14#include <cstring> // std::memcpy
15#include <initializer_list> // std::initializer_list
16#include <iterator> // std::reverse_iterator, std::input_iterator, std::sentinel_for, std::iterator_traits, std::random_access_iterator_tag, std::begin, std::end
17#include <memory> // std::uninitialized_..., std::destroy_n, std::construct_at
18#include <new> // std::launder
19#include <stdexcept> // std::out_of_range, std::length_error
20#include <type_traits> // std::is_..._v, std::conditional_t
21#include <utility> // std::move, std::exchange, std::forward
22
23namespace grem {
24
25#ifdef _MSC_VER
26#pragma warning(push)
27#pragma warning(disable : 4324)
28#endif
29template <typename T, std::size_t N>
31private:
32 // clang-format off
33 using Size =
34 std::conditional_t<N <= 255ull, std::uint8_t,
35 std::conditional_t<N <= 65535ull, std::uint16_t,
36 std::conditional_t<N <= 4294967295ull, std::uint32_t,
37 std::uint64_t>>>;
38 // clang-format on
39
40public:
41 using value_type = T;
42 using size_type = std::size_t;
43 using difference_type = std::ptrdiff_t;
47 using const_pointer = const value_type*;
50 using reverse_iterator = std::reverse_iterator<iterator>;
51 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
52
53 constexpr InplaceArrayList() noexcept = default;
54
55 constexpr InplaceArrayList(size_type count, const T& value) {
56 resize(count, value);
57 }
58
59 constexpr explicit InplaceArrayList(size_type count) {
61 }
62
63 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
64 constexpr InplaceArrayList(InputIterator first, Sentinel last) {
65 assign(first, last);
66 }
67
68 constexpr InplaceArrayList(std::initializer_list<T> ilist)
69 : InplaceArrayList(ilist.begin(), ilist.end()) {}
70
71 constexpr ~InplaceArrayList() = default;
72
73 constexpr ~InplaceArrayList() requires(!std::is_trivially_destructible_v<T>) {
74 clear();
75 }
76
77 constexpr InplaceArrayList(const InplaceArrayList&) = default;
78
79 constexpr InplaceArrayList(const InplaceArrayList& other) requires(!std::is_trivially_copyable_v<T>)
80 : InplaceArrayList(other.begin(), other.end()) {}
81
82 constexpr InplaceArrayList(InplaceArrayList&&) noexcept = default;
83
84 constexpr InplaceArrayList(InplaceArrayList&& other) noexcept(
85 std::is_nothrow_move_constructible_v<T>) // NOLINT(performance-noexcept-move-constructor, cppcoreguidelines-noexcept-move-operations)
86 requires(!std::is_trivially_copyable_v<T>) {
87 *this = std::move(other);
88 }
89
90 constexpr InplaceArrayList& operator=(const InplaceArrayList&) = default;
91
92 constexpr InplaceArrayList& operator=(const InplaceArrayList& other) requires(!std::is_trivially_copyable_v<T>) {
93 if (this == &other) {
94 return *this;
95 }
96 assign(other.begin(), other.end());
97 return *this;
98 }
99
100 constexpr InplaceArrayList& operator=(InplaceArrayList&&) noexcept = default;
101
102 constexpr InplaceArrayList& operator=(InplaceArrayList&& other) noexcept(
103 std::is_nothrow_move_constructible_v<T> && // NOLINT(performance-noexcept-move-constructor, cppcoreguidelines-noexcept-move-operations)
104 std::is_nothrow_move_assignable_v<T>) requires(!std::is_trivially_copyable_v<T>) {
105 if (this == &other) {
106 return *this;
107 }
108 if (size() == other.size()) {
109 std::move(other.begin(), other.end(), begin());
110 other.clear();
111 return *this;
112 }
113 clear();
114 T* const otherElements = other.data();
115 std::uninitialized_move_n(otherElements, other.size(), reinterpret_cast<T*>(buffer));
116 std::destroy_n(otherElements, other.size());
117 elementCount = std::exchange(other.elementCount, Size{0});
118 return *this;
119 }
120
121 constexpr InplaceArrayList& operator=(std::initializer_list<T> ilist) {
122 assign(ilist);
123 return *this;
124 }
125
126 constexpr void assign(size_type count, const T& value) {
127 if constexpr (std::is_copy_assignable_v<T>) {
128 while (size() > count) {
129 pop_back();
130 }
131 std::fill_n(begin(), size(), value);
132 } else {
133 clear();
134 }
135 for (size_type index = size(); index < count; ++index) {
136 push_back(value);
137 }
138 }
139
140 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
141 constexpr void assign(InputIterator first, Sentinel last) {
142 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
143 const size_type count = static_cast<size_type>(last - first);
144 if constexpr (std::is_copy_assignable_v<T>) {
145 while (size() > count) {
146 pop_back();
147 }
148 for (size_type index = 0; index < size(); ++index) {
149 (*this)[index] = *first++;
150 }
151 } else {
152 clear();
153 }
154 insert(end(), first, last);
155 } else {
156 clear();
157 insert(end(), first, last);
158 }
159 }
160
161 constexpr void assign(std::initializer_list<T> ilist) {
162 assign(ilist.begin(), ilist.end());
163 }
164
165 template <typename R>
166 constexpr void assign_range(R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
167 assign(std::begin(r), std::end(r));
168 }
169
170 [[nodiscard]] constexpr reference at(size_type pos) {
171 if (pos >= size()) {
172 throw std::out_of_range{"pos >= size()"};
173 }
174 return data()[pos];
175 }
176
177 [[nodiscard]] constexpr const_reference at(size_type pos) const {
178 if (pos >= size()) {
179 throw std::out_of_range{"pos >= size()"};
180 }
181 return data()[pos];
182 }
183
184 [[nodiscard]] constexpr reference operator[](size_type pos) {
185 GREM_ASSERT(pos < size());
186 return data()[pos];
187 }
188
189 [[nodiscard]] constexpr const_reference operator[](size_type pos) const {
190 GREM_ASSERT(pos < size());
191 return data()[pos];
192 }
193
194 [[nodiscard]] constexpr reference front() {
195 GREM_ASSERT(!empty());
196 return data()[0];
197 }
198
199 [[nodiscard]] constexpr const_reference front() const {
200 GREM_ASSERT(!empty());
201 return data()[0];
202 }
203
204 [[nodiscard]] constexpr reference back() {
205 GREM_ASSERT(!empty());
206 return data()[size() - 1];
207 }
208
209 [[nodiscard]] constexpr const_reference back() const {
210 GREM_ASSERT(!empty());
211 return data()[size() - 1];
212 }
213
214 [[nodiscard]] constexpr pointer data() noexcept {
215 return std::launder(reinterpret_cast<T*>(buffer));
216 }
217
218 [[nodiscard]] constexpr const_pointer data() const noexcept {
219 return std::launder(reinterpret_cast<const T*>(buffer));
220 }
221
222 [[nodiscard]] constexpr iterator begin() noexcept {
223 return data();
224 }
225
226 [[nodiscard]] constexpr const_iterator begin() const noexcept {
227 return data();
228 }
229
230 [[nodiscard]] constexpr const_iterator cbegin() const noexcept {
231 return begin();
232 }
233
234 [[nodiscard]] constexpr iterator end() noexcept {
235 return data() + size();
236 }
237
238 [[nodiscard]] constexpr const_iterator end() const noexcept {
239 return data() + size();
240 }
241
242 [[nodiscard]] constexpr const_iterator cend() const noexcept {
243 return end();
244 }
245
246 [[nodiscard]] constexpr reverse_iterator rbegin() noexcept {
247 return reverse_iterator{end()};
248 }
249
250 [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept {
251 return const_reverse_iterator{end()};
252 }
253
254 [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept {
256 }
257
258 [[nodiscard]] constexpr reverse_iterator rend() noexcept {
259 return reverse_iterator{begin()};
260 }
261
262 [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept {
264 }
265
266 [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept {
268 }
269
270 [[nodiscard]] constexpr bool empty() const noexcept {
271 return size() == 0;
272 }
273
274 [[nodiscard]] constexpr size_type size() const noexcept {
275 return static_cast<size_type>(elementCount);
276 }
277
278 [[nodiscard]] constexpr size_type max_size() const noexcept {
279 return N;
280 }
281
282 [[nodiscard]] constexpr size_type capacity() const noexcept {
283 return N;
284 }
285
286 constexpr void clear() noexcept {
287 std::destroy_n(data(), size());
288 elementCount = 0;
289 }
290
291 constexpr iterator insert(const_iterator pos, const T& value) {
292 return emplace(pos, value);
293 }
294
295 constexpr iterator insert(const_iterator pos, T&& value) {
296 return emplace(pos, std::move(value));
297 }
298
299 constexpr iterator insert(const_iterator pos, size_type count, const T& value) {
300 const difference_type offset = pos - cbegin();
301 if (count > max_size() - size()) {
302 throw std::length_error{"count > max_size() - size()"};
303 }
304 for (size_type i = 0; i < count; ++i) {
305 push_back(value);
306 }
307 const iterator it = begin() + offset;
308 std::rotate(it, end() - static_cast<difference_type>(count), end());
309 return it;
310 }
311
312 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
313 constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last) {
314 const difference_type offset = pos - cbegin();
315 copyAppend(first, last);
316 const size_type count = size() - static_cast<size_type>(offset);
317 const iterator it = begin() + offset;
318 std::rotate(it, end() - static_cast<difference_type>(count), end());
319 return it;
320 }
321
322 constexpr iterator insert(const_iterator pos, std::initializer_list<T> ilist) {
323 return insert(pos, ilist.begin(), ilist.end());
324 }
325
326 template <typename R>
327 constexpr iterator insert_range(const_iterator pos, R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
328 return insert(pos, std::begin(r), std::end(r));
329 }
330
331 template <typename R>
332 constexpr void append_range(R&& r) {
333 insert_range(end(), std::forward<R>(r));
334 }
335
336 template <typename... Args>
337 constexpr iterator emplace(const_iterator pos, Args&&... args) {
338 const difference_type offset = pos - cbegin();
339 emplace_back(std::forward<Args>(args)...);
340 const iterator it = begin() + offset;
341 std::rotate(it, end() - 1, end());
342 return it;
343 }
344
346 GREM_ASSERT(!empty());
347 const difference_type offset = pos - cbegin();
348 const iterator it = begin() + offset;
349 std::shift_left(it, end(), 1);
350 pop_back();
351 return it;
352 }
353
355 const size_type count = static_cast<size_type>(last - first);
356 const difference_type offset = first - cbegin();
357 const iterator it = begin() + offset;
358 if (count > 0) {
359 GREM_ASSERT(count <= size());
360 const iterator newEnd = std::shift_left(it, end(), static_cast<difference_type>(count));
361 std::destroy_n(newEnd, count);
362 elementCount = static_cast<Size>(size() - count);
363 }
364 return it;
365 }
366
367 constexpr void push_back(const T& value) {
368 emplace_back(value);
369 }
370
371 constexpr void push_back(T&& value) {
372 emplace_back(std::move(value));
373 }
374
375 template <typename... Args>
376 constexpr reference emplace_back(Args&&... args) {
377 if (size() >= max_size()) {
378 throw std::length_error{"size() >= max_size()"};
379 }
380 reference result = *std::construct_at(data() + size(), std::forward<Args>(args)...);
381 ++elementCount;
382 return result;
383 }
384
385 constexpr void pop_back() {
386 GREM_ASSERT(!empty());
387 std::destroy_at(data() + size() - 1);
388 --elementCount;
389 }
390
391 constexpr void resize(size_type count) {
392 if (count < size()) {
393 std::destroy(begin() + static_cast<difference_type>(count), end());
394 } else if (count > size()) {
395 if (count > max_size()) {
396 throw std::length_error{"count > max_size()"};
397 }
398 std::uninitialized_default_construct_n(data() + size(), count - size());
399 }
400 elementCount = static_cast<Size>(count);
401 }
402
403 constexpr void resize(size_type count, const value_type& value) {
404 if (count < size()) {
405 std::destroy(begin() + static_cast<difference_type>(count), end());
406 } else if (count > size()) {
407 if (count > max_size()) {
408 throw std::length_error{"count > max_size()"};
409 }
410 std::uninitialized_fill_n(data() + size(), count - size(), value);
411 }
412 elementCount = static_cast<Size>(count);
413 }
414
415 constexpr void swap(InplaceArrayList& other) noexcept(std::is_nothrow_move_constructible_v<T>) { // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
416 if (this == &other) {
417 return;
418 }
419 InplaceArrayList temporary = std::move(*this);
420 *this = std::move(other);
421 other = std::move(temporary);
422 }
423
424 [[nodiscard]] constexpr bool operator==(const InplaceArrayList& other) const {
425 return std::equal(begin(), end(), other.begin(), other.end());
426 }
427
428 [[nodiscard]] constexpr bool operator<(const InplaceArrayList& other) const {
429 return std::lexicographical_compare(begin(), end(), other.begin(), other.end());
430 }
431
432 [[nodiscard]] constexpr bool operator<=(const InplaceArrayList& other) const {
433 return !(other < *this);
434 }
435
436 [[nodiscard]] constexpr bool operator>(const InplaceArrayList& other) const {
437 return other < *this;
438 }
439
440 [[nodiscard]] constexpr bool operator>=(const InplaceArrayList& other) const {
441 return !(*this < other);
442 }
443
444 friend constexpr void swap(InplaceArrayList& a, InplaceArrayList& b) noexcept(noexcept(a.swap(b))) { // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
445 a.swap(b);
446 }
447
448 template <typename U>
449 friend constexpr size_type erase(InplaceArrayList& c, const U& value) {
450 const iterator it = std::remove(c.begin(), c.end(), value);
451 const size_type result = static_cast<size_type>(c.end() - it);
452 c.erase(it, c.end());
453 return result;
454 }
455
456 template <typename Predicate>
457 friend constexpr size_type erase_if(InplaceArrayList& c, Predicate predicate) {
458 const iterator it = std::remove_if(c.begin(), c.end(), predicate);
459 const size_type result = static_cast<size_type>(c.end() - it);
460 c.erase(it, c.end());
461 return result;
462 }
463
464private:
465 template <typename InputIterator, typename Sentinel>
466 void copyAppend(InputIterator first, Sentinel last) {
467 if constexpr (
468 std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag> && std::is_nothrow_copy_assignable_v<T>) {
469 const size_type count = static_cast<size_type>(last - first);
470 if (count == 0) {
471 return;
472 }
473 const size_type offset = size();
474 if (count > max_size() - offset) {
475 throw std::length_error{"count > max_size() - size()"};
476 }
477 const size_type newSize = offset + count;
478 if constexpr (std::is_trivially_copyable_v<T> && requires { std::to_address(first); }) {
479 std::memcpy(data() + offset, std::to_address(first), count * sizeof(T));
480 } else {
481 std::copy(first, last, data() + offset);
482 }
483 elementCount = static_cast<Size>(newSize);
484 } else {
485 while (first != last) {
486 push_back(*first++);
487 }
488 }
489 }
490
491 Size elementCount = 0;
492 alignas(T) std::byte buffer[N * sizeof(T)];
493};
494#ifdef _MSC_VER
495#pragma warning(pop)
496#endif
497
498} // namespace grem
499
500#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
constexpr const_reverse_iterator rend() const noexcept
Definition InplaceArrayList.hpp:262
value_type & reference
Definition InplaceArrayList.hpp:44
constexpr const_reference back() const
Definition InplaceArrayList.hpp:209
constexpr iterator emplace(const_iterator pos, Args &&... args)
Definition InplaceArrayList.hpp:337
constexpr iterator erase(const_iterator pos)
Definition InplaceArrayList.hpp:345
constexpr const_reverse_iterator rbegin() const noexcept
Definition InplaceArrayList.hpp:250
constexpr InplaceArrayList(std::initializer_list< T > ilist)
Definition InplaceArrayList.hpp:68
constexpr iterator insert(const_iterator pos, const T &value)
Definition InplaceArrayList.hpp:291
friend constexpr void swap(InplaceArrayList &a, InplaceArrayList &b) noexcept(noexcept(a.swap(b)))
Definition InplaceArrayList.hpp:444
constexpr void assign(InputIterator first, Sentinel last)
Definition InplaceArrayList.hpp:141
friend constexpr size_type erase(InplaceArrayList &c, const U &value)
Definition InplaceArrayList.hpp:449
constexpr const_reference at(size_type pos) const
Definition InplaceArrayList.hpp:177
constexpr void resize(size_type count)
Definition InplaceArrayList.hpp:391
constexpr const_iterator end() const noexcept
Definition InplaceArrayList.hpp:238
constexpr iterator erase(const_iterator first, const_iterator last)
Definition InplaceArrayList.hpp:354
constexpr InplaceArrayList(InputIterator first, Sentinel last)
Definition InplaceArrayList.hpp:64
constexpr const_reverse_iterator crbegin() const noexcept
Definition InplaceArrayList.hpp:254
constexpr InplaceArrayList(const InplaceArrayList &)=default
constexpr void assign(std::initializer_list< T > ilist)
Definition InplaceArrayList.hpp:161
constexpr const_iterator cend() const noexcept
Definition InplaceArrayList.hpp:242
constexpr iterator end() noexcept
Definition InplaceArrayList.hpp:234
constexpr const_reference front() const
Definition InplaceArrayList.hpp:199
constexpr void clear() noexcept
Definition InplaceArrayList.hpp:286
constexpr reference front()
Definition InplaceArrayList.hpp:194
std::reverse_iterator< iterator > reverse_iterator
Definition InplaceArrayList.hpp:50
constexpr void assign(size_type count, const T &value)
Definition InplaceArrayList.hpp:126
constexpr bool operator<(const InplaceArrayList &other) const
Definition InplaceArrayList.hpp:428
constexpr size_type capacity() const noexcept
Definition InplaceArrayList.hpp:282
constexpr const_reference operator[](size_type pos) const
Definition InplaceArrayList.hpp:189
constexpr const_iterator cbegin() const noexcept
Definition InplaceArrayList.hpp:230
constexpr InplaceArrayList & operator=(const InplaceArrayList &other)
Definition InplaceArrayList.hpp:92
std::ptrdiff_t difference_type
Definition InplaceArrayList.hpp:43
constexpr bool operator>(const InplaceArrayList &other) const
Definition InplaceArrayList.hpp:436
constexpr size_type size() const noexcept
Definition InplaceArrayList.hpp:274
const value_type & const_reference
Definition InplaceArrayList.hpp:45
const value_type * const_pointer
Definition InplaceArrayList.hpp:47
constexpr reference back()
Definition InplaceArrayList.hpp:204
constexpr const_pointer data() const noexcept
Definition InplaceArrayList.hpp:218
constexpr void swap(InplaceArrayList &other) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition InplaceArrayList.hpp:415
constexpr reverse_iterator rend() noexcept
Definition InplaceArrayList.hpp:258
constexpr const_reverse_iterator crend() const noexcept
Definition InplaceArrayList.hpp:266
pointer iterator
Definition InplaceArrayList.hpp:48
constexpr size_type max_size() const noexcept
Definition InplaceArrayList.hpp:278
constexpr reference at(size_type pos)
Definition InplaceArrayList.hpp:170
constexpr InplaceArrayList & operator=(const InplaceArrayList &)=default
constexpr bool operator<=(const InplaceArrayList &other) const
Definition InplaceArrayList.hpp:432
constexpr InplaceArrayList(const InplaceArrayList &other)
Definition InplaceArrayList.hpp:79
constexpr ~InplaceArrayList()=default
const_pointer const_iterator
Definition InplaceArrayList.hpp:49
constexpr bool empty() const noexcept
Definition InplaceArrayList.hpp:270
constexpr iterator insert_range(const_iterator pos, R &&r)
Definition InplaceArrayList.hpp:327
constexpr InplaceArrayList(InplaceArrayList &&) noexcept=default
constexpr void push_back(T &&value)
Definition InplaceArrayList.hpp:371
std::size_t size_type
Definition InplaceArrayList.hpp:42
constexpr bool operator==(const InplaceArrayList &other) const
Definition InplaceArrayList.hpp:424
constexpr InplaceArrayList(size_type count)
Definition InplaceArrayList.hpp:59
constexpr void assign_range(R &&r)
Definition InplaceArrayList.hpp:166
constexpr void pop_back()
Definition InplaceArrayList.hpp:385
constexpr reference emplace_back(Args &&... args)
Definition InplaceArrayList.hpp:376
constexpr const_iterator begin() const noexcept
Definition InplaceArrayList.hpp:226
constexpr bool operator>=(const InplaceArrayList &other) const
Definition InplaceArrayList.hpp:440
constexpr iterator insert(const_iterator pos, size_type count, const T &value)
Definition InplaceArrayList.hpp:299
constexpr pointer data() noexcept
Definition InplaceArrayList.hpp:214
constexpr iterator insert(const_iterator pos, std::initializer_list< T > ilist)
Definition InplaceArrayList.hpp:322
constexpr void resize(size_type count, const value_type &value)
Definition InplaceArrayList.hpp:403
constexpr ~InplaceArrayList()
Definition InplaceArrayList.hpp:73
constexpr void append_range(R &&r)
Definition InplaceArrayList.hpp:332
constexpr iterator insert(const_iterator pos, T &&value)
Definition InplaceArrayList.hpp:295
constexpr InplaceArrayList() noexcept=default
constexpr InplaceArrayList & operator=(InplaceArrayList &&) noexcept=default
friend constexpr size_type erase_if(InplaceArrayList &c, Predicate predicate)
Definition InplaceArrayList.hpp:457
constexpr InplaceArrayList & operator=(std::initializer_list< T > ilist)
Definition InplaceArrayList.hpp:121
T value_type
Definition InplaceArrayList.hpp:41
constexpr void push_back(const T &value)
Definition InplaceArrayList.hpp:367
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition InplaceArrayList.hpp:51
constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last)
Definition InplaceArrayList.hpp:313
constexpr reference operator[](size_type pos)
Definition InplaceArrayList.hpp:184
constexpr iterator begin() noexcept
Definition InplaceArrayList.hpp:222
constexpr reverse_iterator rbegin() noexcept
Definition InplaceArrayList.hpp:246
Definition concepts.hpp:106
@ T
't'
Definition keyboard.hpp:90
Definition aliases.hpp:9
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234