mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: HashTable/HashMap return whether action was performed for set/remove
This allows performing an action based on whether something was actually added or removed without having to look it up prior to calling set() or remove().
This commit is contained in:
parent
df54229954
commit
dadd53e4f2
Notes:
sideshowbarker
2024-07-19 05:00:19 +09:00
Author: https://github.com/tomuta Commit: https://github.com/SerenityOS/serenity/commit/dadd53e4f2b Pull-request: https://github.com/SerenityOS/serenity/pull/2718 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/awesomekling Reviewed-by: https://github.com/bugaevc
2 changed files with 25 additions and 12 deletions
11
AK/HashMap.h
11
AK/HashMap.h
|
@ -54,13 +54,16 @@ public:
|
|||
size_t capacity() const { return m_table.capacity(); }
|
||||
void clear() { m_table.clear(); }
|
||||
|
||||
void set(const K& key, const V& value) { m_table.set({ key, value }); }
|
||||
void set(const K& key, V&& value) { m_table.set({ key, move(value) }); }
|
||||
void remove(const K& key)
|
||||
HashSetResult set(const K& key, const V& value) { return m_table.set({ key, value }); }
|
||||
HashSetResult set(const K& key, V&& value) { return m_table.set({ key, move(value) }); }
|
||||
bool remove(const K& key)
|
||||
{
|
||||
auto it = find(key);
|
||||
if (it != end())
|
||||
if (it != end()) {
|
||||
m_table.remove(it);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void remove_one_randomly() { m_table.remove(m_table.begin()); }
|
||||
|
||||
|
|
|
@ -34,6 +34,11 @@
|
|||
|
||||
namespace AK {
|
||||
|
||||
enum class HashSetResult {
|
||||
InsertedNewEntry,
|
||||
ReplacedExistingEntry
|
||||
};
|
||||
|
||||
template<typename T, typename>
|
||||
class HashTable;
|
||||
|
||||
|
@ -157,8 +162,8 @@ public:
|
|||
rehash(capacity);
|
||||
}
|
||||
|
||||
void set(const T&);
|
||||
void set(T&&);
|
||||
HashSetResult set(const T&);
|
||||
HashSetResult set(T&&);
|
||||
bool contains(const T&) const;
|
||||
void clear();
|
||||
|
||||
|
@ -208,11 +213,14 @@ public:
|
|||
return find(TraitsForT::hash(value), [&](auto& other) { return TraitsForT::equals(value, other); });
|
||||
}
|
||||
|
||||
void remove(const T& value)
|
||||
bool remove(const T& value)
|
||||
{
|
||||
auto it = find(value);
|
||||
if (it != end())
|
||||
if (it != end()) {
|
||||
remove(it);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void remove(Iterator);
|
||||
|
@ -251,7 +259,7 @@ private:
|
|||
};
|
||||
|
||||
template<typename T, typename TraitsForT>
|
||||
void HashTable<T, TraitsForT>::set(T&& value)
|
||||
HashSetResult HashTable<T, TraitsForT>::set(T&& value)
|
||||
{
|
||||
if (!m_capacity)
|
||||
rehash(1);
|
||||
|
@ -259,7 +267,7 @@ void HashTable<T, TraitsForT>::set(T&& value)
|
|||
for (auto& e : bucket) {
|
||||
if (TraitsForT::equals(e, value)) {
|
||||
e = move(value);
|
||||
return;
|
||||
return HashSetResult::ReplacedExistingEntry;
|
||||
}
|
||||
}
|
||||
if (size() >= capacity()) {
|
||||
|
@ -269,10 +277,11 @@ void HashTable<T, TraitsForT>::set(T&& value)
|
|||
bucket.append(move(value));
|
||||
}
|
||||
m_size++;
|
||||
return HashSetResult::InsertedNewEntry;
|
||||
}
|
||||
|
||||
template<typename T, typename TraitsForT>
|
||||
void HashTable<T, TraitsForT>::set(const T& value)
|
||||
HashSetResult HashTable<T, TraitsForT>::set(const T& value)
|
||||
{
|
||||
if (!m_capacity)
|
||||
rehash(1);
|
||||
|
@ -280,7 +289,7 @@ void HashTable<T, TraitsForT>::set(const T& value)
|
|||
for (auto& e : bucket) {
|
||||
if (TraitsForT::equals(e, value)) {
|
||||
e = value;
|
||||
return;
|
||||
return HashSetResult::ReplacedExistingEntry;
|
||||
}
|
||||
}
|
||||
if (size() >= capacity()) {
|
||||
|
@ -290,6 +299,7 @@ void HashTable<T, TraitsForT>::set(const T& value)
|
|||
bucket.append(value);
|
||||
}
|
||||
m_size++;
|
||||
return HashSetResult::InsertedNewEntry;
|
||||
}
|
||||
|
||||
template<typename T, typename TraitsForT>
|
||||
|
|
Loading…
Reference in a new issue