From 725430194d8e25fc78eb5e7f3911cb44c2eaa07a Mon Sep 17 00:00:00 2001 From: cat Date: Wed, 28 Jan 2026 02:49:03 +0200 Subject: [PATCH] Tombstoning finished UNTESTED --- headers/Containers/DualkeyMap.hpp | 58 ++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/headers/Containers/DualkeyMap.hpp b/headers/Containers/DualkeyMap.hpp index 0af7a7b..00be503 100644 --- a/headers/Containers/DualkeyMap.hpp +++ b/headers/Containers/DualkeyMap.hpp @@ -57,7 +57,8 @@ public: } } - bool Delete(std::optional firstKey, std::optional secondKey) { + std::size_t Delete(std::optional firstKey, + std::optional secondKey) { bool isFirstKeyGiven = firstKey.has_value(); bool isSecondKeyGiven = secondKey.has_value(); @@ -65,28 +66,59 @@ public: Systems::Logging::Log("Failed to Delete! Dualkey maps require at least 1 " "key to be given, doing nothing.", "Dualkey Map", Systems::Logging::LogLevel::Warning); - return false; + return 0; } std::size_t firstKeyHash = isFirstKeyGiven ? std::hash{}(firstKey.value()) : 0; std::size_t secondKeyHash = isSecondKeyGiven ? std::hash{}(secondKey.value()) : 0; - std::size_t index = 0; - + std::size_t index = 0, amountDeleted = 0; + uint8_t stateOfIndexing = isFirstKeyGiven + (isSecondKeyGiven << 1); for (DualkeyHash *hash : hashList) { - if (firstKeyHash == hash->firstKeyHash && - secondKeyHash == hash->secondKeyHash && - firstKey.value() == hash->firstKey && - secondKey.value() == hash->secondKey) { - delete hash; - hashList[index] = nullptr; - tombstones.push(index); - return true; + + // Tombstone + if (hash == nullptr) [[unlikely]] { + continue; } + + switch (stateOfIndexing) { + case 1: // Only first key is given + if (firstKeyHash == hash->firstKeyHash && + firstKey.value() == hash->firstKey) { + delete hash; + hashList[index] = nullptr; + tombstones.push(index); + ++amountDeleted; + } + continue; + + case 2: // Only second key is given + if (secondKeyHash == hash->secondKeyHash && + secondKey.value() == hash->secondKey) { + delete hash; + hashList[index] = nullptr; + tombstones.push(index); + ++amountDeleted; + } + continue; + + case 3: + if (firstKeyHash == hash->firstKeyHash && + secondKeyHash == hash->secondKeyHash && + firstKey.value() == hash->firstKey && + secondKey.value() == hash->secondKey) { + delete hash; + hashList[index] = nullptr; + tombstones.push(index); + return 1; + } + continue; + } + ++index; } - return false; + return amountDeleted; } [[nodiscard("Discarding an expensive operation's result!")]]