4#ifndef GREM_CORE_DATA_SMALL_BUFFER_HPP
5#define GREM_CORE_DATA_SMALL_BUFFER_HPP
13#include <initializer_list>
17#include <memory_resource>
26template <
typename T, std::
size_t SmallCapacity,
typename Allocator = std::allocator<T>>
29 static constexpr bool USE_MALLOC_AND_REALLOC = std::is_same_v<Allocator, std::allocator<T>> && std::is_trivially_copyable_v<T> && std::is_trivially_destructible_v<T> &&
30 std::is_nothrow_default_constructible_v<T> &&
alignof(T) <=
alignof(std::max_align_t);
35 using size_type =
typename std::allocator_traits<Allocator>::size_type;
36 using difference_type =
typename std::allocator_traits<Allocator>::difference_type;
39 using pointer =
typename std::allocator_traits<Allocator>::pointer;
40 using const_pointer =
typename std::allocator_traits<Allocator>::const_pointer;
50 : allocator(allocator) {
51 elements = smallElements;
64 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
65 SmallBuffer(InputIterator first, Sentinel last,
const Allocator& allocator = Allocator())
70 SmallBuffer(std::initializer_list<T> ilist,
const Allocator& allocator = Allocator())
78 :
SmallBuffer(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.
get_allocator())) {}
84 noexcept(std::is_nothrow_move_constructible_v<T>)
88 : allocator(allocator) {
89 if (other.elementCapacity <= SmallCapacity) {
90 elements = smallElements;
91 while (elementCount < other.elementCount) {
92 smallElements[elementCount] = std::move_if_noexcept(other.smallElements[elementCount]);
95 other.elementCount = 0;
96 elementCapacity = other.elementCapacity;
98 elements = std::exchange(other.elements, other.smallElements);
99 elementCount = std::exchange(other.elementCount,
size_type{0});
100 elementCapacity = std::exchange(other.elementCapacity, SmallCapacity);
105 if (
this == &other) {
108 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
109 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
121 std::is_nothrow_move_constructible_v<T> &&
122 (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<Allocator>::is_always_equal::value)) {
123 if (
this == &other) {
126 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value && !std::allocator_traits<Allocator>::is_always_equal::value) {
127 if (allocator != other.allocator) {
129 reserveAtLeast(other.size());
130 for (T& element : other) {
131 push_back(std::move_if_noexcept(element));
138 if (other.elementCapacity <= SmallCapacity) {
139 elements = smallElements;
140 while (elementCount < other.elementCount) {
141 smallElements[elementCount] = std::move_if_noexcept(other.smallElements[elementCount]);
144 other.elementCount = 0;
145 elementCapacity = other.elementCapacity;
147 elements = std::exchange(other.elements, other.smallElements);
148 elementCount = std::exchange(other.elementCount,
size_type{0});
149 elementCapacity = std::exchange(other.elementCapacity, SmallCapacity);
151 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
152 allocator = other.allocator;
163 if constexpr (std::is_copy_assignable_v<T>) {
164 if (
count < elementCount) {
165 elementCount =
count;
176 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
177 void assign(InputIterator first, Sentinel last) {
178 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
180 if constexpr (std::is_copy_assignable_v<T>) {
181 if (
count < elementCount) {
182 elementCount =
count;
185 elements[index] = *first++;
197 void assign(std::initializer_list<T> ilist) {
198 assign(ilist.begin(), ilist.end());
201 template <
typename R>
203 assign(std::begin(r), std::end(r));
212 throw std::out_of_range{
"pos >= size()"};
214 return elements[pos];
219 throw std::out_of_range{
"pos >= size()"};
221 return elements[pos];
227 return elements[pos];
233 return elements[pos];
251 return elements[elementCount - 1];
257 return elements[elementCount - 1];
281 return elements + elementCount;
285 return elements + elementCount;
316 [[nodiscard]]
bool empty() const noexcept {
317 return elementCount == 0;
326 static_cast<size_type>(std::numeric_limits<difference_type>::max()),
327 static_cast<size_type>(std::numeric_limits<size_type>::max() /
sizeof(T)),
328 static_cast<size_type>(std::allocator_traits<Allocator>::max_size(allocator)),
336 throw std::length_error{
"newCapacity > max_size()"};
338 if constexpr (USE_MALLOC_AND_REALLOC) {
339 if (newCapacity > std::numeric_limits<size_type>::max() /
sizeof(T)) {
340 throw std::bad_array_new_length{};
342 static_assert(std::is_trivially_copyable_v<T>);
343 static_assert(std::is_trivially_destructible_v<T>);
344 void* newMemory =
nullptr;
345 if (elementCapacity <= SmallCapacity) {
346 newMemory = malloc(newCapacity *
sizeof(T));
348 throw std::bad_alloc{};
350 std::uninitialized_copy_n(elements, elementCapacity,
static_cast<pointer>(newMemory));
352 newMemory = realloc(elements, newCapacity *
sizeof(T));
354 throw std::bad_alloc{};
357 elements =
static_cast<pointer>(newMemory);
358 static_assert(std::is_nothrow_default_constructible_v<T>);
359 std::uninitialized_default_construct_n(elements + elementCapacity, newCapacity - elementCapacity);
361 const pointer newElements = std::allocator_traits<Allocator>::allocate(allocator, newCapacity);
364 while (newElementCapacity < elementCount) {
365 std::allocator_traits<Allocator>::construct(allocator, std::to_address(newElements) + newElementCapacity,
366 std::move_if_noexcept(elements[newElementCapacity]));
367 ++newElementCapacity;
369 while (newElementCapacity < newCapacity) {
370 std::allocator_traits<Allocator>::construct(allocator, std::to_address(newElements) + newElementCapacity);
371 ++newElementCapacity;
374 while (newElementCapacity-- > 0) {
375 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(newElements) + newElementCapacity);
377 std::allocator_traits<Allocator>::deallocate(allocator, newElements, newCapacity);
380 if (elementCapacity > SmallCapacity) {
381 for (
size_type i = 0; i < elementCapacity; ++i) {
382 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(elements) + i);
384 std::allocator_traits<Allocator>::deallocate(allocator, elements, elementCapacity);
386 elements = newElements;
388 elementCapacity = newCapacity;
393 return elementCapacity;
410 std::rotate(it,
end() - 1,
end());
418 std::rotate(it,
end() - 1,
end());
425 throw std::length_error{
"count > max_size() - size()"};
436 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
439 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
442 throw std::length_error{
"count > max_size() - size()"};
445 while (first != last) {
451 while (first != last) {
458 return begin() + offset;
462 return insert(pos, ilist.begin(), ilist.end());
465 template <
typename R>
467 return insert(pos, std::begin(r), std::end(r));
470 template <
typename R>
479 std::rotate(it,
end() - 1,
end());
486 throw std::length_error{
"count > max_size() - size()"};
489 elementCount +=
count;
499 std::rotate(it, it + 1,
end());
517 reserveAtLeast(
size() + 1);
518 T& result = elements[elementCount];
542 reserveAtLeast(
count);
543 elementCount =
count;
548 reserveAtLeast(
count);
551 elementCount =
count;
556 reserveAtLeast(
count);
558 overwrite(elements[i]);
561 elementCount =
count;
565 std::allocator_traits<Allocator>::propagate_on_container_swap::value ||
566 (std::allocator_traits<Allocator>::is_always_equal::value && std::is_nothrow_swappable_v<T> && std::is_nothrow_move_constructible_v<T>)) {
567 if (
this == &other) {
570 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_swap::value && !std::allocator_traits<Allocator>::is_always_equal::value) {
571 if (allocator != other.allocator) {
572 throw std::logic_error{
"get_allocator() != other.get_allocator()"};
575 if (elementCapacity <= SmallCapacity || other.elementCapacity <= SmallCapacity) {
577 *
this = std::move(other);
578 other = std::move(temporary);
581 swap(elements, other.elements);
582 swap(elementCount, other.elementCount);
583 swap(elementCapacity, other.elementCapacity);
585 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
587 swap(allocator, other.allocator);
596 return std::lexicographical_compare(
begin(),
end(), other.
begin(), other.
end());
600 return !(other < *
this);
604 return other < *
this;
608 return !(*
this < other);
615 template <
typename U>
623 template <
typename Predicate>
632 void reset() noexcept {
633 if (elementCapacity > SmallCapacity) {
634 for (size_type i = 0; i < elementCapacity; ++i) {
635 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(elements) + i);
637 if constexpr (USE_MALLOC_AND_REALLOC) {
640 std::allocator_traits<Allocator>::deallocate(allocator, elements, elementCapacity);
643 elements = smallElements;
645 elementCapacity = SmallCapacity;
648 void reserveAtLeast(size_type newCapacity) {
649 if (
const size_type cap = capacity(); newCapacity > cap) {
651 const size_type maxSize = max_size();
652 const size_type halfCap = cap / 2;
653 if (newCapacity > maxSize || halfCap > maxSize - cap) {
654 throw std::length_error{
"newCapacity > max_size()"};
656 newCapacity = std::max(newCapacity, cap + halfCap);
657 if constexpr (USE_MALLOC_AND_REALLOC) {
658 if (newCapacity > std::numeric_limits<size_type>::max() /
sizeof(T)) {
659 throw std::bad_array_new_length{};
661 static_assert(std::is_trivially_copyable_v<T>);
662 static_assert(std::is_trivially_destructible_v<T>);
663 void* newMemory =
nullptr;
664 if (elementCapacity <= SmallCapacity) {
665 newMemory = malloc(newCapacity *
sizeof(T));
667 throw std::bad_alloc{};
669 std::uninitialized_copy_n(elements, elementCapacity,
static_cast<pointer>(newMemory));
671 newMemory = realloc(elements, newCapacity *
sizeof(T));
673 throw std::bad_alloc{};
676 elements =
static_cast<pointer>(newMemory);
677 static_assert(std::is_nothrow_default_constructible_v<T>);
678 std::uninitialized_default_construct_n(elements + elementCapacity, newCapacity - elementCapacity);
680 const pointer newElements = std::allocator_traits<Allocator>::allocate(allocator, newCapacity);
681 size_type newElementCapacity = 0;
683 while (newElementCapacity < elementCount) {
684 std::allocator_traits<Allocator>::construct(allocator, std::to_address(newElements) + newElementCapacity,
685 std::move_if_noexcept(elements[newElementCapacity]));
686 ++newElementCapacity;
688 while (newElementCapacity < newCapacity) {
689 std::allocator_traits<Allocator>::construct(allocator, std::to_address(newElements) + newElementCapacity);
690 ++newElementCapacity;
693 while (newElementCapacity-- > 0) {
694 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(newElements) + newElementCapacity);
696 std::allocator_traits<Allocator>::deallocate(allocator, newElements, newCapacity);
699 if (elementCapacity > SmallCapacity) {
700 for (size_type i = 0; i < elementCapacity; ++i) {
701 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(elements) + i);
703 std::allocator_traits<Allocator>::deallocate(allocator, elements, elementCapacity);
705 elements = newElements;
707 elementCapacity = newCapacity;
712 size_type elementCount = 0;
713 size_type elementCapacity = SmallCapacity;
714 T smallElements[SmallCapacity];
715 [[no_unique_address]] allocator_type allocator;
722template <
typename T, std::
size_t SmallCapacity>
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition SmallBuffer.hpp:27
constexpr void resize_and_overwrite_added_values(size_type count, auto overwrite)
Definition SmallBuffer.hpp:554
const_pointer data() const noexcept
Definition SmallBuffer.hpp:264
SmallBuffer(const Allocator &allocator) noexcept
Definition SmallBuffer.hpp:49
reverse_iterator rbegin() noexcept
Definition SmallBuffer.hpp:292
SmallBuffer(InputIterator first, Sentinel last, const Allocator &allocator=Allocator())
Definition SmallBuffer.hpp:65
typename std::allocator_traits< Allocator >::const_pointer const_pointer
Definition SmallBuffer.hpp:40
iterator insert(const_iterator pos, const T &value)
Definition SmallBuffer.hpp:406
const_reference at(size_type pos) const
Definition SmallBuffer.hpp:217
iterator insert(const_iterator pos, std::initializer_list< T > ilist)
Definition SmallBuffer.hpp:461
~SmallBuffer()
Definition SmallBuffer.hpp:73
const_reverse_iterator rend() const noexcept
Definition SmallBuffer.hpp:308
reference back()
Definition SmallBuffer.hpp:248
friend size_type erase_if(SmallBuffer &c, Predicate predicate)
Definition SmallBuffer.hpp:624
SmallBuffer(const SmallBuffer &other, const Allocator &allocator)
Definition SmallBuffer.hpp:80
bool operator>(const SmallBuffer &other) const
Definition SmallBuffer.hpp:603
iterator insert(const_iterator pos, size_type count, const T &value)
Definition SmallBuffer.hpp:422
void resize(size_type count)
Definition SmallBuffer.hpp:541
value_type & reference
Definition SmallBuffer.hpp:37
void assign_range(R &&r)
Definition SmallBuffer.hpp:202
friend size_type erase(SmallBuffer &c, const U &value)
Definition SmallBuffer.hpp:616
pointer data() noexcept
Definition SmallBuffer.hpp:260
void assign(std::initializer_list< T > ilist)
Definition SmallBuffer.hpp:197
SmallBuffer & operator=(std::initializer_list< T > ilist)
Definition SmallBuffer.hpp:157
constexpr void push_back(T &&value)
Definition SmallBuffer.hpp:532
SmallBuffer(const SmallBuffer &other)
Definition SmallBuffer.hpp:77
friend void swap(SmallBuffer &a, SmallBuffer &b) noexcept
Definition SmallBuffer.hpp:611
reference at(size_type pos)
Definition SmallBuffer.hpp:210
constexpr void push_back(const T &value)
Definition SmallBuffer.hpp:528
bool operator>=(const SmallBuffer &other) const
Definition SmallBuffer.hpp:607
const_reverse_iterator crend() const noexcept
Definition SmallBuffer.hpp:312
const_reference operator[](size_type pos) const
Definition SmallBuffer.hpp:230
iterator insert_unspecified_value(const_iterator pos, size_type count)
Definition SmallBuffer.hpp:483
const_iterator end() const noexcept
Definition SmallBuffer.hpp:284
Allocator allocator_type
Definition SmallBuffer.hpp:34
void resize(size_type count, const value_type &value)
Definition SmallBuffer.hpp:546
iterator insert_unspecified_value(const_iterator pos)
Definition SmallBuffer.hpp:475
reference operator[](size_type pos)
Definition SmallBuffer.hpp:224
const_pointer const_iterator
Definition SmallBuffer.hpp:42
void reserve(size_type newCapacity)
Definition SmallBuffer.hpp:332
reverse_iterator rend() noexcept
Definition SmallBuffer.hpp:304
iterator begin() noexcept
Definition SmallBuffer.hpp:268
std::reverse_iterator< iterator > reverse_iterator
Definition SmallBuffer.hpp:43
iterator end() noexcept
Definition SmallBuffer.hpp:280
SmallBuffer & operator=(SmallBuffer &&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 SmallBuffer.hpp:120
bool operator==(const SmallBuffer &other) const
Definition SmallBuffer.hpp:591
pointer iterator
Definition SmallBuffer.hpp:41
iterator erase(const_iterator pos)
Definition SmallBuffer.hpp:495
iterator insert_range(const_iterator pos, R &&r)
Definition SmallBuffer.hpp:466
typename std::allocator_traits< Allocator >::size_type size_type
Definition SmallBuffer.hpp:35
const_iterator begin() const noexcept
Definition SmallBuffer.hpp:272
const_reference back() const
Definition SmallBuffer.hpp:254
const_reverse_iterator crbegin() const noexcept
Definition SmallBuffer.hpp:300
bool operator<=(const SmallBuffer &other) const
Definition SmallBuffer.hpp:599
typename std::allocator_traits< Allocator >::difference_type difference_type
Definition SmallBuffer.hpp:36
const_iterator cbegin() const noexcept
Definition SmallBuffer.hpp:276
iterator insert(const_iterator pos, InputIterator first, Sentinel last)
Definition SmallBuffer.hpp:437
const_iterator cend() const noexcept
Definition SmallBuffer.hpp:288
void assign(InputIterator first, Sentinel last)
Definition SmallBuffer.hpp:177
SmallBuffer(size_type count, const T &value, const Allocator &allocator=Allocator())
Definition SmallBuffer.hpp:54
iterator erase(const_iterator first, const_iterator last)
Definition SmallBuffer.hpp:504
SmallBuffer(std::initializer_list< T > ilist, const Allocator &allocator=Allocator())
Definition SmallBuffer.hpp:70
void pop_back()
Definition SmallBuffer.hpp:536
const_reference front() const
Definition SmallBuffer.hpp:242
reference front()
Definition SmallBuffer.hpp:236
size_type max_size() const noexcept
Definition SmallBuffer.hpp:324
void assign(size_type count, const T &value)
Definition SmallBuffer.hpp:162
allocator_type get_allocator() const noexcept
Definition SmallBuffer.hpp:206
SmallBuffer(SmallBuffer &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition SmallBuffer.hpp:83
SmallBuffer(SmallBuffer &&other, const Allocator &allocator)
Definition SmallBuffer.hpp:87
bool operator<(const SmallBuffer &other) const
Definition SmallBuffer.hpp:595
size_type capacity() const noexcept
Definition SmallBuffer.hpp:392
constexpr reference push_back_and_overwrite(auto overwrite)
Definition SmallBuffer.hpp:516
SmallBuffer() noexcept(noexcept(Allocator()))
Definition SmallBuffer.hpp:46
bool empty() const noexcept
Definition SmallBuffer.hpp:316
void swap(SmallBuffer &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 SmallBuffer.hpp:564
size_type size() const noexcept
Definition SmallBuffer.hpp:320
void clear() noexcept
Definition SmallBuffer.hpp:402
void shrink_to_fit()
Definition SmallBuffer.hpp:396
SmallBuffer(size_type count, const Allocator &allocator=Allocator())
Definition SmallBuffer.hpp:59
void append_range(R &&r)
Definition SmallBuffer.hpp:471
const value_type & const_reference
Definition SmallBuffer.hpp:38
constexpr reference push_back_unspecified_value()
Definition SmallBuffer.hpp:524
T value_type
Definition SmallBuffer.hpp:33
const_reverse_iterator rbegin() const noexcept
Definition SmallBuffer.hpp:296
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition SmallBuffer.hpp:44
SmallBuffer & operator=(const SmallBuffer &other)
Definition SmallBuffer.hpp:104
iterator insert(const_iterator pos, T &&value)
Definition SmallBuffer.hpp:414
Definition concepts.hpp:106
@ T
't'
Definition keyboard.hpp:90
Definition Allocation.hpp:554
grem::SmallBuffer< T, SmallCapacity, std::pmr::polymorphic_allocator< T > > SmallBuffer
Definition SmallBuffer.hpp:723
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