| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  oa_hash_map.h                                                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | #ifndef OA_HASH_MAP_H
 | 
					
						
							|  |  |  | #define OA_HASH_MAP_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/math/math_funcs.h"
 | 
					
						
							|  |  |  | #include "core/os/memory.h"
 | 
					
						
							| 
									
										
										
										
											2020-11-07 19:33:38 -03:00
										 |  |  | #include "core/templates/hashfuncs.h"
 | 
					
						
							| 
									
										
										
										
											2024-08-21 13:32:25 -05:00
										 |  |  | #include "core/templates/pair.h"
 | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2019-09-21 14:32:40 +08:00
										 |  |  |  * A HashMap implementation that uses open addressing with Robin Hood hashing. | 
					
						
							|  |  |  |  * Robin Hood hashing swaps out entries that have a smaller probing distance | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  |  * than the to-be-inserted entry, that evens out the average probing distance | 
					
						
							| 
									
										
										
										
											2019-09-21 14:32:40 +08:00
										 |  |  |  * and enables faster lookups. Backward shift deletion is employed to further | 
					
						
							|  |  |  |  * improve the performance and to avoid infinite loops in rare cases. | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  |  * The entries are stored inplace, so huge keys or values might fill cache lines | 
					
						
							|  |  |  |  * a lot faster. | 
					
						
							| 
									
										
										
										
											2020-05-15 00:40:28 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Only used keys and values are constructed. For free positions there's space | 
					
						
							|  |  |  |  * in the arrays for each, but that memory is kept uninitialized. | 
					
						
							| 
									
										
										
										
											2020-07-13 14:13:38 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-05-20 14:05:01 +02:00
										 |  |  |  * The assignment operator copy the pairs from one map to the other. | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | template <typename TKey, typename TValue, | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 		typename Hasher = HashMapHasherDefault, | 
					
						
							| 
									
										
										
										
											2020-03-17 07:33:00 +01:00
										 |  |  | 		typename Comparator = HashMapComparatorDefault<TKey>> | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | class OAHashMap { | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-05-20 14:05:01 +02:00
										 |  |  | 	TValue *values = nullptr; | 
					
						
							|  |  |  | 	TKey *keys = nullptr; | 
					
						
							|  |  |  | 	uint32_t *hashes = nullptr; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-20 14:05:01 +02:00
										 |  |  | 	uint32_t capacity = 0; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 17:01:17 +02:00
										 |  |  | 	uint32_t num_elements = 0; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 	static const uint32_t EMPTY_HASH = 0; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:22:52 +02:00
										 |  |  | 	_FORCE_INLINE_ uint32_t _hash(const TKey &p_key) const { | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		uint32_t hash = Hasher::hash(p_key); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		if (hash == EMPTY_HASH) { | 
					
						
							|  |  |  | 			hash = EMPTY_HASH + 1; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		return hash; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:22:52 +02:00
										 |  |  | 	_FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_pos, uint32_t p_hash) const { | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		uint32_t original_pos = p_hash % capacity; | 
					
						
							| 
									
										
										
										
											2019-09-21 14:32:40 +08:00
										 |  |  | 		return (p_pos - original_pos + capacity) % capacity; | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 	_FORCE_INLINE_ void _construct(uint32_t p_pos, uint32_t p_hash, const TKey &p_key, const TValue &p_value) { | 
					
						
							|  |  |  | 		memnew_placement(&keys[p_pos], TKey(p_key)); | 
					
						
							|  |  |  | 		memnew_placement(&values[p_pos], TValue(p_value)); | 
					
						
							|  |  |  | 		hashes[p_pos] = p_hash; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		num_elements++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:22:52 +02:00
										 |  |  | 	bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) const { | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		uint32_t hash = _hash(p_key); | 
					
						
							|  |  |  | 		uint32_t pos = hash % capacity; | 
					
						
							|  |  |  | 		uint32_t distance = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 11:00:19 +02:00
										 |  |  | 		while (true) { | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 			if (hashes[pos] == EMPTY_HASH) { | 
					
						
							|  |  |  | 				return false; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 			if (distance > _get_probe_length(pos, hashes[pos])) { | 
					
						
							|  |  |  | 				return false; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 			if (hashes[pos] == hash && Comparator::compare(keys[pos], p_key)) { | 
					
						
							|  |  |  | 				r_pos = pos; | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 			pos = (pos + 1) % capacity; | 
					
						
							|  |  |  | 			distance++; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 	void _insert_with_hash(uint32_t p_hash, const TKey &p_key, const TValue &p_value) { | 
					
						
							|  |  |  | 		uint32_t hash = p_hash; | 
					
						
							|  |  |  | 		uint32_t distance = 0; | 
					
						
							|  |  |  | 		uint32_t pos = hash % capacity; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		TKey key = p_key; | 
					
						
							|  |  |  | 		TValue value = p_value; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 11:00:19 +02:00
										 |  |  | 		while (true) { | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 			if (hashes[pos] == EMPTY_HASH) { | 
					
						
							| 
									
										
										
										
											2018-10-11 12:53:32 +02:00
										 |  |  | 				_construct(pos, hash, key, value); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 			// not an empty slot, let's check the probing length of the existing one
 | 
					
						
							|  |  |  | 			uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos]); | 
					
						
							|  |  |  | 			if (existing_probe_len < distance) { | 
					
						
							|  |  |  | 				SWAP(hash, hashes[pos]); | 
					
						
							|  |  |  | 				SWAP(key, keys[pos]); | 
					
						
							|  |  |  | 				SWAP(value, values[pos]); | 
					
						
							|  |  |  | 				distance = existing_probe_len; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 			pos = (pos + 1) % capacity; | 
					
						
							|  |  |  | 			distance++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-16 00:22:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 21:30:52 +02:00
										 |  |  | 	void _resize_and_rehash(uint32_t p_new_capacity) { | 
					
						
							|  |  |  | 		uint32_t old_capacity = capacity; | 
					
						
							| 
									
										
										
										
											2020-05-20 14:05:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Capacity can't be 0.
 | 
					
						
							| 
									
										
										
										
											2022-03-08 15:10:48 +01:00
										 |  |  | 		capacity = MAX(1u, p_new_capacity); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		TKey *old_keys = keys; | 
					
						
							|  |  |  | 		TValue *old_values = values; | 
					
						
							|  |  |  | 		uint32_t *old_hashes = hashes; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		num_elements = 0; | 
					
						
							| 
									
										
										
										
											2020-05-15 00:40:28 +02:00
										 |  |  | 		keys = static_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity)); | 
					
						
							|  |  |  | 		values = static_cast<TValue *>(Memory::alloc_static(sizeof(TValue) * capacity)); | 
					
						
							|  |  |  | 		hashes = static_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity)); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-26 17:38:02 +02:00
										 |  |  | 		for (uint32_t i = 0; i < capacity; i++) { | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 			hashes[i] = 0; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-20 14:05:01 +02:00
										 |  |  | 		if (old_capacity == 0) { | 
					
						
							|  |  |  | 			// Nothing to do.
 | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		for (uint32_t i = 0; i < old_capacity; i++) { | 
					
						
							|  |  |  | 			if (old_hashes[i] == EMPTY_HASH) { | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 			_insert_with_hash(old_hashes[i], old_keys[i], old_values[i]); | 
					
						
							| 
									
										
										
										
											2020-05-15 00:40:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			old_keys[i].~TKey(); | 
					
						
							|  |  |  | 			old_values[i].~TValue(); | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 00:40:28 +02:00
										 |  |  | 		Memory::free_static(old_keys); | 
					
						
							|  |  |  | 		Memory::free_static(old_values); | 
					
						
							|  |  |  | 		Memory::free_static(old_hashes); | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 21:30:52 +02:00
										 |  |  | 	void _resize_and_rehash() { | 
					
						
							|  |  |  | 		_resize_and_rehash(capacity * 2); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | public: | 
					
						
							|  |  |  | 	_FORCE_INLINE_ uint32_t get_capacity() const { return capacity; } | 
					
						
							|  |  |  | 	_FORCE_INLINE_ uint32_t get_num_elements() const { return num_elements; } | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 12:04:21 +00:00
										 |  |  | 	bool is_empty() const { | 
					
						
							| 
									
										
										
										
											2019-08-16 00:22:52 +02:00
										 |  |  | 		return num_elements == 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void clear() { | 
					
						
							|  |  |  | 		for (uint32_t i = 0; i < capacity; i++) { | 
					
						
							| 
									
										
										
										
											2019-08-25 21:30:52 +02:00
										 |  |  | 			if (hashes[i] == EMPTY_HASH) { | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			hashes[i] = EMPTY_HASH; | 
					
						
							| 
									
										
										
										
											2019-08-16 00:22:52 +02:00
										 |  |  | 			values[i].~TValue(); | 
					
						
							|  |  |  | 			keys[i].~TKey(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		num_elements = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 	void insert(const TKey &p_key, const TValue &p_value) { | 
					
						
							| 
									
										
										
										
											2019-09-21 14:32:40 +08:00
										 |  |  | 		if (num_elements + 1 > 0.9 * capacity) { | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 			_resize_and_rehash(); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		uint32_t hash = _hash(p_key); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_insert_with_hash(hash, p_key, p_value); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void set(const TKey &p_key, const TValue &p_data) { | 
					
						
							|  |  |  | 		uint32_t pos = 0; | 
					
						
							|  |  |  | 		bool exists = _lookup_pos(p_key, pos); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (exists) { | 
					
						
							| 
									
										
										
										
											2020-05-15 00:40:28 +02:00
										 |  |  | 			values[pos] = p_data; | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			insert(p_key, p_data); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * returns true if the value was found, false otherwise. | 
					
						
							|  |  |  | 	 * | 
					
						
							| 
									
										
										
										
											2021-06-07 10:17:32 +02:00
										 |  |  | 	 * if r_data is not nullptr then the value will be written to the object | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 	 * it points to. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:22:52 +02:00
										 |  |  | 	bool lookup(const TKey &p_key, TValue &r_data) const { | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		uint32_t pos = 0; | 
					
						
							|  |  |  | 		bool exists = _lookup_pos(p_key, pos); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		if (exists) { | 
					
						
							| 
									
										
										
										
											2020-05-15 00:40:28 +02:00
										 |  |  | 			r_data = values[pos]; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 09:20:15 +02:00
										 |  |  | 	const TValue *lookup_ptr(const TKey &p_key) const { | 
					
						
							|  |  |  | 		uint32_t pos = 0; | 
					
						
							|  |  |  | 		bool exists = _lookup_pos(p_key, pos); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (exists) { | 
					
						
							|  |  |  | 			return &values[pos]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return nullptr; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	TValue *lookup_ptr(const TKey &p_key) { | 
					
						
							| 
									
										
										
										
											2019-06-07 13:07:57 -03:00
										 |  |  | 		uint32_t pos = 0; | 
					
						
							|  |  |  | 		bool exists = _lookup_pos(p_key, pos); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (exists) { | 
					
						
							|  |  |  | 			return &values[pos]; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		return nullptr; | 
					
						
							| 
									
										
										
										
											2019-06-07 13:07:57 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:22:52 +02:00
										 |  |  | 	_FORCE_INLINE_ bool has(const TKey &p_key) const { | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		uint32_t _pos = 0; | 
					
						
							|  |  |  | 		return _lookup_pos(p_key, _pos); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void remove(const TKey &p_key) { | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		uint32_t pos = 0; | 
					
						
							|  |  |  | 		bool exists = _lookup_pos(p_key, pos); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		if (!exists) { | 
					
						
							|  |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-21 14:32:40 +08:00
										 |  |  | 		uint32_t next_pos = (pos + 1) % capacity; | 
					
						
							|  |  |  | 		while (hashes[next_pos] != EMPTY_HASH && | 
					
						
							|  |  |  | 				_get_probe_length(next_pos, hashes[next_pos]) != 0) { | 
					
						
							|  |  |  | 			SWAP(hashes[next_pos], hashes[pos]); | 
					
						
							|  |  |  | 			SWAP(keys[next_pos], keys[pos]); | 
					
						
							|  |  |  | 			SWAP(values[next_pos], values[pos]); | 
					
						
							|  |  |  | 			pos = next_pos; | 
					
						
							|  |  |  | 			next_pos = (pos + 1) % capacity; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		hashes[pos] = EMPTY_HASH; | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		values[pos].~TValue(); | 
					
						
							|  |  |  | 		keys[pos].~TKey(); | 
					
						
							| 
									
										
										
										
											2019-09-21 14:32:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		num_elements--; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 21:30:52 +02:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * reserves space for a number of elements, useful to avoid many resizes and rehashes | 
					
						
							|  |  |  | 	 *  if adding a known (possibly large) number of elements at once, must be larger than old | 
					
						
							|  |  |  | 	 *  capacity. | 
					
						
							|  |  |  | 	 **/ | 
					
						
							|  |  |  | 	void reserve(uint32_t p_new_capacity) { | 
					
						
							|  |  |  | 		ERR_FAIL_COND(p_new_capacity < capacity); | 
					
						
							|  |  |  | 		_resize_and_rehash(p_new_capacity); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 	struct Iterator { | 
					
						
							|  |  |  | 		bool valid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const TKey *key; | 
					
						
							| 
									
										
										
										
											2022-04-04 15:06:57 +02:00
										 |  |  | 		TValue *value = nullptr; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	private: | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		uint32_t pos; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 		friend class OAHashMap; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Iterator iter() const { | 
					
						
							|  |  |  | 		Iterator it; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		it.valid = true; | 
					
						
							|  |  |  | 		it.pos = 0; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		return next_iter(it); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Iterator next_iter(const Iterator &p_iter) const { | 
					
						
							|  |  |  | 		if (!p_iter.valid) { | 
					
						
							|  |  |  | 			return p_iter; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Iterator it; | 
					
						
							|  |  |  | 		it.valid = false; | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		it.pos = p_iter.pos; | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		it.key = nullptr; | 
					
						
							|  |  |  | 		it.value = nullptr; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 		for (uint32_t i = it.pos; i < capacity; i++) { | 
					
						
							|  |  |  | 			it.pos = i + 1; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 			if (hashes[i] == EMPTY_HASH) { | 
					
						
							|  |  |  | 				continue; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			it.valid = true; | 
					
						
							|  |  |  | 			it.key = &keys[i]; | 
					
						
							|  |  |  | 			it.value = &values[i]; | 
					
						
							|  |  |  | 			return it; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return it; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-21 13:32:25 -05:00
										 |  |  | 	OAHashMap(std::initializer_list<KeyValue<TKey, TValue>> p_init) { | 
					
						
							|  |  |  | 		reserve(p_init.size()); | 
					
						
							|  |  |  | 		for (const KeyValue<TKey, TValue> &E : p_init) { | 
					
						
							|  |  |  | 			set(E.key, E.value); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-20 14:05:01 +02:00
										 |  |  | 	OAHashMap(const OAHashMap &p_other) { | 
					
						
							|  |  |  | 		(*this) = p_other; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 15:19:26 +01:00
										 |  |  | 	void operator=(const OAHashMap &p_other) { | 
					
						
							| 
									
										
										
										
											2020-05-20 14:05:01 +02:00
										 |  |  | 		if (capacity != 0) { | 
					
						
							|  |  |  | 			clear(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_resize_and_rehash(p_other.capacity); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (Iterator it = p_other.iter(); it.valid; it = p_other.next_iter(it)) { | 
					
						
							|  |  |  | 			set(*it.key, *it.value); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-16 00:22:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 14:40:55 +02:00
										 |  |  | 	OAHashMap(uint32_t p_initial_capacity = 64) { | 
					
						
							| 
									
										
										
										
											2020-05-20 14:05:01 +02:00
										 |  |  | 		// Capacity can't be 0.
 | 
					
						
							| 
									
										
										
										
											2022-03-08 15:10:48 +01:00
										 |  |  | 		capacity = MAX(1u, p_initial_capacity); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 00:40:28 +02:00
										 |  |  | 		keys = static_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity)); | 
					
						
							|  |  |  | 		values = static_cast<TValue *>(Memory::alloc_static(sizeof(TValue) * capacity)); | 
					
						
							|  |  |  | 		hashes = static_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity)); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-20 14:05:01 +02:00
										 |  |  | 		for (uint32_t i = 0; i < capacity; i++) { | 
					
						
							| 
									
										
										
										
											2019-08-25 21:30:52 +02:00
										 |  |  | 			hashes[i] = EMPTY_HASH; | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	~OAHashMap() { | 
					
						
							| 
									
										
										
										
											2020-05-15 00:40:28 +02:00
										 |  |  | 		for (uint32_t i = 0; i < capacity; i++) { | 
					
						
							|  |  |  | 			if (hashes[i] == EMPTY_HASH) { | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			values[i].~TValue(); | 
					
						
							|  |  |  | 			keys[i].~TKey(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Memory::free_static(keys); | 
					
						
							|  |  |  | 		Memory::free_static(values); | 
					
						
							|  |  |  | 		Memory::free_static(hashes); | 
					
						
							| 
									
										
										
										
											2017-09-19 02:40:13 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 11:10:34 +01:00
										 |  |  | #endif // OA_HASH_MAP_H
 |