4#ifndef GREM_CORE_DATA_ALLOCATION_HPP
5#define GREM_CORE_DATA_ALLOCATION_HPP
13#include <initializer_list>
17#include <memory_resource>
24template <
typename T,
typename Allocator = std::allocator<T>>
33 using pointer =
typename std::allocator_traits<Allocator>::pointer;
34 using const_pointer =
typename std::allocator_traits<Allocator>::const_pointer;
42 constexpr explicit
Allocation(const Allocator& allocator) noexcept
43 : allocator(allocator) {}
49 throw std::length_error{
"count > max_size()"};
51 const pointer newElements = std::allocator_traits<Allocator>::allocate(this->allocator,
count);
52 T*
const pNewBegin = std::to_address(newElements);
53 T*
const pNewEnd = pNewBegin +
count;
54 if constexpr (std::is_trivially_default_constructible_v<T>) {
55 std::uninitialized_default_construct(pNewBegin, pNewEnd);
59 while (pNew != pNewEnd) {
60 std::allocator_traits<Allocator>::construct(this->allocator, pNew++);
63 while (pNew != pNewBegin) {
64 std::allocator_traits<Allocator>::destroy(this->allocator, --pNew);
66 std::allocator_traits<Allocator>::deallocate(this->allocator, newElements,
count);
70 elements = newElements;
80 template <std::input_iterator InputIterator>
86 template <std::random_access_iterator RandomAccessIterator, std::sentinel_for<RandomAccessIterator> Sentinel>
87 constexpr Allocation(RandomAccessIterator first, Sentinel last,
const Allocator& allocator = Allocator())
93 :
Allocation(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.
get_allocator())) {}
99 :
Allocation(std::move(other), other.get_allocator()) {}
102 : elements(std::exchange(other.elements,
nullptr))
103 , elementCount(std::exchange(other.elementCount,
size_type{0}))
104 , allocator(allocator) {}
106 constexpr Allocation(std::initializer_list<T> ilist,
const Allocator& allocator = Allocator())
114 if (
this == &other) {
117 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
118 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
130 std::allocator_traits<
131 Allocator>::propagate_on_container_move_assignment::value ||
132 std::allocator_traits<Allocator>::is_always_equal::value) {
133 if (
this == &other) {
136 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<Allocator>::is_always_equal::value) {
138 elements = std::exchange(other.elements,
nullptr);
139 elementCount = std::exchange(other.elementCount,
size_type{0});
140 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
141 allocator = other.allocator;
144 if (allocator == other.allocator) {
146 elements = std::exchange(other.elements,
nullptr);
147 elementCount = std::exchange(other.elementCount,
size_type{0});
162 if constexpr (std::is_copy_assignable_v<T>) {
164 for (T& element : *
this) {
172 throw std::length_error{
"count > max_size()"};
174 const pointer newElements = std::allocator_traits<Allocator>::allocate(allocator,
count);
175 T*
const pNewBegin = std::to_address(newElements);
176 T*
const pNewEnd = pNewBegin +
count;
177 if constexpr (std::is_trivially_copy_constructible_v<T>) {
178 std::uninitialized_fill(pNewBegin, pNewEnd, value);
182 while (pNew != pNewEnd) {
183 std::allocator_traits<Allocator>::construct(allocator, pNew++, value);
186 while (pNew != pNewBegin) {
187 std::allocator_traits<Allocator>::destroy(allocator, --pNew);
189 std::allocator_traits<Allocator>::deallocate(allocator, newElements,
count);
194 elements = newElements;
195 elementCount =
count;
201 template <std::input_iterator InputIterator>
203 if constexpr (std::is_copy_assignable_v<T>) {
205 for (T& element : *
this) {
213 throw std::length_error{
"count > max_size()"};
215 const pointer newElements = std::allocator_traits<Allocator>::allocate(allocator,
count);
216 T*
const pNewBegin = std::to_address(newElements);
217 if constexpr (std::is_trivially_copy_constructible_v<T>) {
218 std::uninitialized_copy_n(first,
count, pNewBegin);
220 T*
const pNewEnd = pNewBegin +
count;
223 while (pNew != pNewEnd) {
224 std::allocator_traits<Allocator>::construct(allocator, pNew++, *first++);
227 while (pNew != pNewBegin) {
228 std::allocator_traits<Allocator>::destroy(allocator, --pNew);
230 std::allocator_traits<Allocator>::deallocate(allocator, newElements,
count);
235 elements = newElements;
236 elementCount =
count;
242 template <std::random_access_iterator RandomAccessIterator, std::sentinel_for<RandomAccessIterator> Sentinel>
243 constexpr void assign(RandomAccessIterator first, Sentinel last) {
247 constexpr void assign(std::initializer_list<T> ilist) {
248 assign(ilist.begin(), ilist.end());
251 template <
typename R>
253 assign(std::begin(r), std::end(r));
258 T*
const pBegin = std::to_address(elements);
259 T*
const pEnd = pBegin + elementCount;
260 if constexpr (std::is_trivially_destructible_v<T>) {
261 std::destroy(pBegin, pEnd);
263 for (T* p = pBegin; p != pEnd; ++p) {
264 std::allocator_traits<Allocator>::destroy(allocator, p);
267 std::allocator_traits<Allocator>::deallocate(allocator, elements, elementCount);
277 throw std::length_error{
"count > max_size()"};
279 const pointer newElements = std::allocator_traits<Allocator>::allocate(allocator,
count);
280 T*
const pNewBegin = std::to_address(newElements);
281 T*
const pOldBegin = std::to_address(elements);
282 T*
const pNewEnd = pNewBegin +
count;
283 T*
const pOldEnd = pOldBegin + elementCount;
284 if constexpr (std::is_trivially_move_constructible_v<T> && std::is_trivially_default_constructible_v<T>) {
285 if (
count > elementCount) {
286 std::uninitialized_move(pOldBegin, pOldEnd, pNewBegin);
287 std::uninitialized_default_construct(pNewBegin + elementCount, pNewEnd);
289 std::uninitialized_move(pOldBegin, pOldBegin +
count, pNewBegin);
295 while (pNew != pNewEnd && pOld != pOldEnd) {
296 if constexpr (std::is_nothrow_move_assignable_v<T>) {
297 std::allocator_traits<Allocator>::construct(allocator, pNew++, std::move(*pOld++));
299 std::allocator_traits<Allocator>::construct(allocator, pNew++, *pOld++);
302 while (pNew != pNewEnd) {
303 std::allocator_traits<Allocator>::construct(allocator, pNew++);
306 while (pNew != pNewBegin) {
307 std::allocator_traits<Allocator>::destroy(allocator, --pNew);
309 std::allocator_traits<Allocator>::deallocate(allocator, newElements,
count);
314 elements = newElements;
315 elementCount =
count;
326 throw std::length_error{
"count > max_size()"};
328 const pointer newElements = std::allocator_traits<Allocator>::allocate(allocator,
count);
329 T*
const pNewBegin = std::to_address(newElements);
330 T*
const pNewEnd = pNewBegin +
count;
331 T*
const pOldBegin = std::to_address(elements);
332 T*
const pOldEnd = pOldBegin + elementCount;
333 if constexpr (std::is_trivially_move_constructible_v<T> && std::is_trivially_copy_constructible_v<T>) {
334 if (
count > elementCount) {
335 std::uninitialized_move(pOldBegin, pOldEnd, pNewBegin);
336 std::uninitialized_fill(pNewBegin + elementCount, pNewEnd, value);
338 std::uninitialized_move(pOldBegin, pOldBegin +
count, pNewBegin);
344 while (pNew != pNewEnd && pOld != pOldEnd) {
345 if constexpr (std::is_nothrow_move_assignable_v<T>) {
346 std::allocator_traits<Allocator>::construct(allocator, pNew++, std::move(*pOld++));
348 std::allocator_traits<Allocator>::construct(allocator, pNew++, *pOld++);
351 while (pNew != pNewEnd) {
352 std::allocator_traits<Allocator>::construct(allocator, pNew++, value);
355 while (pNew != pNewBegin) {
356 std::allocator_traits<Allocator>::destroy(allocator, --pNew);
358 std::allocator_traits<Allocator>::deallocate(allocator, newElements,
count);
363 elements = newElements;
364 elementCount =
count;
376 if (pos >= elementCount) {
377 throw std::out_of_range{
"pos >= size()"};
383 if (pos >= elementCount) {
384 throw std::out_of_range{
"pos >= size()"};
392 return elements[pos];
398 return elements[pos];
416 return elements[elementCount - 1];
422 return elements[elementCount - 1];
446 return iterator{elements + elementCount};
481 [[nodiscard]]
constexpr bool empty() const noexcept {
482 return elementCount == 0;
491 static_cast<size_type>(std::numeric_limits<difference_type>::max()),
492 static_cast<size_type>(std::numeric_limits<size_type>::max() /
sizeof(T)),
493 static_cast<size_type>(std::allocator_traits<Allocator>::max_size(allocator)),
497 constexpr void fill(
const T& value) {
498 std::fill_n(elements, elementCount, value);
502 std::allocator_traits<Allocator>::is_always_equal::value) {
503 if (
this == &other) {
507 swap(elements, other.elements);
508 swap(elementCount, other.elementCount);
509 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
510 swap(allocator, other.allocator);
511 }
else if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
525 return std::lexicographical_compare(
begin(),
end(), other.
begin(), other.
end());
529 return !(other < *
this);
533 return other < *
this;
537 return !(*
this < other);
541 const pointer result = std::exchange(elements,
nullptr);
549 [[no_unique_address]] Allocator allocator;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition Allocation.hpp:25
typename std::allocator_traits< Allocator >::const_pointer const_pointer
Definition Allocation.hpp:34
std::size_t size_type
Definition Allocation.hpp:29
Allocator allocator_type
Definition Allocation.hpp:28
constexpr bool operator==(const Allocation &other) const
Definition Allocation.hpp:520
constexpr const_reference front() const
Definition Allocation.hpp:407
constexpr reference back()
Definition Allocation.hpp:413
constexpr reverse_iterator rbegin() noexcept
Definition Allocation.hpp:457
constexpr void swap(Allocation &other) noexcept(std::allocator_traits< Allocator >::is_always_equal::value)
Definition Allocation.hpp:501
const_pointer const_iterator
Definition Allocation.hpp:36
constexpr bool operator>(const Allocation &other) const
Definition Allocation.hpp:532
constexpr Allocation(Allocation &&other) noexcept
Definition Allocation.hpp:98
constexpr Allocation(RandomAccessIterator first, Sentinel last, const Allocator &allocator=Allocator())
Definition Allocation.hpp:87
constexpr reference at(size_type pos)
Definition Allocation.hpp:375
constexpr Allocation & operator=(Allocation &&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 Allocation.hpp:129
constexpr Allocation() noexcept=default
constexpr void assign(RandomAccessIterator first, Sentinel last)
Definition Allocation.hpp:243
constexpr void resize(size_type count)
Definition Allocation.hpp:273
std::ptrdiff_t difference_type
Definition Allocation.hpp:30
constexpr Allocation(const Allocation &other, const Allocator &allocator)
Definition Allocation.hpp:95
constexpr Allocation & operator=(std::initializer_list< T > ilist)
Definition Allocation.hpp:156
constexpr const_reverse_iterator rbegin() const noexcept
Definition Allocation.hpp:461
constexpr iterator begin() noexcept
Definition Allocation.hpp:433
constexpr void assign_range(R &&r)
Definition Allocation.hpp:252
constexpr bool operator>=(const Allocation &other) const
Definition Allocation.hpp:536
constexpr void assign(InputIterator first, size_type count)
Definition Allocation.hpp:202
constexpr Allocation(std::initializer_list< T > ilist, const Allocator &allocator=Allocator())
Definition Allocation.hpp:106
constexpr const_reverse_iterator crbegin() const noexcept
Definition Allocation.hpp:465
constexpr pointer data() noexcept
Definition Allocation.hpp:425
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition Allocation.hpp:38
constexpr const_reverse_iterator crend() const noexcept
Definition Allocation.hpp:477
pointer iterator
Definition Allocation.hpp:35
constexpr allocator_type get_allocator() const noexcept
Definition Allocation.hpp:371
constexpr void assign(size_type count, const T &value)
Definition Allocation.hpp:161
T value_type
Definition Allocation.hpp:27
friend constexpr void swap(Allocation &a, Allocation &b) noexcept(noexcept(a.swap(b)))
Definition Allocation.hpp:516
std::reverse_iterator< iterator > reverse_iterator
Definition Allocation.hpp:37
constexpr const_reference at(size_type pos) const
Definition Allocation.hpp:382
constexpr const_reference back() const
Definition Allocation.hpp:419
constexpr Allocation(const Allocation &other)
Definition Allocation.hpp:92
constexpr const_iterator end() const noexcept
Definition Allocation.hpp:449
constexpr size_type size() const noexcept
Definition Allocation.hpp:485
constexpr const_reverse_iterator rend() const noexcept
Definition Allocation.hpp:473
constexpr const_iterator begin() const noexcept
Definition Allocation.hpp:437
constexpr Allocation & operator=(const Allocation &other)
Definition Allocation.hpp:113
constexpr Allocation(Allocation &&other, const Allocator &allocator) noexcept
Definition Allocation.hpp:101
T & reference
Definition Allocation.hpp:31
constexpr reverse_iterator rend() noexcept
Definition Allocation.hpp:469
constexpr bool operator<=(const Allocation &other) const
Definition Allocation.hpp:528
constexpr size_type max_size() const noexcept
Definition Allocation.hpp:489
constexpr iterator end() noexcept
Definition Allocation.hpp:445
constexpr Allocation(InputIterator first, size_type count, const Allocator &allocator=Allocator())
Definition Allocation.hpp:81
constexpr bool empty() const noexcept
Definition Allocation.hpp:481
constexpr void assign(std::initializer_list< T > ilist)
Definition Allocation.hpp:247
constexpr reference operator[](size_type pos)
Definition Allocation.hpp:389
constexpr reference front()
Definition Allocation.hpp:401
constexpr const_iterator cbegin() const noexcept
Definition Allocation.hpp:441
constexpr bool operator<(const Allocation &other) const
Definition Allocation.hpp:524
constexpr ~Allocation()
Definition Allocation.hpp:109
constexpr void fill(const T &value)
Definition Allocation.hpp:497
constexpr Allocation(size_type count, const Allocator &allocator=Allocator())
Definition Allocation.hpp:45
constexpr const_reference operator[](size_type pos) const
Definition Allocation.hpp:395
const T & const_reference
Definition Allocation.hpp:32
constexpr const_iterator cend() const noexcept
Definition Allocation.hpp:453
constexpr pointer release() noexcept
Definition Allocation.hpp:540
constexpr const_pointer data() const noexcept
Definition Allocation.hpp:429
constexpr void resize(size_type count, const T &value)
Definition Allocation.hpp:322
constexpr void clear() noexcept
Definition Allocation.hpp:256
constexpr Allocation(size_type count, const T &value, const Allocator &allocator=Allocator())
Definition Allocation.hpp:75
Definition concepts.hpp:106
Definition Allocation.hpp:554
grem::Allocation< T, std::pmr::polymorphic_allocator< T > > Allocation
Definition Allocation.hpp:557
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234