2020-01-18 03:38:21 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2023-02-11 15:07:58 -05:00
|
|
|
* Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2020-01-18 03:38:21 -05:00
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 03:38:21 -05:00
|
|
|
*/
|
|
|
|
|
2018-10-10 05:53:07 -04:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-14 20:22:08 -04:00
|
|
|
#include <AK/HashTable.h>
|
2019-07-24 04:25:43 -04:00
|
|
|
#include <AK/Optional.h>
|
2019-04-14 20:22:08 -04:00
|
|
|
#include <AK/Vector.h>
|
2021-09-19 18:25:22 -04:00
|
|
|
#include <initializer_list>
|
2020-12-28 06:49:16 -05:00
|
|
|
|
2018-10-10 05:53:07 -04:00
|
|
|
namespace AK {
|
|
|
|
|
2023-09-20 18:14:35 -04:00
|
|
|
// A map datastructure, mapping keys K to values V, based on a hash table with closed hashing.
|
|
|
|
// HashMap can optionally provide ordered iteration based on the order of keys when IsOrdered = true.
|
|
|
|
// HashMap is based on HashTable, which should be used instead if just a set datastructure is required.
|
2022-12-09 11:39:56 -05:00
|
|
|
template<typename K, typename V, typename KeyTraits, typename ValueTraits, bool IsOrdered>
|
2018-10-10 05:53:07 -04:00
|
|
|
class HashMap {
|
|
|
|
private:
|
|
|
|
struct Entry {
|
|
|
|
K key;
|
|
|
|
V value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EntryTraits {
|
2022-04-01 13:58:27 -04:00
|
|
|
static unsigned hash(Entry const& entry) { return KeyTraits::hash(entry.key); }
|
|
|
|
static bool equals(Entry const& a, Entry const& b) { return KeyTraits::equals(a.key, b.key); }
|
2018-10-10 05:53:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2021-05-18 10:13:34 -04:00
|
|
|
using KeyType = K;
|
|
|
|
using ValueType = V;
|
|
|
|
|
2021-01-10 18:29:28 -05:00
|
|
|
HashMap() = default;
|
2018-10-10 05:53:07 -04:00
|
|
|
|
2020-12-28 06:49:16 -05:00
|
|
|
HashMap(std::initializer_list<Entry> list)
|
|
|
|
{
|
2022-12-06 01:16:20 -05:00
|
|
|
MUST(try_ensure_capacity(list.size()));
|
2024-03-01 08:23:03 -05:00
|
|
|
for (auto& [key, value] : list)
|
|
|
|
set(key, value);
|
2020-12-28 06:49:16 -05:00
|
|
|
}
|
|
|
|
|
2023-05-07 17:39:33 -04:00
|
|
|
HashMap(HashMap const&) = default; // FIXME: Not OOM-safe! Use clone() instead.
|
|
|
|
HashMap(HashMap&& other) noexcept = default;
|
|
|
|
HashMap& operator=(HashMap const& other) = default; // FIXME: Not OOM-safe! Use clone() instead.
|
|
|
|
HashMap& operator=(HashMap&& other) noexcept = default;
|
|
|
|
|
2021-04-11 04:23:30 -04:00
|
|
|
[[nodiscard]] bool is_empty() const
|
2020-12-28 06:49:16 -05:00
|
|
|
{
|
|
|
|
return m_table.is_empty();
|
|
|
|
}
|
2021-04-11 04:23:30 -04:00
|
|
|
[[nodiscard]] size_t size() const { return m_table.size(); }
|
|
|
|
[[nodiscard]] size_t capacity() const { return m_table.capacity(); }
|
2018-10-25 06:35:49 -04:00
|
|
|
void clear() { m_table.clear(); }
|
2021-09-20 17:43:52 -04:00
|
|
|
void clear_with_capacity() { m_table.clear_with_capacity(); }
|
2018-10-10 05:53:07 -04:00
|
|
|
|
2022-10-16 18:06:11 -04:00
|
|
|
HashSetResult set(K const& key, V const& value) { return m_table.set({ key, value }); }
|
|
|
|
HashSetResult set(K const& key, V&& value) { return m_table.set({ key, move(value) }); }
|
2022-01-21 05:34:23 -05:00
|
|
|
HashSetResult set(K&& key, V&& value) { return m_table.set({ move(key), move(value) }); }
|
2022-10-16 18:06:11 -04:00
|
|
|
ErrorOr<HashSetResult> try_set(K const& key, V const& value) { return m_table.try_set({ key, value }); }
|
|
|
|
ErrorOr<HashSetResult> try_set(K const& key, V&& value) { return m_table.try_set({ key, move(value) }); }
|
2022-01-21 05:34:23 -05:00
|
|
|
ErrorOr<HashSetResult> try_set(K&& key, V&& value) { return m_table.try_set({ move(key), move(value) }); }
|
2021-08-13 20:07:39 -04:00
|
|
|
|
2022-10-16 18:06:11 -04:00
|
|
|
bool remove(K const& key)
|
2019-06-29 15:09:40 -04:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
2020-07-06 17:44:33 -04:00
|
|
|
if (it != end()) {
|
2019-06-29 15:09:40 -04:00
|
|
|
m_table.remove(it);
|
2020-07-06 17:44:33 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-06-29 15:09:40 -04:00
|
|
|
}
|
2018-10-10 05:53:07 -04:00
|
|
|
|
2021-12-15 09:18:30 -05:00
|
|
|
template<Concepts::HashCompatible<K> Key>
|
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) bool remove(Key const& key)
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it != end()) {
|
|
|
|
m_table.remove(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-05 10:53:24 -05:00
|
|
|
template<typename TUnaryPredicate>
|
2022-04-12 13:21:05 -04:00
|
|
|
bool remove_all_matching(TUnaryPredicate const& predicate)
|
2022-01-05 10:53:24 -05:00
|
|
|
{
|
2024-06-04 16:19:15 -04:00
|
|
|
return m_table.remove_all_matching([&](auto& entry) {
|
2022-03-06 13:11:17 -05:00
|
|
|
return predicate(entry.key, entry.value);
|
|
|
|
});
|
2022-01-05 10:53:24 -05:00
|
|
|
}
|
|
|
|
|
2021-06-13 10:26:08 -04:00
|
|
|
using HashTableType = HashTable<Entry, EntryTraits, IsOrdered>;
|
2020-11-11 17:21:01 -05:00
|
|
|
using IteratorType = typename HashTableType::Iterator;
|
|
|
|
using ConstIteratorType = typename HashTableType::ConstIterator;
|
2018-10-10 05:53:07 -04:00
|
|
|
|
2021-07-21 12:18:29 -04:00
|
|
|
[[nodiscard]] IteratorType begin() { return m_table.begin(); }
|
|
|
|
[[nodiscard]] IteratorType end() { return m_table.end(); }
|
2022-10-16 18:06:11 -04:00
|
|
|
[[nodiscard]] IteratorType find(K const& key)
|
2019-07-24 04:25:43 -04:00
|
|
|
{
|
2024-03-16 02:17:09 -04:00
|
|
|
if (m_table.is_empty())
|
|
|
|
return m_table.end();
|
AK+Kernel: Unify Traits<T>::equals()'s argument order on different types
There was a small mishmash of argument order, as seen on the table:
| Traits<T>::equals(U, T) | Traits<T>::equals(T, U)
============= | ======================= | =======================
uses equals() | HashMap | Vector, HashTable
defines equals() | *String[^1] | ByteBuffer
[^1]: String, DeprecatedString, their Fly-type equivalents and KString.
This mostly meant that you couldn't use a StringView for finding a value
in Vector<String>.
I'm changing the order of arguments to make the trait type itself first
(`Traits<T>::equals(T, U)`), as I think it's more expected and makes us
more consistent with the rest of the functions that put the stored type
first (like StringUtils functions and binary_serach). I've also renamed
the variable name "other" in find functions to "entry" to give more
importance to the value.
With this change, each of the following lines will now compile
successfully:
Vector<String>().contains_slow("WHF!"sv);
HashTable<String>().contains("WHF!"sv);
HashMap<ByteBuffer, int>().contains("WHF!"sv.bytes());
2023-08-21 10:38:11 -04:00
|
|
|
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(entry.key, key); });
|
2019-07-24 04:25:43 -04:00
|
|
|
}
|
2021-07-12 17:27:34 -04:00
|
|
|
template<typename TUnaryPredicate>
|
2021-07-21 12:18:29 -04:00
|
|
|
[[nodiscard]] IteratorType find(unsigned hash, TUnaryPredicate predicate)
|
2019-08-24 16:29:05 -04:00
|
|
|
{
|
2021-07-12 17:27:34 -04:00
|
|
|
return m_table.find(hash, predicate);
|
2019-08-24 16:29:05 -04:00
|
|
|
}
|
2018-10-10 05:53:07 -04:00
|
|
|
|
2021-07-21 12:18:29 -04:00
|
|
|
[[nodiscard]] ConstIteratorType begin() const { return m_table.begin(); }
|
|
|
|
[[nodiscard]] ConstIteratorType end() const { return m_table.end(); }
|
2022-10-16 18:06:11 -04:00
|
|
|
[[nodiscard]] ConstIteratorType find(K const& key) const
|
2019-07-24 04:25:43 -04:00
|
|
|
{
|
2024-03-16 02:17:09 -04:00
|
|
|
if (m_table.is_empty())
|
|
|
|
return m_table.end();
|
AK+Kernel: Unify Traits<T>::equals()'s argument order on different types
There was a small mishmash of argument order, as seen on the table:
| Traits<T>::equals(U, T) | Traits<T>::equals(T, U)
============= | ======================= | =======================
uses equals() | HashMap | Vector, HashTable
defines equals() | *String[^1] | ByteBuffer
[^1]: String, DeprecatedString, their Fly-type equivalents and KString.
This mostly meant that you couldn't use a StringView for finding a value
in Vector<String>.
I'm changing the order of arguments to make the trait type itself first
(`Traits<T>::equals(T, U)`), as I think it's more expected and makes us
more consistent with the rest of the functions that put the stored type
first (like StringUtils functions and binary_serach). I've also renamed
the variable name "other" in find functions to "entry" to give more
importance to the value.
With this change, each of the following lines will now compile
successfully:
Vector<String>().contains_slow("WHF!"sv);
HashTable<String>().contains("WHF!"sv);
HashMap<ByteBuffer, int>().contains("WHF!"sv.bytes());
2023-08-21 10:38:11 -04:00
|
|
|
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(entry.key, key); });
|
2019-07-24 04:25:43 -04:00
|
|
|
}
|
2021-07-12 17:27:34 -04:00
|
|
|
template<typename TUnaryPredicate>
|
2021-07-21 12:18:29 -04:00
|
|
|
[[nodiscard]] ConstIteratorType find(unsigned hash, TUnaryPredicate predicate) const
|
2019-08-24 16:29:05 -04:00
|
|
|
{
|
2021-07-12 17:27:34 -04:00
|
|
|
return m_table.find(hash, predicate);
|
2019-08-24 16:29:05 -04:00
|
|
|
}
|
2018-10-10 05:53:07 -04:00
|
|
|
|
2021-11-07 08:52:20 -05:00
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2022-01-29 13:01:35 -05:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] IteratorType find(Key const& key)
|
2021-11-07 08:52:20 -05:00
|
|
|
{
|
2024-03-16 02:17:09 -04:00
|
|
|
if (m_table.is_empty())
|
|
|
|
return m_table.end();
|
AK+Kernel: Unify Traits<T>::equals()'s argument order on different types
There was a small mishmash of argument order, as seen on the table:
| Traits<T>::equals(U, T) | Traits<T>::equals(T, U)
============= | ======================= | =======================
uses equals() | HashMap | Vector, HashTable
defines equals() | *String[^1] | ByteBuffer
[^1]: String, DeprecatedString, their Fly-type equivalents and KString.
This mostly meant that you couldn't use a StringView for finding a value
in Vector<String>.
I'm changing the order of arguments to make the trait type itself first
(`Traits<T>::equals(T, U)`), as I think it's more expected and makes us
more consistent with the rest of the functions that put the stored type
first (like StringUtils functions and binary_serach). I've also renamed
the variable name "other" in find functions to "entry" to give more
importance to the value.
With this change, each of the following lines will now compile
successfully:
Vector<String>().contains_slow("WHF!"sv);
HashTable<String>().contains("WHF!"sv);
HashMap<ByteBuffer, int>().contains("WHF!"sv.bytes());
2023-08-21 10:38:11 -04:00
|
|
|
return m_table.find(Traits<Key>::hash(key), [&](auto& entry) { return Traits<K>::equals(entry.key, key); });
|
2021-11-07 08:52:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2022-01-29 13:01:35 -05:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] ConstIteratorType find(Key const& key) const
|
2021-11-07 08:52:20 -05:00
|
|
|
{
|
2024-03-16 02:17:09 -04:00
|
|
|
if (m_table.is_empty())
|
|
|
|
return m_table.end();
|
AK+Kernel: Unify Traits<T>::equals()'s argument order on different types
There was a small mishmash of argument order, as seen on the table:
| Traits<T>::equals(U, T) | Traits<T>::equals(T, U)
============= | ======================= | =======================
uses equals() | HashMap | Vector, HashTable
defines equals() | *String[^1] | ByteBuffer
[^1]: String, DeprecatedString, their Fly-type equivalents and KString.
This mostly meant that you couldn't use a StringView for finding a value
in Vector<String>.
I'm changing the order of arguments to make the trait type itself first
(`Traits<T>::equals(T, U)`), as I think it's more expected and makes us
more consistent with the rest of the functions that put the stored type
first (like StringUtils functions and binary_serach). I've also renamed
the variable name "other" in find functions to "entry" to give more
importance to the value.
With this change, each of the following lines will now compile
successfully:
Vector<String>().contains_slow("WHF!"sv);
HashTable<String>().contains("WHF!"sv);
HashMap<ByteBuffer, int>().contains("WHF!"sv.bytes());
2023-08-21 10:38:11 -04:00
|
|
|
return m_table.find(Traits<Key>::hash(key), [&](auto& entry) { return Traits<K>::equals(entry.key, key); });
|
2021-11-07 08:52:20 -05:00
|
|
|
}
|
|
|
|
|
2021-11-10 17:00:21 -05:00
|
|
|
ErrorOr<void> try_ensure_capacity(size_t capacity) { return m_table.try_ensure_capacity(capacity); }
|
2019-05-27 07:07:20 -04:00
|
|
|
|
2024-03-05 20:07:48 -05:00
|
|
|
void ensure_capacity(size_t capacity) { return m_table.ensure_capacity(capacity); }
|
|
|
|
|
2022-12-09 11:39:56 -05:00
|
|
|
Optional<typename ValueTraits::ConstPeekType> get(K const& key) const
|
|
|
|
requires(!IsPointer<typename ValueTraits::PeekType>)
|
2021-05-08 05:12:32 -04:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
2022-12-09 11:39:56 -05:00
|
|
|
Optional<typename ValueTraits::ConstPeekType> get(K const& key) const
|
|
|
|
requires(IsPointer<typename ValueTraits::PeekType>)
|
2021-05-08 05:12:32 -04:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
2022-12-09 11:39:56 -05:00
|
|
|
Optional<typename ValueTraits::PeekType> get(K const& key)
|
|
|
|
requires(!IsConst<typename ValueTraits::PeekType>)
|
2018-12-31 09:10:12 -05:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
2019-07-24 04:25:43 -04:00
|
|
|
return {};
|
2018-12-31 09:10:12 -05:00
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
2021-11-07 08:52:20 -05:00
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2023-02-02 11:02:39 -05:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) Optional<typename ValueTraits::ConstPeekType> get(Key const& key) const
|
2022-12-09 11:39:56 -05:00
|
|
|
requires(!IsPointer<typename ValueTraits::PeekType>)
|
2021-11-07 08:52:20 -05:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2023-02-02 11:02:39 -05:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) Optional<typename ValueTraits::ConstPeekType> get(Key const& key) const
|
2022-12-09 11:39:56 -05:00
|
|
|
requires(IsPointer<typename ValueTraits::PeekType>)
|
2021-11-07 08:52:20 -05:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2022-12-09 11:39:56 -05:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) Optional<typename ValueTraits::PeekType> get(Key const& key)
|
|
|
|
requires(!IsConst<typename ValueTraits::PeekType>)
|
2021-11-07 08:52:20 -05:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
2022-10-16 18:06:11 -04:00
|
|
|
[[nodiscard]] bool contains(K const& key) const
|
2018-12-31 09:10:12 -05:00
|
|
|
{
|
|
|
|
return find(key) != end();
|
|
|
|
}
|
|
|
|
|
2021-11-07 08:52:20 -05:00
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2023-03-03 04:27:50 -05:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] bool contains(Key const& value) const
|
2021-11-07 08:52:20 -05:00
|
|
|
{
|
|
|
|
return find(value) != end();
|
|
|
|
}
|
|
|
|
|
2022-03-06 13:14:29 -05:00
|
|
|
void remove(IteratorType it)
|
2019-01-31 21:50:06 -05:00
|
|
|
{
|
2022-03-06 13:14:29 -05:00
|
|
|
m_table.remove(it);
|
2019-01-31 21:50:06 -05:00
|
|
|
}
|
|
|
|
|
2023-02-02 10:55:50 -05:00
|
|
|
Optional<V> take(K const& key)
|
|
|
|
{
|
|
|
|
if (auto it = find(key); it != end()) {
|
|
|
|
auto value = move(it->value);
|
|
|
|
m_table.remove(it);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::HashCompatible<K> Key>
|
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) Optional<V> take(Key const& key)
|
|
|
|
{
|
|
|
|
if (auto it = find(key); it != end()) {
|
|
|
|
auto value = move(it->value);
|
|
|
|
m_table.remove(it);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-10-16 18:06:11 -04:00
|
|
|
V& ensure(K const& key)
|
2019-04-14 20:22:08 -04:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
2021-09-09 18:41:40 -04:00
|
|
|
if (it != end())
|
|
|
|
return it->value;
|
|
|
|
auto result = set(key, V());
|
|
|
|
VERIFY(result == HashSetResult::InsertedNewEntry);
|
2019-04-14 20:22:08 -04:00
|
|
|
return find(key)->value;
|
|
|
|
}
|
|
|
|
|
2021-09-04 11:27:59 -04:00
|
|
|
template<typename Callback>
|
|
|
|
V& ensure(K const& key, Callback initialization_callback)
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
2021-09-09 18:41:40 -04:00
|
|
|
if (it != end())
|
|
|
|
return it->value;
|
|
|
|
auto result = set(key, initialization_callback());
|
|
|
|
VERIFY(result == HashSetResult::InsertedNewEntry);
|
2021-09-04 11:27:59 -04:00
|
|
|
return find(key)->value;
|
|
|
|
}
|
|
|
|
|
2022-12-10 21:03:02 -05:00
|
|
|
template<typename Callback>
|
|
|
|
ErrorOr<V> try_ensure(K const& key, Callback initialization_callback)
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it != end())
|
|
|
|
return it->value;
|
2023-01-23 19:22:31 -05:00
|
|
|
if constexpr (FallibleFunction<Callback>) {
|
|
|
|
auto result = TRY(try_set(key, TRY(initialization_callback())));
|
|
|
|
VERIFY(result == HashSetResult::InsertedNewEntry);
|
|
|
|
} else {
|
|
|
|
auto result = TRY(try_set(key, initialization_callback()));
|
|
|
|
VERIFY(result == HashSetResult::InsertedNewEntry);
|
|
|
|
}
|
2022-12-10 21:03:02 -05:00
|
|
|
return find(key)->value;
|
|
|
|
}
|
|
|
|
|
2021-07-21 12:18:29 -04:00
|
|
|
[[nodiscard]] Vector<K> keys() const
|
2019-04-14 20:22:08 -04:00
|
|
|
{
|
|
|
|
Vector<K> list;
|
|
|
|
list.ensure_capacity(size());
|
2024-03-01 08:23:03 -05:00
|
|
|
for (auto const& [key, _] : *this)
|
|
|
|
list.unchecked_append(key);
|
2019-04-14 20:22:08 -04:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-09-11 23:17:40 -04:00
|
|
|
[[nodiscard]] u32 hash() const
|
|
|
|
{
|
|
|
|
u32 hash = 0;
|
2024-03-01 08:23:03 -05:00
|
|
|
for (auto const& [key, value] : *this) {
|
|
|
|
auto entry_hash = pair_int_hash(key.hash(), value.hash());
|
2021-09-11 23:17:40 -04:00
|
|
|
hash = pair_int_hash(hash, entry_hash);
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2023-05-13 14:25:22 -04:00
|
|
|
template<typename NewKeyTraits = KeyTraits, typename NewValueTraits = ValueTraits, bool NewIsOrdered = IsOrdered>
|
|
|
|
ErrorOr<HashMap<K, V, NewKeyTraits, NewValueTraits, NewIsOrdered>> clone() const
|
2023-02-11 15:07:58 -05:00
|
|
|
{
|
2023-05-13 14:25:22 -04:00
|
|
|
HashMap<K, V, NewKeyTraits, NewValueTraits, NewIsOrdered> hash_map_clone;
|
2024-03-01 08:23:03 -05:00
|
|
|
TRY(hash_map_clone.try_ensure_capacity(size()));
|
|
|
|
for (auto const& [key, value] : *this)
|
|
|
|
hash_map_clone.set(key, value);
|
2023-02-11 15:07:58 -05:00
|
|
|
return hash_map_clone;
|
|
|
|
}
|
|
|
|
|
2018-10-10 05:53:07 -04:00
|
|
|
private:
|
2019-06-27 08:19:50 -04:00
|
|
|
HashTableType m_table;
|
2018-10-10 05:53:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 06:18:30 -05:00
|
|
|
#if USING_AK_GLOBALLY
|
2018-10-10 05:53:07 -04:00
|
|
|
using AK::HashMap;
|
2021-06-15 16:59:39 -04:00
|
|
|
using AK::OrderedHashMap;
|
2022-11-26 06:18:30 -05:00
|
|
|
#endif
|