GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Buffer.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_BUFFER_HPP
5#define GREM_CORE_DATA_BUFFER_HPP
6
8
10
11#include <algorithm> // std::min, std::max, std::rotate, std::equal, std::lexicographical_compare, std::remove, std::remove_if, std::copy, std::move, std::fill_n
12#include <cstddef> // std::max_align_t
13#include <cstring> // std::memcpy
14#include <initializer_list> // std::initializer_list
15#include <iterator> // std::reverse_iterator, std::input_iterator, std::sentinel_for, std::iterator_traits, std::random_access_iterator_tag, std::begin, std::end
16#include <limits> // std::numeric_limits
17#include <memory> // std::allocator, std::allocator_traits, std::to_address, std::uninitialized_...
18#include <memory_resource> // std::pmr::polymorphic_allocator
19#include <new> // std::launder, std::bad_alloc, std::bad_array_new_length
20#include <stdexcept> // std::out_of_range, std::length_error
21#include <stdlib.h> // malloc, realloc, free // NOLINT(modernize-deprecated-headers)
22#include <type_traits> // std::is_..._v
23#include <utility> // std::move, std::forward, std::exchange, std::swap
24
25namespace grem {
26
27template <typename T, typename Allocator = std::allocator<T>>
28class Buffer {
29private:
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);
32
33public:
34 using value_type = T;
35 using allocator_type = Allocator;
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;
44 using reverse_iterator = std::reverse_iterator<iterator>;
45 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
46
47 Buffer() noexcept = default;
48
49 explicit Buffer(const Allocator& allocator) noexcept
50 : allocator(allocator) {}
51
52 Buffer(size_type count, const T& value, const Allocator& allocator = Allocator())
53 : Buffer(allocator) {
54 resize(count, value);
55 }
56
57 explicit Buffer(size_type count, const Allocator& allocator = Allocator())
58 : Buffer(allocator) {
60 }
61
62 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
63 Buffer(InputIterator first, Sentinel last, const Allocator& allocator = Allocator())
64 : Buffer(allocator) {
65 assign(first, last);
66 }
67
68 Buffer(std::initializer_list<T> ilist, const Allocator& allocator = Allocator())
69 : Buffer(ilist.begin(), ilist.end(), allocator) {}
70
72 reset();
73 }
74
75 Buffer(const Buffer& other)
76 : Buffer(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.get_allocator())) {}
77
78 Buffer(const Buffer& other, const Allocator& allocator)
79 : Buffer(other.begin(), other.end(), allocator) {}
80
81 Buffer(Buffer&& other) noexcept
82 : Buffer(std::move(other), other.get_allocator()) {}
83
84 Buffer(Buffer&& other, const Allocator& allocator) noexcept // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
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) {}
89
90 Buffer& operator=(const Buffer& other) {
91 if (this == &other) {
92 return *this;
93 }
94 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
95 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
96 if (allocator != other.get_allocator()) {
97 reset();
98 }
99 }
100 allocator = other.get_allocator();
101 }
102 assign(other.begin(), other.end());
103 return *this;
104 }
105
106 Buffer& operator=(Buffer&& other) noexcept(
107 std::allocator_traits<
108 Allocator>::propagate_on_container_move_assignment::value || // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
109 std::allocator_traits<Allocator>::is_always_equal::value) {
110 if (this == &other) {
111 return *this;
112 }
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) {
115 clear();
116 reserve(other.size());
117 moveAppend(other.begin(), other.end());
118 other.clear();
119 return *this;
120 }
121 }
122 reset();
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;
128 }
129 return *this;
130 }
131
132 Buffer& operator=(std::initializer_list<T> ilist) noexcept {
133 assign(ilist);
134 return *this;
135 }
136
137 void assign(size_type count, const T& value) {
138 if constexpr (std::is_copy_assignable_v<T>) {
139 if (count < elementCount) {
140 elementCount = count;
141 }
142 reserve(count);
143 std::fill_n(begin(), size(), value);
144 } else {
145 clear();
146 reserve(count);
147 }
148 for (size_type index = size(); index < count; ++index) {
149 push_back(value);
150 }
151 }
152
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>) {
156 const size_type count = static_cast<size_type>(last - first);
157 if constexpr (std::is_copy_assignable_v<T>) {
158 if (count < elementCount) {
159 elementCount = count;
160 }
161 reserve(count);
162 for (size_type index = 0; index < size(); ++index) {
163 elements[index] = *first++;
164 }
165 } else {
166 clear();
167 reserve(count);
168 }
169 copyAppend(first, last);
170 } else {
171 clear();
172 copyAppend(first, last);
173 }
174 }
175
176 void assign(std::initializer_list<T> ilist) {
177 assign(ilist.begin(), ilist.end());
178 }
179
180 template <typename R>
181 void assign_range(R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
182 assign(std::begin(r), std::end(r));
183 }
184
185 [[nodiscard]] allocator_type get_allocator() const noexcept {
186 return allocator;
187 }
188
189 [[nodiscard]] reference at(size_type pos) {
190 if (pos >= elementCount) {
191 throw std::out_of_range{"pos >= size()"};
192 }
193 return elements[pos];
194 }
195
196 [[nodiscard]] const_reference at(size_type pos) const {
197 if (pos >= elementCount) {
198 throw std::out_of_range{"pos >= size()"};
199 }
200 return elements[pos];
201 }
202
203 [[nodiscard]] reference operator[](size_type pos) {
204 GREM_ASSERT(elements);
205 GREM_ASSERT(pos < elementCount);
206 return elements[pos];
207 }
208
209 [[nodiscard]] const_reference operator[](size_type pos) const {
210 GREM_ASSERT(elements);
211 GREM_ASSERT(pos < elementCount);
212 return elements[pos];
213 }
214
215 [[nodiscard]] reference front() {
216 GREM_ASSERT(elements);
217 GREM_ASSERT(!empty());
218 return elements[0];
219 }
220
221 [[nodiscard]] const_reference front() const {
222 GREM_ASSERT(elements);
223 GREM_ASSERT(!empty());
224 return elements[0];
225 }
226
227 [[nodiscard]] reference back() {
228 GREM_ASSERT(elements);
229 GREM_ASSERT(!empty());
230 return elements[elementCount - 1];
231 }
232
233 [[nodiscard]] const_reference back() const {
234 GREM_ASSERT(elements);
235 GREM_ASSERT(!empty());
236 return elements[elementCount - 1];
237 }
238
239 [[nodiscard]] pointer data() noexcept {
240 return elements;
241 }
242
243 [[nodiscard]] const_pointer data() const noexcept {
244 return elements;
245 }
246
247 [[nodiscard]] iterator begin() noexcept {
248 return elements;
249 }
250
251 [[nodiscard]] const_iterator begin() const noexcept {
252 return elements;
253 }
254
255 [[nodiscard]] const_iterator cbegin() const noexcept {
256 return begin();
257 }
258
259 [[nodiscard]] iterator end() noexcept {
260 return elements + elementCount;
261 }
262
263 [[nodiscard]] const_iterator end() const noexcept {
264 return elements + elementCount;
265 }
266
267 [[nodiscard]] const_iterator cend() const noexcept {
268 return end();
269 }
270
271 [[nodiscard]] reverse_iterator rbegin() noexcept {
272 return reverse_iterator{end()};
273 }
274
275 [[nodiscard]] const_reverse_iterator rbegin() const noexcept {
276 return const_reverse_iterator{end()};
277 }
278
279 [[nodiscard]] const_reverse_iterator crbegin() const noexcept {
280 return rbegin();
281 }
282
283 [[nodiscard]] reverse_iterator rend() noexcept {
284 return reverse_iterator{begin()};
285 }
286
287 [[nodiscard]] const_reverse_iterator rend() const noexcept {
289 }
290
291 [[nodiscard]] const_reverse_iterator crend() const noexcept {
292 return rend();
293 }
294
295 [[nodiscard]] bool empty() const noexcept {
296 return elementCount == 0;
297 }
298
299 [[nodiscard]] size_type size() const noexcept {
300 return elementCount;
301 }
302
303 [[nodiscard]] size_type max_size() const noexcept {
304 return std::min({
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)),
308 });
309 }
310
311 void reserve(size_type newCapacity) {
312 if (newCapacity > capacity()) {
313 if (newCapacity > max_size()) {
314 throw std::length_error{"newCapacity > max_size()"};
315 }
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{};
319 }
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)); // NOLINT(cppcoreguidelines-no-malloc, cppcoreguidelines-owning-memory)
323 if (!newMemory) {
324 throw std::bad_alloc{};
325 }
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;
331 } else {
332 while (elementCapacity < newCapacity) {
333 std::allocator_traits<Allocator>::construct(this->allocator, std::to_address(elements) + elementCapacity);
334 ++elementCapacity;
335 }
336 }
337 } else {
338 if constexpr (std::is_nothrow_move_constructible_v<T>) {
339 *this = Buffer(MoveTag{}, begin(), end(), newCapacity, allocator);
340 } else {
341 *this = Buffer(cbegin(), cend(), newCapacity, allocator);
342 }
343 }
344 }
345 }
346
347 [[nodiscard]] size_type capacity() const noexcept {
348 return elementCapacity;
349 }
350
352 if (size() < capacity()) {
353 if constexpr (std::is_nothrow_move_constructible_v<T>) {
354 *this = Buffer(MoveTag{}, begin(), end(), size(), allocator);
355 } else {
356 *this = Buffer(cbegin(), cend(), size(), allocator);
357 }
358 }
359 }
360
361 void clear() noexcept {
362 elementCount = 0;
363 }
364
365 iterator insert(const_iterator pos, const T& value) {
366 const difference_type offset = pos - cbegin();
367 push_back(value);
368 const iterator it = begin() + offset;
369 std::rotate(it, end() - 1, end());
370 return it;
371 }
372
374 const difference_type offset = pos - cbegin();
375 push_back(std::move(value));
376 const iterator it = begin() + offset;
377 std::rotate(it, end() - 1, end());
378 return it;
379 }
380
382 const difference_type offset = pos - cbegin();
383 if (count > max_size() - size()) {
384 throw std::length_error{"count > max_size() - size()"};
385 }
386 reserveAtLeast(size() + count);
387 for (size_type i = 0; i < count; ++i) {
388 push_back(value);
389 }
390 const iterator it = begin() + offset;
391 std::rotate(it, end() - static_cast<difference_type>(count), end());
392 return it;
393 }
394
395 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
396 iterator insert(const_iterator pos, InputIterator first, Sentinel last) {
397 const difference_type offset = pos - cbegin();
398 copyAppend(first, last);
399 const iterator it = begin() + offset;
400 const size_type count = size() - static_cast<size_type>(offset);
401 std::rotate(it, end() - static_cast<difference_type>(count), end());
402 return it;
403 }
404
405 iterator insert(const_iterator pos, std::initializer_list<T> ilist) {
406 return insert(pos, ilist.begin(), ilist.end());
407 }
408
409 template <typename R>
410 iterator insert_range(const_iterator pos, R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
411 return insert(pos, std::begin(r), std::end(r));
412 }
413
414 template <typename R>
415 void append_range(R&& r) {
416 insert_range(end(), std::forward<R>(r));
417 }
418
420 const difference_type offset = pos - cbegin();
422 const iterator it = begin() + offset;
423 std::rotate(it, end() - 1, end());
424 return it;
425 }
426
428 const difference_type offset = pos - cbegin();
429 if (count > max_size() - size()) {
430 throw std::length_error{"count > max_size() - size()"};
431 }
432 reserveAtLeast(size() + count);
433 elementCount += count;
434 const iterator it = begin() + offset;
435 std::rotate(it, end() - static_cast<difference_type>(count), end());
436 return it;
437 }
438
440 GREM_ASSERT(!empty());
441 const difference_type offset = pos - cbegin();
442 const iterator it = begin() + offset;
443 std::rotate(it, it + 1, end());
444 --elementCount;
445 return it;
446 }
447
449 const difference_type offset = first - cbegin();
450 const difference_type count = last - first;
451 GREM_ASSERT(offset <= static_cast<difference_type>(size()));
452 GREM_ASSERT(count >= 0);
453 GREM_ASSERT(offset + count <= static_cast<difference_type>(size()));
454 const iterator it = begin() + offset;
455 std::rotate(it, it + count, end());
456 elementCount -= count;
457 return it;
458 }
459
461 reserveAtLeast(size() + 1);
462 T& storage = elements[elementCount];
463 overwrite(storage);
464 ++elementCount;
465 return storage;
466 }
467
469 return push_back_and_overwrite([](T&) -> void {});
470 }
471
472 void push_back(const T& value) {
473 push_back_and_overwrite([&](T& storage) -> void { storage = value; });
474 }
475
476 void push_back(T&& value) {
477 push_back_and_overwrite([&](T& storage) -> void { storage = std::move(value); });
478 }
479
480 void pop_back() {
481 GREM_ASSERT(!empty());
482 --elementCount;
483 }
484
486 if (count < elementCount) {
487 elementCount = count;
488 } else if (count > elementCount) {
489 reserveAtLeast(count);
490 elementCount = count;
491 }
492 }
493
494 void resize(size_type count, const value_type& value) {
495 resize_and_overwrite_added_values(count, [&](T& storage) -> void { storage = value; });
496 }
497
499 if (count < elementCount) {
500 elementCount = count;
501 } else if (count > elementCount) {
502 reserveAtLeast(count);
503 while (elementCount < count) {
504 overwrite(elements[elementCount]);
505 ++elementCount;
506 }
507 }
508 }
509
510 void swap(Buffer& other) noexcept(std::allocator_traits<Allocator>::propagate_on_container_swap::value || // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
511 (std::allocator_traits<Allocator>::is_always_equal::value && std::is_nothrow_swappable_v<T>)) {
512 if (this == &other) {
513 return;
514 }
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()"};
518 }
519 }
520 using std::swap;
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);
526 }
527 }
528
529 [[nodiscard]] bool operator==(const Buffer& other) const {
530 return std::equal(begin(), end(), other.begin(), other.end());
531 }
532
533 [[nodiscard]] bool operator<(const Buffer& other) const {
534 return std::lexicographical_compare(begin(), end(), other.begin(), other.end());
535 }
536
537 [[nodiscard]] bool operator<=(const Buffer& other) const {
538 return !(other < *this);
539 }
540
541 [[nodiscard]] bool operator>(const Buffer& other) const {
542 return other < *this;
543 }
544
545 [[nodiscard]] bool operator>=(const Buffer& other) const {
546 return !(*this < other);
547 }
548
549 friend void swap(Buffer& a, Buffer& b) noexcept {
550 a.swap(b);
551 }
552
553 template <typename U>
554 friend size_type erase(Buffer& c, const U& value) {
555 const iterator it = std::remove(c.begin(), c.end(), value);
556 const size_type result = static_cast<size_type>(c.end() - it);
557 c.erase(it, c.end());
558 return result;
559 }
560
561 template <typename Predicate>
562 friend size_type erase_if(Buffer& c, Predicate predicate) {
563 const iterator it = std::remove_if(c.begin(), c.end(), predicate);
564 const size_type result = static_cast<size_type>(c.end() - it);
565 c.erase(it, c.end());
566 return result;
567 }
568
569private:
570 struct MoveTag {};
571
572 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
573 Buffer(MoveTag, InputIterator first, Sentinel last, size_type capacity, const Allocator& allocator)
574 : Buffer(allocator) {
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{};
578 }
579 void* const newMemory = malloc(capacity * sizeof(T)); // NOLINT(cppcoreguidelines-no-malloc, cppcoreguidelines-owning-memory)
580 if (!newMemory) {
581 throw std::bad_alloc{};
582 }
583 elements = static_cast<pointer>(newMemory);
584 } else {
585 elements = std::allocator_traits<Allocator>::allocate(this->allocator, capacity); // NOLINT(cppcoreguidelines-prefer-member-initializer)
586 }
587 if constexpr (std::is_trivially_default_constructible_v<T>) {
588 std::uninitialized_default_construct(std::to_address(elements), std::to_address(elements) + capacity);
589 elementCapacity = capacity;
590 } else {
591 while (elementCapacity < capacity) {
592 std::allocator_traits<Allocator>::construct(this->allocator, std::to_address(elements) + elementCapacity);
593 ++elementCapacity;
594 }
595 }
596 moveAppend(first, last);
597 }
598
599 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
600 Buffer(InputIterator first, Sentinel last, size_type capacity, const Allocator& allocator)
601 : Buffer(allocator) {
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{};
605 }
606 void* const newMemory = malloc(capacity * sizeof(T)); // NOLINT(cppcoreguidelines-no-malloc, cppcoreguidelines-owning-memory)
607 if (!newMemory) {
608 throw std::bad_alloc{};
609 }
610 elements = static_cast<pointer>(newMemory);
611 } else {
612 elements = std::allocator_traits<Allocator>::allocate(this->allocator, capacity); // NOLINT(cppcoreguidelines-prefer-member-initializer)
613 }
614 if constexpr (std::is_trivially_default_constructible_v<T>) {
615 std::uninitialized_default_construct(std::to_address(elements), std::to_address(elements) + capacity);
616 elementCapacity = capacity;
617 } else {
618 while (elementCapacity < capacity) {
619 std::allocator_traits<Allocator>::construct(this->allocator, std::to_address(elements) + elementCapacity);
620 ++elementCapacity;
621 }
622 }
623 copyAppend(first, last);
624 }
625
626 void reset() noexcept {
627 if (elements) {
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);
632 }
633 if constexpr (USE_MALLOC_AND_REALLOC) {
634 free(elements); // NOLINT(cppcoreguidelines-no-malloc, cppcoreguidelines-owning-memory)
635 } else {
636 std::allocator_traits<Allocator>::deallocate(allocator, elements, elementCapacity);
637 }
638 elements = nullptr;
639 elementCount = 0;
640 elementCapacity = 0;
641 }
642 }
643
644 void reserveAtLeast(size_type newCapacity) {
645 if (const size_type cap = capacity(); newCapacity > cap) {
646 const size_type maxSize = max_size();
647 const size_type halfCap = cap / 2;
648 if (newCapacity > maxSize || halfCap > maxSize - cap) {
649 throw std::length_error{"newCapacity > max_size()"};
650 }
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{};
655 }
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)); // NOLINT(cppcoreguidelines-no-malloc, cppcoreguidelines-owning-memory)
659 if (!newMemory) {
660 throw std::bad_alloc{};
661 }
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);
666 ++elementCapacity;
667 }
668 } else {
669 if constexpr (std::is_nothrow_move_constructible_v<T>) {
670 *this = Buffer(MoveTag{}, begin(), end(), newCapacity, allocator);
671 } else {
672 *this = Buffer(cbegin(), cend(), newCapacity, allocator);
673 }
674 }
675 }
676 }
677
678 template <typename InputIterator, typename Sentinel>
679 void copyAppend(InputIterator first, Sentinel last) {
680 if constexpr (
681 std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag> && std::is_nothrow_copy_assignable_v<T>) {
682 const size_type count = static_cast<size_type>(last - first);
683 if (count == 0) {
684 return;
685 }
686 const size_type offset = size();
687 if (count > max_size() - offset) {
688 throw std::length_error{"count > max_size() - size()"};
689 }
690 const size_type newSize = offset + count;
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));
694 } else {
695 std::copy(first, last, elements + offset);
696 }
697 elementCount = newSize;
698 } else {
699 while (first != last) {
700 push_back(*first++);
701 }
702 }
703 }
704
705 template <typename InputIterator, typename Sentinel>
706 void moveAppend(InputIterator first, Sentinel last) {
707 if constexpr (
708 std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag> && std::is_nothrow_move_assignable_v<T>) {
709 const size_type count = static_cast<size_type>(last - first);
710 if (count == 0) {
711 return;
712 }
713 const size_type offset = size();
714 if (count > max_size() - offset) {
715 throw std::length_error{"count > max_size() - size()"};
716 }
717 const size_type newSize = offset + count;
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));
721 } else {
722 std::move(first, last, elements + offset);
723 }
724 elementCount = newSize;
725 } else {
726 while (first != last) {
727 push_back(std::move(*first++));
728 }
729 }
730 }
731
732 pointer elements = nullptr;
733 size_type elementCount = 0;
734 size_type elementCapacity = 0;
735 [[no_unique_address]] Allocator allocator;
736};
737
738} // namespace grem
739
740namespace grem::pmr {
741
742template <typename T>
744
745} // namespace grem::pmr
746
747#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition Buffer.hpp:28
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
Definition aliases.hpp:9
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234