123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- #ifndef HASH_MAP_H
- #define HASH_MAP_H
- #include "core/error_macros.h"
- #include "core/hashfuncs.h"
- #include "core/list.h"
- #include "core/math/math_funcs.h"
- #include "core/os/memory.h"
- #include "core/ustring.h"
- template <class TKey, class TData, class Hasher = HashMapHasherDefault, class Comparator = HashMapComparatorDefault<TKey>, uint8_t MIN_HASH_TABLE_POWER = 3, uint8_t RELATIONSHIP = 8>
- class HashMap {
- public:
- struct Pair {
- TKey key;
- TData data;
- Pair() {}
- Pair(const TKey &p_key, const TData &p_data) :
- key(p_key),
- data(p_data) {
- }
- };
- struct Element {
- private:
- friend class HashMap;
- uint32_t hash;
- Element *next;
- Element() { next = 0; }
- Pair pair;
- public:
- const TKey &key() const {
- return pair.key;
- }
- TData &value() {
- return pair.data;
- }
- const TData &value() const {
- return pair.value();
- }
- };
- private:
- Element **hash_table;
- uint8_t hash_table_power;
- uint32_t elements;
- void make_hash_table() {
- ERR_FAIL_COND(hash_table);
- hash_table = memnew_arr(Element *, (1 << MIN_HASH_TABLE_POWER));
- hash_table_power = MIN_HASH_TABLE_POWER;
- elements = 0;
- for (int i = 0; i < (1 << MIN_HASH_TABLE_POWER); i++)
- hash_table[i] = 0;
- }
- void erase_hash_table() {
- ERR_FAIL_COND(elements);
- memdelete_arr(hash_table);
- hash_table = 0;
- hash_table_power = 0;
- elements = 0;
- }
- void check_hash_table() {
- int new_hash_table_power = -1;
- if ((int)elements > ((1 << hash_table_power) * RELATIONSHIP)) {
-
- new_hash_table_power = hash_table_power + 1;
- while ((int)elements > ((1 << new_hash_table_power) * RELATIONSHIP)) {
- new_hash_table_power++;
- }
- } else if ((hash_table_power > (int)MIN_HASH_TABLE_POWER) && ((int)elements < ((1 << (hash_table_power - 1)) * RELATIONSHIP))) {
-
- new_hash_table_power = hash_table_power - 1;
- while ((int)elements < ((1 << (new_hash_table_power - 1)) * RELATIONSHIP)) {
- new_hash_table_power--;
- }
- if (new_hash_table_power < (int)MIN_HASH_TABLE_POWER)
- new_hash_table_power = MIN_HASH_TABLE_POWER;
- }
- if (new_hash_table_power == -1)
- return;
- Element **new_hash_table = memnew_arr(Element *, ((uint64_t)1 << new_hash_table_power));
- if (!new_hash_table) {
- ERR_PRINT("Out of Memory");
- return;
- }
- for (int i = 0; i < (1 << new_hash_table_power); i++) {
- new_hash_table[i] = 0;
- }
- 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;
- }
- }
- if (hash_table)
- memdelete_arr(hash_table);
- hash_table = new_hash_table;
- hash_table_power = new_hash_table_power;
- }
-
- _FORCE_INLINE_ const Element *get_element(const TKey &p_key) const {
- uint32_t hash = Hasher::hash(p_key);
- uint32_t index = hash & ((1 << hash_table_power) - 1);
- Element *e = hash_table[index];
- while (e) {
-
- if (e->hash == hash && Comparator::compare(e->pair.key, p_key)) {
-
- return e;
- }
- e = e->next;
- }
- return NULL;
- }
- Element *create_element(const TKey &p_key) {
-
- Element *e = memnew(Element);
- ERR_FAIL_COND_V(!e, NULL);
- uint32_t hash = Hasher::hash(p_key);
- uint32_t index = hash & ((1 << hash_table_power) - 1);
- e->next = hash_table[index];
- e->hash = hash;
- e->pair.key = p_key;
- hash_table[index] = e;
- elements++;
- return e;
- }
- void copy_from(const HashMap &p_t) {
- if (&p_t == this)
- return;
- clear();
- if (!p_t.hash_table || p_t.hash_table_power == 0)
- return;
- hash_table = memnew_arr(Element *, (uint64_t)1 << p_t.hash_table_power);
- hash_table_power = p_t.hash_table_power;
- elements = p_t.elements;
- for (int i = 0; i < (1 << p_t.hash_table_power); i++) {
- hash_table[i] = NULL;
- const Element *e = p_t.hash_table[i];
- while (e) {
- Element *le = memnew(Element);
- *le = *e;
-
- le->next = hash_table[i];
- hash_table[i] = le;
- e = e->next;
- }
- }
- }
- public:
- Element *set(const TKey &p_key, const TData &p_data) {
- return set(Pair(p_key, p_data));
- }
- Element *set(const Pair &p_pair) {
- Element *e = NULL;
- if (!hash_table)
- make_hash_table();
- else
- e = const_cast<Element *>(get_element(p_pair.key));
-
- if (!e) {
- e = create_element(p_pair.key);
- if (!e)
- return NULL;
- check_hash_table();
- }
- e->pair.data = p_pair.data;
- return e;
- }
- bool has(const TKey &p_key) const {
- return getptr(p_key) != NULL;
- }
-
- const TData &get(const TKey &p_key) const {
- const TData *res = getptr(p_key);
- ERR_FAIL_COND_V(!res, *res);
- return *res;
- }
- TData &get(const TKey &p_key) {
- TData *res = getptr(p_key);
- ERR_FAIL_COND_V(!res, *res);
- return *res;
- }
-
- _FORCE_INLINE_ TData *getptr(const TKey &p_key) {
- if (unlikely(!hash_table))
- return NULL;
- Element *e = const_cast<Element *>(get_element(p_key));
- if (e)
- return &e->pair.data;
- return NULL;
- }
- _FORCE_INLINE_ const TData *getptr(const TKey &p_key) const {
- if (unlikely(!hash_table))
- return NULL;
- const Element *e = const_cast<Element *>(get_element(p_key));
- if (e)
- return &e->pair.data;
- return NULL;
- }
-
- template <class C>
- _FORCE_INLINE_ TData *custom_getptr(C p_custom_key, uint32_t p_custom_hash) {
- if (unlikely(!hash_table))
- return NULL;
- uint32_t hash = p_custom_hash;
- uint32_t index = hash & ((1 << hash_table_power) - 1);
- Element *e = hash_table[index];
- while (e) {
-
- if (e->hash == hash && Comparator::compare(e->pair.key, p_custom_key)) {
-
- return &e->pair.data;
- }
- e = e->next;
- }
- return NULL;
- }
- template <class C>
- _FORCE_INLINE_ const TData *custom_getptr(C p_custom_key, uint32_t p_custom_hash) const {
- if (unlikely(!hash_table))
- return NULL;
- uint32_t hash = p_custom_hash;
- uint32_t index = hash & ((1 << hash_table_power) - 1);
- const Element *e = hash_table[index];
- while (e) {
-
- if (e->hash == hash && Comparator::compare(e->pair.key, p_custom_key)) {
-
- return &e->pair.data;
- }
- e = e->next;
- }
- return NULL;
- }
-
- bool erase(const TKey &p_key) {
- if (unlikely(!hash_table))
- return false;
- uint32_t hash = Hasher::hash(p_key);
- uint32_t index = hash & ((1 << hash_table_power) - 1);
- Element *e = hash_table[index];
- Element *p = NULL;
- while (e) {
-
- if (e->hash == hash && Comparator::compare(e->pair.key, p_key)) {
- if (p) {
- p->next = e->next;
- } else {
-
- hash_table[index] = e->next;
- }
- memdelete(e);
- elements--;
- if (elements == 0)
- erase_hash_table();
- else
- check_hash_table();
- return true;
- }
- p = e;
- e = e->next;
- }
- return false;
- }
- inline const TData &operator[](const TKey &p_key) const {
- return get(p_key);
- }
- inline TData &operator[](const TKey &p_key) {
- Element *e = NULL;
- if (!hash_table)
- make_hash_table();
- else
- e = const_cast<Element *>(get_element(p_key));
-
- if (!e) {
- e = create_element(p_key);
- CRASH_COND(!e);
- check_hash_table();
- }
- return e->pair.data;
- }
-
- const TKey *next(const TKey *p_key) const {
- if (unlikely(!hash_table))
- return NULL;
- if (!p_key) {
- for (int i = 0; i < (1 << hash_table_power); i++) {
- if (hash_table[i]) {
- return &hash_table[i]->pair.key;
- }
- }
- } else {
- const Element *e = get_element(*p_key);
- ERR_FAIL_COND_V(!e, NULL);
- if (e->next) {
-
- return &e->next->pair.key;
- } else {
-
- uint32_t index = e->hash & ((1 << hash_table_power) - 1);
- index++;
- for (int i = index; i < (1 << hash_table_power); i++) {
- if (hash_table[i]) {
- return &hash_table[i]->pair.key;
- }
- }
- }
-
- }
- return NULL;
- }
- inline unsigned int size() const {
- return elements;
- }
- inline bool empty() const {
- return elements == 0;
- }
- void clear() {
-
- if (hash_table) {
- for (int i = 0; i < (1 << hash_table_power); i++) {
- while (hash_table[i]) {
- Element *e = hash_table[i];
- hash_table[i] = e->next;
- memdelete(e);
- }
- }
- memdelete_arr(hash_table);
- }
- hash_table = 0;
- hash_table_power = 0;
- elements = 0;
- }
- void operator=(const HashMap &p_table) {
- copy_from(p_table);
- }
- HashMap() {
- hash_table = NULL;
- elements = 0;
- hash_table_power = 0;
- }
- void get_key_value_ptr_array(const Pair **p_pairs) const {
- if (unlikely(!hash_table))
- return;
- for (int i = 0; i < (1 << hash_table_power); i++) {
- Element *e = hash_table[i];
- while (e) {
- *p_pairs = &e->pair;
- p_pairs++;
- e = e->next;
- }
- }
- }
- void get_key_list(List<TKey> *p_keys) const {
- if (unlikely(!hash_table))
- return;
- for (int i = 0; i < (1 << hash_table_power); i++) {
- Element *e = hash_table[i];
- while (e) {
- p_keys->push_back(e->pair.key);
- e = e->next;
- }
- }
- }
- HashMap(const HashMap &p_table) {
- hash_table = NULL;
- elements = 0;
- hash_table_power = 0;
- copy_from(p_table);
- }
- ~HashMap() {
- clear();
- }
- };
- #endif
|