From 084eb8266b9a3f9f55048312730b5e620af791d4 Mon Sep 17 00:00:00 2001 From: cat Date: Mon, 2 Mar 2026 16:57:52 +0200 Subject: [PATCH] Changed hashmaps to not use pointers, saving on allocations --- headers/Containers/Hashmap.hpp | 46 ++++++++++++---------------------- 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/headers/Containers/Hashmap.hpp b/headers/Containers/Hashmap.hpp index f7d0c3c..1ec8657 100644 --- a/headers/Containers/Hashmap.hpp +++ b/headers/Containers/Hashmap.hpp @@ -30,18 +30,16 @@ public: keyHashPosition = keyHash % storage.size(); // Empty bucket - if (storage[keyHashPosition] == nullptr) { - storage[keyHashPosition] = new std::vector; - } else { + if (!storage[keyHashPosition].empty()) { // Throws Systems::Logging::Log("Trying to inserting same key twice! Throwing...", "Hashmap", Systems::Logging::LogLevel::Error, Has(key)); } - storage[keyHashPosition]->emplace_back(key, std::move(value), keyHash); + storage[keyHashPosition].emplace_back(key, std::move(value), keyHash); currentLoadFactor = (++count) / static_cast(bucketCount); - return storage[keyHashPosition]->back().value; + return storage[keyHashPosition].back().value; } void Remove(const Key &key) { @@ -51,8 +49,8 @@ public: // Throws Systems::Logging::Log("Trying to remove a non-existant key! Throwing...", "Hashmap", Systems::Logging::LogLevel::Error, - storage[keyHashPosition] == nullptr); - std::erase_if(*storage[keyHashPosition], + storage[keyHashPosition].empty()); + std::erase_if(storage[keyHashPosition], [keyHash, &key](const hashStorage &hash) { return hash.hash == keyHash && hash.key == key; }); @@ -69,11 +67,11 @@ public: keyHashPosition = keyHash % storage.size(); // Empty bucket - if (storage[keyHashPosition] == nullptr) { + if (storage[keyHashPosition].empty()) { return false; } - for (const hashStorage &hash : *storage[keyHashPosition]) { + for (const hashStorage &hash : storage[keyHashPosition]) { if (hash.hash == keyHash && hash.key == key) { return true; } @@ -90,9 +88,9 @@ public: Systems::Logging::Log( "Trying to access a non-existant bucket for a key! Throwing...", "Hashmap", Systems::Logging::LogLevel::Error, - storage[keyHashPosition] == nullptr); + storage[keyHashPosition].empty()); - for (hashStorage &hash : *storage[keyHashPosition]) { + for (hashStorage &hash : storage[keyHashPosition]) { if (hash.hash == keyHash && hash.key == key) { return hash.value; } @@ -103,14 +101,7 @@ public: } void Clear() noexcept { - for (bucket *entry : storage) { - if (entry == nullptr) { - continue; - } - - entry->clear(); - delete entry; - } + storage.clear(); count = 0; } @@ -142,22 +133,17 @@ private: } currentlyRehashing = true; - std::vector oldStorage = std::move(storage); - storage = std::vector(); + std::vector oldStorage = std::move(storage); + storage = std::vector(); storage.resize(goalBucketCount); // Repopulate and cleanup - for (bucket *entry : oldStorage) { - if (entry == nullptr) { - continue; - } - - for (const hashStorage &hash : *entry) { + for (bucket &entry : oldStorage) { + for (const hashStorage &hash : entry) { Insert(hash.key, hash.value); } - entry->clear(); - delete entry; + entry.clear(); } // It's necessary to write these again due to insert above @@ -175,7 +161,7 @@ private: }; using bucket = std::vector; - std::vector storage; + std::vector storage; std::size_t count = 0, bucketCount = minimumBucketCount; float currentLoadFactor = 0; bool currentlyRehashing = false; // Lock for Insert in rehash