Gaia-ECS v1.0.0
A simple and powerful entity component system
Loading...
Searching...
No Matches
array_utils.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include "gaia/core/utility.h"
5#include "gaia/mem/mem_utils.h"
6
7namespace gaia {
8 namespace cnt {
10 enum class ResizePolicy : uint8_t {
12 Exact,
16 Scale,
17 };
18
20 namespace detail {
29 template <typename SizeType>
30 GAIA_NODISCARD constexpr SizeType resize_capacity(ResizePolicy policy, SizeType cap, SizeType count) noexcept {
31 return policy == ResizePolicy::Scale ? core::get_max(count, (SizeType)((cap * 3 + 1) / 2)) : count;
32 }
33
45 template <bool SOA, typename Array, typename Func>
46 uint32_t retain_array(Array& arr, uint32_t& count, Func&& func) {
47 using T = typename Array::value_type;
48 uint32_t erased = 0;
49 uint32_t idxDst = 0;
50 uint32_t idxSrc = 0;
51
52 while (idxSrc < count) {
53 if (func(arr[idxSrc])) {
54 if (idxDst < idxSrc) {
55 auto* ptr = (uint8_t*)arr.data();
56 mem::move_element<T, SOA>(ptr, ptr, idxDst, idxSrc, arr.capacity(), arr.capacity());
57 }
58 ++idxDst;
59 } else {
60 ++erased;
61 }
62 ++idxSrc;
63 }
64
66 if constexpr (!SOA) {
67 if (erased != 0)
68 core::call_dtor_n(arr.data() + idxDst, erased);
69 }
70 count -= erased;
71 return idxDst;
72 }
73 } // namespace detail
75 } // namespace cnt
76} // namespace gaia