| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  local_vector.h                                                       */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							|  |  |  | /*                      https://godotengine.org                          */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2022-01-13 09:45:09 +01:00
										 |  |  | /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef LOCAL_VECTOR_H
 | 
					
						
							|  |  |  | #define LOCAL_VECTOR_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "core/error_macros.h"
 | 
					
						
							|  |  |  | #include "core/os/memory.h"
 | 
					
						
							|  |  |  | #include "core/sort_array.h"
 | 
					
						
							|  |  |  | #include "core/vector.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <class T, class U = uint32_t, bool force_trivial = false> | 
					
						
							|  |  |  | class LocalVector { | 
					
						
							| 
									
										
										
										
											2021-08-17 13:40:39 +01:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | 	U count = 0; | 
					
						
							|  |  |  | 	U capacity = 0; | 
					
						
							|  |  |  | 	T *data = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	T *ptr() { | 
					
						
							|  |  |  | 		return data; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const T *ptr() const { | 
					
						
							|  |  |  | 		return data; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ void push_back(T p_elem) { | 
					
						
							|  |  |  | 		if (unlikely(count == capacity)) { | 
					
						
							|  |  |  | 			if (capacity == 0) { | 
					
						
							|  |  |  | 				capacity = 1; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				capacity <<= 1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			data = (T *)memrealloc(data, capacity * sizeof(T)); | 
					
						
							|  |  |  | 			CRASH_COND_MSG(!data, "Out of memory"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!__has_trivial_constructor(T) && !force_trivial) { | 
					
						
							|  |  |  | 			memnew_placement(&data[count++], T(p_elem)); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			data[count++] = p_elem; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void remove(U p_index) { | 
					
						
							|  |  |  | 		ERR_FAIL_UNSIGNED_INDEX(p_index, count); | 
					
						
							|  |  |  | 		count--; | 
					
						
							|  |  |  | 		for (U i = p_index; i < count; i++) { | 
					
						
							|  |  |  | 			data[i] = data[i + 1]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (!__has_trivial_destructor(T) && !force_trivial) { | 
					
						
							|  |  |  | 			data[count].~T(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-21 13:13:20 +01:00
										 |  |  | 	// Removes the item copying the last value into the position of the one to
 | 
					
						
							|  |  |  | 	// remove. It's generally faster than `remove`.
 | 
					
						
							|  |  |  | 	void remove_unordered(U p_index) { | 
					
						
							|  |  |  | 		ERR_FAIL_INDEX(p_index, count); | 
					
						
							|  |  |  | 		count--; | 
					
						
							|  |  |  | 		if (count > p_index) { | 
					
						
							|  |  |  | 			data[p_index] = data[count]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (!__has_trivial_destructor(T) && !force_trivial) { | 
					
						
							|  |  |  | 			data[count].~T(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | 	void erase(const T &p_val) { | 
					
						
							|  |  |  | 		int64_t idx = find(p_val); | 
					
						
							|  |  |  | 		if (idx >= 0) { | 
					
						
							|  |  |  | 			remove(idx); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 12:30:45 +01:00
										 |  |  | 	U erase_multiple_unordered(const T &p_val) { | 
					
						
							|  |  |  | 		U from = 0; | 
					
						
							|  |  |  | 		U count = 0; | 
					
						
							|  |  |  | 		while (true) { | 
					
						
							|  |  |  | 			int64_t idx = find(p_val, from); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (idx == -1) { | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			remove_unordered(idx); | 
					
						
							|  |  |  | 			from = idx; | 
					
						
							|  |  |  | 			count++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return count; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | 	void invert() { | 
					
						
							|  |  |  | 		for (U i = 0; i < count / 2; i++) { | 
					
						
							|  |  |  | 			SWAP(data[i], data[count - i - 1]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ void clear() { resize(0); } | 
					
						
							|  |  |  | 	_FORCE_INLINE_ void reset() { | 
					
						
							|  |  |  | 		clear(); | 
					
						
							|  |  |  | 		if (data) { | 
					
						
							|  |  |  | 			memfree(data); | 
					
						
							|  |  |  | 			data = nullptr; | 
					
						
							|  |  |  | 			capacity = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	_FORCE_INLINE_ bool empty() const { return count == 0; } | 
					
						
							|  |  |  | 	_FORCE_INLINE_ void reserve(U p_size) { | 
					
						
							|  |  |  | 		p_size = nearest_power_of_2_templated(p_size); | 
					
						
							|  |  |  | 		if (p_size > capacity) { | 
					
						
							|  |  |  | 			capacity = p_size; | 
					
						
							|  |  |  | 			data = (T *)memrealloc(data, capacity * sizeof(T)); | 
					
						
							|  |  |  | 			CRASH_COND_MSG(!data, "Out of memory"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ U size() const { return count; } | 
					
						
							|  |  |  | 	void resize(U p_size) { | 
					
						
							|  |  |  | 		if (p_size < count) { | 
					
						
							|  |  |  | 			if (!__has_trivial_destructor(T) && !force_trivial) { | 
					
						
							|  |  |  | 				for (U i = p_size; i < count; i++) { | 
					
						
							|  |  |  | 					data[i].~T(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			count = p_size; | 
					
						
							|  |  |  | 		} else if (p_size > count) { | 
					
						
							|  |  |  | 			if (unlikely(p_size > capacity)) { | 
					
						
							|  |  |  | 				if (capacity == 0) { | 
					
						
							|  |  |  | 					capacity = 1; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				while (capacity < p_size) { | 
					
						
							|  |  |  | 					capacity <<= 1; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				data = (T *)memrealloc(data, capacity * sizeof(T)); | 
					
						
							|  |  |  | 				CRASH_COND_MSG(!data, "Out of memory"); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (!__has_trivial_constructor(T) && !force_trivial) { | 
					
						
							|  |  |  | 				for (U i = count; i < p_size; i++) { | 
					
						
							|  |  |  | 					memnew_placement(&data[i], T); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			count = p_size; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	_FORCE_INLINE_ const T &operator[](U p_index) const { | 
					
						
							|  |  |  | 		CRASH_BAD_UNSIGNED_INDEX(p_index, count); | 
					
						
							|  |  |  | 		return data[p_index]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	_FORCE_INLINE_ T &operator[](U p_index) { | 
					
						
							|  |  |  | 		CRASH_BAD_UNSIGNED_INDEX(p_index, count); | 
					
						
							|  |  |  | 		return data[p_index]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 18:45:34 +08:00
										 |  |  | 	void fill(T p_val) { | 
					
						
							|  |  |  | 		for (U i = 0; i < count; i++) { | 
					
						
							|  |  |  | 			data[i] = p_val; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | 	void insert(U p_pos, T p_val) { | 
					
						
							|  |  |  | 		ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1); | 
					
						
							|  |  |  | 		if (p_pos == count) { | 
					
						
							|  |  |  | 			push_back(p_val); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			resize(count + 1); | 
					
						
							| 
									
										
										
										
											2021-08-26 11:54:56 +02:00
										 |  |  | 			for (U i = count - 1; i > p_pos; i--) { | 
					
						
							| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | 				data[i] = data[i - 1]; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			data[p_pos] = p_val; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int64_t find(const T &p_val, U p_from = 0) const { | 
					
						
							| 
									
										
										
										
											2021-06-29 13:37:02 +02:00
										 |  |  | 		for (U i = p_from; i < count; i++) { | 
					
						
							| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | 			if (data[i] == p_val) { | 
					
						
							|  |  |  | 				return int64_t(i); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	template <class C> | 
					
						
							|  |  |  | 	void sort_custom() { | 
					
						
							|  |  |  | 		U len = count; | 
					
						
							|  |  |  | 		if (len == 0) { | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		SortArray<T, C> sorter; | 
					
						
							|  |  |  | 		sorter.sort(data, len); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void sort() { | 
					
						
							| 
									
										
										
										
											2021-05-04 14:20:36 +02:00
										 |  |  | 		sort_custom<_DefaultComparator<T>>(); | 
					
						
							| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void ordered_insert(T p_val) { | 
					
						
							|  |  |  | 		U i; | 
					
						
							|  |  |  | 		for (i = 0; i < count; i++) { | 
					
						
							|  |  |  | 			if (p_val < data[i]) { | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		insert(i, p_val); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	operator Vector<T>() const { | 
					
						
							|  |  |  | 		Vector<T> ret; | 
					
						
							|  |  |  | 		ret.resize(size()); | 
					
						
							|  |  |  | 		T *w = ret.ptrw(); | 
					
						
							| 
									
										
										
										
											2021-04-29 12:34:11 +02:00
										 |  |  | 		memcpy(w, data, sizeof(T) * count); | 
					
						
							| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | 		return ret; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vector<uint8_t> to_byte_array() const { //useful to pass stuff to gpu or variant
 | 
					
						
							|  |  |  | 		Vector<uint8_t> ret; | 
					
						
							|  |  |  | 		ret.resize(count * sizeof(T)); | 
					
						
							|  |  |  | 		uint8_t *w = ret.ptrw(); | 
					
						
							| 
									
										
										
										
											2021-04-29 12:34:11 +02:00
										 |  |  | 		memcpy(w, data, sizeof(T) * count); | 
					
						
							| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | 		return ret; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ LocalVector() {} | 
					
						
							|  |  |  | 	_FORCE_INLINE_ LocalVector(const LocalVector &p_from) { | 
					
						
							|  |  |  | 		resize(p_from.size()); | 
					
						
							|  |  |  | 		for (U i = 0; i < p_from.count; i++) { | 
					
						
							|  |  |  | 			data[i] = p_from.data[i]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	inline LocalVector &operator=(const LocalVector &p_from) { | 
					
						
							|  |  |  | 		resize(p_from.size()); | 
					
						
							|  |  |  | 		for (U i = 0; i < p_from.count; i++) { | 
					
						
							|  |  |  | 			data[i] = p_from.data[i]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return *this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	inline LocalVector &operator=(const Vector<T> &p_from) { | 
					
						
							|  |  |  | 		resize(p_from.size()); | 
					
						
							|  |  |  | 		for (U i = 0; i < count; i++) { | 
					
						
							|  |  |  | 			data[i] = p_from[i]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return *this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ ~LocalVector() { | 
					
						
							|  |  |  | 		if (data) { | 
					
						
							|  |  |  | 			reset(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 13:40:39 +01:00
										 |  |  | // Integer default version
 | 
					
						
							|  |  |  | template <class T, class I = int32_t, bool force_trivial = false> | 
					
						
							|  |  |  | class LocalVectori : public LocalVector<T, I, force_trivial> { | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 18:16:39 +02:00
										 |  |  | #endif // LOCAL_VECTOR_H
 |