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