2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* hash_map.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00: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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifndef HASH_MAP_H
|
|
|
|
#define HASH_MAP_H
|
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/error/error_macros.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/math/math_funcs.h"
|
|
|
|
#include "core/os/memory.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/string/ustring.h"
|
|
|
|
#include "core/templates/hashfuncs.h"
|
|
|
|
#include "core/templates/list.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class HashMap
|
2016-03-08 23:00:52 +00:00
|
|
|
*
|
2014-02-10 01:10:30 +00:00
|
|
|
* Implementation of a standard Hashing HashMap, for quick lookups of Data associated with a Key.
|
|
|
|
* The implementation provides hashers for the default types, if you need a special kind of hasher, provide
|
|
|
|
* your own.
|
|
|
|
* @param TKey Key, search is based on it, needs to be hasheable. It is unique in this container.
|
|
|
|
* @param TData Data, data associated with the key
|
|
|
|
* @param Hasher Hasher object, needs to provide a valid static hash function for TKey
|
2022-01-04 19:26:22 +00:00
|
|
|
* @param Comparator comparator object, needs to be able to safely compare two TKey values.
|
|
|
|
* It needs to ensure that x == x for any items inserted in the map. Bear in mind that nan != nan when implementing an equality check.
|
2014-02-10 01:10:30 +00:00
|
|
|
* @param MIN_HASH_TABLE_POWER Miminum size of the hash table, as a power of two. You rarely need to change this parameter.
|
|
|
|
* @param RELATIONSHIP Relationship at which the hash table is resized. if amount of elements is RELATIONSHIP
|
|
|
|
* times bigger than the hash table, table is resized to solve this condition. if RELATIONSHIP is zero, table is always MIN_HASH_TABLE_POWER.
|
2016-03-08 23:00:52 +00:00
|
|
|
*
|
2021-10-28 13:43:36 +00:00
|
|
|
*/
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class TKey, class TData, class Hasher = HashMapHasherDefault, class Comparator = HashMapComparatorDefault<TKey>, uint8_t MIN_HASH_TABLE_POWER = 3, uint8_t RELATIONSHIP = 8>
|
2014-02-10 01:10:30 +00:00
|
|
|
class HashMap {
|
2016-03-08 23:00:52 +00:00
|
|
|
public:
|
2014-02-10 01:10:30 +00:00
|
|
|
struct Pair {
|
|
|
|
TKey key;
|
|
|
|
TData data;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-02-17 12:12:38 +00:00
|
|
|
Pair(const TKey &p_key) :
|
|
|
|
key(p_key),
|
|
|
|
data() {}
|
2017-12-06 20:36:34 +00:00
|
|
|
Pair(const TKey &p_key, const TData &p_data) :
|
|
|
|
key(p_key),
|
|
|
|
data(p_data) {
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
struct Element {
|
|
|
|
private:
|
|
|
|
friend class HashMap;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-11-23 16:38:46 +00:00
|
|
|
uint32_t hash = 0;
|
2020-05-12 15:01:17 +00:00
|
|
|
Element *next = nullptr;
|
|
|
|
Element() {}
|
2014-02-10 01:10:30 +00:00
|
|
|
Pair pair;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
public:
|
|
|
|
const TKey &key() const {
|
|
|
|
return pair.key;
|
|
|
|
}
|
|
|
|
|
|
|
|
TData &value() {
|
|
|
|
return pair.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TData &value() const {
|
|
|
|
return pair.value();
|
|
|
|
}
|
2021-02-17 12:12:38 +00:00
|
|
|
|
|
|
|
Element(const TKey &p_key) :
|
|
|
|
pair(p_key) {}
|
|
|
|
Element(const Element &p_other) :
|
2021-10-13 15:25:31 +00:00
|
|
|
hash(p_other.hash),
|
2021-02-17 12:12:38 +00:00
|
|
|
pair(p_other.pair.key, p_other.pair.data) {}
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
private:
|
2020-05-12 15:01:17 +00:00
|
|
|
Element **hash_table = nullptr;
|
|
|
|
uint8_t hash_table_power = 0;
|
|
|
|
uint32_t elements = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void make_hash_table() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(hash_table);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
hash_table = memnew_arr(Element *, (1 << MIN_HASH_TABLE_POWER));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
hash_table_power = MIN_HASH_TABLE_POWER;
|
2017-03-05 15:44:50 +00:00
|
|
|
elements = 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
for (int i = 0; i < (1 << MIN_HASH_TABLE_POWER); i++) {
|
2020-05-14 08:15:48 +00:00
|
|
|
hash_table[i] = nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void erase_hash_table() {
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_MSG(elements, "Cannot erase hash table if there are still elements inside.");
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
memdelete_arr(hash_table);
|
2020-05-14 08:15:48 +00:00
|
|
|
hash_table = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
hash_table_power = 0;
|
|
|
|
elements = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void check_hash_table() {
|
2017-03-05 15:44:50 +00:00
|
|
|
int new_hash_table_power = -1;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if ((int)elements > ((1 << hash_table_power) * RELATIONSHIP)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
/* rehash up */
|
2017-03-05 15:44:50 +00:00
|
|
|
new_hash_table_power = hash_table_power + 1;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while ((int)elements > ((1 << new_hash_table_power) * RELATIONSHIP)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
new_hash_table_power++;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if ((hash_table_power > (int)MIN_HASH_TABLE_POWER) && ((int)elements < ((1 << (hash_table_power - 1)) * RELATIONSHIP))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
/* rehash down */
|
2017-03-05 15:44:50 +00:00
|
|
|
new_hash_table_power = hash_table_power - 1;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while ((int)elements < ((1 << (new_hash_table_power - 1)) * RELATIONSHIP)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
new_hash_table_power--;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (new_hash_table_power < (int)MIN_HASH_TABLE_POWER) {
|
2017-03-05 15:44:50 +00:00
|
|
|
new_hash_table_power = MIN_HASH_TABLE_POWER;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (new_hash_table_power == -1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-10-04 13:38:52 +00:00
|
|
|
Element **new_hash_table = memnew_arr(Element *, ((uint64_t)1 << new_hash_table_power));
|
2019-08-15 02:57:49 +00:00
|
|
|
ERR_FAIL_COND_MSG(!new_hash_table, "Out of memory.");
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < (1 << new_hash_table_power); i++) {
|
2020-05-14 08:15:48 +00:00
|
|
|
new_hash_table[i] = nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-06-03 19:52:50 +00:00
|
|
|
if (hash_table) {
|
|
|
|
for (int i = 0; i < (1 << hash_table_power); i++) {
|
|
|
|
while (hash_table[i]) {
|
|
|
|
Element *se = hash_table[i];
|
|
|
|
hash_table[i] = se->next;
|
|
|
|
int new_pos = se->hash & ((1 << new_hash_table_power) - 1);
|
|
|
|
se->next = new_hash_table[new_pos];
|
|
|
|
new_hash_table[new_pos] = se;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
memdelete_arr(hash_table);
|
2019-06-03 19:52:50 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
hash_table = new_hash_table;
|
|
|
|
hash_table_power = new_hash_table_power;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* I want to have only one function.. */
|
2017-08-13 08:32:39 +00:00
|
|
|
_FORCE_INLINE_ const Element *get_element(const TKey &p_key) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t hash = Hasher::hash(p_key);
|
|
|
|
uint32_t index = hash & ((1 << hash_table_power) - 1);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
Element *e = hash_table[index];
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
while (e) {
|
|
|
|
/* checking hash first avoids comparing key, which may take longer */
|
2017-03-05 15:44:50 +00:00
|
|
|
if (e->hash == hash && Comparator::compare(e->pair.key, p_key)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
/* the pair exists in this hashtable, so just update data */
|
|
|
|
return e;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
e = e->next;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
Element *create_element(const TKey &p_key) {
|
|
|
|
/* if element doesn't exist, create it */
|
2021-02-17 12:12:38 +00:00
|
|
|
Element *e = memnew(Element(p_key));
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!e, nullptr, "Out of memory.");
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t hash = Hasher::hash(p_key);
|
|
|
|
uint32_t index = hash & ((1 << hash_table_power) - 1);
|
2016-03-08 23:00:52 +00:00
|
|
|
e->next = hash_table[index];
|
2014-02-10 01:10:30 +00:00
|
|
|
e->hash = hash;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
hash_table[index] = e;
|
2014-02-10 01:10:30 +00:00
|
|
|
elements++;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return e;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void copy_from(const HashMap &p_t) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (&p_t == this) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return; /* much less bother with that */
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
clear();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!p_t.hash_table || p_t.hash_table_power == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return; /* not copying from empty table */
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-10-04 13:38:52 +00:00
|
|
|
hash_table = memnew_arr(Element *, (uint64_t)1 << p_t.hash_table_power);
|
2017-03-05 15:44:50 +00:00
|
|
|
hash_table_power = p_t.hash_table_power;
|
|
|
|
elements = p_t.elements;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < (1 << p_t.hash_table_power); i++) {
|
2020-04-01 23:20:12 +00:00
|
|
|
hash_table[i] = nullptr;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
const Element *e = p_t.hash_table[i];
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (e) {
|
2021-02-17 12:12:38 +00:00
|
|
|
Element *le = memnew(Element(*e)); /* local element */
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/* add to list and reassign pointers */
|
2017-03-05 15:44:50 +00:00
|
|
|
le->next = hash_table[i];
|
|
|
|
hash_table[i] = le;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
e = e->next;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
public:
|
2017-08-13 08:32:39 +00:00
|
|
|
Element *set(const TKey &p_key, const TData &p_data) {
|
|
|
|
return set(Pair(p_key, p_data));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
Element *set(const Pair &p_pair) {
|
2020-04-01 23:20:12 +00:00
|
|
|
Element *e = nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!hash_table) {
|
2014-02-10 01:10:30 +00:00
|
|
|
make_hash_table(); // if no table, make one
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-08-13 08:32:39 +00:00
|
|
|
e = const_cast<Element *>(get_element(p_pair.key));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/* if we made it up to here, the pair doesn't exist, create and assign */
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!e) {
|
2017-08-13 08:32:39 +00:00
|
|
|
e = create_element(p_pair.key);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!e) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-14 04:22:15 +00:00
|
|
|
check_hash_table(); // perform mantenience routine
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
e->pair.data = p_pair.data;
|
2017-08-13 08:32:39 +00:00
|
|
|
return e;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool has(const TKey &p_key) const {
|
2020-04-01 23:20:12 +00:00
|
|
|
return getptr(p_key) != nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/**
|
2016-03-08 23:00:52 +00:00
|
|
|
* Get a key from data, return a const reference.
|
2020-04-01 23:20:12 +00:00
|
|
|
* WARNING: this doesn't check errors, use either getptr and check nullptr, or check
|
2014-02-10 01:10:30 +00:00
|
|
|
* first with has(key)
|
|
|
|
*/
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const TData &get(const TKey &p_key) const {
|
|
|
|
const TData *res = getptr(p_key);
|
2019-10-25 12:20:01 +00:00
|
|
|
CRASH_COND_MSG(!res, "Map key not found.");
|
2014-02-10 01:10:30 +00:00
|
|
|
return *res;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
TData &get(const TKey &p_key) {
|
|
|
|
TData *res = getptr(p_key);
|
2019-10-25 12:20:01 +00:00
|
|
|
CRASH_COND_MSG(!res, "Map key not found.");
|
2014-02-10 01:10:30 +00:00
|
|
|
return *res;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/**
|
2021-06-07 08:17:32 +00:00
|
|
|
* Same as get, except it can return nullptr when item was not found.
|
2014-02-10 01:10:30 +00:00
|
|
|
* This is mainly used for speed purposes.
|
|
|
|
*/
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ TData *getptr(const TKey &p_key) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (unlikely(!hash_table)) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
Element *e = const_cast<Element *>(get_element(p_key));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (e) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return &e->pair.data;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ const TData *getptr(const TKey &p_key) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (unlikely(!hash_table)) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
const Element *e = const_cast<Element *>(get_element(p_key));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (e) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return &e->pair.data;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-07 08:17:32 +00:00
|
|
|
* Same as get, except it can return nullptr when item was not found.
|
2014-02-10 01:10:30 +00:00
|
|
|
* This version is custom, will take a hash and a custom key (that should support operator==()
|
|
|
|
*/
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class C>
|
|
|
|
_FORCE_INLINE_ TData *custom_getptr(C p_custom_key, uint32_t p_custom_hash) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (unlikely(!hash_table)) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
uint32_t hash = p_custom_hash;
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t index = hash & ((1 << hash_table_power) - 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
Element *e = hash_table[index];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
while (e) {
|
|
|
|
/* checking hash first avoids comparing key, which may take longer */
|
2017-03-05 15:44:50 +00:00
|
|
|
if (e->hash == hash && Comparator::compare(e->pair.key, p_custom_key)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
/* the pair exists in this hashtable, so just update data */
|
|
|
|
return &e->pair.data;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
e = e->next;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
template <class C>
|
|
|
|
_FORCE_INLINE_ const TData *custom_getptr(C p_custom_key, uint32_t p_custom_hash) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (unlikely(!hash_table)) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
uint32_t hash = p_custom_hash;
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t index = hash & ((1 << hash_table_power) - 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
const Element *e = hash_table[index];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
while (e) {
|
|
|
|
/* checking hash first avoids comparing key, which may take longer */
|
2017-03-05 15:44:50 +00:00
|
|
|
if (e->hash == hash && Comparator::compare(e->pair.key, p_custom_key)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
/* the pair exists in this hashtable, so just update data */
|
|
|
|
return &e->pair.data;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
e = e->next;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-24 20:45:31 +00:00
|
|
|
* Erase an item, return true if erasing was successful
|
2014-02-10 01:10:30 +00:00
|
|
|
*/
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool erase(const TKey &p_key) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (unlikely(!hash_table)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t hash = Hasher::hash(p_key);
|
|
|
|
uint32_t index = hash & ((1 << hash_table_power) - 1);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
Element *e = hash_table[index];
|
2020-04-01 23:20:12 +00:00
|
|
|
Element *p = nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
while (e) {
|
|
|
|
/* checking hash first avoids comparing key, which may take longer */
|
2017-03-05 15:44:50 +00:00
|
|
|
if (e->hash == hash && Comparator::compare(e->pair.key, p_key)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (p) {
|
2017-03-05 15:44:50 +00:00
|
|
|
p->next = e->next;
|
2016-03-08 23:00:52 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
//begin of list
|
2017-03-05 15:44:50 +00:00
|
|
|
hash_table[index] = e->next;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
memdelete(e);
|
|
|
|
elements--;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (elements == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
erase_hash_table();
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2016-03-08 23:00:52 +00:00
|
|
|
check_hash_table();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p = e;
|
|
|
|
e = e->next;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
inline const TData &operator[](const TKey &p_key) const { //constref
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return get(p_key);
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
inline TData &operator[](const TKey &p_key) { //assignment
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
Element *e = nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!hash_table) {
|
2014-02-10 01:10:30 +00:00
|
|
|
make_hash_table(); // if no table, make one
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-08-13 08:32:39 +00:00
|
|
|
e = const_cast<Element *>(get_element(p_key));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/* if we made it up to here, the pair doesn't exist, create */
|
|
|
|
if (!e) {
|
2017-08-13 08:32:39 +00:00
|
|
|
e = create_element(p_key);
|
2017-05-26 19:11:16 +00:00
|
|
|
CRASH_COND(!e);
|
2014-05-14 04:22:15 +00:00
|
|
|
check_hash_table(); // perform mantenience routine
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return e->pair.data;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next key to p_key, and the first key if p_key is null.
|
2021-06-07 08:17:32 +00:00
|
|
|
* Returns a pointer to the next key if found, nullptr otherwise.
|
2014-02-10 01:10:30 +00:00
|
|
|
* Adding/Removing elements while iterating will, of course, have unexpected results, don't do it.
|
2016-03-08 23:00:52 +00:00
|
|
|
*
|
2014-02-10 01:10:30 +00:00
|
|
|
* Example:
|
2016-03-08 23:00:52 +00:00
|
|
|
*
|
2020-04-01 23:20:12 +00:00
|
|
|
* const TKey *k=nullptr;
|
2016-03-08 23:00:52 +00:00
|
|
|
*
|
2014-02-10 01:10:30 +00:00
|
|
|
* while( (k=table.next(k)) ) {
|
2016-03-08 23:00:52 +00:00
|
|
|
*
|
2014-02-10 01:10:30 +00:00
|
|
|
* print( *k );
|
|
|
|
* }
|
2021-10-28 13:43:36 +00:00
|
|
|
*
|
|
|
|
*/
|
2017-03-05 15:44:50 +00:00
|
|
|
const TKey *next(const TKey *p_key) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (unlikely(!hash_table)) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!p_key) { /* get the first key */
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < (1 << hash_table_power); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (hash_table[i]) {
|
|
|
|
return &hash_table[i]->pair.key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { /* get the next key */
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-13 08:32:39 +00:00
|
|
|
const Element *e = get_element(*p_key);
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!e, nullptr, "Invalid key supplied.");
|
2014-02-10 01:10:30 +00:00
|
|
|
if (e->next) {
|
|
|
|
/* if there is a "next" in the list, return that */
|
|
|
|
return &e->next->pair.key;
|
|
|
|
} else {
|
2017-08-13 08:32:39 +00:00
|
|
|
/* go to next elements */
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t index = e->hash & ((1 << hash_table_power) - 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
index++;
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = index; i < (1 << hash_table_power); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (hash_table[i]) {
|
|
|
|
return &hash_table[i]->pair.key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/* nothing found, was at end */
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr; /* nothing found */
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
inline unsigned int size() const {
|
|
|
|
return elements;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-12-15 12:04:21 +00:00
|
|
|
inline bool is_empty() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return elements == 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void clear() {
|
|
|
|
/* clean up */
|
|
|
|
if (hash_table) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < (1 << hash_table_power); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
while (hash_table[i]) {
|
2017-08-13 08:32:39 +00:00
|
|
|
Element *e = hash_table[i];
|
2017-03-05 15:44:50 +00:00
|
|
|
hash_table[i] = e->next;
|
|
|
|
memdelete(e);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
memdelete_arr(hash_table);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-05-14 08:15:48 +00:00
|
|
|
hash_table = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
hash_table_power = 0;
|
|
|
|
elements = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void operator=(const HashMap &p_table) {
|
2014-02-10 01:10:30 +00:00
|
|
|
copy_from(p_table);
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-06-05 07:40:02 +00:00
|
|
|
void get_key_list(List<TKey> *r_keys) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (unlikely(!hash_table)) {
|
2017-01-08 17:16:21 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < (1 << hash_table_power); i++) {
|
2017-08-13 08:32:39 +00:00
|
|
|
Element *e = hash_table[i];
|
2017-03-05 15:44:50 +00:00
|
|
|
while (e) {
|
2020-06-05 07:40:02 +00:00
|
|
|
r_keys->push_back(e->pair.key);
|
2017-03-05 15:44:50 +00:00
|
|
|
e = e->next;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
HashMap() {}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
HashMap(const HashMap &p_table) {
|
2014-02-10 01:10:30 +00:00
|
|
|
copy_from(p_table);
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
~HashMap() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-25 10:10:34 +00:00
|
|
|
#endif // HASH_MAP_H
|