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"
19 namespace darr_detail {
27 template <
typename T,
typename Allocator = mem::DefaultAllocatorAdaptor>
68 reserve(m_cap != 0 ? (m_cap * 3 + 1) / 2 : 4);
93 template <
typename InputIt>
95 const auto count = (
size_type)core::distance(first, last);
98 if constexpr (std::is_pointer_v<InputIt>) {
100 operator[](
i) = first[
i];
101 }
else if constexpr (std::is_same_v<typename InputIt::iterator_category, core::random_access_iterator_tag>) {
103 operator[](
i) = *(first[
i]);
106 for (
auto it = first;
it != last; ++
it)
107 operator[](
i++) = *
it;
122 other.m_pData =
nullptr;
139 GAIA_ASSERT(core::addressof(
other) !=
this);
142 mem::copy_elements<T, false>(
152 GAIA_ASSERT(core::addressof(
other) !=
this);
157 m_pData =
other.m_pData;
161 other.m_pData =
nullptr;
172 GAIA_CLANG_WARNING_PUSH()
174 GAIA_CLANG_WARNING_DISABLE("-Wcast-align")
179 return reinterpret_cast<pointer>(m_pData);
199 GAIA_NODISCARD
decltype(
auto)
operator[](
size_type pos)
const noexcept {
204 GAIA_CLANG_WARNING_POP()
216 mem::move_ctor_elements<T>(m_pData,
pDataOld, m_cnt);
231 if (m_pData ==
nullptr) {
234 GAIA_MEM_SANI_ADD_BLOCK(
value_size, m_pData, count, count);
235 core::call_ctor_n(
data(), count);
245 core::call_dtor_n(&
data()[count], m_cnt - count);
246 GAIA_MEM_SANI_POP_N(
value_size, m_pData, m_cap, m_cnt, m_cnt - count);
253 if (count <= m_cap) {
255 GAIA_MEM_SANI_PUSH_N(
value_size, m_pData, m_cap, m_cnt, count - m_cnt);
256 core::call_ctor_n(&
data()[m_cnt], count - m_cnt);
262 const auto cap = detail::resize_capacity(
policy, m_cap, count);
267 mem::move_ctor_elements<T>(m_pData,
pDataOld, m_cnt);
269 core::call_ctor_n(&
data()[m_cnt], count - m_cnt);
282 if (count <= m_cnt) {
287 if constexpr (std::is_copy_constructible_v<value_type>) {
317 template <
typename...
Args>
319 if GAIA_UNLIKELY (m_cnt == m_cap) {
320 const auto cap = m_cap != 0 ? (m_cap * 3 + 1) / 2 : 4;
324 core::call_ctor(&
static_cast<T*
>(
static_cast<void*
>(
pDataNew))[m_cnt], GAIA_FWD(
args)...);
325 mem::move_ctor_elements<T>(
pDataNew, m_pData, m_cnt);
330 GAIA_MEM_SANI_PUSH(
value_size, m_pData, m_cap, m_cnt);
331 core::call_ctor(&
data()[m_cnt], GAIA_FWD(
args)...);
338 GAIA_ASSERT(!
empty());
340 auto* ptr = &
data()[m_cnt - 1];
341 core::call_dtor(ptr);
342 GAIA_MEM_SANI_POP(
value_size, m_pData, m_cap, m_cnt);
360 GAIA_MEM_SANI_PUSH(
value_size, m_pData, m_cap, m_cnt);
363 core::call_ctor(ptr, std::move_if_noexcept(value));
366 mem::shift_elements_right<T, false>(m_pData,
idxDst - 1,
idxSrc, m_cap);
367 mem::move_element<T, false>((
uint8_t*)ptr, (
uint8_t*)&value, 0, 0, 1, 1);
383 T value(GAIA_MOV(
arg));
388 GAIA_MEM_SANI_PUSH(
value_size, m_pData, m_cap, m_cnt);
391 core::call_ctor(ptr, std::move_if_noexcept(value));
394 mem::shift_elements_right<T, false>(m_pData,
idxDst - 1,
idxSrc, m_cap);
395 mem::move_element<T, false>((
uint8_t*)ptr, (
uint8_t*)&value, 0, 0, 1, 1);
416 mem::shift_elements_left<T, false>(m_pData,
idxDst,
idxSrc, m_cap);
418 auto* ptr = &
data()[m_cnt - 1];
419 core::call_dtor(ptr);
420 GAIA_MEM_SANI_POP(
value_size, m_pData, m_cap, m_cnt);
432 GAIA_ASSERT(first >=
data())
434 GAIA_ASSERT(last > first);
442 const auto cnt = (
size_type)(last - first);
444 mem::shift_elements_left_fast<T, false>(m_pData,
idxDst,
idxSrc, cnt, m_cap);
446 auto* ptr = &
data()[m_cnt - cnt];
447 core::call_dtor_n(ptr, cnt);
463 const auto cnt =
size();
470 GAIA_MEM_SANI_ADD_BLOCK(
value_size, m_pData, cnt, cnt);
471 mem::move_ctor_elements<T>(m_pData,
pDataOld, cnt);
482 template <
typename Func>
485 const auto newSize = detail::retain_array<false>(*
this, m_cnt, func);
517 GAIA_ASSERT(!
empty());
524 GAIA_ASSERT(!
empty());
531 GAIA_ASSERT(!
empty());
538 GAIA_ASSERT(!
empty());
618 if (m_cnt !=
other.m_cnt)
622 if (!(
operator[](
i) ==
other[
i]))
Array of elements of type.
Definition darray_ext_impl.h:31
GAIA_NODISCARD size_type size() const noexcept
Returns the number of elements.
Definition darray_ext_impl.h:561
GAIA_NODISCARD auto begin() noexcept
Returns an iterator to the first element.
Definition darray_ext_impl.h:613
GAIA_NODISCARD auto end() noexcept
Returns an iterator one past the last element.
Definition darray_ext_impl.h:649
darr_ext_detail::size_type size_type
Unsigned type used for sizes and indices.
Definition darray_ext_impl.h:50
darr_ext_detail::difference_type difference_type
Type used for iterator differences.
Definition darray_ext_impl.h:48
GAIA_NODISCARD size_type capacity() const noexcept
Returns the number of elements that fit without reallocation.
Definition darray_ext_impl.h:573
Array with variable size of elements of type.
Definition darray_impl.h:28
darr(const darr &other)
Copy-constructs a container.
Definition darray_impl.h:117
iterator insert(iterator pos, T &&arg)
Insert the element to the position given by iterator pos.
Definition darray_impl.h:379
iterator insert(iterator pos, const T &arg)
Insert the element to the position given by iterator pos.
Definition darray_impl.h:351
GAIA_NODISCARD auto rbegin() noexcept
Returns a reverse traversal iterator to the last element.
Definition darray_impl.h:562
GAIA_NODISCARD auto begin() const noexcept
Returns an iterator to the first element.
Definition darray_impl.h:550
GAIA_NODISCARD auto rbegin() const noexcept
Returns a reverse traversal iterator to the last element.
Definition darray_impl.h:568
void resize(size_type count, ResizePolicy policy=ResizePolicy::Exact)
Changes the number of elements.
Definition darray_impl.h:226
const_pointer const_iterator
Read-only random-access iterator type.
Definition darray_impl.h:50
GAIA_NODISCARD auto cend() const noexcept
Returns a read-only iterator one past the last element.
Definition darray_impl.h:592
decltype(auto) emplace_back(Args &&... args)
Constructs and appends an element.
Definition darray_impl.h:318
darr_detail::size_type size_type
Unsigned type used for sizes and indices.
Definition darray_impl.h:45
GAIA_NODISCARD decltype(auto) operator[](size_type pos) noexcept
Accesses an element without bounds checking in optimized builds.
Definition darray_impl.h:191
GAIA_NODISCARD decltype(auto) back() const noexcept
Accesses the last element.
Definition darray_impl.h:537
GAIA_NODISCARD auto cbegin() const noexcept
Returns a read-only iterator to the first element.
Definition darray_impl.h:556
darr & operator=(const darr &other)
Copy-assigns the container.
Definition darray_impl.h:138
darr(InputIt first, InputIt last)
Constructs a container from an iterator range.
Definition darray_impl.h:94
GAIA_NODISCARD decltype(auto) front() const noexcept
Accesses the first element.
Definition darray_impl.h:523
GAIA_NODISCARD size_type capacity() const noexcept
Returns the number of elements that fit without reallocation.
Definition darray_impl.h:504
darr & operator=(darr &&other) noexcept
Move-assigns the container.
Definition darray_impl.h:151
void reserve(size_type cap)
Ensures storage for at least the requested number of elements.
Definition darray_impl.h:208
GAIA_NODISCARD const_pointer data() const noexcept
Returns a pointer to the element storage.
Definition darray_impl.h:184
GAIA_NODISCARD size_type size() const noexcept
Returns the number of elements.
Definition darray_impl.h:492
GAIA_NODISCARD decltype(auto) back() noexcept
Accesses the last element.
Definition darray_impl.h:530
void clear() noexcept
Removes all elements.
Definition darray_impl.h:456
GAIA_NODISCARD auto begin() noexcept
Returns an iterator to the first element.
Definition darray_impl.h:544
void shrink_to_fit()
Reduces allocated storage to match the current size when possible.
Definition darray_impl.h:461
GAIA_NODISCARD auto crend() const noexcept
Returns the read-only reverse traversal sentinel preceding the first element.
Definition darray_impl.h:610
iterator erase(iterator first, iterator last) noexcept
Removes the elements in the range [first, last)
Definition darray_impl.h:431
darr(size_type count, const_reference value)
Constructs a container with copies of a value.
Definition darray_impl.h:79
darr(size_type count)
Constructs a container with the requested number of value-initialized elements.
Definition darray_impl.h:85
static constexpr size_t value_size
Size of one element in bytes.
Definition darray_impl.h:59
core::reverse_iterator< iterator, difference_type > reverse_iterator
Mutable reverse random-access iterator type.
Definition darray_impl.h:52
GAIA_NODISCARD auto rend() const noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition darray_impl.h:604
darr & operator=(std::initializer_list< T > il)
Replaces the elements from an initializer list.
Definition darray_impl.h:130
GAIA_NODISCARD auto rend() noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition darray_impl.h:598
core::reverse_iterator< const_iterator, difference_type > const_reverse_iterator
Read-only reverse random-access iterator type.
Definition darray_impl.h:54
auto retain(Func &&func)
Removes all elements that fail the predicate. The predicate may modify its element but must not chang...
Definition darray_impl.h:483
GAIA_NODISCARD bool operator==(const darr &other) const noexcept
Compares two containers element by element.
Definition darray_impl.h:617
GAIA_NODISCARD decltype(auto) front() noexcept
Accesses the first element.
Definition darray_impl.h:516
void resize(size_type count, const_reference value, ResizePolicy policy=ResizePolicy::Exact)
Changes the size and initializes new elements from a value.
Definition darray_impl.h:281
iterator erase(iterator pos) noexcept
Removes the element at pos.
Definition darray_impl.h:406
void push_back(T &&arg)
Appends an element.
Definition darray_impl.h:309
darr(std::initializer_list< T > il)
Constructs a container from an initializer list.
Definition darray_impl.h:113
pointer iterator
Mutable random-access iterator type.
Definition darray_impl.h:48
darr_detail::difference_type difference_type
Type used for iterator differences.
Definition darray_impl.h:43
GAIA_NODISCARD bool empty() const noexcept
Checks whether the container has no elements.
Definition darray_impl.h:498
void pop_back() noexcept
Removes the last element.
Definition darray_impl.h:337
darr(darr &&other) noexcept
Move-constructs a container.
Definition darray_impl.h:121
GAIA_NODISCARD pointer data() noexcept
Returns a pointer to the element storage.
Definition darray_impl.h:178
void push_back(const T &arg)
Appends an element.
Definition darray_impl.h:303
GAIA_NODISCARD auto crbegin() const noexcept
Returns a read-only reverse traversal iterator to the last element.
Definition darray_impl.h:574
T * pointer
Mutable element pointer type.
Definition darray_impl.h:37
GAIA_NODISCARD size_type max_size() const noexcept
Returns the maximum number of elements supported by this container.
Definition darray_impl.h:510
GAIA_NODISCARD constexpr bool operator!=(const darr &other) const noexcept
Checks whether two containers differ.
Definition darray_impl.h:630
const T * const_pointer
Read-only element pointer type.
Definition darray_impl.h:39
GAIA_NODISCARD auto end() noexcept
Returns an iterator one past the last element.
Definition darray_impl.h:580
GAIA_NODISCARD auto end() const noexcept
Returns an iterator one past the last element.
Definition darray_impl.h:586
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
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