2#include "gaia/config/config.h"
5#include <initializer_list>
9#include "gaia/core/iterator.h"
10#include "gaia/core/utility.h"
11#include "gaia/mem/data_layout_policy.h"
12#include "gaia/mem/mem_sani.h"
13#include "gaia/mem/mem_utils.h"
18 namespace darr_detail {
26 template <
typename T,
typename Allocator = mem::DefaultAllocatorAdaptor>
62 const auto cnt =
size();
66 if GAIA_LIKELY (
cap != 0 && cnt <
cap)
70 if GAIA_UNLIKELY (m_pData ==
nullptr) {
77 m_cap = (
cap * 3 + 1) / 2;
81 GAIA_MEM_SANI_ADD_BLOCK(
value_size, m_pData, m_cap, cnt);
82 mem::move_elements<T, false>(m_pData,
pDataOld, cnt, 0, m_cap,
cap);
87 darr() noexcept = default;
108 template <
typename InputIt>
110 const auto count = (
size_type)core::distance(first, last);
113 if constexpr (std::is_pointer_v<InputIt>) {
115 operator[](
i) = first[
i];
116 }
else if constexpr (std::is_same_v<typename InputIt::iterator_category, core::random_access_iterator_tag>) {
118 operator[](
i) = *(first[
i]);
121 for (
auto it = first;
it != last; ++
it)
122 operator[](++
i) = *
it;
136 darr(
darr&& other)
noexcept: m_pData(other.m_pData), m_cnt(other.m_cnt), m_cap(other.m_cap) {
137 other.m_pData =
nullptr;
154 GAIA_ASSERT(core::addressof(other) !=
this);
157 mem::copy_elements<T, false>(
167 GAIA_ASSERT(core::addressof(other) !=
this);
172 m_pData = other.m_pData;
176 other.m_pData =
nullptr;
187 GAIA_CLANG_WARNING_PUSH()
189 GAIA_CLANG_WARNING_DISABLE("-Wcast-align")
194 return reinterpret_cast<pointer>(m_pData);
214 GAIA_NODISCARD
decltype(
auto)
operator[](
size_type pos)
const noexcept {
219 GAIA_CLANG_WARNING_POP()
231 mem::move_elements<T, false>(m_pData,
pDataOld, m_cnt, 0,
cap, m_cap);
245 if (m_pData ==
nullptr) {
248 GAIA_MEM_SANI_ADD_BLOCK(
value_size, m_pData, count, count);
249 core::call_ctor_n(m_pData, count);
259 core::call_dtor_n(&
data()[count], m_cnt - count);
260 GAIA_MEM_SANI_POP_N(
value_size, m_pData, m_cap, m_cnt, m_cnt - count);
267 if (count <= m_cap) {
269 GAIA_MEM_SANI_PUSH_N(
value_size, m_pData, m_cap, m_cnt, count - m_cnt);
270 core::call_ctor_n(&
data()[m_cnt], count - m_cnt);
278 GAIA_MEM_SANI_ADD_BLOCK(
value_size, m_pData, count, count);
280 mem::move_elements<T, false>(m_pData,
pDataOld, m_cnt, 0, count, m_cap);
282 core::call_ctor_n(&
data()[m_cnt], count - m_cnt);
297 if constexpr (std::is_copy_constructible_v<value_type>) {
303 operator[](
i) = value;
312 GAIA_MEM_SANI_PUSH(
value_size, m_pData, m_cap, m_cnt);
313 auto* ptr = &
data()[m_cnt++];
314 core::call_ctor(ptr,
arg);
322 GAIA_MEM_SANI_PUSH(
value_size, m_pData, m_cap, m_cnt);
323 auto* ptr = &
data()[m_cnt++];
324 core::call_ctor(ptr, GAIA_MOV(
arg));
331 template <
typename...
Args>
335 GAIA_MEM_SANI_PUSH(
value_size, m_pData, m_cap, m_cnt);
336 auto* ptr = &
data()[m_cnt++];
337 core::call_ctor(ptr, GAIA_FWD(
args)...);
343 GAIA_ASSERT(!
empty());
345 auto* ptr = &
data()[m_cnt - 1];
346 core::call_dtor(ptr);
347 GAIA_MEM_SANI_POP(
value_size, m_pData, m_cap, m_cnt);
364 GAIA_MEM_SANI_PUSH(
value_size, m_pData, m_cap, m_cnt);
365 mem::shift_elements_right<T, false>(m_pData,
idxDst,
idxSrc, m_cap);
366 auto* ptr = &
data()[m_cnt];
367 core::call_ctor(ptr,
arg);
386 GAIA_MEM_SANI_PUSH(
value_size, m_pData, m_cap, m_cnt);
387 mem::shift_elements_right<T, false>(m_pData,
idxDst,
idxSrc, m_cap);
389 core::call_ctor(ptr, GAIA_MOV(
arg));
409 mem::shift_elements_left<T, false>(m_pData,
idxDst,
idxSrc, m_cap);
411 auto* ptr = &
data()[m_cnt - 1];
412 core::call_dtor(ptr);
413 GAIA_MEM_SANI_POP(
value_size, m_pData, m_cap, m_cnt);
425 GAIA_ASSERT(first >=
data())
427 GAIA_ASSERT(last > first);
435 const auto cnt = (
size_type)(last - first);
437 mem::shift_elements_left_fast<T, false>(m_pData,
idxDst,
idxSrc, cnt, m_cap);
439 auto* ptr = &
data()[m_cnt - cnt];
440 core::call_dtor_n(ptr, cnt);
456 const auto cnt =
size();
463 GAIA_MEM_SANI_ADD_BLOCK(
value_size, m_pData, m_cap, m_cnt);
464 mem::move_elements<T, false>(m_pData,
pDataOld, cnt, 0);
473 template <
typename Func>
480 if (func(
operator[](
idxSrc))) {
482 mem::move_element<T, false>(m_pData, m_pData,
idxDst,
idxSrc, m_cap, m_cap);
484 core::call_dtor(ptr);
489 core::call_dtor(ptr);
529 GAIA_ASSERT(!
empty());
536 GAIA_ASSERT(!
empty());
543 GAIA_ASSERT(!
empty());
550 GAIA_ASSERT(!
empty());
630 if (m_cnt != other.m_cnt)
634 if (!(
operator[](
i) == other[
i]))
Array with variable size of elements of type.
Definition darray_impl.h:27
darr(const darr &other)
Copy-constructs a container.
Definition darray_impl.h:132
iterator insert(iterator pos, T &&arg)
Insert the element to the position given by iterator pos.
Definition darray_impl.h:378
iterator insert(iterator pos, const T &arg)
Insert the element to the position given by iterator pos.
Definition darray_impl.h:356
GAIA_NODISCARD auto rbegin() noexcept
Returns a reverse traversal iterator to the last element.
Definition darray_impl.h:574
GAIA_NODISCARD auto begin() const noexcept
Returns an iterator to the first element.
Definition darray_impl.h:562
GAIA_NODISCARD auto rbegin() const noexcept
Returns a reverse traversal iterator to the last element.
Definition darray_impl.h:580
const_pointer const_iterator
Read-only random-access iterator type.
Definition darray_impl.h:49
GAIA_NODISCARD auto cend() const noexcept
Returns a read-only iterator one past the last element.
Definition darray_impl.h:604
decltype(auto) emplace_back(Args &&... args)
Constructs and appends an element.
Definition darray_impl.h:332
darr_detail::size_type size_type
Unsigned type used for sizes and indices.
Definition darray_impl.h:44
GAIA_NODISCARD decltype(auto) operator[](size_type pos) noexcept
Accesses an element without bounds checking in optimized builds.
Definition darray_impl.h:206
GAIA_NODISCARD decltype(auto) back() const noexcept
Accesses the last element.
Definition darray_impl.h:549
GAIA_NODISCARD auto cbegin() const noexcept
Returns a read-only iterator to the first element.
Definition darray_impl.h:568
darr & operator=(const darr &other)
Copy-assigns the container.
Definition darray_impl.h:153
darr(InputIt first, InputIt last)
Constructs a container from an iterator range.
Definition darray_impl.h:109
GAIA_NODISCARD decltype(auto) front() const noexcept
Accesses the first element.
Definition darray_impl.h:535
GAIA_NODISCARD size_type capacity() const noexcept
Returns the number of elements that fit without reallocation.
Definition darray_impl.h:516
darr & operator=(darr &&other) noexcept
Move-assigns the container.
Definition darray_impl.h:166
void reserve(size_type cap)
Ensures storage for at least the requested number of elements.
Definition darray_impl.h:223
GAIA_NODISCARD const_pointer data() const noexcept
Returns a pointer to the element storage.
Definition darray_impl.h:199
GAIA_NODISCARD size_type size() const noexcept
Returns the number of elements.
Definition darray_impl.h:504
GAIA_NODISCARD decltype(auto) back() noexcept
Accesses the last element.
Definition darray_impl.h:542
void clear() noexcept
Removes all elements.
Definition darray_impl.h:449
GAIA_NODISCARD auto begin() noexcept
Returns an iterator to the first element.
Definition darray_impl.h:556
void shrink_to_fit()
Reduces allocated storage to match the current size when possible.
Definition darray_impl.h:454
GAIA_NODISCARD auto crend() const noexcept
Returns the read-only reverse traversal sentinel preceding the first element.
Definition darray_impl.h:622
iterator erase(iterator first, iterator last) noexcept
Removes the elements in the range [first, last)
Definition darray_impl.h:424
darr(size_type count, const_reference value)
Constructs a container with copies of a value.
Definition darray_impl.h:94
darr(size_type count)
Constructs a container with the requested number of value-initialized elements.
Definition darray_impl.h:100
void resize(size_type count)
Changes the number of elements.
Definition darray_impl.h:240
static constexpr size_t value_size
Size of one element in bytes.
Definition darray_impl.h:54
GAIA_NODISCARD auto rend() const noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition darray_impl.h:616
darr & operator=(std::initializer_list< T > il)
Replaces the elements from an initializer list.
Definition darray_impl.h:145
GAIA_NODISCARD auto rend() noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition darray_impl.h:610
auto retain(Func &&func)
Removes all elements that fail the predicate.
Definition darray_impl.h:474
GAIA_NODISCARD bool operator==(const darr &other) const noexcept
Compares two containers element by element.
Definition darray_impl.h:629
GAIA_NODISCARD decltype(auto) front() noexcept
Accesses the first element.
Definition darray_impl.h:528
iterator erase(iterator pos) noexcept
Removes the element at pos.
Definition darray_impl.h:399
void push_back(T &&arg)
Appends an element.
Definition darray_impl.h:319
darr(std::initializer_list< T > il)
Constructs a container from an initializer list.
Definition darray_impl.h:128
pointer iterator
Mutable random-access iterator type.
Definition darray_impl.h:47
darr_detail::difference_type difference_type
Type used for iterator differences.
Definition darray_impl.h:42
GAIA_NODISCARD bool empty() const noexcept
Checks whether the container has no elements.
Definition darray_impl.h:510
void pop_back() noexcept
Removes the last element.
Definition darray_impl.h:342
darr(darr &&other) noexcept
Move-constructs a container.
Definition darray_impl.h:136
void resize(size_type count, const_reference value)
Changes the size and initializes new elements from a value.
Definition darray_impl.h:293
GAIA_NODISCARD pointer data() noexcept
Returns a pointer to the element storage.
Definition darray_impl.h:193
void push_back(const T &arg)
Appends an element.
Definition darray_impl.h:309
GAIA_NODISCARD auto crbegin() const noexcept
Returns a read-only reverse traversal iterator to the last element.
Definition darray_impl.h:586
T * pointer
Mutable element pointer type.
Definition darray_impl.h:36
GAIA_NODISCARD size_type max_size() const noexcept
Returns the maximum number of elements supported by this container.
Definition darray_impl.h:522
GAIA_NODISCARD constexpr bool operator!=(const darr &other) const noexcept
Checks whether two containers differ.
Definition darray_impl.h:642
const T * const_pointer
Read-only element pointer type.
Definition darray_impl.h:38
GAIA_NODISCARD auto end() noexcept
Returns an iterator one past the last element.
Definition darray_impl.h:592
GAIA_NODISCARD auto end() const noexcept
Returns an iterator one past the last element.
Definition darray_impl.h:598
View policy for accessing and storing data in the AoS way. Good for random access and when accessing ...
Definition data_layout_policy.h:162
std::add_pointer_t< ValueType > TargetCastType
Pointer type used to address stored values.
Definition data_layout_policy.h:164
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:238
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:230