diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-10-08 21:43:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-08 21:43:52 +0200 |
commit | 16b27304d90880b0a3a6bfefbb19f2718d5919af (patch) | |
tree | 86578aac2c2c7f83683a9359a47404a0083dad54 | |
parent | 58aa020a19162becc9a00fed8a240eb12b0ab964 (diff) | |
parent | 73697d4de655db86dacae754d40ed25f3b952e6e (diff) |
Merge pull request #53579 from RandomShaper/better_hash_map
-rw-r--r-- | core/templates/hash_map.h | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/core/templates/hash_map.h b/core/templates/hash_map.h index 1257b54449..b5bb0d7396 100644 --- a/core/templates/hash_map.h +++ b/core/templates/hash_map.h @@ -62,7 +62,9 @@ public: TKey key; TData data; - Pair() {} + Pair(const TKey &p_key) : + key(p_key), + data() {} Pair(const TKey &p_key, const TData &p_data) : key(p_key), data(p_data) { @@ -90,6 +92,11 @@ public: const TData &value() const { return pair.value(); } + + Element(const TKey &p_key) : + pair(p_key) {} + Element(const Element &p_other) : + pair(p_other.pair.key, p_other.pair.data) {} }; private: @@ -192,14 +199,12 @@ private: Element *create_element(const TKey &p_key) { /* if element doesn't exist, create it */ - Element *e = memnew(Element); + Element *e = memnew(Element(p_key)); ERR_FAIL_COND_V_MSG(!e, nullptr, "Out of memory."); 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; - e->pair.data = TData(); hash_table[index] = e; elements++; @@ -228,9 +233,7 @@ private: const Element *e = p_t.hash_table[i]; while (e) { - Element *le = memnew(Element); /* local element */ - - *le = *e; /* copy data */ + Element *le = memnew(Element(*e)); /* local element */ /* add to list and reassign pointers */ le->next = hash_table[i]; |