Debian Codegraph @

core/templates/fixed_vector.h

raw symbols SCIP
1 /**************************************************************************/
2 /* fixed_vector.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/templates/span.h"
34
35 GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Warray-bounds")
36
37 /**
38 * A high performance Vector of fixed capacity.
39 * Especially useful if you need to create an array on the stack, to
40 * prevent dynamic allocations (especially in bottleneck code).
41 *
42 * Choose CAPACITY such that it is enough for all elements that could be added through all branches.
43 *
44 */
45 template <class T, uint32_t CAPACITY>
46 class FixedVector {
47 // This declaration allows us to access other FixedVector's private members.
48 template <class T_, uint32_t CAPACITY_>
49 friend class FixedVector;
50
51 uint32_t _size = 0;
52 alignas(T) uint8_t _data[CAPACITY * sizeof(T)];
53
54 constexpr static uint32_t DATA_PADDING = MAX(alignof(T), alignof(uint32_t)) - alignof(uint32_t);
55
56 public:
57 _FORCE_INLINE_ constexpr FixedVector() = default;
58
59 constexpr FixedVector(std::initializer_list<T> p_init) {
60 ERR_FAIL_COND(p_init.size() > CAPACITY);
61 for (const T &element : p_init) {
62 memnew_placement(ptr() + _size++, T(element));
63 }
64 }
65
66 constexpr FixedVector(const FixedVector &p_from) {
67 if constexpr (std::is_trivially_copyable_v<T>) {
68 // Copy size and all provided elements at once.
69 memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
70 } else {
71 for (const T &element : p_from) {
72 memnew_placement(ptr() + _size++, T(element));
73 }
74 }
75 }
76
77 constexpr FixedVector(FixedVector &&p_from) {
78 // Copy size and all provided elements at once.
79 // Note: Assumes trivial relocatability.
80 memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
81 p_from._size = 0;
82 }
83
84 constexpr FixedVector &operator=(const FixedVector &p_from) {
85 if constexpr (std::is_trivially_copyable_v<T>) {
86 // Copy size and all provided elements at once.
87 memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
88 } else {
89 // Destruct extraneous elements.
90 if constexpr (!std::is_trivially_destructible_v<T>) {
91 for (uint32_t i = p_from.size(); i < _size; i++) {
92 ptr()[i].~T();
93 }
94 }
95
96 _size = 0; // Loop-assign the rest.
97 for (const T &element : p_from) {
98 ptr()[_size++] = element;
99 }
100 }
101 return *this;
102 }
103
104 constexpr FixedVector &operator=(FixedVector &&p_from) {
105 // Destruct extraneous elements.
106 if constexpr (!std::is_trivially_destructible_v<T>) {
107 for (uint32_t i = p_from.size(); i < _size; i++) {
108 ptr()[i].~T();
109 }
110 }
111
112 // Relocate elements (and size) into our buffer.
113 memcpy((void *)&_size, (void *)&p_from._size, sizeof(_size) + DATA_PADDING + p_from.size() * sizeof(T));
114 p_from._size = 0;
115
116 return *this;
117 }
118
119 ~FixedVector() {
120 if constexpr (!std::is_trivially_destructible_v<T>) {
121 for (uint32_t i = 0; i < _size; i++) {
122 ptr()[i].~T();
123 }
124 }
125 }
126
127 _FORCE_INLINE_ constexpr T *ptr() { return (T *)(_data); }
128 _FORCE_INLINE_ constexpr const T *ptr() const { return (const T *)(_data); }
129
130 _FORCE_INLINE_ constexpr operator Span<T>() const { return Span<T>(ptr(), size()); }
131 _FORCE_INLINE_ constexpr Span<T> span() const { return operator Span<T>(); }
132
133 _FORCE_INLINE_ constexpr uint32_t size() const { return _size; }
134 _FORCE_INLINE_ constexpr bool is_empty() const { return !_size; }
135 _FORCE_INLINE_ constexpr bool is_full() const { return _size == CAPACITY; }
136 _FORCE_INLINE_ constexpr uint32_t capacity() const { return CAPACITY; }
137
138 _FORCE_INLINE_ constexpr void clear() { resize_initialized(0); }
139
140 /// Changes the size of the vector.
141 /// If p_size > size(), constructs new elements.
142 /// If p_size < size(), destructs new elements.
143 constexpr Error resize_initialized(uint32_t p_size) {
144 if (p_size > _size) {
145 ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
146 memnew_arr_placement(ptr() + _size, p_size - _size);
147 } else if (p_size < _size) {
148 if constexpr (!std::is_trivially_destructible_v<T>) {
149 for (uint32_t i = p_size; i < _size; i++) {
150 ptr()[i].~T();
151 }
152 }
153 }
154
155 _size = p_size;
156 return OK;
157 }
158
159 /// Changes the size of the vector.
160 /// The initializer of new elements is skipped, making this function faster than resize_initialized.
161 /// The caller is required to initialize the new values.
162 constexpr Error resize_uninitialized(uint32_t p_size) {
163 static_assert(std::is_trivially_destructible_v<T>, "resize_uninitialized is unsafe to call if T is not trivially destructible.");
164 ERR_FAIL_COND_V(p_size > CAPACITY, ERR_OUT_OF_MEMORY);
165 _size = p_size;
166 return OK;
167 }
168
169 constexpr void push_back(const T &p_val) {
170 ERR_FAIL_COND(_size >= CAPACITY);
171 memnew_placement(ptr() + _size, T(p_val));
172 _size++;
173 }
174
175 constexpr void push_back(T &&p_val) {
176 ERR_FAIL_COND(_size >= CAPACITY);
177 memnew_placement(ptr() + _size, T(std::move(p_val)));
178 _size++;
179 }
180
181 constexpr void pop_back() {
182 ERR_FAIL_COND(_size == 0);
183 _size--;
184 ptr()[_size].~T();
185 }
186
187 // NOTE: Subscripts sanity check the bounds to avoid undefined behavior.
188 // This is slower than direct buffer access and can prevent autovectorization.
189 // If the bounds are known, use ptr() subscript instead.
190 constexpr const T &operator[](uint32_t p_index) const {
191 CRASH_COND(p_index >= _size);
192 return ptr()[p_index];
193 }
194
195 constexpr T &operator[](uint32_t p_index) {
196 CRASH_COND(p_index >= _size);
197 return ptr()[p_index];
198 }
199
200 _FORCE_INLINE_ constexpr T *begin() { return ptr(); }
201 _FORCE_INLINE_ constexpr T *end() { return ptr() + _size; }
202
203 _FORCE_INLINE_ constexpr const T *begin() const { return ptr(); }
204 _FORCE_INLINE_ constexpr const T *end() const { return ptr() + _size; }
205 };
206
207 GODOT_GCC_WARNING_POP