From f76241914c8a2b860c1e6fd3d7172aa22e5b0d9d Mon Sep 17 00:00:00 2001 From: Hendiadyoin1 Date: Mon, 20 Sep 2021 23:43:52 +0200 Subject: [PATCH] AK: Allow to clear HashTables/Maps with capacity --- AK/HashMap.h | 1 + AK/HashTable.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/AK/HashMap.h b/AK/HashMap.h index 733a7057cc..5303bdf32d 100644 --- a/AK/HashMap.h +++ b/AK/HashMap.h @@ -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) }); } diff --git a/AK/HashTable.h b/AK/HashTable.h index 7af8f89e07..52a9a6a060 100644 --- a/AK/HashTable.h +++ b/AK/HashTable.h @@ -255,6 +255,21 @@ public: { *this = HashTable(); } + void clear_with_capacity() + { + if constexpr (!Detail::IsTriviallyDestructible) { + 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 ErrorOr try_set(U&& value, HashSetExistingEntryBehavior existing_entry_behavior = HashSetExistingEntryBehavior::Replace)