From 71c54198fa733efd6bfc99a73cbc024fe9bdc901 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Tue, 8 Jun 2021 23:42:07 +0300 Subject: [PATCH] AK: Allow changing the HashTable behaviour for sets on existing entries Specifically, replacing the existing entry or just keeping it and canceling the set. --- AK/HashTable.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/AK/HashTable.h b/AK/HashTable.h index 5d55c4f9cf..9c4c2f6227 100644 --- a/AK/HashTable.h +++ b/AK/HashTable.h @@ -15,7 +15,13 @@ namespace AK { enum class HashSetResult { InsertedNewEntry, - ReplacedExistingEntry + ReplacedExistingEntry, + KeptExistingEntry +}; + +enum class HashSetExistingEntryBehavior { + Keep, + Replace }; template @@ -184,10 +190,12 @@ public: } template - HashSetResult set(U&& value) + HashSetResult set(U&& value, HashSetExistingEntryBehavior existing_entry_behaviour = HashSetExistingEntryBehavior::Replace) { auto& bucket = lookup_for_writing(value); if (bucket.used) { + if (existing_entry_behaviour == HashSetExistingEntryBehavior::Keep) + return HashSetResult::KeptExistingEntry; (*bucket.slot()) = forward(value); return HashSetResult::ReplacedExistingEntry; }