4#ifndef GREM_CORE_DATA_ARRAY_LIST_HPP
5#define GREM_CORE_DATA_ARRAY_LIST_HPP
14#include <initializer_list>
18#include <memory_resource>
26template <
typename T,
typename Allocator = std::allocator<T>>
29 using Size = std::uint32_t;
34 using size_type =
typename std::allocator_traits<Allocator>::size_type;
35 using difference_type =
typename std::allocator_traits<Allocator>::difference_type;
38 using pointer =
typename std::allocator_traits<Allocator>::pointer;
39 using const_pointer =
typename std::allocator_traits<Allocator>::const_pointer;
47 constexpr explicit
ArrayList(const Allocator& allocator) noexcept
48 : allocator(allocator) {}
60 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
61 constexpr ArrayList(InputIterator first, Sentinel last,
const Allocator& allocator = Allocator())
66 constexpr ArrayList(std::initializer_list<T> ilist,
const Allocator& allocator = Allocator())
74 :
ArrayList(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.
get_allocator())) {}
80 :
ArrayList(std::move(other), other.get_allocator()) {}
83 : elements(std::exchange(other.elements,
nullptr))
84 , elementCount(std::exchange(other.elementCount, Size{0}))
85 , elementCapacity(std::exchange(other.elementCapacity, Size{0}))
86 , allocator(allocator) {}
92 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
93 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
105 std::allocator_traits<
106 Allocator>::propagate_on_container_move_assignment::value ||
107 std::allocator_traits<Allocator>::is_always_equal::value) {
108 if (
this == &other) {
111 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value && !std::allocator_traits<Allocator>::is_always_equal::value) {
112 if (allocator != other.allocator) {
115 moveAppend(other.begin(), other.end());
121 elements = std::exchange(other.elements,
nullptr);
122 elementCount = std::exchange(other.elementCount, Size{0});
123 elementCapacity = std::exchange(other.elementCapacity, Size{0});
124 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
125 allocator = other.allocator;
136 if constexpr (std::is_copy_assignable_v<T>) {
151 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
152 constexpr void assign(InputIterator first, Sentinel last) {
153 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
155 if constexpr (std::is_copy_assignable_v<T>) {
161 elements[index] = *first++;
167 copyAppend(first, last);
170 copyAppend(first, last);
174 constexpr void assign(std::initializer_list<T> ilist) {
175 assign(ilist.begin(), ilist.end());
178 template <
typename R>
180 assign(std::begin(r), std::end(r));
188 if (pos >= elementCount) {
189 throw std::out_of_range{
"pos >= size()"};
191 return elements[pos];
195 if (pos >= elementCount) {
196 throw std::out_of_range{
"pos >= size()"};
198 return elements[pos];
204 return elements[pos];
210 return elements[pos];
228 return elements[elementCount - 1];
234 return elements[elementCount - 1];
258 return elements + elementCount;
262 return elements + elementCount;
293 [[nodiscard]]
constexpr bool empty() const noexcept {
294 return elementCount == 0;
298 return static_cast<size_type>(elementCount);
303 static_cast<size_type>(std::numeric_limits<Size>::max()),
304 static_cast<size_type>(std::numeric_limits<difference_type>::max()),
305 static_cast<size_type>(std::numeric_limits<size_type>::max() /
sizeof(T)),
306 static_cast<size_type>(std::allocator_traits<Allocator>::max_size(allocator)),
313 throw std::length_error{
"newCapacity > max_size()"};
315 if constexpr (std::is_nothrow_move_constructible_v<T>) {
324 return static_cast<size_type>(elementCapacity);
329 if constexpr (std::is_nothrow_move_constructible_v<T>) {
339 T*
const pBegin = std::to_address(elements);
340 T*
const pEnd = pBegin + elementCount;
341 if constexpr (std::is_trivially_destructible_v<T>) {
342 std::destroy(pBegin, pEnd);
344 for (T* p = pBegin; p != pEnd; ++p) {
345 std::allocator_traits<Allocator>::destroy(allocator, p);
357 return emplace(pos, std::move(value));
363 throw std::length_error{
"count > max_size() - size()"};
374 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
377 copyAppend(first, last);
385 return insert(pos, ilist.begin(), ilist.end());
388 template <
typename R>
390 return insert(pos, std::begin(r), std::end(r));
393 template <
typename R>
398 template <
typename... Args>
403 std::rotate(it,
end() - 1,
end());
411 std::shift_left(it,
end(), 1);
423 T*
const pBegin = std::to_address(elements);
424 T*
const pEnd = pBegin + elementCount;
425 T*
const pNewEnd = pBegin + (newEnd -
begin());
426 if constexpr (std::is_trivially_destructible_v<T>) {
427 std::destroy(pNewEnd, pEnd);
429 for (T* p = pNewEnd; p != pEnd; ++p) {
430 std::allocator_traits<Allocator>::destroy(allocator, p);
433 elementCount -=
static_cast<Size
>(
count);
446 template <
typename... Args>
448 reserveAtLeast(
size() + 1);
449 T*
const p = std::to_address(elements) + elementCount;
450 std::allocator_traits<Allocator>::construct(allocator, p, std::forward<Args>(args)...);
452 return *std::launder(p);
458 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(elements) + elementCount);
463 T*
const pBegin = std::to_address(elements);
464 T*
const pEnd = pBegin + elementCount;
465 T*
const pNewEnd = pBegin +
count;
466 if constexpr (std::is_trivially_destructible_v<T>) {
467 std::destroy(pNewEnd, pEnd);
469 for (T* p = pNewEnd; p != pEnd; ++p) {
470 std::allocator_traits<Allocator>::destroy(allocator, p);
473 elementCount =
static_cast<Size
>(
count);
475 reserveAtLeast(
count);
476 if constexpr (std::is_trivially_default_constructible_v<T>) {
477 std::uninitialized_default_construct(std::to_address(elements) + elementCount, std::to_address(elements) +
count);
478 elementCount =
static_cast<Size
>(
count);
480 while (elementCount <
count) {
481 std::allocator_traits<Allocator>::construct(allocator, std::to_address(elements) + elementCount);
490 T*
const pBegin = std::to_address(elements);
491 T*
const pEnd = pBegin + elementCount;
492 T*
const pNewEnd = pBegin +
count;
493 if constexpr (std::is_trivially_destructible_v<T>) {
494 std::destroy(pNewEnd, pEnd);
496 for (T* p = pNewEnd; p != pEnd; ++p) {
497 std::allocator_traits<Allocator>::destroy(allocator, p);
500 elementCount =
static_cast<Size
>(
count);
502 reserveAtLeast(
count);
503 if constexpr (std::is_trivially_copy_constructible_v<T>) {
504 std::uninitialized_fill(std::to_address(elements) + elementCount, std::to_address(elements) +
count, value);
505 elementCount =
static_cast<Size
>(
count);
507 while (elementCount <
count) {
508 std::allocator_traits<Allocator>::construct(allocator, std::to_address(elements) + elementCount, value);
516 std::allocator_traits<Allocator>::propagate_on_container_swap::value ||
517 (std::allocator_traits<Allocator>::is_always_equal::value && std::is_nothrow_swappable_v<T>)) {
518 if (
this == &other) {
521 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_swap::value && !std::allocator_traits<Allocator>::is_always_equal::value) {
522 if (allocator != other.allocator) {
523 throw std::logic_error{
"get_allocator() != other.get_allocator()"};
527 swap(elements, other.elements);
528 swap(elementCount, other.elementCount);
529 swap(elementCapacity, other.elementCapacity);
530 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
531 swap(allocator, other.allocator);
540 return std::lexicographical_compare(
begin(),
end(), other.
begin(), other.
end());
544 return !(other < *
this);
548 return other < *
this;
552 return !(*
this < other);
559 template <
typename U>
567 template <
typename Predicate>
578 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
581 elements = std::allocator_traits<Allocator>::allocate(this->allocator,
capacity);
582 elementCapacity =
static_cast<Size
>(
capacity);
583 moveAppend(first, last);
586 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
589 elements = std::allocator_traits<Allocator>::allocate(this->allocator,
capacity);
590 elementCapacity =
static_cast<Size
>(
capacity);
591 copyAppend(first, last);
594 constexpr void reset() noexcept {
596 T*
const pBegin = std::to_address(elements);
597 T*
const pEnd = pBegin + elementCount;
598 if constexpr (std::is_trivially_destructible_v<T>) {
599 std::destroy(pBegin, pEnd);
601 for (T* p = pBegin; p != pEnd; ++p) {
602 std::allocator_traits<Allocator>::destroy(allocator, p);
605 std::allocator_traits<Allocator>::deallocate(allocator, elements,
static_cast<size_type>(elementCapacity));
612 constexpr void reserveAtLeast(
size_type newCapacity) {
616 if (newCapacity > maxSize || halfCap > maxSize - cap) {
617 throw std::length_error{
"newCapacity > max_size()"};
619 newCapacity = std::max(newCapacity, cap + halfCap);
620 if constexpr (std::is_nothrow_move_constructible_v<T>) {
628 template <
typename InputIterator,
typename Sentinel>
629 constexpr void copyAppend(InputIterator first, Sentinel last) {
630 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
637 throw std::length_error{
"count > max_size() - size()"};
640 reserveAtLeast(newSize);
641 if constexpr (std::is_trivially_copyable_v<T> &&
requires { std::to_address(first); }) {
642 if (!std::is_constant_evaluated()) {
643 std::memcpy(std::to_address(elements) + offset, std::to_address(first),
count *
sizeof(T));
644 elementCount =
static_cast<Size
>(newSize);
649 while (first != last) {
654 template <
typename InputIterator,
typename Sentinel>
655 constexpr void moveAppend(InputIterator first, Sentinel last) {
656 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
663 throw std::length_error{
"count > max_size() - size()"};
666 reserveAtLeast(newSize);
667 if constexpr (std::is_trivially_copyable_v<T> &&
requires { std::to_address(first); }) {
668 if (!std::is_constant_evaluated()) {
669 std::memcpy(std::to_address(elements) + offset, std::to_address(first),
count *
sizeof(T));
670 elementCount =
static_cast<Size
>(newSize);
675 while (first != last) {
681 Size elementCount = 0;
682 Size elementCapacity = 0;
683 [[no_unique_address]] Allocator allocator;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition ArrayList.hpp:27
constexpr size_type capacity() const noexcept
Definition ArrayList.hpp:323
friend constexpr size_type erase_if(ArrayList &c, Predicate predicate)
Definition ArrayList.hpp:568
constexpr reverse_iterator rend() noexcept
Definition ArrayList.hpp:281
typename std::allocator_traits< Allocator >::size_type size_type
Definition ArrayList.hpp:34
constexpr void clear() noexcept
Definition ArrayList.hpp:337
constexpr ArrayList & operator=(ArrayList &&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 ArrayList.hpp:104
constexpr bool operator==(const ArrayList &other) const
Definition ArrayList.hpp:535
constexpr ArrayList(size_type count, const T &value, const Allocator &allocator=Allocator())
Definition ArrayList.hpp:50
constexpr bool operator<(const ArrayList &other) const
Definition ArrayList.hpp:539
constexpr void resize(size_type count, const value_type &value)
Definition ArrayList.hpp:488
constexpr bool empty() const noexcept
Definition ArrayList.hpp:293
typename std::allocator_traits< Allocator >::const_pointer const_pointer
Definition ArrayList.hpp:39
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition ArrayList.hpp:43
constexpr const_reverse_iterator rend() const noexcept
Definition ArrayList.hpp:285
constexpr iterator insert(const_iterator pos, std::initializer_list< T > ilist)
Definition ArrayList.hpp:384
constexpr pointer data() noexcept
Definition ArrayList.hpp:237
constexpr const_iterator cbegin() const noexcept
Definition ArrayList.hpp:253
constexpr void assign_range(R &&r)
Definition ArrayList.hpp:179
constexpr reference operator[](size_type pos)
Definition ArrayList.hpp:201
constexpr allocator_type get_allocator() const noexcept
Definition ArrayList.hpp:183
constexpr void assign(std::initializer_list< T > ilist)
Definition ArrayList.hpp:174
constexpr bool operator>=(const ArrayList &other) const
Definition ArrayList.hpp:551
constexpr ArrayList(const ArrayList &other)
Definition ArrayList.hpp:73
constexpr reverse_iterator rbegin() noexcept
Definition ArrayList.hpp:269
constexpr const_reference operator[](size_type pos) const
Definition ArrayList.hpp:207
constexpr ArrayList & operator=(const ArrayList &other)
Definition ArrayList.hpp:88
constexpr reference emplace_back(Args &&... args)
Definition ArrayList.hpp:447
constexpr iterator insert_range(const_iterator pos, R &&r)
Definition ArrayList.hpp:389
const value_type & const_reference
Definition ArrayList.hpp:37
constexpr void swap(ArrayList &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 ArrayList.hpp:515
constexpr ArrayList(size_type count, const Allocator &allocator=Allocator())
Definition ArrayList.hpp:55
constexpr bool operator<=(const ArrayList &other) const
Definition ArrayList.hpp:543
typename std::allocator_traits< Allocator >::pointer pointer
Definition ArrayList.hpp:38
constexpr iterator insert(const_iterator pos, const T &value)
Definition ArrayList.hpp:352
constexpr void shrink_to_fit()
Definition ArrayList.hpp:327
value_type & reference
Definition ArrayList.hpp:36
typename std::allocator_traits< Allocator >::difference_type difference_type
Definition ArrayList.hpp:35
constexpr const_reverse_iterator rbegin() const noexcept
Definition ArrayList.hpp:273
constexpr ArrayList(const ArrayList &other, const Allocator &allocator)
Definition ArrayList.hpp:76
constexpr size_type size() const noexcept
Definition ArrayList.hpp:297
constexpr const_reverse_iterator crbegin() const noexcept
Definition ArrayList.hpp:277
constexpr const_reference front() const
Definition ArrayList.hpp:219
constexpr bool operator>(const ArrayList &other) const
Definition ArrayList.hpp:547
constexpr void assign(InputIterator first, Sentinel last)
Definition ArrayList.hpp:152
constexpr reference at(size_type pos)
Definition ArrayList.hpp:187
constexpr iterator erase(const_iterator first, const_iterator last)
Definition ArrayList.hpp:416
constexpr void assign(size_type count, const T &value)
Definition ArrayList.hpp:135
constexpr const_iterator end() const noexcept
Definition ArrayList.hpp:261
constexpr iterator insert(const_iterator pos, size_type count, const T &value)
Definition ArrayList.hpp:360
constexpr iterator emplace(const_iterator pos, Args &&... args)
Definition ArrayList.hpp:399
constexpr size_type max_size() const noexcept
Definition ArrayList.hpp:301
constexpr iterator erase(const_iterator pos)
Definition ArrayList.hpp:407
friend constexpr void swap(ArrayList &a, ArrayList &b) noexcept
Definition ArrayList.hpp:555
Allocator allocator_type
Definition ArrayList.hpp:33
friend constexpr size_type erase(ArrayList &c, const U &value)
Definition ArrayList.hpp:560
pointer iterator
Definition ArrayList.hpp:40
constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last)
Definition ArrayList.hpp:375
constexpr const_reverse_iterator crend() const noexcept
Definition ArrayList.hpp:289
constexpr ArrayList() noexcept=default
const_pointer const_iterator
Definition ArrayList.hpp:41
std::reverse_iterator< iterator > reverse_iterator
Definition ArrayList.hpp:42
constexpr ~ArrayList()
Definition ArrayList.hpp:69
constexpr reference back()
Definition ArrayList.hpp:225
constexpr void push_back(const T &value)
Definition ArrayList.hpp:438
constexpr reference front()
Definition ArrayList.hpp:213
constexpr const_reference at(size_type pos) const
Definition ArrayList.hpp:194
constexpr ArrayList(InputIterator first, Sentinel last, const Allocator &allocator=Allocator())
Definition ArrayList.hpp:61
constexpr const_iterator begin() const noexcept
Definition ArrayList.hpp:249
constexpr ArrayList(std::initializer_list< T > ilist, const Allocator &allocator=Allocator())
Definition ArrayList.hpp:66
constexpr ArrayList & operator=(std::initializer_list< T > ilist) noexcept
Definition ArrayList.hpp:130
constexpr const_reference back() const
Definition ArrayList.hpp:231
constexpr void push_back(T &&value)
Definition ArrayList.hpp:442
constexpr iterator begin() noexcept
Definition ArrayList.hpp:245
constexpr void reserve(size_type newCapacity)
Definition ArrayList.hpp:310
constexpr void append_range(R &&r)
Definition ArrayList.hpp:394
constexpr ArrayList(ArrayList &&other) noexcept
Definition ArrayList.hpp:79
constexpr void pop_back()
Definition ArrayList.hpp:455
constexpr const_iterator cend() const noexcept
Definition ArrayList.hpp:265
constexpr ArrayList(ArrayList &&other, const Allocator &allocator) noexcept
Definition ArrayList.hpp:82
constexpr void resize(size_type count)
Definition ArrayList.hpp:461
constexpr const_pointer data() const noexcept
Definition ArrayList.hpp:241
T value_type
Definition ArrayList.hpp:32
constexpr iterator end() noexcept
Definition ArrayList.hpp:257
constexpr iterator insert(const_iterator pos, T &&value)
Definition ArrayList.hpp:356
Definition concepts.hpp:106
@ T
't'
Definition keyboard.hpp:90
Definition Allocation.hpp:554
grem::ArrayList< T, std::pmr::polymorphic_allocator< T > > ArrayList
Definition ArrayList.hpp:691
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234