4#ifndef GREM_CORE_DATA_INPLACE_BUFFER_HPP
5#define GREM_CORE_DATA_INPLACE_BUFFER_HPP
15#include <initializer_list>
23template <
typename T, std::
size_t N>
28 std::conditional_t<N <= 255ull, std::uint8_t,
29 std::conditional_t<N <= 65535ull, std::uint16_t,
30 std::conditional_t<N <= 4294967295ull, std::uint32_t,
57 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
79 std::is_nothrow_move_constructible_v<T>)
80 requires(!std::is_trivially_copyable_v<T>) {
81 *
this = std::move(other);
90 assign(other.begin(), other.end());
97 std::is_nothrow_move_constructible_v<T> &&
98 std::is_nothrow_move_assignable_v<T>) requires(!std::is_trivially_copyable_v<T>) {
102 if (
size() == other.size()) {
103 std::move(other.begin(), other.end(), begin());
108 std::move(other.begin(), other.end(), elements);
109 elementCount = std::exchange(other.elementCount, Size{0});
119 if constexpr (std::is_copy_assignable_v<T>) {
121 elementCount =
static_cast<Size
>(
count);
132 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
133 constexpr void assign(InputIterator first, Sentinel last) {
134 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
136 if constexpr (std::is_copy_assignable_v<T>) {
138 elementCount =
static_cast<Size
>(
count);
141#if defined(__GNUC__) && !defined(__clang__)
142#pragma GCC diagnostic push
143#pragma GCC diagnostic ignored "-Wstringop-overflow"
145 elements[index] = *first++;
146#if defined(__GNUC__) && !defined(__clang__)
147#pragma GCC diagnostic pop
160 constexpr void assign(std::initializer_list<T> ilist) {
161 assign(ilist.begin(), ilist.end());
164 template <
typename R>
166 assign(std::begin(r), std::end(r));
171 throw std::out_of_range{
"pos >= size()"};
173 return elements[pos];
178 throw std::out_of_range{
"pos >= size()"};
180 return elements[pos];
185 return elements[pos];
190 return elements[pos];
205 return elements[
size() - 1];
210 return elements[
size() - 1];
234 return elements +
size();
238 return elements +
size();
269 [[nodiscard]]
constexpr bool empty() const noexcept {
274 return static_cast<size_type>(elementCount);
293 std::rotate(it,
end() - 1,
end());
301 std::rotate(it,
end() - 1,
end());
308 throw std::length_error{
"count > max_size() - size()"};
318 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
321 copyAppend(first, last);
329 return insert(pos, ilist.begin(), ilist.end());
332 template <
typename R>
334 return insert(pos, std::begin(r), std::end(r));
337 template <
typename R>
346 std::rotate(it,
end() - 1,
end());
353 throw std::length_error{
"count > max_size() - size()"};
355 elementCount =
static_cast<Size
>(
size() +
count);
365 std::rotate(it, it + 1,
end());
378 elementCount -=
count;
384 throw std::length_error{
"size() >= max_size()"};
386 T& result = elements[elementCount];
411 throw std::length_error{
"count > max_size()"};
413 elementCount =
static_cast<Size
>(
count);
419 throw std::length_error{
"count > max_size()"};
423 elementCount =
static_cast<Size
>(
count);
429 throw std::length_error{
"count > max_size()"};
431 for (Size i = elementCount; i <
count; ++i) {
432 overwrite(elements[i]);
435 elementCount =
static_cast<Size
>(
count);
439 if (
this == &other) {
443 *
this = std::move(other);
444 other = std::move(temporary);
452 return std::lexicographical_compare(
begin(),
end(), other.
begin(), other.
end());
456 return !(other < *
this);
460 return other < *
this;
464 return !(*
this < other);
471 template <
typename U>
479 template <
typename Predicate>
488 template <
typename InputIterator,
typename Sentinel>
489 void copyAppend(InputIterator first, Sentinel last) {
491 std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag> && std::is_nothrow_copy_assignable_v<T>) {
498 throw std::length_error{
"count > max_size() - size()"};
501 if constexpr (std::is_trivially_copyable_v<T> &&
requires { std::to_address(first); }) {
502 std::memcpy(
data() + offset, std::to_address(first),
count *
sizeof(T));
504 std::copy(first, last,
data() + offset);
506 elementCount =
static_cast<Size
>(newSize);
508 while (first != last) {
514 Size elementCount = 0;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
constexpr void clear() noexcept
Definition InplaceBuffer.hpp:285
const_pointer const_iterator
Definition InplaceBuffer.hpp:43
constexpr void append_range(R &&r)
Definition InplaceBuffer.hpp:338
constexpr InplaceBuffer & operator=(const InplaceBuffer &)=default
friend constexpr size_type erase(InplaceBuffer &c, const U &value)
Definition InplaceBuffer.hpp:472
constexpr void push_back(T &&value)
Definition InplaceBuffer.hpp:400
std::ptrdiff_t difference_type
Definition InplaceBuffer.hpp:37
constexpr const_reverse_iterator crend() const noexcept
Definition InplaceBuffer.hpp:265
std::size_t size_type
Definition InplaceBuffer.hpp:36
constexpr size_type capacity() const noexcept
Definition InplaceBuffer.hpp:281
constexpr InplaceBuffer & operator=(InplaceBuffer &&) noexcept=default
constexpr InplaceBuffer(size_type count)
Definition InplaceBuffer.hpp:53
constexpr const_iterator cbegin() const noexcept
Definition InplaceBuffer.hpp:229
constexpr size_type max_size() const noexcept
Definition InplaceBuffer.hpp:277
constexpr void resize_and_overwrite_added_values(size_type count, auto overwrite)
Definition InplaceBuffer.hpp:426
T value_type
Definition InplaceBuffer.hpp:35
constexpr iterator end() noexcept
Definition InplaceBuffer.hpp:233
constexpr pointer data() noexcept
Definition InplaceBuffer.hpp:213
constexpr iterator insert(const_iterator pos, T &&value)
Definition InplaceBuffer.hpp:297
constexpr void swap(InplaceBuffer &other) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition InplaceBuffer.hpp:438
pointer iterator
Definition InplaceBuffer.hpp:42
constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last)
Definition InplaceBuffer.hpp:319
constexpr const_reverse_iterator crbegin() const noexcept
Definition InplaceBuffer.hpp:253
constexpr InplaceBuffer(const InplaceBuffer &)=default
const value_type & const_reference
Definition InplaceBuffer.hpp:39
constexpr bool operator<=(const InplaceBuffer &other) const
Definition InplaceBuffer.hpp:455
constexpr const_iterator begin() const noexcept
Definition InplaceBuffer.hpp:225
constexpr reference push_back_and_overwrite(auto overwrite)
Definition InplaceBuffer.hpp:382
friend constexpr void swap(InplaceBuffer &a, InplaceBuffer &b) noexcept(noexcept(a.swap(b)))
Definition InplaceBuffer.hpp:467
constexpr void pop_back()
Definition InplaceBuffer.hpp:404
constexpr reference operator[](size_type pos)
Definition InplaceBuffer.hpp:183
constexpr const_iterator cend() const noexcept
Definition InplaceBuffer.hpp:241
constexpr InplaceBuffer(const InplaceBuffer &other)
Definition InplaceBuffer.hpp:73
constexpr bool operator>=(const InplaceBuffer &other) const
Definition InplaceBuffer.hpp:463
constexpr InplaceBuffer & operator=(const InplaceBuffer &other)
Definition InplaceBuffer.hpp:86
constexpr const_reference operator[](size_type pos) const
Definition InplaceBuffer.hpp:188
constexpr iterator erase(const_iterator first, const_iterator last)
Definition InplaceBuffer.hpp:370
constexpr InplaceBuffer(InputIterator first, Sentinel last)
Definition InplaceBuffer.hpp:58
constexpr void push_back(const T &value)
Definition InplaceBuffer.hpp:396
constexpr void assign(size_type count, const T &value)
Definition InplaceBuffer.hpp:118
constexpr const_reverse_iterator rbegin() const noexcept
Definition InplaceBuffer.hpp:249
value_type & reference
Definition InplaceBuffer.hpp:38
constexpr const_reference back() const
Definition InplaceBuffer.hpp:208
constexpr const_reference front() const
Definition InplaceBuffer.hpp:198
constexpr ~InplaceBuffer()
Definition InplaceBuffer.hpp:67
constexpr size_type size() const noexcept
Definition InplaceBuffer.hpp:273
constexpr bool operator>(const InplaceBuffer &other) const
Definition InplaceBuffer.hpp:459
constexpr iterator insert_unspecified_value(const_iterator pos)
Definition InplaceBuffer.hpp:342
constexpr iterator insert_range(const_iterator pos, R &&r)
Definition InplaceBuffer.hpp:333
constexpr reference front()
Definition InplaceBuffer.hpp:193
constexpr void resize(size_type count)
Definition InplaceBuffer.hpp:409
constexpr iterator erase(const_iterator pos)
Definition InplaceBuffer.hpp:361
constexpr InplaceBuffer(InplaceBuffer &&) noexcept=default
constexpr iterator begin() noexcept
Definition InplaceBuffer.hpp:221
constexpr const_iterator end() const noexcept
Definition InplaceBuffer.hpp:237
constexpr bool operator<(const InplaceBuffer &other) const
Definition InplaceBuffer.hpp:451
constexpr reverse_iterator rbegin() noexcept
Definition InplaceBuffer.hpp:245
constexpr InplaceBuffer & operator=(std::initializer_list< T > ilist)
Definition InplaceBuffer.hpp:113
constexpr void resize(size_type count, const value_type &value)
Definition InplaceBuffer.hpp:416
constexpr const_reverse_iterator rend() const noexcept
Definition InplaceBuffer.hpp:261
constexpr void assign(std::initializer_list< T > ilist)
Definition InplaceBuffer.hpp:160
constexpr reference back()
Definition InplaceBuffer.hpp:203
constexpr ~InplaceBuffer()=default
constexpr const_reference at(size_type pos) const
Definition InplaceBuffer.hpp:176
constexpr reverse_iterator rend() noexcept
Definition InplaceBuffer.hpp:257
friend constexpr size_type erase_if(InplaceBuffer &c, Predicate predicate)
Definition InplaceBuffer.hpp:480
constexpr InplaceBuffer() noexcept=default
constexpr const_pointer data() const noexcept
Definition InplaceBuffer.hpp:217
constexpr reference push_back_unspecified_value()
Definition InplaceBuffer.hpp:392
constexpr iterator insert(const_iterator pos, const T &value)
Definition InplaceBuffer.hpp:289
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition InplaceBuffer.hpp:45
constexpr iterator insert(const_iterator pos, size_type count, const T &value)
Definition InplaceBuffer.hpp:305
const value_type * const_pointer
Definition InplaceBuffer.hpp:41
constexpr void assign(InputIterator first, Sentinel last)
Definition InplaceBuffer.hpp:133
constexpr InplaceBuffer(std::initializer_list< T > ilist)
Definition InplaceBuffer.hpp:62
constexpr void assign_range(R &&r)
Definition InplaceBuffer.hpp:165
constexpr iterator insert(const_iterator pos, std::initializer_list< T > ilist)
Definition InplaceBuffer.hpp:328
constexpr reference at(size_type pos)
Definition InplaceBuffer.hpp:169
std::reverse_iterator< iterator > reverse_iterator
Definition InplaceBuffer.hpp:44
constexpr bool operator==(const InplaceBuffer &other) const
Definition InplaceBuffer.hpp:447
constexpr bool empty() const noexcept
Definition InplaceBuffer.hpp:269
constexpr iterator insert_unspecified_value(const_iterator pos, size_type count)
Definition InplaceBuffer.hpp:350
Definition concepts.hpp:106
@ N
'n'
Definition keyboard.hpp:84
@ T
't'
Definition keyboard.hpp:90
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234