mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +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:
parent
59eedd6de0
commit
71c54198fa
Notes:
sideshowbarker
2024-07-18 12:34:26 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/71c54198fa7 Pull-request: https://github.com/SerenityOS/serenity/pull/7928 Reviewed-by: https://github.com/linusg
1 changed files with 10 additions and 2 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue