Debian Codegraph @

core/templates/vector.h

raw symbols SCIP
1 /**************************************************************************/
2 /* 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 /**
34 * @class Vector
35 * Vector container. Simple copy-on-write container.
36 *
37 * LocalVector is an alternative available for internal use when COW is not
38 * required.
39 */
40
41 #include "core/error/error_macros.h"
42 #include "core/templates/cowdata.h"
43 #include "core/templates/sort_array.h"
44
45 #include <initializer_list>
46
47 template <typename T>
48 class Vector;
49
50 template <typename T>
51 class VectorWriteProxy {
52 public:
53 _FORCE_INLINE_ T &operator[](typename CowData<T>::Size p_index) {
54 CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size());
55
56 return ((Vector<T> *)(this))->_cowdata.ptrw()[p_index];
57 }
58 };
59
60 template <typename T>
61 class Vector {
62 friend class VectorWriteProxy<T>;
63
64 public:
65 VectorWriteProxy<T> write;
66 typedef typename CowData<T>::Size Size;
67 typedef typename CowData<T>::USize USize;
68
69 private:
70 CowData<T> _cowdata;
71
72 public:
73 // Must take a copy instead of a reference (see GH-31736).
74 _FORCE_INLINE_ bool push_back(T p_elem) { return _cowdata.push_back(std::move(p_elem)); }
75 _FORCE_INLINE_ bool append(T p_elem) { return _cowdata.push_back(std::move(p_elem)); } //alias
76 void fill(T p_elem);
77
78 void remove_at(Size p_index) { _cowdata.remove_at(p_index); }
79 _FORCE_INLINE_ bool erase(const T &p_val) {
80 Size idx = find(p_val);
81 if (idx >= 0) {
82 remove_at(idx);
83 return true;
84 }
85 return false;
86 }
87
88 void reverse();
89
90 _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
91 _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
92 _FORCE_INLINE_ Size size() const { return _cowdata.size(); }
93 _FORCE_INLINE_ USize capacity() const { return _cowdata.capacity(); }
94
95 _FORCE_INLINE_ operator Span<T>() const { return _cowdata.span(); }
96 _FORCE_INLINE_ Span<T> span() const { return _cowdata.span(); }
97
98 _FORCE_INLINE_ void clear() { _cowdata.clear(); }
99 _FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); }
100
101 _FORCE_INLINE_ T get(Size p_index) { return _cowdata.get(p_index); }
102 _FORCE_INLINE_ const T &get(Size p_index) const { return _cowdata.get(p_index); }
103 _FORCE_INLINE_ void set(Size p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
104
105 /// Resize the vector.
106 /// Elements are initialized (or not) depending on what the default C++ behavior for this type is.
107 _FORCE_INLINE_ Error resize(Size p_size) {
108 return _cowdata.template resize<!std::is_trivially_constructible_v<T>>(p_size);
109 }
110
111 /// Resize and set all values to 0 / false / nullptr.
112 /// This is only available for zero constructible types.
113 _FORCE_INLINE_ Error resize_initialized(Size p_size) {
114 return _cowdata.template resize<true>(p_size);
115 }
116
117 /// Resize and keep memory uninitialized.
118 /// This means that any newly added elements have an unknown value, and are expected to be set after the `resize_uninitialized` call.
119 /// This is only available for trivially destructible types (otherwise, trivial resize might be UB).
120 _FORCE_INLINE_ Error resize_uninitialized(Size p_size) {
121 // resize() statically asserts that T is compatible, no need to do it ourselves.
122 return _cowdata.template resize<false>(p_size);
123 }
124
125 /// Reserves capacity for at least p_size total elements.
126 /// You can use `reserve` before repeated insertions to improve performance.
127 /// The capacity grows in 1.5x increments when possible, and uses `p_size`
128 /// exactly otherwise.
129 Error reserve(Size p_size) {
130 ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
131 return _cowdata.reserve(p_size);
132 }
133
134 /// Reserves capacity for exactly p_size total elements.
135 /// This can be useful to reduce RAM use of large vectors, but wastes CPU
136 /// time if more than p_size elements are added after the `reserve_exact` call.
137 /// Prefer using `reserve`, unless the vector (or copies of it) will never
138 /// grow again after p_size elements are inserted.
139 Error reserve_exact(Size p_size) {
140 ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
141 return _cowdata.reserve_exact(p_size);
142 }
143
144 _FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); }
145 // Must take a copy instead of a reference (see GH-31736).
146 Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, std::move(p_val)); }
147 Size find(const T &p_val, Size p_from = 0) const {
148 if (p_from < 0) {
149 p_from = size() + p_from;
150 }
151 if (p_from < 0 || p_from >= size()) {
152 return -1;
153 }
154 return span().find(p_val, p_from);
155 }
156 Size rfind(const T &p_val, Size p_from = -1) const {
157 if (p_from < 0) {
158 p_from = size() + p_from;
159 }
160 if (p_from < 0 || p_from >= size()) {
161 return -1;
162 }
163 return span().rfind(p_val, p_from);
164 }
165 Size count(const T &p_val) const { return span().count(p_val); }
166
167 // Must take a copy instead of a reference (see GH-31736).
168 void append_array(Vector<T> p_other);
169
170 _FORCE_INLINE_ bool has(const T &p_val) const { return find(p_val) != -1; }
171
172 void sort() {
173 sort_custom<Comparator<T>>();
174 }
175
176 template <typename Comparator, bool Validate = SORT_ARRAY_VALIDATE_ENABLED, typename... Args>
177 void sort_custom(Args &&...args) {
178 Size len = _cowdata.size();
179 if (len == 0) {
180 return;
181 }
182
183 T *data = ptrw();
184 SortArray<T, Comparator, Validate> sorter{ args... };
185 sorter.sort(data, len);
186 }
187
188 Size bsearch(const T &p_value, bool p_before) const {
189 return bsearch_custom<Comparator<T>>(p_value, p_before);
190 }
191
192 template <typename Comparator, typename Value, typename... Args>
193 Size bsearch_custom(const Value &p_value, bool p_before, Args &&...args) const {
194 return span().bisect(p_value, p_before, Comparator{ args... });
195 }
196
197 Vector<T> duplicate() const {
198 return *this;
199 }
200
201 void ordered_insert(const T &p_val) {
202 Size i;
203 for (i = 0; i < _cowdata.size(); i++) {
204 if (p_val < operator[](i)) {
205 break;
206 }
207 }
208 insert(i, p_val);
209 }
210
211 void operator=(const Vector &p_from) { _cowdata = p_from._cowdata; }
212 void operator=(Vector &&p_from) { _cowdata = std::move(p_from._cowdata); }
213
214 Vector<uint8_t> to_byte_array() const {
215 Vector<uint8_t> ret;
216 if (is_empty()) {
217 return ret;
218 }
219 size_t alloc_size = size() * sizeof(T);
220 ret.resize(alloc_size);
221 if (alloc_size) {
222 memcpy(ret.ptrw(), ptr(), alloc_size);
223 }
224 return ret;
225 }
226
227 Vector<T> slice(Size p_begin, Size p_end = CowData<T>::MAX_INT) const {
228 Vector<T> result;
229
230 const Size s = size();
231
232 Size begin = CLAMP(p_begin, -s, s);
233 if (begin < 0) {
234 begin += s;
235 }
236 Size end = CLAMP(p_end, -s, s);
237 if (end < 0) {
238 end += s;
239 }
240
241 ERR_FAIL_COND_V(begin > end, result);
242
243 Size result_size = end - begin;
244 result.resize(result_size);
245
246 const T *const r = ptr();
247 T *const w = result.ptrw();
248 for (Size i = 0; i < result_size; ++i) {
249 w[i] = r[begin + i];
250 }
251
252 return result;
253 }
254
255 bool operator==(const Vector<T> &p_arr) const { return span() == p_arr.span(); }
256 bool operator!=(const Vector<T> &p_arr) const { return span() != p_arr.span(); }
257
258 struct Iterator {
259 _FORCE_INLINE_ T &operator*() const {
260 return *elem_ptr;
261 }
262 _FORCE_INLINE_ T *operator->() const { return elem_ptr; }
263 _FORCE_INLINE_ Iterator &operator++() {
264 elem_ptr++;
265 return *this;
266 }
267 _FORCE_INLINE_ Iterator &operator--() {
268 elem_ptr--;
269 return *this;
270 }
271
272 _FORCE_INLINE_ bool operator==(const Iterator &b) const { return elem_ptr == b.elem_ptr; }
273 _FORCE_INLINE_ bool operator!=(const Iterator &b) const { return elem_ptr != b.elem_ptr; }
274
275 Iterator(T *p_ptr) { elem_ptr = p_ptr; }
276 Iterator() {}
277 Iterator(const Iterator &p_it) { elem_ptr = p_it.elem_ptr; }
278
279 private:
280 T *elem_ptr = nullptr;
281 };
282
283 struct ConstIterator {
284 _FORCE_INLINE_ const T &operator*() const {
285 return *elem_ptr;
286 }
287 _FORCE_INLINE_ const T *operator->() const { return elem_ptr; }
288 _FORCE_INLINE_ ConstIterator &operator++() {
289 elem_ptr++;
290 return *this;
291 }
292 _FORCE_INLINE_ ConstIterator &operator--() {
293 elem_ptr--;
294 return *this;
295 }
296
297 _FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return elem_ptr == b.elem_ptr; }
298 _FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return elem_ptr != b.elem_ptr; }
299
300 ConstIterator(const T *p_ptr) { elem_ptr = p_ptr; }
301 ConstIterator() {}
302 ConstIterator(const ConstIterator &p_it) { elem_ptr = p_it.elem_ptr; }
303
304 private:
305 const T *elem_ptr = nullptr;
306 };
307
308 _FORCE_INLINE_ Iterator begin() {
309 return Iterator(ptrw());
310 }
311 _FORCE_INLINE_ Iterator end() {
312 return Iterator(ptrw() + size());
313 }
314
315 _FORCE_INLINE_ ConstIterator begin() const {
316 return ConstIterator(ptr());
317 }
318 _FORCE_INLINE_ ConstIterator end() const {
319 return ConstIterator(ptr() + size());
320 }
321
322 _FORCE_INLINE_ Vector() {}
323 _FORCE_INLINE_ Vector(std::initializer_list<T> p_init) :
324 _cowdata(p_init) {}
325 _FORCE_INLINE_ Vector(const Vector &p_from) = default;
326 _FORCE_INLINE_ Vector(Vector &&p_from) = default;
327 };
328
329 template <typename T>
330 void Vector<T>::reverse() {
331 T *p = ptrw();
332 for (Size i = 0; i < size() / 2; i++) {
333 SWAP(p[i], p[size() - i - 1]);
334 }
335 }
336
337 template <typename T>
338 void Vector<T>::append_array(Vector<T> p_other) {
339 const Size ds = p_other.size();
340 if (ds == 0) {
341 return;
342 }
343 const Size bs = size();
344 resize(bs + ds);
345 T *p = ptrw();
346 for (Size i = 0; i < ds; ++i) {
347 p[bs + i] = p_other[i];
348 }
349 }
350
351 template <typename T>
352 void Vector<T>::fill(T p_elem) {
353 T *p = ptrw();
354 for (Size i = 0; i < size(); i++) {
355 p[i] = p_elem;
356 }
357 }
358
359 // Zero-constructing Vector initializes CowData.ptr() to nullptr and thus empty.
360 template <typename T>
361 struct is_zero_constructible<Vector<T>> : std::true_type {};