| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  vector.h                                                             */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											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).   */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03: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.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #ifndef VECTOR_H
 | 
					
						
							|  |  |  | #define VECTOR_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @class Vector | 
					
						
							|  |  |  |  * @author Juan Linietsky | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  |  * Vector container. Regular Vector Container. Use with care and for smaller arrays when possible. Use PoolVector for large arrays. | 
					
						
							| 
									
										
										
										
											2021-10-28 14:15:02 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "core/cowdata.h"
 | 
					
						
							|  |  |  | #include "core/error_macros.h"
 | 
					
						
							|  |  |  | #include "core/os/memory.h"
 | 
					
						
							| 
									
										
										
										
											2019-02-12 13:30:56 +01:00
										 |  |  | #include "core/sort_array.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | template <class T> | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | class VectorWriteProxy { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	_FORCE_INLINE_ T &operator[](int p_index) { | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 		CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size()); | 
					
						
							| 
									
										
										
										
											2016-02-19 07:13:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 		return ((Vector<T> *)(this))->_cowdata.ptrw()[p_index]; | 
					
						
							| 
									
										
										
										
											2016-02-19 07:13:16 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | template <class T> | 
					
						
							|  |  |  | class Vector { | 
					
						
							|  |  |  | 	friend class VectorWriteProxy<T>; | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	VectorWriteProxy<T> write; | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | private: | 
					
						
							|  |  |  | 	CowData<T> _cowdata; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-12-26 17:38:08 +01:00
										 |  |  | 	bool push_back(T p_elem); | 
					
						
							| 
									
										
										
										
											2022-04-22 18:45:34 +08:00
										 |  |  | 	void fill(T p_elem); | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 	void remove(int p_index) { _cowdata.remove(p_index); } | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	void erase(const T &p_val) { | 
					
						
							|  |  |  | 		int idx = find(p_val); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 		if (idx >= 0) { | 
					
						
							| 
									
										
										
										
											2021-05-04 14:28:27 +02:00
										 |  |  | 			remove(idx); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	void invert(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 	_FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); } | 
					
						
							|  |  |  | 	_FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); } | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	_FORCE_INLINE_ void clear() { resize(0); } | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 	_FORCE_INLINE_ bool empty() const { return _cowdata.empty(); } | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 	_FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); } | 
					
						
							| 
									
										
										
										
											2020-09-11 11:58:47 +01:00
										 |  |  | 	_FORCE_INLINE_ const T &get(int p_index) const { return _cowdata.get(p_index); } | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 	_FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); } | 
					
						
							|  |  |  | 	_FORCE_INLINE_ int size() const { return _cowdata.size(); } | 
					
						
							|  |  |  | 	Error resize(int p_size) { return _cowdata.resize(p_size); } | 
					
						
							|  |  |  | 	_FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata.get(p_index); } | 
					
						
							| 
									
										
										
										
											2019-12-26 17:38:08 +01:00
										 |  |  | 	Error insert(int p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); } | 
					
						
							| 
									
										
										
										
											2019-01-07 17:02:55 +00:00
										 |  |  | 	int find(const T &p_val, int p_from = 0) const { return _cowdata.find(p_val, p_from); } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 17:38:08 +01:00
										 |  |  | 	void append_array(Vector<T> p_other); | 
					
						
							| 
									
										
										
										
											2017-11-21 01:11:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	template <class C> | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	void sort_custom() { | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 		int len = _cowdata.size(); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 		if (len == 0) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		T *data = ptrw(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		SortArray<T, C> sorter; | 
					
						
							|  |  |  | 		sorter.sort(data, len); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void sort() { | 
					
						
							| 
									
										
										
										
											2021-05-04 14:20:36 +02:00
										 |  |  | 		sort_custom<_DefaultComparator<T>>(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	void ordered_insert(const T &p_val) { | 
					
						
							|  |  |  | 		int i; | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 		for (i = 0; i < _cowdata.size(); i++) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			if (p_val < operator[](i)) { | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2015-03-19 23:53:54 +00:00
										 |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 		insert(i, p_val); | 
					
						
							| 
									
										
										
										
											2015-03-19 23:53:54 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 	_FORCE_INLINE_ Vector() {} | 
					
						
							|  |  |  | 	_FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); } | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	inline Vector &operator=(const Vector &p_from) { | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 		_cowdata._ref(p_from._cowdata); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		return *this; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-29 16:57:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 08:47:45 +02:00
										 |  |  | 	Vector<uint8_t> to_byte_array() const { | 
					
						
							|  |  |  | 		Vector<uint8_t> ret; | 
					
						
							|  |  |  | 		ret.resize(size() * sizeof(T)); | 
					
						
							|  |  |  | 		memcpy(ret.ptrw(), ptr(), sizeof(T) * size()); | 
					
						
							|  |  |  | 		return ret; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vector<T> slice(int p_begin, int p_end = INT32_MAX) const { | 
					
						
							|  |  |  | 		Vector<T> result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const int s = size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		int begin = CLAMP(p_begin, -s, s); | 
					
						
							|  |  |  | 		if (begin < 0) { | 
					
						
							|  |  |  | 			begin += s; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		int end = CLAMP(p_end, -s, s); | 
					
						
							|  |  |  | 		if (end < 0) { | 
					
						
							|  |  |  | 			end += s; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ERR_FAIL_COND_V(begin > end, result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		int result_size = end - begin; | 
					
						
							|  |  |  | 		result.resize(result_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const T *const r = ptr(); | 
					
						
							|  |  |  | 		T *const w = result.ptrw(); | 
					
						
							|  |  |  | 		for (int i = 0; i < result_size; ++i) { | 
					
						
							|  |  |  | 			w[i] = r[begin + i]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return result; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 20:03:02 +00:00
										 |  |  | 	_FORCE_INLINE_ ~Vector() {} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | template <class T> | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void Vector<T>::invert() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < size() / 2; i++) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		T *p = ptrw(); | 
					
						
							|  |  |  | 		SWAP(p[i], p[size() - i - 1]); | 
					
						
							| 
									
										
										
										
											2017-01-06 10:15:44 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 01:11:39 +01:00
										 |  |  | template <class T> | 
					
						
							| 
									
										
										
										
											2019-12-26 17:38:08 +01:00
										 |  |  | void Vector<T>::append_array(Vector<T> p_other) { | 
					
						
							| 
									
										
										
										
											2017-11-21 01:11:39 +01:00
										 |  |  | 	const int ds = p_other.size(); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (ds == 0) { | 
					
						
							| 
									
										
										
										
											2017-11-21 01:11:39 +01:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-21 01:11:39 +01:00
										 |  |  | 	const int bs = size(); | 
					
						
							|  |  |  | 	resize(bs + ds); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	for (int i = 0; i < ds; ++i) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 		ptrw()[bs + i] = p_other[i]; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | template <class T> | 
					
						
							| 
									
										
										
										
											2019-12-26 17:38:08 +01:00
										 |  |  | bool Vector<T>::push_back(T p_elem) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	Error err = resize(size() + 1); | 
					
						
							| 
									
										
										
										
											2019-06-11 14:49:34 +02:00
										 |  |  | 	ERR_FAIL_COND_V(err, true); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	set(size() - 1, p_elem); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 18:45:34 +08:00
										 |  |  | template <class T> | 
					
						
							|  |  |  | void Vector<T>::fill(T p_elem) { | 
					
						
							|  |  |  | 	T *p = ptrw(); | 
					
						
							|  |  |  | 	for (int i = 0; i < size(); i++) { | 
					
						
							|  |  |  | 		p[i] = p_elem; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #endif
 |