mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 14:07:45 +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
1 changed files with 10 additions and 2 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue