Avoid the need for copy assignment in HashMap key/data types

This commit is contained in:
Pedro J. Estébanez 2021-02-17 13:12:38 +01:00
parent 2f942ab846
commit 5eb80bb1a3

View file

@ -62,7 +62,9 @@ public:
TKey key; TKey key;
TData data; TData data;
Pair() {} Pair(const TKey &p_key) :
key(p_key),
data() {}
Pair(const TKey &p_key, const TData &p_data) : Pair(const TKey &p_key, const TData &p_data) :
key(p_key), key(p_key),
data(p_data) { data(p_data) {
@ -90,6 +92,12 @@ public:
const TData &value() const { const TData &value() const {
return pair.value(); return pair.value();
} }
Element(const TKey &p_key) :
pair(p_key) {}
Element(const Element &p_other) :
hash(p_other.hash),
pair(p_other.pair.key, p_other.pair.data) {}
}; };
private: private:
@ -192,14 +200,12 @@ private:
Element *create_element(const TKey &p_key) { Element *create_element(const TKey &p_key) {
/* if element doesn't exist, create it */ /* 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."); ERR_FAIL_COND_V_MSG(!e, nullptr, "Out of memory.");
uint32_t hash = Hasher::hash(p_key); uint32_t hash = Hasher::hash(p_key);
uint32_t index = hash & ((1 << hash_table_power) - 1); uint32_t index = hash & ((1 << hash_table_power) - 1);
e->next = hash_table[index]; e->next = hash_table[index];
e->hash = hash; e->hash = hash;
e->pair.key = p_key;
e->pair.data = TData();
hash_table[index] = e; hash_table[index] = e;
elements++; elements++;
@ -228,9 +234,7 @@ private:
const Element *e = p_t.hash_table[i]; const Element *e = p_t.hash_table[i];
while (e) { while (e) {
Element *le = memnew(Element); /* local element */ Element *le = memnew(Element(*e)); /* local element */
*le = *e; /* copy data */
/* add to list and reassign pointers */ /* add to list and reassign pointers */
le->next = hash_table[i]; le->next = hash_table[i];