AK: Allow to clear HashTables/Maps with capacity

This commit is contained in:
Hendiadyoin1 2021-09-20 23:43:52 +02:00 committed by Andreas Kling
parent 5137f96bd6
commit f76241914c
2 changed files with 16 additions and 0 deletions

View file

@ -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) }); }

View file

@ -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)