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

HashTable: Correctly pass args to set

Problem:
- Using regular functions rather than function templates results in
  the arguments not being deduced. This then requires the same
  function to be written multiple times and for `move` to be used
  rather than `forward`.

Solution:
- Collapse multiple function overloads to a single function template
  with a deduced argument. This allows the argument to be a forwarding
  reference and bind to either an l-value or r-value and forward the
  value.
This commit is contained in:
Lenny Maiorani 2021-01-15 15:59:55 -07:00 committed by Andreas Kling
parent 2490fc79ad
commit 537bedbf38

View file

@ -203,15 +203,16 @@ public:
*this = HashTable(); *this = HashTable();
} }
HashSetResult set(T&& value) template<typename U = T>
HashSetResult set(U&& value)
{ {
auto& bucket = lookup_for_writing(value); auto& bucket = lookup_for_writing(value);
if (bucket.used) { if (bucket.used) {
(*bucket.slot()) = move(value); (*bucket.slot()) = forward<U>(value);
return HashSetResult::ReplacedExistingEntry; return HashSetResult::ReplacedExistingEntry;
} }
new (bucket.slot()) T(move(value)); new (bucket.slot()) T(forward<U>(value));
bucket.used = true; bucket.used = true;
if (bucket.deleted) { if (bucket.deleted) {
bucket.deleted = false; bucket.deleted = false;
@ -221,11 +222,6 @@ public:
return HashSetResult::InsertedNewEntry; return HashSetResult::InsertedNewEntry;
} }
HashSetResult set(const T& value)
{
return set(T(value));
}
template<typename Finder> template<typename Finder>
Iterator find(unsigned hash, Finder finder) Iterator find(unsigned hash, Finder finder)
{ {