GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
SmallBuffer.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_SMALL_BUFFER_HPP
5#define GREM_CORE_DATA_SMALL_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::fill_n, std::copy, std::move
12#include <cstddef> // std::size_t, std::max_align_t
13#include <initializer_list> // std::initializer_list
14#include <iterator> // std::..._iterator, std::sentinel_for, std::iterator_traits, std::..._iterator_tag, std::begin, std::end
15#include <limits> // std::numeric_limits
16#include <memory> // std::allocator, std::allocator_traits, std::uninitialized_...
17#include <memory_resource> // std::pmr::polymorphic_allocator
18#include <new> // std::bad_alloc, std::bad_array_new_length
19#include <stdexcept> // std::out_of_range, std::length_error
20#include <stdlib.h> // malloc, realloc, free // NOLINT(modernize-deprecated-headers)
21#include <type_traits> // std::is_..._v
22#include <utility> // std::move, std::move_if_noexcept, std::forward, std::exchange, std::swap
23
24namespace grem {
25
26template <typename T, std::size_t SmallCapacity, typename Allocator = std::allocator<T>>
28private:
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);
31
32public:
33 using value_type = T;
34 using allocator_type = Allocator;
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;
43 using reverse_iterator = std::reverse_iterator<iterator>;
44 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
45
46 SmallBuffer() noexcept(noexcept(Allocator()))
47 : SmallBuffer(Allocator()) {}
48
49 explicit SmallBuffer(const Allocator& allocator) noexcept
50 : allocator(allocator) {
51 elements = smallElements;
52 }
53
54 SmallBuffer(size_type count, const T& value, const Allocator& allocator = Allocator())
55 : SmallBuffer(allocator) {
56 resize(count, value);
57 }
58
59 explicit SmallBuffer(size_type count, const Allocator& allocator = Allocator())
60 : SmallBuffer(allocator) {
62 }
63
64 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
65 SmallBuffer(InputIterator first, Sentinel last, const Allocator& allocator = Allocator())
66 : SmallBuffer(allocator) {
67 assign(first, last);
68 }
69
70 SmallBuffer(std::initializer_list<T> ilist, const Allocator& allocator = Allocator())
71 : SmallBuffer(ilist.begin(), ilist.end(), allocator) {}
72
74 reset();
75 }
76
78 : SmallBuffer(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.get_allocator())) {}
79
80 SmallBuffer(const SmallBuffer& other, const Allocator& allocator)
81 : SmallBuffer(other.begin(), other.end(), allocator) {}
82
84 noexcept(std::is_nothrow_move_constructible_v<T>) // NOLINT(performance-noexcept-move-constructor, cppcoreguidelines-noexcept-move-operations)
85 : SmallBuffer(std::move(other), other.get_allocator()) {}
86
87 SmallBuffer(SmallBuffer&& other, const Allocator& allocator) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
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]);
93 ++elementCount;
94 }
95 other.elementCount = 0;
96 elementCapacity = other.elementCapacity;
97 } else {
98 elements = std::exchange(other.elements, other.smallElements);
99 elementCount = std::exchange(other.elementCount, size_type{0});
100 elementCapacity = std::exchange(other.elementCapacity, SmallCapacity);
101 }
102 }
103
105 if (this == &other) {
106 return *this;
107 }
108 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
109 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
110 if (allocator != other.get_allocator()) {
111 reset();
112 }
113 }
114 allocator = other.get_allocator();
115 }
116 assign(other.begin(), other.end());
117 return *this;
118 }
119
121 std::is_nothrow_move_constructible_v<T> && // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
122 (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<Allocator>::is_always_equal::value)) {
123 if (this == &other) {
124 return *this;
125 }
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) {
128 clear();
129 reserveAtLeast(other.size());
130 for (T& element : other) {
131 push_back(std::move_if_noexcept(element));
132 }
133 other.clear();
134 return *this;
135 }
136 }
137 reset();
138 if (other.elementCapacity <= SmallCapacity) {
139 elements = smallElements;
140 while (elementCount < other.elementCount) {
141 smallElements[elementCount] = std::move_if_noexcept(other.smallElements[elementCount]);
142 ++elementCount;
143 }
144 other.elementCount = 0;
145 elementCapacity = other.elementCapacity;
146 } else {
147 elements = std::exchange(other.elements, other.smallElements);
148 elementCount = std::exchange(other.elementCount, size_type{0});
149 elementCapacity = std::exchange(other.elementCapacity, SmallCapacity);
150 }
151 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
152 allocator = other.allocator;
153 }
154 return *this;
155 }
156
157 SmallBuffer& operator=(std::initializer_list<T> ilist) {
158 assign(ilist);
159 return *this;
160 }
161
162 void assign(size_type count, const T& value) {
163 if constexpr (std::is_copy_assignable_v<T>) {
164 if (count < elementCount) {
165 elementCount = count;
166 }
167 std::fill_n(begin(), size(), value);
168 } else {
169 clear();
170 }
171 for (size_type index = size(); index < count; ++index) {
172 push_back(value);
173 }
174 }
175
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>) {
179 const size_type count = static_cast<size_type>(last - first);
180 if constexpr (std::is_copy_assignable_v<T>) {
181 if (count < elementCount) {
182 elementCount = count;
183 }
184 for (size_type index = 0; index < size(); ++index) {
185 elements[index] = *first++;
186 }
187 } else {
188 clear();
189 }
190 insert(end(), first, last);
191 } else {
192 clear();
193 insert(end(), first, last);
194 }
195 }
196
197 void assign(std::initializer_list<T> ilist) {
198 assign(ilist.begin(), ilist.end());
199 }
200
201 template <typename R>
202 void assign_range(R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
203 assign(std::begin(r), std::end(r));
204 }
205
206 [[nodiscard]] allocator_type get_allocator() const noexcept {
207 return allocator;
208 }
209
210 [[nodiscard]] reference at(size_type pos) {
211 if (pos >= size()) {
212 throw std::out_of_range{"pos >= size()"};
213 }
214 return elements[pos];
215 }
216
217 [[nodiscard]] const_reference at(size_type pos) const {
218 if (pos >= size()) {
219 throw std::out_of_range{"pos >= size()"};
220 }
221 return elements[pos];
222 }
223
224 [[nodiscard]] reference operator[](size_type pos) {
225 GREM_ASSERT(elements);
226 GREM_ASSERT(pos < size());
227 return elements[pos];
228 }
229
230 [[nodiscard]] const_reference operator[](size_type pos) const {
231 GREM_ASSERT(elements);
232 GREM_ASSERT(pos < size());
233 return elements[pos];
234 }
235
236 [[nodiscard]] reference front() {
237 GREM_ASSERT(elements);
238 GREM_ASSERT(!empty());
239 return elements[0];
240 }
241
242 [[nodiscard]] const_reference front() const {
243 GREM_ASSERT(elements);
244 GREM_ASSERT(!empty());
245 return elements[0];
246 }
247
248 [[nodiscard]] reference back() {
249 GREM_ASSERT(elements);
250 GREM_ASSERT(!empty());
251 return elements[elementCount - 1];
252 }
253
254 [[nodiscard]] const_reference back() const {
255 GREM_ASSERT(elements);
256 GREM_ASSERT(!empty());
257 return elements[elementCount - 1];
258 }
259
260 [[nodiscard]] pointer data() noexcept {
261 return elements;
262 }
263
264 [[nodiscard]] const_pointer data() const noexcept {
265 return elements;
266 }
267
268 [[nodiscard]] iterator begin() noexcept {
269 return elements;
270 }
271
272 [[nodiscard]] const_iterator begin() const noexcept {
273 return elements;
274 }
275
276 [[nodiscard]] const_iterator cbegin() const noexcept {
277 return begin();
278 }
279
280 [[nodiscard]] iterator end() noexcept {
281 return elements + elementCount;
282 }
283
284 [[nodiscard]] const_iterator end() const noexcept {
285 return elements + elementCount;
286 }
287
288 [[nodiscard]] const_iterator cend() const noexcept {
289 return end();
290 }
291
292 [[nodiscard]] reverse_iterator rbegin() noexcept {
293 return reverse_iterator{end()};
294 }
295
296 [[nodiscard]] const_reverse_iterator rbegin() const noexcept {
297 return const_reverse_iterator{end()};
298 }
299
300 [[nodiscard]] const_reverse_iterator crbegin() const noexcept {
301 return rbegin();
302 }
303
304 [[nodiscard]] reverse_iterator rend() noexcept {
305 return reverse_iterator{begin()};
306 }
307
308 [[nodiscard]] const_reverse_iterator rend() const noexcept {
310 }
311
312 [[nodiscard]] const_reverse_iterator crend() const noexcept {
313 return rend();
314 }
315
316 [[nodiscard]] bool empty() const noexcept {
317 return elementCount == 0;
318 }
319
320 [[nodiscard]] size_type size() const noexcept {
321 return elementCount;
322 }
323
324 [[nodiscard]] size_type max_size() const noexcept {
325 return std::min({
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)),
329 });
330 }
331
332 void reserve(size_type newCapacity) {
333 if (newCapacity > capacity()) {
334 GREM_ASSERT(newCapacity > SmallCapacity);
335 if (newCapacity > max_size()) {
336 throw std::length_error{"newCapacity > max_size()"};
337 }
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{};
341 }
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)); // NOLINT(cppcoreguidelines-no-malloc, cppcoreguidelines-owning-memory)
347 if (!newMemory) {
348 throw std::bad_alloc{};
349 }
350 std::uninitialized_copy_n(elements, elementCapacity, static_cast<pointer>(newMemory));
351 } else {
352 newMemory = realloc(elements, newCapacity * sizeof(T)); // NOLINT(cppcoreguidelines-no-malloc, cppcoreguidelines-owning-memory)
353 if (!newMemory) {
354 throw std::bad_alloc{};
355 }
356 }
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);
360 } else {
361 const pointer newElements = std::allocator_traits<Allocator>::allocate(allocator, newCapacity);
362 size_type newElementCapacity = 0;
363 try {
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;
368 }
369 while (newElementCapacity < newCapacity) {
370 std::allocator_traits<Allocator>::construct(allocator, std::to_address(newElements) + newElementCapacity);
371 ++newElementCapacity;
372 }
373 } catch (...) {
374 while (newElementCapacity-- > 0) {
375 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(newElements) + newElementCapacity);
376 }
377 std::allocator_traits<Allocator>::deallocate(allocator, newElements, newCapacity);
378 throw;
379 }
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);
383 }
384 std::allocator_traits<Allocator>::deallocate(allocator, elements, elementCapacity);
385 }
386 elements = newElements;
387 }
388 elementCapacity = newCapacity;
389 }
390 }
391
392 [[nodiscard]] size_type capacity() const noexcept {
393 return elementCapacity;
394 }
395
397 if (size() < capacity()) {
398 *this = SmallBuffer{std::make_move_iterator(begin()), std::make_move_iterator(end()), get_allocator()};
399 }
400 }
401
402 void clear() noexcept {
403 elementCount = 0;
404 }
405
406 iterator insert(const_iterator pos, const T& value) {
407 const difference_type offset = pos - cbegin();
408 push_back(value);
409 const iterator it = begin() + offset;
410 std::rotate(it, end() - 1, end());
411 return it;
412 }
413
415 const difference_type offset = pos - cbegin();
416 push_back(std::move(value));
417 const iterator it = begin() + offset;
418 std::rotate(it, end() - 1, end());
419 return it;
420 }
421
423 const difference_type offset = pos - cbegin();
424 if (count > max_size() - size()) {
425 throw std::length_error{"count > max_size() - size()"};
426 }
427 reserveAtLeast(size() + count);
428 for (size_type i = 0; i < count; ++i) {
429 push_back(value);
430 }
431 const iterator it = begin() + offset;
432 std::rotate(it, end() - static_cast<difference_type>(count), end());
433 return it;
434 }
435
436 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
437 iterator insert(const_iterator pos, InputIterator first, Sentinel last) {
438 const difference_type offset = pos - cbegin();
439 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
440 const size_type count = static_cast<size_type>(last - first);
441 if (count > max_size() - size()) {
442 throw std::length_error{"count > max_size() - size()"};
443 }
444 reserveAtLeast(size() + count);
445 while (first != last) {
446 push_back(*first++);
447 }
448 const iterator it = begin() + offset;
449 std::rotate(it, end() - static_cast<difference_type>(count), end());
450 } else {
451 while (first != last) {
452 push_back(*first++);
453 }
454 const size_type count = size() - static_cast<size_type>(offset);
455 const iterator it = begin() + offset;
456 std::rotate(it, end() - static_cast<difference_type>(count), end());
457 }
458 return begin() + offset;
459 }
460
461 iterator insert(const_iterator pos, std::initializer_list<T> ilist) {
462 return insert(pos, ilist.begin(), ilist.end());
463 }
464
465 template <typename R>
466 iterator insert_range(const_iterator pos, R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
467 return insert(pos, std::begin(r), std::end(r));
468 }
469
470 template <typename R>
471 void append_range(R&& r) {
472 insert_range(end(), std::forward<R>(r));
473 }
474
476 const difference_type offset = pos - cbegin();
478 const iterator it = begin() + offset;
479 std::rotate(it, end() - 1, end());
480 return it;
481 }
482
484 const difference_type offset = pos - cbegin();
485 if (count > max_size() - size()) {
486 throw std::length_error{"count > max_size() - size()"};
487 }
488 reserveAtLeast(size() + count);
489 elementCount += count;
490 const iterator it = begin() + offset;
491 std::rotate(it, end() - static_cast<difference_type>(count), end());
492 return it;
493 }
494
496 GREM_ASSERT(!empty());
497 const difference_type offset = pos - cbegin();
498 const iterator it = begin() + offset;
499 std::rotate(it, it + 1, end());
500 --elementCount;
501 return it;
502 }
503
505 const difference_type offset = first - cbegin();
506 const difference_type count = last - first;
507 GREM_ASSERT(offset <= static_cast<difference_type>(size()));
508 GREM_ASSERT(count >= 0);
509 GREM_ASSERT(offset + count <= static_cast<difference_type>(size()));
510 const iterator it = begin() + offset;
511 std::rotate(it, it + count, end());
512 elementCount -= static_cast<size_type>(count);
513 return it;
514 }
515
516 constexpr reference push_back_and_overwrite(auto overwrite) {
517 reserveAtLeast(size() + 1);
518 T& result = elements[elementCount];
519 overwrite(result);
520 ++elementCount;
521 return result;
522 }
523
525 return push_back_and_overwrite([](T&) -> void {});
526 }
527
528 constexpr void push_back(const T& value) {
529 push_back_and_overwrite([&](T& storage) -> void { storage = value; });
530 }
531
532 constexpr void push_back(T&& value) {
533 push_back_and_overwrite([&](T& storage) -> void { storage = std::move(value); });
534 }
535
536 void pop_back() {
537 GREM_ASSERT(!empty());
538 --elementCount;
539 }
540
542 reserveAtLeast(count);
543 elementCount = count;
544 }
545
546 void resize(size_type count, const value_type& value) {
547 if (count > size()) {
548 reserveAtLeast(count);
549 std::fill_n(data() + size(), count - size(), value);
550 }
551 elementCount = count;
552 }
553
554 constexpr void resize_and_overwrite_added_values(size_type count, auto overwrite) {
555 if (count > size()) {
556 reserveAtLeast(count);
557 for (size_type i = elementCount; i < count; ++i) {
558 overwrite(elements[i]);
559 }
560 }
561 elementCount = count;
562 }
563
564 void swap(SmallBuffer& other) noexcept(
565 std::allocator_traits<Allocator>::propagate_on_container_swap::value || // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
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) {
568 return;
569 }
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()"};
573 }
574 }
575 if (elementCapacity <= SmallCapacity || other.elementCapacity <= SmallCapacity) {
576 SmallBuffer temporary = std::move(*this);
577 *this = std::move(other);
578 other = std::move(temporary);
579 } else {
580 using std::swap;
581 swap(elements, other.elements);
582 swap(elementCount, other.elementCount);
583 swap(elementCapacity, other.elementCapacity);
584 }
585 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
586 using std::swap;
587 swap(allocator, other.allocator);
588 }
589 }
590
591 [[nodiscard]] bool operator==(const SmallBuffer& other) const {
592 return std::equal(begin(), end(), other.begin(), other.end());
593 }
594
595 [[nodiscard]] bool operator<(const SmallBuffer& other) const {
596 return std::lexicographical_compare(begin(), end(), other.begin(), other.end());
597 }
598
599 [[nodiscard]] bool operator<=(const SmallBuffer& other) const {
600 return !(other < *this);
601 }
602
603 [[nodiscard]] bool operator>(const SmallBuffer& other) const {
604 return other < *this;
605 }
606
607 [[nodiscard]] bool operator>=(const SmallBuffer& other) const {
608 return !(*this < other);
609 }
610
611 friend void swap(SmallBuffer& a, SmallBuffer& b) noexcept {
612 a.swap(b);
613 }
614
615 template <typename U>
616 friend size_type erase(SmallBuffer& c, const U& value) {
617 const iterator it = std::remove(c.begin(), c.end(), value);
618 const size_type result = static_cast<size_type>(c.end() - it);
619 c.erase(it, c.end());
620 return result;
621 }
622
623 template <typename Predicate>
624 friend size_type erase_if(SmallBuffer& c, Predicate predicate) {
625 const iterator it = std::remove_if(c.begin(), c.end(), predicate);
626 const size_type result = static_cast<size_type>(c.end() - it);
627 c.erase(it, c.end());
628 return result;
629 }
630
631private:
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);
636 }
637 if constexpr (USE_MALLOC_AND_REALLOC) {
638 free(elements); // NOLINT(cppcoreguidelines-no-malloc)
639 } else {
640 std::allocator_traits<Allocator>::deallocate(allocator, elements, elementCapacity);
641 }
642 }
643 elements = smallElements;
644 elementCount = 0;
645 elementCapacity = SmallCapacity;
646 }
647
648 void reserveAtLeast(size_type newCapacity) {
649 if (const size_type cap = capacity(); newCapacity > cap) {
650 GREM_ASSERT(newCapacity > SmallCapacity);
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()"};
655 }
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{};
660 }
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)); // NOLINT(cppcoreguidelines-no-malloc, cppcoreguidelines-owning-memory)
666 if (!newMemory) {
667 throw std::bad_alloc{};
668 }
669 std::uninitialized_copy_n(elements, elementCapacity, static_cast<pointer>(newMemory));
670 } else {
671 newMemory = realloc(elements, newCapacity * sizeof(T)); // NOLINT(cppcoreguidelines-no-malloc, cppcoreguidelines-owning-memory)
672 if (!newMemory) {
673 throw std::bad_alloc{};
674 }
675 }
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);
679 } else {
680 const pointer newElements = std::allocator_traits<Allocator>::allocate(allocator, newCapacity);
681 size_type newElementCapacity = 0;
682 try {
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;
687 }
688 while (newElementCapacity < newCapacity) {
689 std::allocator_traits<Allocator>::construct(allocator, std::to_address(newElements) + newElementCapacity);
690 ++newElementCapacity;
691 }
692 } catch (...) {
693 while (newElementCapacity-- > 0) {
694 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(newElements) + newElementCapacity);
695 }
696 std::allocator_traits<Allocator>::deallocate(allocator, newElements, newCapacity);
697 throw;
698 }
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);
702 }
703 std::allocator_traits<Allocator>::deallocate(allocator, elements, elementCapacity);
704 }
705 elements = newElements;
706 }
707 elementCapacity = newCapacity;
708 }
709 }
710
711 pointer elements = nullptr;
712 size_type elementCount = 0;
713 size_type elementCapacity = SmallCapacity;
714 T smallElements[SmallCapacity];
715 [[no_unique_address]] allocator_type allocator;
716};
717
718} // namespace grem
719
720namespace grem::pmr {
721
722template <typename T, std::size_t SmallCapacity>
724
725} // namespace grem::pmr
726
727#endif
#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
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 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
Definition aliases.hpp:9
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