AK: Allow to clear HashTables/Maps with capacity
This commit is contained in:
parent
5137f96bd6
commit
f76241914c
Notes:
sideshowbarker
2024-07-18 01:17:35 +09:00
Author: https://github.com/Hendiadyoin1 Commit: https://github.com/SerenityOS/serenity/commit/f76241914c8 Pull-request: https://github.com/SerenityOS/serenity/pull/10723
2 changed files with 16 additions and 0 deletions
|
@ -46,6 +46,7 @@ public:
|
|||
[[nodiscard]] size_t size() const { return m_table.size(); }
|
||||
[[nodiscard]] size_t capacity() const { return m_table.capacity(); }
|
||||
void clear() { m_table.clear(); }
|
||||
void clear_with_capacity() { m_table.clear_with_capacity(); }
|
||||
|
||||
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) }); }
|
||||
|
|
|
@ -255,6 +255,21 @@ public:
|
|||
{
|
||||
*this = HashTable();
|
||||
}
|
||||
void clear_with_capacity()
|
||||
{
|
||||
if constexpr (!Detail::IsTriviallyDestructible<T>) {
|
||||
for (auto* bucket : *this)
|
||||
bucket->~T();
|
||||
}
|
||||
__builtin_memset(m_buckets, 0, size_in_bytes(capacity()));
|
||||
m_size = 0;
|
||||
m_deleted_count = 0;
|
||||
|
||||
if constexpr (IsOrdered)
|
||||
m_collection_data = { nullptr, nullptr };
|
||||
else
|
||||
m_buckets[m_capacity].end = true;
|
||||
}
|
||||
|
||||
template<typename U = T>
|
||||
ErrorOr<HashSetResult> try_set(U&& value, HashSetExistingEntryBehavior existing_entry_behavior = HashSetExistingEntryBehavior::Replace)
|
||||
|
|
Loading…
Add table
Reference in a new issue