| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  ordered_hash_map.h                                                   */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-01-01 11:16:22 +01:00
										 |  |  | /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | #ifndef ORDERED_HASH_MAP_H
 | 
					
						
							|  |  |  | #define ORDERED_HASH_MAP_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/hash_map.h"
 | 
					
						
							|  |  |  | #include "core/list.h"
 | 
					
						
							|  |  |  | #include "core/pair.h"
 | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * A hash map which allows to iterate elements in insertion order. | 
					
						
							|  |  |  |  * Insertion, lookup, deletion have O(1) complexity. | 
					
						
							|  |  |  |  * The API aims to be consistent with Map rather than HashMap, because the | 
					
						
							|  |  |  |  * former is more frequently used and is more coherent with the rest of the | 
					
						
							|  |  |  |  * codebase. | 
					
						
							|  |  |  |  * Deletion during iteration is safe and will preserve the order. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | template <class K, class V, class Hasher = HashMapHasherDefault, class Comparator = HashMapComparatorDefault<K>, uint8_t MIN_HASH_TABLE_POWER = 3, uint8_t RELATIONSHIP = 8> | 
					
						
							|  |  |  | class OrderedHashMap { | 
					
						
							| 
									
										
										
										
											2020-03-17 07:33:00 +01:00
										 |  |  | 	typedef List<Pair<const K *, V>> InternalList; | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 	typedef HashMap<K, typename InternalList::Element *, Hasher, Comparator, MIN_HASH_TABLE_POWER, RELATIONSHIP> InternalMap; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	InternalList list; | 
					
						
							|  |  |  | 	InternalMap map; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	class Element { | 
					
						
							|  |  |  | 		friend class OrderedHashMap<K, V, Hasher, Comparator, MIN_HASH_TABLE_POWER, RELATIONSHIP>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		typename InternalList::Element *list_element; | 
					
						
							|  |  |  | 		typename InternalList::Element *prev_element; | 
					
						
							| 
									
										
										
										
											2017-09-07 21:48:50 +02:00
										 |  |  | 		typename InternalList::Element *next_element; | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Element(typename InternalList::Element *p_element) { | 
					
						
							|  |  |  | 			list_element = p_element; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (list_element) { | 
					
						
							|  |  |  | 				next_element = list_element->next(); | 
					
						
							|  |  |  | 				prev_element = list_element->prev(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | 		_FORCE_INLINE_ Element() : | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 				list_element(nullptr), | 
					
						
							|  |  |  | 				prev_element(nullptr), | 
					
						
							|  |  |  | 				next_element(nullptr) { | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Element next() const { | 
					
						
							|  |  |  | 			return Element(next_element); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Element prev() const { | 
					
						
							|  |  |  | 			return Element(prev_element); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | 		Element(const Element &other) : | 
					
						
							|  |  |  | 				list_element(other.list_element), | 
					
						
							|  |  |  | 				prev_element(other.prev_element), | 
					
						
							|  |  |  | 				next_element(other.next_element) { | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Element &operator=(const Element &other) { | 
					
						
							|  |  |  | 			list_element = other.list_element; | 
					
						
							|  |  |  | 			next_element = other.next_element; | 
					
						
							|  |  |  | 			prev_element = other.prev_element; | 
					
						
							|  |  |  | 			return *this; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:08:21 +01:00
										 |  |  | 		_FORCE_INLINE_ bool operator==(const Element &p_other) const { | 
					
						
							|  |  |  | 			return this->list_element == p_other.list_element; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		_FORCE_INLINE_ bool operator!=(const Element &p_other) const { | 
					
						
							|  |  |  | 			return this->list_element != p_other.list_element; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		operator bool() const { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 			return (list_element != nullptr); | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const K &key() const { | 
					
						
							|  |  |  | 			CRASH_COND(!list_element); | 
					
						
							|  |  |  | 			return *(list_element->get().first); | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		V &value() { | 
					
						
							|  |  |  | 			CRASH_COND(!list_element); | 
					
						
							|  |  |  | 			return list_element->get().second; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const V &value() const { | 
					
						
							|  |  |  | 			CRASH_COND(!list_element); | 
					
						
							|  |  |  | 			return list_element->get().second; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		V &get() { | 
					
						
							|  |  |  | 			CRASH_COND(!list_element); | 
					
						
							|  |  |  | 			return list_element->get().second; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const V &get() const { | 
					
						
							|  |  |  | 			CRASH_COND(!list_element); | 
					
						
							|  |  |  | 			return list_element->get().second; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	class ConstElement { | 
					
						
							|  |  |  | 		friend class OrderedHashMap<K, V, Hasher, Comparator, MIN_HASH_TABLE_POWER, RELATIONSHIP>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const typename InternalList::Element *list_element; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | 		ConstElement(const typename InternalList::Element *p_element) : | 
					
						
							|  |  |  | 				list_element(p_element) { | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | 		_FORCE_INLINE_ ConstElement() : | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 				list_element(nullptr) { | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | 		ConstElement(const ConstElement &other) : | 
					
						
							|  |  |  | 				list_element(other.list_element) { | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ConstElement &operator=(const ConstElement &other) { | 
					
						
							|  |  |  | 			list_element = other.list_element; | 
					
						
							|  |  |  | 			return *this; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ConstElement next() const { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 			return ConstElement(list_element ? list_element->next() : nullptr); | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ConstElement prev() const { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 			return ConstElement(list_element ? list_element->prev() : nullptr); | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:08:21 +01:00
										 |  |  | 		_FORCE_INLINE_ bool operator==(const ConstElement &p_other) const { | 
					
						
							|  |  |  | 			return this->list_element == p_other.list_element; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		_FORCE_INLINE_ bool operator!=(const ConstElement &p_other) const { | 
					
						
							|  |  |  | 			return this->list_element != p_other.list_element; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		operator bool() const { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 			return (list_element != nullptr); | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const K &key() const { | 
					
						
							|  |  |  | 			CRASH_COND(!list_element); | 
					
						
							|  |  |  | 			return *(list_element->get().first); | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const V &value() const { | 
					
						
							|  |  |  | 			CRASH_COND(!list_element); | 
					
						
							|  |  |  | 			return list_element->get().second; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const V &get() const { | 
					
						
							|  |  |  | 			CRASH_COND(!list_element); | 
					
						
							|  |  |  | 			return list_element->get().second; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ConstElement find(const K &p_key) const { | 
					
						
							| 
									
										
										
										
											2017-11-03 14:33:19 +01:00
										 |  |  | 		typename InternalList::Element *const *list_element = map.getptr(p_key); | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 		if (list_element) { | 
					
						
							|  |  |  | 			return ConstElement(*list_element); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		return ConstElement(nullptr); | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Element find(const K &p_key) { | 
					
						
							|  |  |  | 		typename InternalList::Element **list_element = map.getptr(p_key); | 
					
						
							|  |  |  | 		if (list_element) { | 
					
						
							|  |  |  | 			return Element(*list_element); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		return Element(nullptr); | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Element insert(const K &p_key, const V &p_value) { | 
					
						
							|  |  |  | 		typename InternalList::Element **list_element = map.getptr(p_key); | 
					
						
							|  |  |  | 		if (list_element) { | 
					
						
							|  |  |  | 			(*list_element)->get().second = p_value; | 
					
						
							|  |  |  | 			return Element(*list_element); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		typename InternalList::Element *new_element = list.push_back(Pair<const K *, V>(nullptr, p_value)); | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 		typename InternalMap::Element *e = map.set(p_key, new_element); | 
					
						
							|  |  |  | 		new_element->get().first = &e->key(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return Element(new_element); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void erase(Element &p_element) { | 
					
						
							|  |  |  | 		map.erase(p_element.key()); | 
					
						
							|  |  |  | 		list.erase(p_element.list_element); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		p_element.list_element = nullptr; | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool erase(const K &p_key) { | 
					
						
							|  |  |  | 		typename InternalList::Element **list_element = map.getptr(p_key); | 
					
						
							|  |  |  | 		if (list_element) { | 
					
						
							|  |  |  | 			list.erase(*list_element); | 
					
						
							|  |  |  | 			map.erase(p_key); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	inline bool has(const K &p_key) const { | 
					
						
							|  |  |  | 		return map.has(p_key); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const V &operator[](const K &p_key) const { | 
					
						
							|  |  |  | 		ConstElement e = find(p_key); | 
					
						
							|  |  |  | 		CRASH_COND(!e); | 
					
						
							|  |  |  | 		return e.value(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	V &operator[](const K &p_key) { | 
					
						
							|  |  |  | 		Element e = find(p_key); | 
					
						
							|  |  |  | 		if (!e) { | 
					
						
							|  |  |  | 			// consistent with Map behaviour
 | 
					
						
							|  |  |  | 			e = insert(p_key, V()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return e.value(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	inline Element front() { | 
					
						
							|  |  |  | 		return Element(list.front()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	inline Element back() { | 
					
						
							|  |  |  | 		return Element(list.back()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	inline ConstElement front() const { | 
					
						
							|  |  |  | 		return ConstElement(list.front()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	inline ConstElement back() const { | 
					
						
							|  |  |  | 		return ConstElement(list.back()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	inline bool empty() const { return list.empty(); } | 
					
						
							|  |  |  | 	inline int size() const { return list.size(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-20 01:57:29 +02:00
										 |  |  | 	const void *id() const { | 
					
						
							|  |  |  | 		return list.id(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-13 15:32:39 +07:00
										 |  |  | 	void clear() { | 
					
						
							|  |  |  | 		map.clear(); | 
					
						
							|  |  |  | 		list.clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 	void _copy_from(const OrderedHashMap &p_map) { | 
					
						
							|  |  |  | 		for (ConstElement E = p_map.front(); E; E = E.next()) { | 
					
						
							|  |  |  | 			insert(E.key(), E.value()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	void operator=(const OrderedHashMap &p_map) { | 
					
						
							|  |  |  | 		_copy_from(p_map); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	OrderedHashMap(const OrderedHashMap &p_map) { | 
					
						
							|  |  |  | 		_copy_from(p_map); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ OrderedHashMap() { | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-07 21:48:50 +02:00
										 |  |  | #endif // ORDERED_HASH_MAP_H
 |