GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
ArrayList.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_ARRAY_LIST_HPP
5#define GREM_CORE_DATA_ARRAY_LIST_HPP
6
8
10
11#include <algorithm> // std::min, std::max, std::rotate, std::shift_left, std::equal, std::lexicographical_compare, std::remove, std::remove_if, std::fill_n
12#include <cstdint> // std::uint32_t
13#include <cstring> // std::memcpy
14#include <initializer_list> // std::initializer_list
15#include <iterator> // std::reverse_iterator, std::input_iterator, std::sentinel_for, std::iterator_traits, std::random_access_iterator_tag, std::begin, std::end
16#include <limits> // std::numeric_limits
17#include <memory> // std::allocator, std::allocator_traits, std::to_address, std::uninitialized_..., std::destroy
18#include <memory_resource> // std::pmr::polymorphic_allocator
19#include <new> // std::launder
20#include <stdexcept> // std::out_of_range, std::length_error
21#include <type_traits> // std::is_..._v, std::is_constant_evaluated
22#include <utility> // std::move, std::forward, std::exchange, std::swap
23
24namespace grem {
25
26template <typename T, typename Allocator = std::allocator<T>>
27class ArrayList {
28private:
29 using Size = std::uint32_t; // If you need to be able to support more than 4 billion elements, use Buffer instead.
30
31public:
32 using value_type = T;
33 using allocator_type = Allocator;
34 using size_type = typename std::allocator_traits<Allocator>::size_type;
35 using difference_type = typename std::allocator_traits<Allocator>::difference_type;
38 using pointer = typename std::allocator_traits<Allocator>::pointer;
39 using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
42 using reverse_iterator = std::reverse_iterator<iterator>;
43 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
44
45 constexpr ArrayList() noexcept = default;
46
47 constexpr explicit ArrayList(const Allocator& allocator) noexcept
48 : allocator(allocator) {}
49
50 constexpr ArrayList(size_type count, const T& value, const Allocator& allocator = Allocator())
51 : ArrayList(allocator) {
52 resize(count, value);
53 }
54
55 constexpr explicit ArrayList(size_type count, const Allocator& allocator = Allocator())
56 : ArrayList(allocator) {
58 }
59
60 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
61 constexpr ArrayList(InputIterator first, Sentinel last, const Allocator& allocator = Allocator())
62 : ArrayList(allocator) {
63 assign(first, last);
64 }
65
66 constexpr ArrayList(std::initializer_list<T> ilist, const Allocator& allocator = Allocator())
67 : ArrayList(ilist.begin(), ilist.end(), allocator) {}
68
69 constexpr ~ArrayList() {
70 reset();
71 }
72
73 constexpr ArrayList(const ArrayList& other)
74 : ArrayList(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.get_allocator())) {}
75
76 constexpr ArrayList(const ArrayList& other, const Allocator& allocator)
77 : ArrayList(other.begin(), other.end(), allocator) {}
78
79 constexpr ArrayList(ArrayList&& other) noexcept
80 : ArrayList(std::move(other), other.get_allocator()) {}
81
82 constexpr ArrayList(ArrayList&& other, const Allocator& allocator) noexcept // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
83 : elements(std::exchange(other.elements, nullptr))
84 , elementCount(std::exchange(other.elementCount, Size{0}))
85 , elementCapacity(std::exchange(other.elementCapacity, Size{0}))
86 , allocator(allocator) {}
87
88 constexpr ArrayList& operator=(const ArrayList& other) {
89 if (this == &other) {
90 return *this;
91 }
92 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
93 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
94 if (allocator != other.get_allocator()) {
95 reset();
96 }
97 }
98 allocator = other.get_allocator();
99 }
100 assign(other.begin(), other.end());
101 return *this;
102 }
103
104 constexpr ArrayList& operator=(ArrayList&& other) noexcept(
105 std::allocator_traits<
106 Allocator>::propagate_on_container_move_assignment::value || // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
107 std::allocator_traits<Allocator>::is_always_equal::value) {
108 if (this == &other) {
109 return *this;
110 }
111 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value && !std::allocator_traits<Allocator>::is_always_equal::value) {
112 if (allocator != other.allocator) {
113 clear();
114 reserve(other.size());
115 moveAppend(other.begin(), other.end());
116 other.clear();
117 return *this;
118 }
119 }
120 reset();
121 elements = std::exchange(other.elements, nullptr);
122 elementCount = std::exchange(other.elementCount, Size{0});
123 elementCapacity = std::exchange(other.elementCapacity, Size{0});
124 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
125 allocator = other.allocator;
126 }
127 return *this;
128 }
129
130 constexpr ArrayList& operator=(std::initializer_list<T> ilist) noexcept {
131 assign(ilist);
132 return *this;
133 }
134
135 constexpr void assign(size_type count, const T& value) {
136 if constexpr (std::is_copy_assignable_v<T>) {
137 while (size() > count) {
138 pop_back();
139 }
140 reserve(count);
141 std::fill_n(begin(), size(), value);
142 } else {
143 clear();
144 reserve(count);
145 }
146 for (size_type index = size(); index < count; ++index) {
147 push_back(value);
148 }
149 }
150
151 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
152 constexpr void assign(InputIterator first, Sentinel last) {
153 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
154 const size_type count = static_cast<size_type>(last - first);
155 if constexpr (std::is_copy_assignable_v<T>) {
156 while (size() > count) {
157 pop_back();
158 }
159 reserve(count);
160 for (size_type index = 0; index < size(); ++index) {
161 elements[index] = *first++;
162 }
163 } else {
164 clear();
165 reserve(count);
166 }
167 copyAppend(first, last);
168 } else {
169 clear();
170 copyAppend(first, last);
171 }
172 }
173
174 constexpr void assign(std::initializer_list<T> ilist) {
175 assign(ilist.begin(), ilist.end());
176 }
177
178 template <typename R>
179 constexpr void assign_range(R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
180 assign(std::begin(r), std::end(r));
181 }
182
183 [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
184 return allocator;
185 }
186
187 [[nodiscard]] constexpr reference at(size_type pos) {
188 if (pos >= elementCount) {
189 throw std::out_of_range{"pos >= size()"};
190 }
191 return elements[pos];
192 }
193
194 [[nodiscard]] constexpr const_reference at(size_type pos) const {
195 if (pos >= elementCount) {
196 throw std::out_of_range{"pos >= size()"};
197 }
198 return elements[pos];
199 }
200
201 [[nodiscard]] constexpr reference operator[](size_type pos) {
202 GREM_ASSERT(elements);
203 GREM_ASSERT(pos < elementCount);
204 return elements[pos];
205 }
206
207 [[nodiscard]] constexpr const_reference operator[](size_type pos) const {
208 GREM_ASSERT(elements);
209 GREM_ASSERT(pos < elementCount);
210 return elements[pos];
211 }
212
213 [[nodiscard]] constexpr reference front() {
214 GREM_ASSERT(elements);
215 GREM_ASSERT(!empty());
216 return elements[0];
217 }
218
219 [[nodiscard]] constexpr const_reference front() const {
220 GREM_ASSERT(elements);
221 GREM_ASSERT(!empty());
222 return elements[0];
223 }
224
225 [[nodiscard]] constexpr reference back() {
226 GREM_ASSERT(elements);
227 GREM_ASSERT(!empty());
228 return elements[elementCount - 1];
229 }
230
231 [[nodiscard]] constexpr const_reference back() const {
232 GREM_ASSERT(elements);
233 GREM_ASSERT(!empty());
234 return elements[elementCount - 1];
235 }
236
237 [[nodiscard]] constexpr pointer data() noexcept {
238 return elements;
239 }
240
241 [[nodiscard]] constexpr const_pointer data() const noexcept {
242 return elements;
243 }
244
245 [[nodiscard]] constexpr iterator begin() noexcept {
246 return elements;
247 }
248
249 [[nodiscard]] constexpr const_iterator begin() const noexcept {
250 return elements;
251 }
252
253 [[nodiscard]] constexpr const_iterator cbegin() const noexcept {
254 return begin();
255 }
256
257 [[nodiscard]] constexpr iterator end() noexcept {
258 return elements + elementCount;
259 }
260
261 [[nodiscard]] constexpr const_iterator end() const noexcept {
262 return elements + elementCount;
263 }
264
265 [[nodiscard]] constexpr const_iterator cend() const noexcept {
266 return end();
267 }
268
269 [[nodiscard]] constexpr reverse_iterator rbegin() noexcept {
270 return reverse_iterator{end()};
271 }
272
273 [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept {
274 return const_reverse_iterator{end()};
275 }
276
277 [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept {
278 return rbegin();
279 }
280
281 [[nodiscard]] constexpr reverse_iterator rend() noexcept {
282 return reverse_iterator{begin()};
283 }
284
285 [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept {
287 }
288
289 [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept {
290 return rend();
291 }
292
293 [[nodiscard]] constexpr bool empty() const noexcept {
294 return elementCount == 0;
295 }
296
297 [[nodiscard]] constexpr size_type size() const noexcept {
298 return static_cast<size_type>(elementCount);
299 }
300
301 [[nodiscard]] constexpr size_type max_size() const noexcept {
302 return std::min({
303 static_cast<size_type>(std::numeric_limits<Size>::max()),
304 static_cast<size_type>(std::numeric_limits<difference_type>::max()),
305 static_cast<size_type>(std::numeric_limits<size_type>::max() / sizeof(T)),
306 static_cast<size_type>(std::allocator_traits<Allocator>::max_size(allocator)),
307 });
308 }
309
310 constexpr void reserve(size_type newCapacity) {
311 if (newCapacity > capacity()) {
312 if (newCapacity > max_size()) {
313 throw std::length_error{"newCapacity > max_size()"};
314 }
315 if constexpr (std::is_nothrow_move_constructible_v<T>) {
316 *this = ArrayList(MoveTag{}, begin(), end(), newCapacity, allocator);
317 } else {
318 *this = ArrayList(cbegin(), cend(), newCapacity, allocator);
319 }
320 }
321 }
322
323 [[nodiscard]] constexpr size_type capacity() const noexcept {
324 return static_cast<size_type>(elementCapacity);
325 }
326
327 constexpr void shrink_to_fit() {
328 if (size() < capacity()) {
329 if constexpr (std::is_nothrow_move_constructible_v<T>) {
330 *this = ArrayList(MoveTag{}, begin(), end(), size(), allocator);
331 } else {
332 *this = ArrayList(cbegin(), cend(), size(), allocator);
333 }
334 }
335 }
336
337 constexpr void clear() noexcept {
338 if (elements) {
339 T* const pBegin = std::to_address(elements);
340 T* const pEnd = pBegin + elementCount;
341 if constexpr (std::is_trivially_destructible_v<T>) {
342 std::destroy(pBegin, pEnd);
343 } else {
344 for (T* p = pBegin; p != pEnd; ++p) {
345 std::allocator_traits<Allocator>::destroy(allocator, p);
346 }
347 }
348 elementCount = 0;
349 }
350 }
351
352 constexpr iterator insert(const_iterator pos, const T& value) {
353 return emplace(pos, value);
354 }
355
356 constexpr iterator insert(const_iterator pos, T&& value) {
357 return emplace(pos, std::move(value));
358 }
359
360 constexpr iterator insert(const_iterator pos, size_type count, const T& value) {
361 const difference_type offset = pos - cbegin();
362 if (count > max_size() - size()) {
363 throw std::length_error{"count > max_size() - size()"};
364 }
365 reserveAtLeast(size() + count);
366 for (size_type i = 0; i < count; ++i) {
367 push_back(value);
368 }
369 const iterator it = begin() + offset;
370 std::rotate(it, end() - static_cast<difference_type>(count), end());
371 return it;
372 }
373
374 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
375 constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last) {
376 const difference_type offset = pos - cbegin();
377 copyAppend(first, last);
378 const iterator it = begin() + offset;
379 const size_type count = size() - static_cast<size_type>(offset);
380 std::rotate(it, end() - static_cast<difference_type>(count), end());
381 return it;
382 }
383
384 constexpr iterator insert(const_iterator pos, std::initializer_list<T> ilist) {
385 return insert(pos, ilist.begin(), ilist.end());
386 }
387
388 template <typename R>
389 constexpr iterator insert_range(const_iterator pos, R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
390 return insert(pos, std::begin(r), std::end(r));
391 }
392
393 template <typename R>
394 constexpr void append_range(R&& r) {
395 insert_range(end(), std::forward<R>(r));
396 }
397
398 template <typename... Args>
399 constexpr iterator emplace(const_iterator pos, Args&&... args) {
400 const difference_type offset = pos - cbegin();
401 emplace_back(std::forward<Args>(args)...);
402 const iterator it = begin() + offset;
403 std::rotate(it, end() - 1, end());
404 return it;
405 }
406
408 GREM_ASSERT(!empty());
409 const difference_type offset = pos - cbegin();
410 const iterator it = begin() + offset;
411 std::shift_left(it, end(), 1);
412 pop_back();
413 return it;
414 }
415
417 const size_type count = static_cast<size_type>(last - first);
418 const difference_type offset = first - cbegin();
419 const iterator it = begin() + offset;
420 if (count > 0) {
421 GREM_ASSERT(count <= size());
422 const iterator newEnd = std::shift_left(it, end(), static_cast<difference_type>(count));
423 T* const pBegin = std::to_address(elements);
424 T* const pEnd = pBegin + elementCount;
425 T* const pNewEnd = pBegin + (newEnd - begin());
426 if constexpr (std::is_trivially_destructible_v<T>) {
427 std::destroy(pNewEnd, pEnd);
428 } else {
429 for (T* p = pNewEnd; p != pEnd; ++p) {
430 std::allocator_traits<Allocator>::destroy(allocator, p);
431 }
432 }
433 elementCount -= static_cast<Size>(count);
434 }
435 return it;
436 }
437
438 constexpr void push_back(const T& value) {
439 emplace_back(value);
440 }
441
442 constexpr void push_back(T&& value) {
443 emplace_back(std::move(value));
444 }
445
446 template <typename... Args>
447 constexpr reference emplace_back(Args&&... args) {
448 reserveAtLeast(size() + 1);
449 T* const p = std::to_address(elements) + elementCount;
450 std::allocator_traits<Allocator>::construct(allocator, p, std::forward<Args>(args)...);
451 ++elementCount;
452 return *std::launder(p);
453 }
454
455 constexpr void pop_back() {
456 GREM_ASSERT(!empty());
457 --elementCount;
458 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(elements) + elementCount);
459 }
460
461 constexpr void resize(size_type count) {
462 if (count < size()) {
463 T* const pBegin = std::to_address(elements);
464 T* const pEnd = pBegin + elementCount;
465 T* const pNewEnd = pBegin + count;
466 if constexpr (std::is_trivially_destructible_v<T>) {
467 std::destroy(pNewEnd, pEnd);
468 } else {
469 for (T* p = pNewEnd; p != pEnd; ++p) {
470 std::allocator_traits<Allocator>::destroy(allocator, p);
471 }
472 }
473 elementCount = static_cast<Size>(count);
474 } else if (count > size()) {
475 reserveAtLeast(count);
476 if constexpr (std::is_trivially_default_constructible_v<T>) {
477 std::uninitialized_default_construct(std::to_address(elements) + elementCount, std::to_address(elements) + count);
478 elementCount = static_cast<Size>(count);
479 } else {
480 while (elementCount < count) {
481 std::allocator_traits<Allocator>::construct(allocator, std::to_address(elements) + elementCount);
482 ++elementCount;
483 }
484 }
485 }
486 }
487
488 constexpr void resize(size_type count, const value_type& value) {
489 if (count < size()) {
490 T* const pBegin = std::to_address(elements);
491 T* const pEnd = pBegin + elementCount;
492 T* const pNewEnd = pBegin + count;
493 if constexpr (std::is_trivially_destructible_v<T>) {
494 std::destroy(pNewEnd, pEnd);
495 } else {
496 for (T* p = pNewEnd; p != pEnd; ++p) {
497 std::allocator_traits<Allocator>::destroy(allocator, p);
498 }
499 }
500 elementCount = static_cast<Size>(count);
501 } else if (count > size()) {
502 reserveAtLeast(count);
503 if constexpr (std::is_trivially_copy_constructible_v<T>) {
504 std::uninitialized_fill(std::to_address(elements) + elementCount, std::to_address(elements) + count, value);
505 elementCount = static_cast<Size>(count);
506 } else {
507 while (elementCount < count) {
508 std::allocator_traits<Allocator>::construct(allocator, std::to_address(elements) + elementCount, value);
509 ++elementCount;
510 }
511 }
512 }
513 }
514
515 constexpr void swap(ArrayList& other) noexcept(
516 std::allocator_traits<Allocator>::propagate_on_container_swap::value || // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
517 (std::allocator_traits<Allocator>::is_always_equal::value && std::is_nothrow_swappable_v<T>)) {
518 if (this == &other) {
519 return;
520 }
521 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_swap::value && !std::allocator_traits<Allocator>::is_always_equal::value) {
522 if (allocator != other.allocator) {
523 throw std::logic_error{"get_allocator() != other.get_allocator()"};
524 }
525 }
526 using std::swap;
527 swap(elements, other.elements);
528 swap(elementCount, other.elementCount);
529 swap(elementCapacity, other.elementCapacity);
530 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
531 swap(allocator, other.allocator);
532 }
533 }
534
535 [[nodiscard]] constexpr bool operator==(const ArrayList& other) const {
536 return std::equal(begin(), end(), other.begin(), other.end());
537 }
538
539 [[nodiscard]] constexpr bool operator<(const ArrayList& other) const {
540 return std::lexicographical_compare(begin(), end(), other.begin(), other.end());
541 }
542
543 [[nodiscard]] constexpr bool operator<=(const ArrayList& other) const {
544 return !(other < *this);
545 }
546
547 [[nodiscard]] constexpr bool operator>(const ArrayList& other) const {
548 return other < *this;
549 }
550
551 [[nodiscard]] constexpr bool operator>=(const ArrayList& other) const {
552 return !(*this < other);
553 }
554
555 friend constexpr void swap(ArrayList& a, ArrayList& b) noexcept {
556 a.swap(b);
557 }
558
559 template <typename U>
560 friend constexpr size_type erase(ArrayList& c, const U& value) {
561 const iterator it = std::remove(c.begin(), c.end(), value);
562 const size_type result = static_cast<size_type>(c.end() - it);
563 c.erase(it, c.end());
564 return result;
565 }
566
567 template <typename Predicate>
568 friend constexpr size_type erase_if(ArrayList& c, Predicate predicate) {
569 const iterator it = std::remove_if(c.begin(), c.end(), predicate);
570 const size_type result = static_cast<size_type>(c.end() - it);
571 c.erase(it, c.end());
572 return result;
573 }
574
575private:
576 struct MoveTag {};
577
578 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
579 constexpr ArrayList(MoveTag, InputIterator first, Sentinel last, size_type capacity, const Allocator& allocator)
580 : ArrayList(allocator) {
581 elements = std::allocator_traits<Allocator>::allocate(this->allocator, capacity); // NOLINT(cppcoreguidelines-prefer-member-initializer)
582 elementCapacity = static_cast<Size>(capacity); // NOLINT(cppcoreguidelines-prefer-member-initializer)
583 moveAppend(first, last);
584 }
585
586 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
587 constexpr ArrayList(InputIterator first, Sentinel last, size_type capacity, const Allocator& allocator)
588 : ArrayList(allocator) {
589 elements = std::allocator_traits<Allocator>::allocate(this->allocator, capacity); // NOLINT(cppcoreguidelines-prefer-member-initializer)
590 elementCapacity = static_cast<Size>(capacity); // NOLINT(cppcoreguidelines-prefer-member-initializer)
591 copyAppend(first, last);
592 }
593
594 constexpr void reset() noexcept {
595 if (elements) {
596 T* const pBegin = std::to_address(elements);
597 T* const pEnd = pBegin + elementCount;
598 if constexpr (std::is_trivially_destructible_v<T>) {
599 std::destroy(pBegin, pEnd);
600 } else {
601 for (T* p = pBegin; p != pEnd; ++p) {
602 std::allocator_traits<Allocator>::destroy(allocator, p);
603 }
604 }
605 std::allocator_traits<Allocator>::deallocate(allocator, elements, static_cast<size_type>(elementCapacity));
606 elements = nullptr;
607 elementCount = 0;
608 elementCapacity = 0;
609 }
610 }
611
612 constexpr void reserveAtLeast(size_type newCapacity) {
613 if (const size_type cap = capacity(); newCapacity > cap) {
614 const size_type maxSize = max_size();
615 const size_type halfCap = cap / 2;
616 if (newCapacity > maxSize || halfCap > maxSize - cap) {
617 throw std::length_error{"newCapacity > max_size()"};
618 }
619 newCapacity = std::max(newCapacity, cap + halfCap);
620 if constexpr (std::is_nothrow_move_constructible_v<T>) {
621 *this = ArrayList(MoveTag{}, begin(), end(), newCapacity, allocator);
622 } else {
623 *this = ArrayList(cbegin(), cend(), newCapacity, allocator);
624 }
625 }
626 }
627
628 template <typename InputIterator, typename Sentinel>
629 constexpr void copyAppend(InputIterator first, Sentinel last) {
630 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
631 const size_type count = static_cast<size_type>(last - first);
632 if (count == 0) {
633 return;
634 }
635 const size_type offset = size();
636 if (count > max_size() - offset) {
637 throw std::length_error{"count > max_size() - size()"};
638 }
639 const size_type newSize = offset + count;
640 reserveAtLeast(newSize);
641 if constexpr (std::is_trivially_copyable_v<T> && requires { std::to_address(first); }) {
642 if (!std::is_constant_evaluated()) {
643 std::memcpy(std::to_address(elements) + offset, std::to_address(first), count * sizeof(T));
644 elementCount = static_cast<Size>(newSize);
645 return;
646 }
647 }
648 }
649 while (first != last) {
650 push_back(*first++);
651 }
652 }
653
654 template <typename InputIterator, typename Sentinel>
655 constexpr void moveAppend(InputIterator first, Sentinel last) {
656 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
657 const size_type count = static_cast<size_type>(last - first);
658 if (count == 0) {
659 return;
660 }
661 const size_type offset = size();
662 if (count > max_size() - offset) {
663 throw std::length_error{"count > max_size() - size()"};
664 }
665 const size_type newSize = offset + count;
666 reserveAtLeast(newSize);
667 if constexpr (std::is_trivially_copyable_v<T> && requires { std::to_address(first); }) {
668 if (!std::is_constant_evaluated()) {
669 std::memcpy(std::to_address(elements) + offset, std::to_address(first), count * sizeof(T));
670 elementCount = static_cast<Size>(newSize);
671 return;
672 }
673 }
674 }
675 while (first != last) {
676 push_back(std::move(*first++));
677 }
678 }
679
680 pointer elements = nullptr;
681 Size elementCount = 0;
682 Size elementCapacity = 0;
683 [[no_unique_address]] Allocator allocator;
684};
685
686} // namespace grem
687
688namespace grem::pmr {
689
690template <typename T>
692
693} // namespace grem::pmr
694
695#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition ArrayList.hpp:27
constexpr size_type capacity() const noexcept
Definition ArrayList.hpp:323
friend constexpr size_type erase_if(ArrayList &c, Predicate predicate)
Definition ArrayList.hpp:568
constexpr reverse_iterator rend() noexcept
Definition ArrayList.hpp:281
typename std::allocator_traits< Allocator >::size_type size_type
Definition ArrayList.hpp:34
constexpr void clear() noexcept
Definition ArrayList.hpp:337
constexpr ArrayList & operator=(ArrayList &&other) noexcept(std::allocator_traits< Allocator >::propagate_on_container_move_assignment::value||//NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor) std::allocator_traits< Allocator >::is_always_equal::value)
Definition ArrayList.hpp:104
constexpr bool operator==(const ArrayList &other) const
Definition ArrayList.hpp:535
constexpr ArrayList(size_type count, const T &value, const Allocator &allocator=Allocator())
Definition ArrayList.hpp:50
constexpr bool operator<(const ArrayList &other) const
Definition ArrayList.hpp:539
constexpr void resize(size_type count, const value_type &value)
Definition ArrayList.hpp:488
constexpr bool empty() const noexcept
Definition ArrayList.hpp:293
typename std::allocator_traits< Allocator >::const_pointer const_pointer
Definition ArrayList.hpp:39
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition ArrayList.hpp:43
constexpr const_reverse_iterator rend() const noexcept
Definition ArrayList.hpp:285
constexpr iterator insert(const_iterator pos, std::initializer_list< T > ilist)
Definition ArrayList.hpp:384
constexpr pointer data() noexcept
Definition ArrayList.hpp:237
constexpr const_iterator cbegin() const noexcept
Definition ArrayList.hpp:253
constexpr void assign_range(R &&r)
Definition ArrayList.hpp:179
constexpr reference operator[](size_type pos)
Definition ArrayList.hpp:201
constexpr allocator_type get_allocator() const noexcept
Definition ArrayList.hpp:183
constexpr void assign(std::initializer_list< T > ilist)
Definition ArrayList.hpp:174
constexpr bool operator>=(const ArrayList &other) const
Definition ArrayList.hpp:551
constexpr ArrayList(const ArrayList &other)
Definition ArrayList.hpp:73
constexpr reverse_iterator rbegin() noexcept
Definition ArrayList.hpp:269
constexpr const_reference operator[](size_type pos) const
Definition ArrayList.hpp:207
constexpr ArrayList & operator=(const ArrayList &other)
Definition ArrayList.hpp:88
constexpr reference emplace_back(Args &&... args)
Definition ArrayList.hpp:447
constexpr iterator insert_range(const_iterator pos, R &&r)
Definition ArrayList.hpp:389
const value_type & const_reference
Definition ArrayList.hpp:37
constexpr void swap(ArrayList &other) noexcept(std::allocator_traits< Allocator >::propagate_on_container_swap::value||//NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)(std::allocator_traits< Allocator >::is_always_equal::value &&std::is_nothrow_swappable_v< T >))
Definition ArrayList.hpp:515
constexpr ArrayList(size_type count, const Allocator &allocator=Allocator())
Definition ArrayList.hpp:55
constexpr bool operator<=(const ArrayList &other) const
Definition ArrayList.hpp:543
typename std::allocator_traits< Allocator >::pointer pointer
Definition ArrayList.hpp:38
constexpr iterator insert(const_iterator pos, const T &value)
Definition ArrayList.hpp:352
constexpr void shrink_to_fit()
Definition ArrayList.hpp:327
value_type & reference
Definition ArrayList.hpp:36
typename std::allocator_traits< Allocator >::difference_type difference_type
Definition ArrayList.hpp:35
constexpr const_reverse_iterator rbegin() const noexcept
Definition ArrayList.hpp:273
constexpr ArrayList(const ArrayList &other, const Allocator &allocator)
Definition ArrayList.hpp:76
constexpr size_type size() const noexcept
Definition ArrayList.hpp:297
constexpr const_reverse_iterator crbegin() const noexcept
Definition ArrayList.hpp:277
constexpr const_reference front() const
Definition ArrayList.hpp:219
constexpr bool operator>(const ArrayList &other) const
Definition ArrayList.hpp:547
constexpr void assign(InputIterator first, Sentinel last)
Definition ArrayList.hpp:152
constexpr reference at(size_type pos)
Definition ArrayList.hpp:187
constexpr iterator erase(const_iterator first, const_iterator last)
Definition ArrayList.hpp:416
constexpr void assign(size_type count, const T &value)
Definition ArrayList.hpp:135
constexpr const_iterator end() const noexcept
Definition ArrayList.hpp:261
constexpr iterator insert(const_iterator pos, size_type count, const T &value)
Definition ArrayList.hpp:360
constexpr iterator emplace(const_iterator pos, Args &&... args)
Definition ArrayList.hpp:399
constexpr size_type max_size() const noexcept
Definition ArrayList.hpp:301
constexpr iterator erase(const_iterator pos)
Definition ArrayList.hpp:407
friend constexpr void swap(ArrayList &a, ArrayList &b) noexcept
Definition ArrayList.hpp:555
Allocator allocator_type
Definition ArrayList.hpp:33
friend constexpr size_type erase(ArrayList &c, const U &value)
Definition ArrayList.hpp:560
pointer iterator
Definition ArrayList.hpp:40
constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last)
Definition ArrayList.hpp:375
constexpr const_reverse_iterator crend() const noexcept
Definition ArrayList.hpp:289
constexpr ArrayList() noexcept=default
const_pointer const_iterator
Definition ArrayList.hpp:41
std::reverse_iterator< iterator > reverse_iterator
Definition ArrayList.hpp:42
constexpr ~ArrayList()
Definition ArrayList.hpp:69
constexpr reference back()
Definition ArrayList.hpp:225
constexpr void push_back(const T &value)
Definition ArrayList.hpp:438
constexpr reference front()
Definition ArrayList.hpp:213
constexpr const_reference at(size_type pos) const
Definition ArrayList.hpp:194
constexpr ArrayList(InputIterator first, Sentinel last, const Allocator &allocator=Allocator())
Definition ArrayList.hpp:61
constexpr const_iterator begin() const noexcept
Definition ArrayList.hpp:249
constexpr ArrayList(std::initializer_list< T > ilist, const Allocator &allocator=Allocator())
Definition ArrayList.hpp:66
constexpr ArrayList & operator=(std::initializer_list< T > ilist) noexcept
Definition ArrayList.hpp:130
constexpr const_reference back() const
Definition ArrayList.hpp:231
constexpr void push_back(T &&value)
Definition ArrayList.hpp:442
constexpr iterator begin() noexcept
Definition ArrayList.hpp:245
constexpr void reserve(size_type newCapacity)
Definition ArrayList.hpp:310
constexpr void append_range(R &&r)
Definition ArrayList.hpp:394
constexpr ArrayList(ArrayList &&other) noexcept
Definition ArrayList.hpp:79
constexpr void pop_back()
Definition ArrayList.hpp:455
constexpr const_iterator cend() const noexcept
Definition ArrayList.hpp:265
constexpr ArrayList(ArrayList &&other, const Allocator &allocator) noexcept
Definition ArrayList.hpp:82
constexpr void resize(size_type count)
Definition ArrayList.hpp:461
constexpr const_pointer data() const noexcept
Definition ArrayList.hpp:241
T value_type
Definition ArrayList.hpp:32
constexpr iterator end() noexcept
Definition ArrayList.hpp:257
constexpr iterator insert(const_iterator pos, T &&value)
Definition ArrayList.hpp:356
Definition concepts.hpp:106
@ T
't'
Definition keyboard.hpp:90
Definition Allocation.hpp:554
grem::ArrayList< T, std::pmr::polymorphic_allocator< T > > ArrayList
Definition ArrayList.hpp:691
Definition aliases.hpp:9
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234