diff options
author | Hendiadyoin1 <leon2002.la@gmail.com> | 2021-09-20 23:43:52 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-11 09:19:17 +0100 |
commit | f76241914c8a2b860c1e6fd3d7172aa22e5b0d9d (patch) | |
tree | 94de3c373dd5a1e63c3da1e2a64a4c8dbc9ef73b | |
parent | 5137f96bd64783d816f4fe9477fcf7f278d32488 (diff) | |
download | serenity-f76241914c8a2b860c1e6fd3d7172aa22e5b0d9d.zip |
AK: Allow to clear HashTables/Maps with capacity
-rw-r--r-- | AK/HashMap.h | 1 | ||||
-rw-r--r-- | AK/HashTable.h | 15 |
2 files changed, 16 insertions, 0 deletions
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<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) |