AK: Allow changing the HashTable behaviour for sets on existing entries

Specifically, replacing the existing entry or just keeping it and
canceling the set.
This commit is contained in:
Idan Horowitz 2021-06-08 23:42:07 +03:00 committed by Linus Groh
parent 59eedd6de0
commit 71c54198fa

View file

@ -15,7 +15,13 @@ namespace AK {
enum class HashSetResult {
InsertedNewEntry,
ReplacedExistingEntry
ReplacedExistingEntry,
KeptExistingEntry
};
enum class HashSetExistingEntryBehavior {
Keep,
Replace
};
template<typename HashTableType, typename T, typename BucketType>
@ -184,10 +190,12 @@ public:
}
template<typename U = T>
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<U>(value);
return HashSetResult::ReplacedExistingEntry;
}