diff options
Diffstat (limited to 'core/hash_map.h')
-rw-r--r-- | core/hash_map.h | 63 |
1 files changed, 41 insertions, 22 deletions
diff --git a/core/hash_map.h b/core/hash_map.h index a4bc811b67..78592f8d82 100644 --- a/core/hash_map.h +++ b/core/hash_map.h @@ -104,8 +104,9 @@ private: hash_table_power = MIN_HASH_TABLE_POWER; elements = 0; - for (int i = 0; i < (1 << MIN_HASH_TABLE_POWER); i++) + for (int i = 0; i < (1 << MIN_HASH_TABLE_POWER); i++) { hash_table[i] = nullptr; + } } void erase_hash_table() { @@ -136,12 +137,14 @@ private: new_hash_table_power--; } - if (new_hash_table_power < (int)MIN_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) + if (new_hash_table_power == -1) { return; + } Element **new_hash_table = memnew_arr(Element *, ((uint64_t)1 << new_hash_table_power)); ERR_FAIL_COND_MSG(!new_hash_table, "Out of memory."); @@ -205,13 +208,15 @@ private: } void copy_from(const HashMap &p_t) { - if (&p_t == this) + if (&p_t == this) { return; /* much less bother with that */ + } clear(); - if (!p_t.hash_table || p_t.hash_table_power == 0) + if (!p_t.hash_table || p_t.hash_table_power == 0) { return; /* not copying from empty table */ + } hash_table = memnew_arr(Element *, (uint64_t)1 << p_t.hash_table_power); hash_table_power = p_t.hash_table_power; @@ -243,17 +248,19 @@ public: Element *set(const Pair &p_pair) { Element *e = nullptr; - if (!hash_table) + if (!hash_table) { make_hash_table(); // if no table, make one - else + } else { e = const_cast<Element *>(get_element(p_pair.key)); + } /* if we made it up to here, the pair doesn't exist, create and assign */ if (!e) { e = create_element(p_pair.key); - if (!e) + if (!e) { return nullptr; + } check_hash_table(); // perform mantenience routine } @@ -289,25 +296,29 @@ public: */ _FORCE_INLINE_ TData *getptr(const TKey &p_key) { - if (unlikely(!hash_table)) + if (unlikely(!hash_table)) { return nullptr; + } Element *e = const_cast<Element *>(get_element(p_key)); - if (e) + if (e) { return &e->pair.data; + } return nullptr; } _FORCE_INLINE_ const TData *getptr(const TKey &p_key) const { - if (unlikely(!hash_table)) + if (unlikely(!hash_table)) { return nullptr; + } const Element *e = const_cast<Element *>(get_element(p_key)); - if (e) + if (e) { return &e->pair.data; + } return nullptr; } @@ -319,8 +330,9 @@ public: template <class C> _FORCE_INLINE_ TData *custom_getptr(C p_custom_key, uint32_t p_custom_hash) { - if (unlikely(!hash_table)) + if (unlikely(!hash_table)) { return nullptr; + } uint32_t hash = p_custom_hash; uint32_t index = hash & ((1 << hash_table_power) - 1); @@ -342,8 +354,9 @@ public: template <class C> _FORCE_INLINE_ const TData *custom_getptr(C p_custom_key, uint32_t p_custom_hash) const { - if (unlikely(!hash_table)) + if (unlikely(!hash_table)) { return nullptr; + } uint32_t hash = p_custom_hash; uint32_t index = hash & ((1 << hash_table_power) - 1); @@ -368,8 +381,9 @@ public: */ bool erase(const TKey &p_key) { - if (unlikely(!hash_table)) + if (unlikely(!hash_table)) { return false; + } uint32_t hash = Hasher::hash(p_key); uint32_t index = hash & ((1 << hash_table_power) - 1); @@ -389,10 +403,11 @@ public: memdelete(e); elements--; - if (elements == 0) + if (elements == 0) { erase_hash_table(); - else + } else { check_hash_table(); + } return true; } @@ -410,10 +425,11 @@ public: inline TData &operator[](const TKey &p_key) { //assignment Element *e = nullptr; - if (!hash_table) + if (!hash_table) { make_hash_table(); // if no table, make one - else + } else { e = const_cast<Element *>(get_element(p_key)); + } /* if we made it up to here, the pair doesn't exist, create */ if (!e) { @@ -441,8 +457,9 @@ public: * */ const TKey *next(const TKey *p_key) const { - if (unlikely(!hash_table)) + if (unlikely(!hash_table)) { return nullptr; + } if (!p_key) { /* get the first key */ @@ -508,8 +525,9 @@ public: } void get_key_value_ptr_array(const Pair **p_pairs) const { - if (unlikely(!hash_table)) + if (unlikely(!hash_table)) { return; + } for (int i = 0; i < (1 << hash_table_power); i++) { Element *e = hash_table[i]; while (e) { @@ -521,8 +539,9 @@ public: } void get_key_list(List<TKey> *p_keys) const { - if (unlikely(!hash_table)) + if (unlikely(!hash_table)) { return; + } for (int i = 0; i < (1 << hash_table_power); i++) { Element *e = hash_table[i]; while (e) { |