1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:17:44 +00:00

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 { enum class HashSetResult {
InsertedNewEntry, InsertedNewEntry,
ReplacedExistingEntry ReplacedExistingEntry,
KeptExistingEntry
};
enum class HashSetExistingEntryBehavior {
Keep,
Replace
}; };
template<typename HashTableType, typename T, typename BucketType> template<typename HashTableType, typename T, typename BucketType>
@ -184,10 +190,12 @@ public:
} }
template<typename U = T> template<typename U = T>
HashSetResult set(U&& value) HashSetResult set(U&& value, HashSetExistingEntryBehavior existing_entry_behaviour = HashSetExistingEntryBehavior::Replace)
{ {
auto& bucket = lookup_for_writing(value); auto& bucket = lookup_for_writing(value);
if (bucket.used) { if (bucket.used) {
if (existing_entry_behaviour == HashSetExistingEntryBehavior::Keep)
return HashSetResult::KeptExistingEntry;
(*bucket.slot()) = forward<U>(value); (*bucket.slot()) = forward<U>(value);
return HashSetResult::ReplacedExistingEntry; return HashSetResult::ReplacedExistingEntry;
} }