diff --git a/headers/Containers/Hashmap.hpp b/headers/Containers/Hashmap.hpp index ed97d6c..6a52222 100644 --- a/headers/Containers/Hashmap.hpp +++ b/headers/Containers/Hashmap.hpp @@ -1,4 +1,3 @@ - /* * SPDX-FileCopyrightText: Dora "cat" * SPDX-License-Identifier: MPL-2.0 @@ -16,22 +15,31 @@ #include namespace Tourmaline::Containers { -template class Hashmap { public: Hashmap() { storage.resize(baseBucketCount); } ~Hashmap() { Clear(); } - void Insert(Key key, Value value) { + Value &Insert(Key key, Value value) { std::size_t keyHash = std::hash{}(key), keyHashPosition = keyHash % storage.size(); // Empty bucket if (storage[keyHashPosition] == nullptr) { storage[keyHashPosition] = new std::vector; + storage[keyHashPosition]->emplace_back(key, std::move(value), keyHash); + return storage[keyHashPosition]->back().value; } - storage[keyHashPosition]->emplace_back(keyHash, key, std::move(value)); + + // 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); + return storage[keyHashPosition]->back().value; } void Remove(const Key &key) { @@ -43,7 +51,7 @@ public: "Hashmap", Systems::Logging::LogLevel::Error, storage[keyHashPosition] == nullptr); std::erase_if(*storage[keyHashPosition], - [key, keyHash](const hashStorage &hash) { + [keyHash, &key](const hashStorage &hash) { return hash.hash == keyHash && hash.key == key; }); } @@ -75,7 +83,7 @@ public: "Hashmap", Systems::Logging::LogLevel::Error, storage[keyHashPosition] == nullptr); - for (const hashStorage &hash : *storage[keyHashPosition]) { + for (hashStorage &hash : *storage[keyHashPosition]) { if (hash.hash == keyHash && hash.key == key) { return hash.value; } @@ -98,9 +106,9 @@ public: private: struct hashStorage { - const std::size_t hash; - const Key key; - mutable Value value; + Key key; + Value value; + std::size_t hash; }; using bucket = std::vector;