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_soa_detail {
27 struct darr_soa_iterator {
35 using size_type = darr_soa_detail::size_type;
47 darr_soa_iterator(uint8_t* ptr,
size_type cnt,
size_type idx): m_ptr(ptr), m_cnt(cnt), m_idx(idx) {}
50 return mem::data_view_policy<T::gaia_Data_Layout, T>::get({m_ptr, m_cnt}, m_idx);
52 T operator->()
const {
53 return mem::data_view_policy<T::gaia_Data_Layout, T>::get({m_ptr, m_cnt}, m_idx);
93 GAIA_ASSERT(m_ptr == other.m_ptr);
98 GAIA_ASSERT(m_ptr == other.m_ptr);
99 return m_idx == other.m_idx;
102 GAIA_ASSERT(m_ptr == other.m_ptr);
103 return m_idx != other.m_idx;
105 GAIA_NODISCARD
bool operator>(
const iterator& other)
const {
106 GAIA_ASSERT(m_ptr == other.m_ptr);
107 return m_idx > other.m_idx;
109 GAIA_NODISCARD
bool operator>=(
const iterator& other)
const {
110 GAIA_ASSERT(m_ptr == other.m_ptr);
111 return m_idx >= other.m_idx;
113 GAIA_NODISCARD
bool operator<(
const iterator& other)
const {
114 GAIA_ASSERT(m_ptr == other.m_ptr);
115 return m_idx < other.m_idx;
117 GAIA_NODISCARD
bool operator<=(
const iterator& other)
const {
118 GAIA_ASSERT(m_ptr == other.m_ptr);
119 return m_idx <= other.m_idx;
123 template <
typename T>
124 struct const_darr_soa_iterator {
129 using size_type = darr_soa_detail::size_type;
131 using iterator = const_darr_soa_iterator;
135 const uint8_t* m_ptr;
140 const_darr_soa_iterator(
const uint8_t* ptr,
size_type cnt,
size_type idx): m_ptr(ptr), m_cnt(cnt), m_idx(idx) {}
142 T operator*()
const {
143 return mem::data_view_policy<T::gaia_Data_Layout, T>::get({m_ptr, m_cnt}, m_idx);
145 T operator->()
const {
146 return mem::data_view_policy<T::gaia_Data_Layout, T>::get({m_ptr, m_cnt}, m_idx);
186 GAIA_ASSERT(m_ptr == other.m_ptr);
191 GAIA_ASSERT(m_ptr == other.m_ptr);
192 return m_idx == other.m_idx;
195 GAIA_ASSERT(m_ptr == other.m_ptr);
196 return m_idx != other.m_idx;
198 GAIA_NODISCARD
bool operator>(
const iterator& other)
const {
199 GAIA_ASSERT(m_ptr == other.m_ptr);
200 return m_idx > other.m_idx;
202 GAIA_NODISCARD
bool operator>=(
const iterator& other)
const {
203 GAIA_ASSERT(m_ptr == other.m_ptr);
204 return m_idx >= other.m_idx;
206 GAIA_NODISCARD
bool operator<(
const iterator& other)
const {
207 GAIA_ASSERT(m_ptr == other.m_ptr);
208 return m_idx < other.m_idx;
210 GAIA_NODISCARD
bool operator<=(
const iterator& other)
const {
211 GAIA_ASSERT(m_ptr == other.m_ptr);
212 return m_idx <= other.m_idx;
220 template <
typename T,
typename Allocator = mem::DefaultAllocatorAdaptor>
222 static_assert(mem::is_soa_layout_v<T>,
"darr_soa can be used only with soa types");
259 const auto cnt =
size();
263 if GAIA_LIKELY (
cap != 0 && cnt <
cap)
267 if GAIA_UNLIKELY (m_pData ==
nullptr) {
274 m_cap = (
cap * 3 + 1) / 2;
279 mem::move_elements<T, true>(m_pData,
pDataOld, cnt, 0, m_cap,
cap);
284 darr_soa() noexcept = default;
305 template <
typename InputIt>
307 const auto count = (
size_type)core::distance(first, last);
310 if constexpr (std::is_pointer_v<InputIt>) {
312 operator[](
i) = first[
i];
313 }
else if constexpr (std::is_same_v<typename InputIt::iterator_category, core::random_access_iterator_tag>) {
315 operator[](
i) = *(first[
i]);
318 for (
auto it = first;
it != last; ++
it)
319 operator[](
i++) = *
it;
336 GAIA_ASSERT(m_pData ==
nullptr);
338 m_pData =
other.m_pData;
344 other.m_pData =
nullptr;
359 GAIA_ASSERT(core::addressof(
other) !=
this);
362 mem::copy_elements<T, true>(
372 GAIA_ASSERT(core::addressof(
other) !=
this);
377 m_pData =
other.m_pData;
381 other.m_pData =
nullptr;
392 GAIA_CLANG_WARNING_PUSH()
394 GAIA_CLANG_WARNING_DISABLE("-Wcast-align")
399 return reinterpret_cast<pointer>(m_pData);
419 GAIA_NODISCARD
decltype(
auto)
operator[](
size_type pos)
const noexcept {
424 GAIA_CLANG_WARNING_POP()
437 mem::move_elements<T, true>(m_pData,
pDataOld, m_cnt, 0,
cap, m_cap);
452 if (m_pData ==
nullptr) {
471 if (count <= m_cap) {
478 const auto cap = detail::resize_capacity(
policy, m_cap, count);
483 mem::move_elements<T, true>(m_pData,
pDataOld, m_cnt, 0,
cap, m_cap);
499 if constexpr (std::is_copy_constructible_v<value_type>) {
505 operator[](
i) = value;
531 template <
typename...
Args>
533 T value(GAIA_FWD(
args)...);
542 GAIA_ASSERT(!
empty());
555 GAIA_ASSERT(
pos <=
end());
562 mem::shift_elements_right<T, true>(m_pData,
idxDst,
idxSrc, m_cap);
576 GAIA_ASSERT(
pos <=
end());
583 mem::shift_elements_right<T, true>(m_pData,
idxDst,
idxSrc, m_cap);
603 mem::shift_elements_left<T, true>(m_pData,
idxDst,
idxSrc, m_cap);
617 GAIA_ASSERT(last > first);
618 GAIA_ASSERT(last <=
end());
625 const auto cnt = (
size_type)(last - first);
627 mem::shift_elements_left_fast<T, true>(m_pData,
idxDst,
idxSrc, cnt, m_cap);
643 const auto cnt =
size();
651 mem::move_elements<T, true>(m_pData,
pDataOld, cnt, 0, cnt,
cap);
662 template <
typename Func>
665 const auto newSize = detail::retain_array<true>(*
this, m_cnt, func);
697 GAIA_ASSERT(!
empty());
704 GAIA_ASSERT(!
empty());
711 GAIA_ASSERT(!
empty());
718 GAIA_ASSERT(!
empty());
798 if (m_cnt !=
other.m_cnt)
802 if (!(
operator[](
i) ==
other[
i]))
817 template <
size_t Item>
826 template <
size_t Item>
829 std::span<const uint8_t>{GAIA_ACC((
const uint8_t*)m_pData),
capacity()});
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
pointer iterator
Mutable random-access iterator type.
Definition darray_ext_impl.h:53
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
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
core::random_access_iterator_tag iterator_category
Iterator category exposed by the container.
Definition darray_ext_impl.h:61
darr_ext_detail::difference_type difference_type
Type used for iterator differences.
Definition darray_ext_impl.h:48
T value_type
Element type stored by the container.
Definition darray_ext_impl.h:36
GAIA_NODISCARD decltype(auto) operator[](size_type pos) noexcept
Accesses an element without bounds checking in optimized builds.
Definition darray_ext_impl.h:249
GAIA_NODISCARD bool operator==(const darr_ext &other) const noexcept
Compares two containers element by element.
Definition darray_ext_impl.h:686
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_soa_impl.h:221
iterator insert(iterator pos, T &&arg)
Insert the element to the position given by iterator pos.
Definition darray_soa_impl.h:574
void resize(size_type count, ResizePolicy policy=ResizePolicy::Exact)
Changes the number of elements.
Definition darray_soa_impl.h:447
auto view_mut() noexcept
Returns a mutable view of one structure-of-arrays member.
Definition darray_soa_impl.h:818
GAIA_NODISCARD const_pointer data() const noexcept
Returns a pointer to the element storage.
Definition darray_soa_impl.h:404
GAIA_NODISCARD bool empty() const noexcept
Checks whether the container has no elements.
Definition darray_soa_impl.h:678
void reserve(size_type cap)
Ensures storage for at least the requested number of elements.
Definition darray_soa_impl.h:428
void push_back(T &&arg)
Appends an element.
Definition darray_soa_impl.h:520
core::reverse_iterator< iterator, difference_type > reverse_iterator
Mutable reverse random-access iterator type.
Definition darray_soa_impl.h:247
void resize(size_type count, const_reference value, ResizePolicy policy=ResizePolicy::Exact)
Changes the size and initializes new elements from a value.
Definition darray_soa_impl.h:495
decltype(auto) emplace_back(Args &&... args)
Constructs and appends an element.
Definition darray_soa_impl.h:532
auto view() const noexcept
Returns a read-only view of one structure-of-arrays member.
Definition darray_soa_impl.h:827
GAIA_NODISCARD auto rbegin() const noexcept
Returns a reverse traversal iterator to the last element.
Definition darray_soa_impl.h:748
darr_soa(const darr_soa &other)
Copy-constructs a container.
Definition darray_soa_impl.h:329
GAIA_NODISCARD auto crbegin() const noexcept
Returns a read-only reverse traversal iterator to the last element.
Definition darray_soa_impl.h:754
GAIA_NODISCARD auto rend() const noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition darray_soa_impl.h:784
void clear() noexcept
Removes all elements.
Definition darray_soa_impl.h:636
GAIA_NODISCARD bool operator==(const darr_soa &other) const noexcept
Compares two containers element by element.
Definition darray_soa_impl.h:797
GAIA_NODISCARD auto cend() const noexcept
Returns a read-only iterator one past the last element.
Definition darray_soa_impl.h:772
const_darr_soa_iterator< T > const_iterator
Read-only random-access iterator type.
Definition darray_soa_impl.h:245
darr_soa_detail::size_type size_type
Unsigned type used for sizes and indices.
Definition darray_soa_impl.h:240
darr_soa_detail::difference_type difference_type
Type used for iterator differences.
Definition darray_soa_impl.h:238
auto retain(Func &&func) noexcept
Removes all elements that fail the predicate. The predicate may modify its element but must not chang...
Definition darray_soa_impl.h:663
GAIA_NODISCARD decltype(auto) operator[](size_type pos) noexcept
Accesses an element without bounds checking in optimized builds.
Definition darray_soa_impl.h:411
darr_soa & operator=(darr_soa &&other) noexcept
Move-assigns the container.
Definition darray_soa_impl.h:371
GAIA_NODISCARD size_type capacity() const noexcept
Returns the number of elements that fit without reallocation.
Definition darray_soa_impl.h:684
GAIA_NODISCARD auto end() const noexcept
Returns an iterator one past the last element.
Definition darray_soa_impl.h:766
iterator erase(iterator first, iterator last) noexcept
Removes the elements in the range [first, last)
Definition darray_soa_impl.h:615
GAIA_NODISCARD decltype(auto) front() const noexcept
Accesses the first element.
Definition darray_soa_impl.h:703
GAIA_NODISCARD auto cbegin() const noexcept
Returns a read-only iterator to the first element.
Definition darray_soa_impl.h:736
darr_soa(size_type count, const_reference value)
Constructs a container with copies of a value.
Definition darray_soa_impl.h:291
GAIA_NODISCARD decltype(auto) back() const noexcept
Accesses the last element.
Definition darray_soa_impl.h:717
GAIA_NODISCARD constexpr bool operator!=(const darr_soa &other) const noexcept
Checks whether two containers differ.
Definition darray_soa_impl.h:810
GAIA_NODISCARD decltype(auto) front() noexcept
Accesses the first element.
Definition darray_soa_impl.h:696
darr_soa(size_type count)
Constructs a container with the requested number of value-initialized elements.
Definition darray_soa_impl.h:297
void push_back(const T &arg)
Appends an element.
Definition darray_soa_impl.h:511
iterator erase(iterator pos) noexcept
Removes the element at pos.
Definition darray_soa_impl.h:594
darr_soa_iterator< T > iterator
Mutable random-access iterator type.
Definition darray_soa_impl.h:243
GAIA_NODISCARD auto crend() const noexcept
Returns the read-only reverse traversal sentinel preceding the first element.
Definition darray_soa_impl.h:790
darr_soa(darr_soa &&other) noexcept
Move-constructs a container.
Definition darray_soa_impl.h:333
GAIA_NODISCARD decltype(auto) back() noexcept
Accesses the last element.
Definition darray_soa_impl.h:710
GAIA_NODISCARD auto rend() noexcept
Returns the reverse traversal sentinel preceding the first element.
Definition darray_soa_impl.h:778
darr_soa & operator=(const darr_soa &other)
Copy-assigns the container.
Definition darray_soa_impl.h:358
void shrink_to_fit()
Reduces allocated storage to match the current size when possible.
Definition darray_soa_impl.h:641
GAIA_NODISCARD size_type max_size() const noexcept
Returns the maximum number of elements supported by this container.
Definition darray_soa_impl.h:690
GAIA_NODISCARD auto rbegin() noexcept
Returns a reverse traversal iterator to the last element.
Definition darray_soa_impl.h:742
GAIA_NODISCARD size_type size() const noexcept
Returns the number of elements.
Definition darray_soa_impl.h:672
darr_soa(std::initializer_list< T > il)
Constructs a container from an initializer list.
Definition darray_soa_impl.h:325
core::reverse_iterator< const_iterator, difference_type > const_reverse_iterator
Read-only reverse random-access iterator type.
Definition darray_soa_impl.h:249
GAIA_NODISCARD auto begin() const noexcept
Returns an iterator to the first element.
Definition darray_soa_impl.h:730
GAIA_NODISCARD auto begin() noexcept
Returns an iterator to the first element.
Definition darray_soa_impl.h:724
GAIA_NODISCARD auto end() noexcept
Returns an iterator one past the last element.
Definition darray_soa_impl.h:760
darr_soa(InputIt first, InputIt last)
Constructs a container from an iterator range.
Definition darray_soa_impl.h:306
iterator insert(iterator pos, const T &arg)
Insert the element to the position given by iterator pos.
Definition darray_soa_impl.h:553
void pop_back() noexcept
Removes the last element.
Definition darray_soa_impl.h:541
darr_soa & operator=(std::initializer_list< T > il)
Replaces the elements from an initializer list.
Definition darray_soa_impl.h:350
GAIA_NODISCARD pointer data() noexcept
Returns a pointer to the element storage.
Definition darray_soa_impl.h:398
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 SoA way. Good for SIMD processing.
Definition data_layout_policy.h:398
GAIA_NODISCARD static constexpr ValueType get(std::span< const uint8_t > s, size_t idx) noexcept
Reconstructs a value from its SoA fields.
Definition data_layout_policy.h:526
static void mem_pop_block(void *pData, size_t cap, size_t count, size_t n)
Poisons removed SoA values for the memory sanitizer.
Definition data_layout_policy.h:510
static void mem_add_block(void *pData, size_t cap, size_t count)
Registers a newly allocated SoA range with the memory sanitizer.
Definition data_layout_policy.h:460
uint8_t * TargetCastType
Pointer type used to address SoA storage.
Definition data_layout_policy.h:404
GAIA_NODISCARD static constexpr auto set(std::span< uint8_t > s, size_t idx) noexcept
Returns a mutable proxy for one complete value.
Definition data_layout_policy.h:578
static void mem_push_block(void *pData, size_t cap, size_t count, size_t n)
Makes newly appended SoA values addressable by the memory sanitizer.
Definition data_layout_policy.h:493
Storage policy for a selected layout and item type.
Definition data_layout_policy.h:121