Fully functioning graveyard TESTED

This commit is contained in:
2026-01-28 03:06:31 +02:00
parent 725430194d
commit 8887547fdc

View File

@@ -23,7 +23,7 @@
namespace Tourmaline::Containers { namespace Tourmaline::Containers {
template <Hashable AKey, Hashable BKey, typename Value, template <Hashable AKey, Hashable BKey, typename Value,
uint64_t baseReservation = 2048> uint64_t baseReservation = 2048, double maxTombstoneRatio = 0.25>
class DualkeyMap { class DualkeyMap {
public: public:
using ResultPair = using ResultPair =
@@ -48,11 +48,11 @@ public:
new DualkeyHash(firstKeyHash, std::move(firstKey), secondKeyHash, new DualkeyHash(firstKeyHash, std::move(firstKey), secondKeyHash,
std::move(secondKey), std::move(value)); std::move(secondKey), std::move(value));
if (tombstones.empty()) { if (graveyard.empty()) {
hashList.push_back(hash); hashList.push_back(hash);
} else { } else {
std::size_t tombstone = tombstones.back(); std::size_t tombstone = graveyard.front();
tombstones.pop(); graveyard.pop();
hashList[tombstone] = hash; hashList[tombstone] = hash;
} }
} }
@@ -88,20 +88,20 @@ public:
firstKey.value() == hash->firstKey) { firstKey.value() == hash->firstKey) {
delete hash; delete hash;
hashList[index] = nullptr; hashList[index] = nullptr;
tombstones.push(index); graveyard.push(index);
++amountDeleted; ++amountDeleted;
} }
continue; break;
case 2: // Only second key is given case 2: // Only second key is given
if (secondKeyHash == hash->secondKeyHash && if (secondKeyHash == hash->secondKeyHash &&
secondKey.value() == hash->secondKey) { secondKey.value() == hash->secondKey) {
delete hash; delete hash;
hashList[index] = nullptr; hashList[index] = nullptr;
tombstones.push(index); graveyard.push(index);
++amountDeleted; ++amountDeleted;
} }
continue; break;
case 3: case 3:
if (firstKeyHash == hash->firstKeyHash && if (firstKeyHash == hash->firstKeyHash &&
@@ -110,12 +110,11 @@ public:
secondKey.value() == hash->secondKey) { secondKey.value() == hash->secondKey) {
delete hash; delete hash;
hashList[index] = nullptr; hashList[index] = nullptr;
tombstones.push(index); graveyard.push(index);
return 1; return 1;
} }
continue; break;
} }
++index; ++index;
} }
return amountDeleted; return amountDeleted;
@@ -203,7 +202,7 @@ private:
// It makes more sense to store the individual hash // It makes more sense to store the individual hash
std::vector<DualkeyHash *> hashList; std::vector<DualkeyHash *> hashList;
std::queue<std::size_t> tombstones; std::queue<std::size_t> graveyard;
}; };
} // namespace Tourmaline::Containers } // namespace Tourmaline::Containers
#endif #endif