2#include "gaia/config/config.h"
5#include <initializer_list>
9#include "gaia/cnt/impl/array_utils.h"
10#include "gaia/core/iterator.h"
11#include "gaia/core/utility.h"
12#include "gaia/mem/data_layout_policy.h"
13#include "gaia/mem/mem_sani.h"
14#include "gaia/mem/mem_utils.h"
15#include "gaia/mem/raw_data_holder.h"
20 namespace darr_ext_detail {
30 template <
typename T, darr_ext_detail::
size_type N,
typename Allocator = mem::DefaultAllocatorAdaptor>
72 mem::raw_data_holder<T, allocated_bytes> m_data;
84 reserve(m_cap != 0 ? (m_cap * 3 + 1) / 2 : 4);
109 template <
typename InputIt>
111 const auto count = (
size_type)core::distance(first, last);
114 if constexpr (std::is_pointer_v<InputIt>) {
116 operator[](
i) = first[
i];
117 }
else if constexpr (std::is_same_v<typename InputIt::iterator_category, core::random_access_iterator_tag>) {
119 operator[](
i) = *(first[
i]);
122 for (
auto it = first;
it != last; ++
it)
123 operator[](
i++) = *
it;
138 GAIA_ASSERT(core::addressof(
other) !=
this);
141 if (
other.m_pDataHeap ==
nullptr) {
146 m_pDataHeap =
nullptr;
149 m_pDataHeap =
other.m_pDataHeap;
150 m_pData = m_pDataHeap;
156 other.m_pDataHeap =
nullptr;
174 GAIA_ASSERT(core::addressof(
other) !=
this);
177 mem::copy_elements<T, false>(
187 GAIA_ASSERT(core::addressof(
other) !=
this);
190 if (m_pDataHeap !=
nullptr)
193 core::call_dtor_n(
data(), m_cnt);
198 if (
other.m_pDataHeap ==
nullptr) {
203 m_pDataHeap =
nullptr;
206 m_pDataHeap =
other.m_pDataHeap;
207 m_pData = m_pDataHeap;
213 other.m_pDataHeap =
nullptr;
222 if (m_pDataHeap !=
nullptr)
225 core::call_dtor_n(
data(), m_cnt);
230 GAIA_CLANG_WARNING_PUSH()
232 GAIA_CLANG_WARNING_DISABLE("-Wcast-align")
237 return reinterpret_cast<pointer>(m_pData);
257 GAIA_NODISCARD
decltype(
auto)
operator[](
size_type pos)
const noexcept {
262 GAIA_CLANG_WARNING_POP()
272 GAIA_MEM_SANI_ADD_BLOCK(
value_size, m_pDataHeap,
cap, m_cnt);
274 mem::move_ctor_elements<T>(m_pDataHeap,
pDataOld, m_cnt);
277 mem::move_ctor_elements<T>(m_pDataHeap, m_data, m_cnt);
278 core::call_dtor_n(
data(), m_cnt);
279 GAIA_MEM_SANI_DEL_BLOCK(
value_size, m_data, m_cap, m_cnt);
283 m_pData = m_pDataHeap;
296 core::call_dtor_n(&
data()[count], m_cnt - count);
297 GAIA_MEM_SANI_POP_N(
value_size,
data(), m_cap, m_cnt, m_cnt - count);
304 if (count <= m_cap) {
306 GAIA_MEM_SANI_PUSH_N(
value_size,
data(), m_cap, m_cnt, count - m_cnt);
307 core::call_ctor_n(&
data()[m_cnt], count - m_cnt);
313 const auto cap = detail::resize_capacity(
policy, m_cap, count);
316 GAIA_MEM_SANI_ADD_BLOCK(
value_size, m_pDataHeap,
cap, count);
319 mem::move_ctor_elements<T>(m_pDataHeap,
pDataOld, m_cnt);
322 mem::move_ctor_elements<T>(m_pDataHeap, m_data, m_cnt);
323 core::call_dtor_n(
data(), m_cnt);
324 GAIA_MEM_SANI_DEL_BLOCK(
value_size, m_data, m_cap, m_cnt);
327 core::call_ctor_n(&
pDataNew[m_cnt], count - m_cnt);
331 m_pData = m_pDataHeap;
339 if (count <= m_cnt) {
344 if constexpr (std::is_copy_constructible_v<value_type>) {
374 template <
typename...
Args>
376 if GAIA_UNLIKELY (m_cnt == m_cap) {
377 const auto cap = m_cap != 0 ? (m_cap * 3 + 1) / 2 : 4;
381 core::call_ctor(&
static_cast<T*
>(
static_cast<void*
>(
pDataNew))[m_cnt], GAIA_FWD(
args)...);
382 mem::move_ctor_elements<T>(
pDataNew, m_pData, m_cnt);
383 if (m_pDataHeap !=
nullptr)
386 core::call_dtor_n(
data(), m_cnt);
387 GAIA_MEM_SANI_DEL_BLOCK(
value_size, m_data, m_cap, m_cnt);
393 GAIA_MEM_SANI_PUSH(
value_size, m_pData, m_cap, m_cnt);
394 core::call_ctor(&
data()[m_cnt], GAIA_FWD(
args)...);
401 GAIA_ASSERT(!
empty());
403 auto* ptr = &
data()[m_cnt - 1];
404 core::call_dtor(ptr);
426 core::call_ctor(ptr, std::move_if_noexcept(value));
429 mem::shift_elements_right<T, false>(m_pData,
idxDst - 1,
idxSrc, m_cap);
430 mem::move_element<T, false>((
uint8_t*)ptr, (
uint8_t*)&value, 0, 0, 1, 1);
446 T value(GAIA_MOV(
arg));
454 core::call_ctor(ptr, std::move_if_noexcept(value));
457 mem::shift_elements_right<T, false>(m_pData,
idxDst - 1,
idxSrc, m_cap);
458 mem::move_element<T, false>((
uint8_t*)ptr, (
uint8_t*)&value, 0, 0, 1, 1);
479 mem::shift_elements_left<T, false>(m_pData,
idxDst,
idxSrc, m_cap);
481 auto* ptr = &
data()[m_cnt - 1];
482 core::call_dtor(ptr);
495 GAIA_ASSERT(first >=
data())
497 GAIA_ASSERT(last > first);
505 const auto cnt = (
size_type)(last - first);
507 mem::shift_elements_left_fast<T, false>(m_pData,
idxDst,
idxSrc, cnt, m_cap);
509 core::call_dtor_n(&
data()[m_cnt - cnt], cnt);
525 const auto cnt =
size();
526 if (m_pDataHeap ==
nullptr || (cnt >
extent &&
cap == cnt))
532 mem::move_ctor_elements<T>(m_data,
pDataOld, cnt);
533 m_pDataHeap =
nullptr;
538 GAIA_MEM_SANI_ADD_BLOCK(
value_size, m_pDataHeap, cnt, cnt);
539 mem::move_ctor_elements<T>(m_pDataHeap,
pDataOld, cnt);
540 m_pData = m_pDataHeap;
551 template <
typename Func>
554 const auto newSize = detail::retain_array<false>(*
this, m_cnt, func);
586 GAIA_ASSERT(!
empty());
593 GAIA_ASSERT(!
empty());
600 GAIA_ASSERT(!
empty());
607 GAIA_ASSERT(!
empty());
687 if (m_cnt !=
other.m_cnt)
691 if (!(
operator[](
i) ==
other[
i]))
706 template <
typename T, uint32_t N, uint32_t... I>
707 darr_ext<std::remove_cv_t<T>, N> to_sarray_impl(T (&a)[N], std::index_sequence<I...> ) {
718 template <
typename T, u
int32_t N>
719 darr_ext<std::remove_cv_t<T>, N> to_sarray(T (&a)[N]) {
720 return detail::to_sarray_impl(a, std::make_index_sequence<N>{});
Array of elements of type.
Definition darray_ext_impl.h:31
void pop_back() noexcept
Removes the last element.
Definition darray_ext_impl.h:400
GAIA_NODISCARD auto end() const noexcept
Returns an iterator one past the last element.
Definition darray_ext_impl.h:655
GAIA_NODISCARD size_type size() const noexcept
Returns the number of elements.
Definition darray_ext_impl.h:561
GAIA_NODISCARD decltype(auto) front() const noexcept
Accesses the first element.
Definition darray_ext_impl.h:592
darr_ext(darr_ext &&other) noexcept
Move-constructs a container.
Definition darray_ext_impl.h:137
darr_ext & operator=(darr_ext &&other) noexcept
Move-assigns the container.
Definition darray_ext_impl.h:186
core::reverse_iterator< iterator, difference_type > reverse_iterator
Mutable reverse random-access iterator type.
Definition darray_ext_impl.h:57
darr_ext(size_type count)
Constructs a container with the requested number of value-initialized elements.
Definition darray_ext_impl.h:101
darr_ext & operator=(std::initializer_list< T > il)
Replaces the elements from an initializer list.
Definition darray_ext_impl.h:165
GAIA_NODISCARD size_type max_size() const noexcept
Returns the maximum number of elements supported by this container.
Definition darray_ext_impl.h:579
iterator insert(iterator pos, T &&arg)
Insert the element to the position given by iterator pos.
Definition darray_ext_impl.h:442
void shrink_to_fit()
Reduces allocated storage to match the current size when possible.
Definition darray_ext_impl.h:523
GAIA_NODISCARD auto rbegin() noexcept
Returns a reverse traversal iterator to the last element.
Definition darray_ext_impl.h:631
GAIA_NODISCARD auto rend() noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition darray_ext_impl.h:667
GAIA_NODISCARD auto crbegin() const noexcept
Returns a read-only reverse traversal iterator to the last element.
Definition darray_ext_impl.h:643
const_pointer const_iterator
Read-only random-access iterator type.
Definition darray_ext_impl.h:55
pointer iterator
Mutable random-access iterator type.
Definition darray_ext_impl.h:53
iterator insert(iterator pos, const T &arg)
Insert the element to the position given by iterator pos.
Definition darray_ext_impl.h:414
GAIA_NODISCARD auto rend() const noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition darray_ext_impl.h:673
darr_ext(size_type count, const_reference value)
Constructs a container with copies of a value.
Definition darray_ext_impl.h:95
GAIA_NODISCARD auto cbegin() const noexcept
Returns a read-only iterator to the first element.
Definition darray_ext_impl.h:625
GAIA_NODISCARD decltype(auto) front() noexcept
Accesses the first element.
Definition darray_ext_impl.h:585
void reserve(size_type cap)
Ensures storage for at least the requested number of elements.
Definition darray_ext_impl.h:266
GAIA_NODISCARD auto begin() noexcept
Returns an iterator to the first element.
Definition darray_ext_impl.h:613
iterator erase(iterator first, iterator last) noexcept
Removes the elements in the range [first, last)
Definition darray_ext_impl.h:494
void resize(size_type count, const_reference value, ResizePolicy policy=ResizePolicy::Exact)
Changes the size and initializes new elements from a value.
Definition darray_ext_impl.h:338
core::reverse_iterator< const_iterator, difference_type > const_reverse_iterator
Read-only reverse random-access iterator type.
Definition darray_ext_impl.h:59
darr_ext(InputIt first, InputIt last)
Constructs a container from an iterator range.
Definition darray_ext_impl.h:110
GAIA_NODISCARD auto rbegin() const noexcept
Returns a reverse traversal iterator to the last element.
Definition darray_ext_impl.h:637
void push_back(const T &arg)
Appends an element.
Definition darray_ext_impl.h:360
GAIA_NODISCARD auto end() noexcept
Returns an iterator one past the last element.
Definition darray_ext_impl.h:649
GAIA_NODISCARD constexpr bool operator!=(const darr_ext &other) const noexcept
Checks whether two containers differ.
Definition darray_ext_impl.h:699
darr_ext_detail::size_type size_type
Unsigned type used for sizes and indices.
Definition darray_ext_impl.h:50
static constexpr size_t value_size
Size of one element in bytes.
Definition darray_ext_impl.h:64
darr_ext(std::initializer_list< T > il)
Constructs a container from an initializer list.
Definition darray_ext_impl.h:129
void resize(size_type count, ResizePolicy policy=ResizePolicy::Exact)
Changes the number of elements.
Definition darray_ext_impl.h:289
GAIA_NODISCARD auto cend() const noexcept
Returns a read-only iterator one past the last element.
Definition darray_ext_impl.h:661
GAIA_NODISCARD auto crend() const noexcept
Returns the read-only reverse traversal sentinel preceding the first element.
Definition darray_ext_impl.h:679
auto retain(Func &&func) noexcept
Removes all elements that fail the predicate. The predicate may modify its element but must not chang...
Definition darray_ext_impl.h:552
T * pointer
Mutable element pointer type.
Definition darray_ext_impl.h:42
darr_ext(const darr_ext &other)
Copy-constructs a container.
Definition darray_ext_impl.h:133
void clear() noexcept
Removes all elements.
Definition darray_ext_impl.h:518
static constexpr size_type extent
Fixed capacity of the container.
Definition darray_ext_impl.h:66
darr_ext & operator=(const darr_ext &other)
Copy-assigns the container.
Definition darray_ext_impl.h:173
darr_ext_detail::difference_type difference_type
Type used for iterator differences.
Definition darray_ext_impl.h:48
GAIA_NODISCARD decltype(auto) back() noexcept
Accesses the last element.
Definition darray_ext_impl.h:599
GAIA_NODISCARD const_pointer data() const noexcept
Returns a pointer to the element storage.
Definition darray_ext_impl.h:242
GAIA_NODISCARD decltype(auto) operator[](size_type pos) noexcept
Accesses an element without bounds checking in optimized builds.
Definition darray_ext_impl.h:249
iterator erase(iterator pos) noexcept
Removes the element at pos.
Definition darray_ext_impl.h:469
decltype(auto) emplace_back(Args &&... args)
Constructs and appends an element.
Definition darray_ext_impl.h:375
GAIA_NODISCARD auto begin() const noexcept
Returns an iterator to the first element.
Definition darray_ext_impl.h:619
GAIA_NODISCARD decltype(auto) back() const noexcept
Accesses the last element.
Definition darray_ext_impl.h:606
const T * const_pointer
Read-only element pointer type.
Definition darray_ext_impl.h:44
void push_back(T &&arg)
Appends an element.
Definition darray_ext_impl.h:366
GAIA_NODISCARD bool operator==(const darr_ext &other) const noexcept
Compares two containers element by element.
Definition darray_ext_impl.h:686
GAIA_NODISCARD pointer data() noexcept
Returns a pointer to the element storage.
Definition darray_ext_impl.h:236
static constexpr uint32_t allocated_bytes
Number of bytes reserved by the inline storage.
Definition darray_ext_impl.h:68
GAIA_NODISCARD bool empty() const noexcept
Checks whether the container has no elements.
Definition darray_ext_impl.h:567
GAIA_NODISCARD size_type capacity() const noexcept
Returns the number of elements that fit without reallocation.
Definition darray_ext_impl.h:573
Random-access iterator that traverses an underlying iterator in reverse. The stored iterator denotes ...
Definition iterator.h:24
View policy for accessing and storing data in the AoS way. Good for random access and when accessing ...
Definition data_layout_policy.h:160
static GAIA_NODISCARD constexpr uint32_t get_min_byte_size(uintptr_t addr, size_t cnt) noexcept
Calculates the bytes required for a value range.
Definition data_layout_policy.h:173
std::add_pointer_t< ValueType > TargetCastType
Pointer type used to address stored values.
Definition data_layout_policy.h:162
GAIA_NODISCARD static constexpr ValueType & set(std::span< ValueType > s, size_t idx) noexcept
Returns a mutable value reference from an AoS span.
Definition data_layout_policy.h:234
GAIA_NODISCARD static constexpr const ValueType & get(std::span< const ValueType > s, size_t idx) noexcept
Returns a read-only value reference from an AoS span.
Definition data_layout_policy.h:226