4#ifndef GREM_CORE_DATA_BUFFER_HPP
5#define GREM_CORE_DATA_BUFFER_HPP
14#include <initializer_list>
18#include <memory_resource>
27template <
typename T,
typename Allocator = std::allocator<T>>
30 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> &&
31 std::is_nothrow_default_constructible_v<T> &&
alignof(T) <=
alignof(std::max_align_t);
36 using size_type =
typename std::allocator_traits<Allocator>::size_type;
37 using difference_type =
typename std::allocator_traits<Allocator>::difference_type;
40 using pointer =
typename std::allocator_traits<Allocator>::pointer;
41 using const_pointer =
typename std::allocator_traits<Allocator>::const_pointer;
49 explicit
Buffer(const Allocator& allocator) noexcept
50 : allocator(allocator) {}
62 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
63 Buffer(InputIterator first, Sentinel last,
const Allocator& allocator = Allocator())
68 Buffer(std::initializer_list<T> ilist,
const Allocator& allocator = Allocator())
76 :
Buffer(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.
get_allocator())) {}
82 :
Buffer(std::move(other), other.get_allocator()) {}
85 : elements(std::exchange(other.elements,
nullptr))
86 , elementCount(std::exchange(other.elementCount,
size_type{0}))
87 , elementCapacity(std::exchange(other.elementCapacity,
size_type{0}))
88 , allocator(allocator) {}
94 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
95 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
107 std::allocator_traits<
108 Allocator>::propagate_on_container_move_assignment::value ||
109 std::allocator_traits<Allocator>::is_always_equal::value) {
110 if (
this == &other) {
113 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value && !std::allocator_traits<Allocator>::is_always_equal::value) {
114 if (allocator != other.allocator) {
117 moveAppend(other.begin(), other.end());
123 elements = std::exchange(other.elements,
nullptr);
124 elementCount = std::exchange(other.elementCount,
size_type{0});
125 elementCapacity = std::exchange(other.elementCapacity,
size_type{0});
126 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
127 allocator = other.allocator;
138 if constexpr (std::is_copy_assignable_v<T>) {
139 if (
count < elementCount) {
140 elementCount =
count;
153 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
154 void assign(InputIterator first, Sentinel last) {
155 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
157 if constexpr (std::is_copy_assignable_v<T>) {
158 if (
count < elementCount) {
159 elementCount =
count;
163 elements[index] = *first++;
169 copyAppend(first, last);
172 copyAppend(first, last);
176 void assign(std::initializer_list<T> ilist) {
177 assign(ilist.begin(), ilist.end());
180 template <
typename R>
182 assign(std::begin(r), std::end(r));
190 if (pos >= elementCount) {
191 throw std::out_of_range{
"pos >= size()"};
193 return elements[pos];
197 if (pos >= elementCount) {
198 throw std::out_of_range{
"pos >= size()"};
200 return elements[pos];
206 return elements[pos];
212 return elements[pos];
230 return elements[elementCount - 1];
236 return elements[elementCount - 1];
260 return elements + elementCount;
264 return elements + elementCount;
295 [[nodiscard]]
bool empty() const noexcept {
296 return elementCount == 0;
305 static_cast<size_type>(std::numeric_limits<difference_type>::max()),
306 static_cast<size_type>(std::numeric_limits<size_type>::max() /
sizeof(T)),
307 static_cast<size_type>(std::allocator_traits<Allocator>::max_size(allocator)),
314 throw std::length_error{
"newCapacity > max_size()"};
316 if constexpr (USE_MALLOC_AND_REALLOC) {
317 if (newCapacity > std::numeric_limits<size_type>::max() /
sizeof(T)) {
318 throw std::bad_array_new_length{};
320 static_assert(std::is_trivially_copyable_v<T>);
321 static_assert(std::is_trivially_destructible_v<T>);
322 void*
const newMemory = realloc(elements, newCapacity *
sizeof(T));
324 throw std::bad_alloc{};
326 elements =
static_cast<pointer>(newMemory);
327 static_assert(std::is_nothrow_default_constructible_v<T>);
328 if constexpr (std::is_trivially_default_constructible_v<T>) {
329 std::uninitialized_default_construct(std::to_address(elements) + elementCapacity, std::to_address(elements) + newCapacity);
330 elementCapacity = newCapacity;
332 while (elementCapacity < newCapacity) {
333 std::allocator_traits<Allocator>::construct(this->allocator, std::to_address(elements) + elementCapacity);
338 if constexpr (std::is_nothrow_move_constructible_v<T>) {
339 *
this =
Buffer(MoveTag{},
begin(),
end(), newCapacity, allocator);
348 return elementCapacity;
353 if constexpr (std::is_nothrow_move_constructible_v<T>) {
369 std::rotate(it,
end() - 1,
end());
377 std::rotate(it,
end() - 1,
end());
384 throw std::length_error{
"count > max_size() - size()"};
395 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
398 copyAppend(first, last);
406 return insert(pos, ilist.begin(), ilist.end());
409 template <
typename R>
411 return insert(pos, std::begin(r), std::end(r));
414 template <
typename R>
423 std::rotate(it,
end() - 1,
end());
430 throw std::length_error{
"count > max_size() - size()"};
433 elementCount +=
count;
443 std::rotate(it, it + 1,
end());
456 elementCount -=
count;
461 reserveAtLeast(
size() + 1);
462 T& storage = elements[elementCount];
486 if (
count < elementCount) {
487 elementCount =
count;
488 }
else if (
count > elementCount) {
489 reserveAtLeast(
count);
490 elementCount =
count;
499 if (
count < elementCount) {
500 elementCount =
count;
501 }
else if (
count > elementCount) {
502 reserveAtLeast(
count);
503 while (elementCount <
count) {
504 overwrite(elements[elementCount]);
510 void swap(
Buffer& other)
noexcept(std::allocator_traits<Allocator>::propagate_on_container_swap::value ||
511 (std::allocator_traits<Allocator>::is_always_equal::value && std::is_nothrow_swappable_v<T>)) {
512 if (
this == &other) {
515 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_swap::value && !std::allocator_traits<Allocator>::is_always_equal::value) {
516 if (allocator != other.allocator) {
517 throw std::logic_error{
"get_allocator() != other.get_allocator()"};
521 swap(elements, other.elements);
522 swap(elementCount, other.elementCount);
523 swap(elementCapacity, other.elementCapacity);
524 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
525 swap(allocator, other.allocator);
534 return std::lexicographical_compare(
begin(),
end(), other.
begin(), other.
end());
538 return !(other < *
this);
542 return other < *
this;
546 return !(*
this < other);
553 template <
typename U>
561 template <
typename Predicate>
572 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
575 if constexpr (USE_MALLOC_AND_REALLOC) {
576 if (
capacity > std::numeric_limits<size_type>::max() /
sizeof(T)) {
577 throw std::bad_array_new_length{};
579 void*
const newMemory = malloc(
capacity *
sizeof(T));
581 throw std::bad_alloc{};
583 elements =
static_cast<pointer>(newMemory);
585 elements = std::allocator_traits<Allocator>::allocate(this->allocator,
capacity);
587 if constexpr (std::is_trivially_default_constructible_v<T>) {
588 std::uninitialized_default_construct(std::to_address(elements), std::to_address(elements) +
capacity);
591 while (elementCapacity <
capacity) {
592 std::allocator_traits<Allocator>::construct(this->allocator, std::to_address(elements) + elementCapacity);
596 moveAppend(first, last);
599 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
602 if constexpr (USE_MALLOC_AND_REALLOC) {
603 if (
capacity > std::numeric_limits<size_type>::max() /
sizeof(T)) {
604 throw std::bad_array_new_length{};
606 void*
const newMemory = malloc(
capacity *
sizeof(T));
608 throw std::bad_alloc{};
610 elements =
static_cast<pointer>(newMemory);
612 elements = std::allocator_traits<Allocator>::allocate(this->allocator,
capacity);
614 if constexpr (std::is_trivially_default_constructible_v<T>) {
615 std::uninitialized_default_construct(std::to_address(elements), std::to_address(elements) +
capacity);
618 while (elementCapacity <
capacity) {
619 std::allocator_traits<Allocator>::construct(this->allocator, std::to_address(elements) + elementCapacity);
623 copyAppend(first, last);
626 void reset() noexcept {
628 T*
const pBegin = std::to_address(elements);
629 T*
const pEnd = pBegin + elementCapacity;
630 for (T* p = pBegin; p != pEnd; ++p) {
631 std::allocator_traits<Allocator>::destroy(allocator, p);
633 if constexpr (USE_MALLOC_AND_REALLOC) {
636 std::allocator_traits<Allocator>::deallocate(allocator, elements, elementCapacity);
644 void reserveAtLeast(
size_type newCapacity) {
648 if (newCapacity > maxSize || halfCap > maxSize - cap) {
649 throw std::length_error{
"newCapacity > max_size()"};
651 newCapacity = std::max(newCapacity, cap + halfCap);
652 if constexpr (USE_MALLOC_AND_REALLOC) {
653 if (newCapacity > std::numeric_limits<size_type>::max() /
sizeof(T)) {
654 throw std::bad_array_new_length{};
656 static_assert(std::is_trivially_copyable_v<T>);
657 static_assert(std::is_trivially_destructible_v<T>);
658 void*
const newMemory = realloc(elements, newCapacity *
sizeof(T));
660 throw std::bad_alloc{};
662 elements =
static_cast<pointer>(newMemory);
663 static_assert(std::is_nothrow_default_constructible_v<T>);
664 while (elementCapacity < newCapacity) {
665 std::allocator_traits<Allocator>::construct(this->allocator, std::to_address(elements) + elementCapacity);
669 if constexpr (std::is_nothrow_move_constructible_v<T>) {
670 *
this =
Buffer(MoveTag{},
begin(),
end(), newCapacity, allocator);
678 template <
typename InputIterator,
typename Sentinel>
679 void copyAppend(InputIterator first, Sentinel last) {
681 std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag> && std::is_nothrow_copy_assignable_v<T>) {
688 throw std::length_error{
"count > max_size() - size()"};
691 reserveAtLeast(newSize);
692 if constexpr (std::is_trivially_copyable_v<T> &&
requires { std::to_address(first); }) {
693 std::memcpy(std::to_address(elements) + offset, std::to_address(first),
count *
sizeof(T));
695 std::copy(first, last, elements + offset);
697 elementCount = newSize;
699 while (first != last) {
705 template <
typename InputIterator,
typename Sentinel>
706 void moveAppend(InputIterator first, Sentinel last) {
708 std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag> && std::is_nothrow_move_assignable_v<T>) {
715 throw std::length_error{
"count > max_size() - size()"};
718 reserveAtLeast(newSize);
719 if constexpr (std::is_trivially_copyable_v<T> &&
requires { std::to_address(first); }) {
720 std::memcpy(std::to_address(elements) + offset, std::to_address(first),
count *
sizeof(T));
722 std::move(first, last, elements + offset);
724 elementCount = newSize;
726 while (first != last) {
735 [[no_unique_address]] Allocator allocator;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
reverse_iterator rend() noexcept
Definition Buffer.hpp:283
reference back()
Definition Buffer.hpp:227
bool empty() const noexcept
Definition Buffer.hpp:295
iterator erase(const_iterator first, const_iterator last)
Definition Buffer.hpp:448
size_type max_size() const noexcept
Definition Buffer.hpp:303
reference at(size_type pos)
Definition Buffer.hpp:189
iterator insert_unspecified_value(const_iterator pos, size_type count)
Definition Buffer.hpp:427
const_iterator cend() const noexcept
Definition Buffer.hpp:267
void resize(size_type count)
Definition Buffer.hpp:485
iterator insert(const_iterator pos, InputIterator first, Sentinel last)
Definition Buffer.hpp:396
Buffer(const Buffer &other)
Definition Buffer.hpp:75
friend void swap(Buffer &a, Buffer &b) noexcept
Definition Buffer.hpp:549
const_iterator begin() const noexcept
Definition Buffer.hpp:251
const_reverse_iterator rend() const noexcept
Definition Buffer.hpp:287
void resize(size_type count, const value_type &value)
Definition Buffer.hpp:494
typename std::allocator_traits< Allocator >::const_pointer const_pointer
Definition Buffer.hpp:41
T value_type
Definition Buffer.hpp:34
bool operator<(const Buffer &other) const
Definition Buffer.hpp:533
const value_type & const_reference
Definition Buffer.hpp:39
iterator insert(const_iterator pos, std::initializer_list< T > ilist)
Definition Buffer.hpp:405
const_pointer data() const noexcept
Definition Buffer.hpp:243
Buffer(std::initializer_list< T > ilist, const Allocator &allocator=Allocator())
Definition Buffer.hpp:68
const_reference front() const
Definition Buffer.hpp:221
Allocator allocator_type
Definition Buffer.hpp:35
typename std::allocator_traits< Allocator >::size_type size_type
Definition Buffer.hpp:36
void assign_range(R &&r)
Definition Buffer.hpp:181
void clear() noexcept
Definition Buffer.hpp:361
value_type & reference
Definition Buffer.hpp:38
const_iterator end() const noexcept
Definition Buffer.hpp:263
const_pointer const_iterator
Definition Buffer.hpp:43
reference operator[](size_type pos)
Definition Buffer.hpp:203
friend size_type erase_if(Buffer &c, Predicate predicate)
Definition Buffer.hpp:562
Buffer() noexcept=default
pointer iterator
Definition Buffer.hpp:42
const_reference back() const
Definition Buffer.hpp:233
const_reference at(size_type pos) const
Definition Buffer.hpp:196
void assign(size_type count, const T &value)
Definition Buffer.hpp:137
reference front()
Definition Buffer.hpp:215
const_reference operator[](size_type pos) const
Definition Buffer.hpp:209
~Buffer()
Definition Buffer.hpp:71
const_reverse_iterator crend() const noexcept
Definition Buffer.hpp:291
size_type capacity() const noexcept
Definition Buffer.hpp:347
bool operator>(const Buffer &other) const
Definition Buffer.hpp:541
Buffer & operator=(Buffer &&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 Buffer.hpp:106
reverse_iterator rbegin() noexcept
Definition Buffer.hpp:271
Buffer(Buffer &&other) noexcept
Definition Buffer.hpp:81
Buffer(const Buffer &other, const Allocator &allocator)
Definition Buffer.hpp:78
iterator begin() noexcept
Definition Buffer.hpp:247
iterator insert_unspecified_value(const_iterator pos)
Definition Buffer.hpp:419
Buffer(InputIterator first, Sentinel last, const Allocator &allocator=Allocator())
Definition Buffer.hpp:63
void assign(InputIterator first, Sentinel last)
Definition Buffer.hpp:154
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition Buffer.hpp:45
iterator insert(const_iterator pos, size_type count, const T &value)
Definition Buffer.hpp:381
allocator_type get_allocator() const noexcept
Definition Buffer.hpp:185
typename std::allocator_traits< Allocator >::difference_type difference_type
Definition Buffer.hpp:37
void resize_and_overwrite_added_values(size_type count, auto overwrite)
Definition Buffer.hpp:498
const_reverse_iterator rbegin() const noexcept
Definition Buffer.hpp:275
void push_back(const T &value)
Definition Buffer.hpp:472
Buffer(Buffer &&other, const Allocator &allocator) noexcept
Definition Buffer.hpp:84
iterator insert(const_iterator pos, T &&value)
Definition Buffer.hpp:373
void shrink_to_fit()
Definition Buffer.hpp:351
iterator insert(const_iterator pos, const T &value)
Definition Buffer.hpp:365
reference push_back_unspecified_value()
Definition Buffer.hpp:468
size_type size() const noexcept
Definition Buffer.hpp:299
bool operator<=(const Buffer &other) const
Definition Buffer.hpp:537
Buffer & operator=(const Buffer &other)
Definition Buffer.hpp:90
iterator insert_range(const_iterator pos, R &&r)
Definition Buffer.hpp:410
void pop_back()
Definition Buffer.hpp:480
Buffer & operator=(std::initializer_list< T > ilist) noexcept
Definition Buffer.hpp:132
void assign(std::initializer_list< T > ilist)
Definition Buffer.hpp:176
friend size_type erase(Buffer &c, const U &value)
Definition Buffer.hpp:554
const_reverse_iterator crbegin() const noexcept
Definition Buffer.hpp:279
Buffer(size_type count, const Allocator &allocator=Allocator())
Definition Buffer.hpp:57
pointer data() noexcept
Definition Buffer.hpp:239
const_iterator cbegin() const noexcept
Definition Buffer.hpp:255
void swap(Buffer &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 Buffer.hpp:510
void append_range(R &&r)
Definition Buffer.hpp:415
void push_back(T &&value)
Definition Buffer.hpp:476
bool operator>=(const Buffer &other) const
Definition Buffer.hpp:545
iterator erase(const_iterator pos)
Definition Buffer.hpp:439
Buffer(size_type count, const T &value, const Allocator &allocator=Allocator())
Definition Buffer.hpp:52
void reserve(size_type newCapacity)
Definition Buffer.hpp:311
bool operator==(const Buffer &other) const
Definition Buffer.hpp:529
iterator end() noexcept
Definition Buffer.hpp:259
typename std::allocator_traits< Allocator >::pointer pointer
Definition Buffer.hpp:40
reference push_back_and_overwrite(auto overwrite)
Definition Buffer.hpp:460
std::reverse_iterator< iterator > reverse_iterator
Definition Buffer.hpp:44
Definition concepts.hpp:106
@ T
't'
Definition keyboard.hpp:90
Definition Allocation.hpp:554
grem::Buffer< T, std::pmr::polymorphic_allocator< T > > Buffer
Definition Buffer.hpp:743
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234