4#ifndef GREM_CORE_DATA_SMALL_ARRAY_LIST_HPP
5#define GREM_CORE_DATA_SMALL_ARRAY_LIST_HPP
14#include <initializer_list>
18#include <memory_resource>
25template <
typename T, std::
size_t SmallCapacity,
typename Allocator = std::allocator<T>>
28 using Size = std::uint32_t;
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;
48 : allocator(allocator) {
49 elements =
reinterpret_cast<T*
>(smallBuffer);
62 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
63 SmallArrayList(InputIterator first, Sentinel last,
const Allocator& allocator = Allocator())
68 SmallArrayList(std::initializer_list<T> ilist,
const Allocator& allocator = Allocator())
82 noexcept(std::is_nothrow_move_constructible_v<T>)
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]));
96 while (elementCount < other.elementCount) {
97 std::allocator_traits<Allocator>::construct(this->allocator, reinterpret_cast<T*>(smallBuffer) + elementCount, otherElements[elementCount]);
101 while (elementCount-- > 0) {
102 std::allocator_traits<Allocator>::destroy(this->allocator,
reinterpret_cast<T*
>(smallBuffer) + elementCount);
107 for (Size i = 0; i < other.elementCount; ++i) {
108 std::allocator_traits<Allocator>::destroy(this->allocator, otherElements + i);
110 other.elementCount = 0;
111 elements = std::launder(
reinterpret_cast<T*
>(smallBuffer));
112 elementCapacity = other.elementCapacity;
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});
121 if (
this == &other) {
124 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
125 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
137 std::is_nothrow_move_constructible_v<T> &&
138 (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<Allocator>::is_always_equal::value)) {
139 if (
this == &other) {
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) {
145 reserveAtLeast(other.size());
146 for (T& element : other) {
147 push_back(std::move_if_noexcept(element));
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]));
163 while (elementCount < other.elementCount) {
164 std::allocator_traits<Allocator>::construct(allocator,
reinterpret_cast<T*
>(smallBuffer) + elementCount, otherElements[elementCount]);
168 while (elementCount-- > 0) {
169 std::allocator_traits<Allocator>::destroy(allocator,
reinterpret_cast<T*
>(smallBuffer) + elementCount);
174 for (Size i = 0; i < other.elementCount; ++i) {
175 std::allocator_traits<Allocator>::destroy(allocator, otherElements + i);
177 other.elementCount = 0;
178 elements = std::launder(
reinterpret_cast<T*
>(smallBuffer));
179 elementCapacity = other.elementCapacity;
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});
185 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
186 allocator = other.allocator;
197 if constexpr (std::is_copy_assignable_v<T>) {
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>) {
214 if constexpr (std::is_copy_assignable_v<T>) {
219 (*this)[index] = *first++;
231 void assign(std::initializer_list<T> ilist) {
232 assign(ilist.begin(), ilist.end());
235 template <
typename R>
237 assign(std::begin(r), std::end(r));
246 throw std::out_of_range{
"pos >= size()"};
248 return elements[pos];
253 throw std::out_of_range{
"pos >= size()"};
255 return elements[pos];
261 return elements[pos];
267 return elements[pos];
285 return elements[elementCount - 1];
291 return elements[elementCount - 1];
315 return elements + elementCount;
319 return elements + elementCount;
350 [[nodiscard]]
bool empty() const noexcept {
351 return elementCount == 0;
355 return static_cast<size_type>(elementCount);
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)),
369 throw std::length_error{
"newCapacity > max_size()"};
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]));
380 while (newElementCount < elementCount) {
381 std::allocator_traits<Allocator>::construct(allocator, std::to_address(newElements) + newElementCount, elements[newElementCount]);
385 while (newElementCount-- > 0) {
386 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(newElements) + newElementCount);
388 std::allocator_traits<Allocator>::deallocate(allocator, newElements, newCapacity);
392 for (Size i = 0; i < elementCount; ++i) {
393 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(elements) + i);
396 std::allocator_traits<Allocator>::deallocate(allocator, elements,
capacity());
398 elements = newElements;
399 elementCapacity =
static_cast<Size
>(newCapacity);
404 return static_cast<size_type>(elementCapacity);
415 for (Size i = 0; i < elementCount; ++i) {
416 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(elements) + i);
427 return emplace(pos, std::move(value));
433 throw std::length_error{
"count > max_size() - size()"};
444 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
447 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
450 throw std::length_error{
"count > max_size() - size()"};
452 reserveAtLeast(
size() +
static_cast<size_type>(last - first));
454 while (first != last) {
464 return insert(pos, ilist.begin(), ilist.end());
467 template <
typename R>
469 return insert(pos, std::begin(r), std::end(r));
472 template <
typename R>
477 template <
typename... Args>
482 std::rotate(it,
end() - 1,
end());
490 std::shift_left(it,
end(), 1);
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);
507 elementCount -=
static_cast<Size
>(
count);
520 template <
typename... 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)...);
526 return *std::launder(p);
532 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(elements) + elementCount);
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);
542 elementCount =
static_cast<Size
>(
count);
544 reserveAtLeast(
count);
545 while (elementCount <
count) {
546 std::allocator_traits<Allocator>::construct(allocator, std::to_address(elements) + elementCount);
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);
559 elementCount =
static_cast<Size
>(
count);
561 reserveAtLeast(
count);
562 while (elementCount <
count) {
563 std::allocator_traits<Allocator>::construct(allocator, std::to_address(elements) + elementCount, value);
570 std::allocator_traits<Allocator>::propagate_on_container_swap::value ||
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) {
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()"};
580 if (
capacity() <= SmallCapacity || other.capacity() <= SmallCapacity) {
582 *
this = std::move(other);
583 other = std::move(temporary);
586 swap(elements, other.elements);
587 swap(elementCount, other.elementCount);
588 swap(elementCapacity, other.elementCapacity);
590 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
592 swap(allocator, other.allocator);
601 return std::lexicographical_compare(
begin(),
end(), other.
begin(), other.
end());
605 return !(other < *
this);
609 return other < *
this;
613 return !(*
this < other);
620 template <
typename U>
628 template <
typename Predicate>
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);
643 if (capacity() > SmallCapacity) {
644 std::allocator_traits<Allocator>::deallocate(allocator, elements, capacity());
646 elements =
reinterpret_cast<T*
>(smallBuffer);
648 elementCapacity = SmallCapacity;
651 void reserveAtLeast(size_type newCapacity) {
652 if (
const size_type cap = capacity(); newCapacity > cap) {
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()"};
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]));
669 while (newElementCount < elementCount) {
670 std::allocator_traits<Allocator>::construct(allocator, std::to_address(newElements) + newElementCount, elements[newElementCount]);
674 while (newElementCount-- > 0) {
675 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(newElements) + newElementCount);
677 std::allocator_traits<Allocator>::deallocate(allocator, newElements, newCapacity);
681 for (Size i = 0; i < elementCount; ++i) {
682 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(elements) + i);
684 if (capacity() > SmallCapacity) {
685 std::allocator_traits<Allocator>::deallocate(allocator, elements, capacity());
687 elements = newElements;
688 elementCapacity =
static_cast<Size
>(newCapacity);
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;
703template <
typename T, std::
size_t SmallCapacity>
#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
void pop_back()
Definition SmallArrayList.hpp:529
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
void clear() noexcept
Definition SmallArrayList.hpp:413
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
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