2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2023-02-11 20:07:58 +00:00
|
|
|
* Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-15 00:22:08 +00:00
|
|
|
#include <AK/HashTable.h>
|
2019-07-24 08:25:43 +00:00
|
|
|
#include <AK/Optional.h>
|
2019-04-15 00:22:08 +00:00
|
|
|
#include <AK/Vector.h>
|
2021-09-19 22:25:22 +00:00
|
|
|
#include <initializer_list>
|
2020-12-28 11:49:16 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2023-09-20 22:14:35 +00: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 16:39:56 +00:00
|
|
|
template<typename K, typename V, typename KeyTraits, typename ValueTraits, bool IsOrdered>
|
2018-10-10 09:53:07 +00:00
|
|
|
class HashMap {
|
|
|
|
private:
|
|
|
|
struct Entry {
|
|
|
|
K key;
|
|
|
|
V value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EntryTraits {
|
2022-04-01 17:58:27 +00: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 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2021-05-18 14:13:34 +00:00
|
|
|
using KeyType = K;
|
|
|
|
using ValueType = V;
|
|
|
|
|
2021-01-10 23:29:28 +00:00
|
|
|
HashMap() = default;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2020-12-28 11:49:16 +00:00
|
|
|
HashMap(std::initializer_list<Entry> list)
|
|
|
|
{
|
2022-12-06 06:16:20 +00:00
|
|
|
MUST(try_ensure_capacity(list.size()));
|
2024-03-01 13:23:03 +00:00
|
|
|
for (auto& [key, value] : list)
|
|
|
|
set(key, value);
|
2020-12-28 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 21:39:33 +00: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 08:23:30 +00:00
|
|
|
[[nodiscard]] bool is_empty() const
|
2020-12-28 11:49:16 +00:00
|
|
|
{
|
|
|
|
return m_table.is_empty();
|
|
|
|
}
|
2021-04-11 08:23:30 +00:00
|
|
|
[[nodiscard]] size_t size() const { return m_table.size(); }
|
|
|
|
[[nodiscard]] size_t capacity() const { return m_table.capacity(); }
|
2018-10-25 10:35:49 +00:00
|
|
|
void clear() { m_table.clear(); }
|
2021-09-20 21:43:52 +00:00
|
|
|
void clear_with_capacity() { m_table.clear_with_capacity(); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2022-10-16 22:06:11 +00: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 10:34:23 +00:00
|
|
|
HashSetResult set(K&& key, V&& value) { return m_table.set({ move(key), move(value) }); }
|
2022-10-16 22:06:11 +00: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 10:34:23 +00:00
|
|
|
ErrorOr<HashSetResult> try_set(K&& key, V&& value) { return m_table.try_set({ move(key), move(value) }); }
|
2021-08-14 00:07:39 +00:00
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
bool remove(K const& key)
|
2019-06-29 19:09:40 +00:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
2020-07-06 21:44:33 +00:00
|
|
|
if (it != end()) {
|
2019-06-29 19:09:40 +00:00
|
|
|
m_table.remove(it);
|
2020-07-06 21:44:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-06-29 19:09:40 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-12-15 14:18:30 +00: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 15:53:24 +00:00
|
|
|
template<typename TUnaryPredicate>
|
2022-04-12 17:21:05 +00:00
|
|
|
bool remove_all_matching(TUnaryPredicate const& predicate)
|
2022-01-05 15:53:24 +00:00
|
|
|
{
|
2024-06-04 20:19:15 +00:00
|
|
|
return m_table.remove_all_matching([&](auto& entry) {
|
2022-03-06 18:11:17 +00:00
|
|
|
return predicate(entry.key, entry.value);
|
|
|
|
});
|
2022-01-05 15:53:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 14:26:08 +00:00
|
|
|
using HashTableType = HashTable<Entry, EntryTraits, IsOrdered>;
|
2020-11-11 22:21:01 +00:00
|
|
|
using IteratorType = typename HashTableType::Iterator;
|
|
|
|
using ConstIteratorType = typename HashTableType::ConstIterator;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-07-21 16:18:29 +00:00
|
|
|
[[nodiscard]] IteratorType begin() { return m_table.begin(); }
|
|
|
|
[[nodiscard]] IteratorType end() { return m_table.end(); }
|
2022-10-16 22:06:11 +00:00
|
|
|
[[nodiscard]] IteratorType find(K const& key)
|
2019-07-24 08:25:43 +00:00
|
|
|
{
|
2024-03-16 06:17:09 +00: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 14:38:11 +00:00
|
|
|
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(entry.key, key); });
|
2019-07-24 08:25:43 +00:00
|
|
|
}
|
2021-07-12 21:27:34 +00:00
|
|
|
template<typename TUnaryPredicate>
|
2021-07-21 16:18:29 +00:00
|
|
|
[[nodiscard]] IteratorType find(unsigned hash, TUnaryPredicate predicate)
|
2019-08-24 20:29:05 +00:00
|
|
|
{
|
2021-07-12 21:27:34 +00:00
|
|
|
return m_table.find(hash, predicate);
|
2019-08-24 20:29:05 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-07-21 16:18:29 +00:00
|
|
|
[[nodiscard]] ConstIteratorType begin() const { return m_table.begin(); }
|
|
|
|
[[nodiscard]] ConstIteratorType end() const { return m_table.end(); }
|
2022-10-16 22:06:11 +00:00
|
|
|
[[nodiscard]] ConstIteratorType find(K const& key) const
|
2019-07-24 08:25:43 +00:00
|
|
|
{
|
2024-03-16 06:17:09 +00: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 14:38:11 +00:00
|
|
|
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(entry.key, key); });
|
2019-07-24 08:25:43 +00:00
|
|
|
}
|
2021-07-12 21:27:34 +00:00
|
|
|
template<typename TUnaryPredicate>
|
2021-07-21 16:18:29 +00:00
|
|
|
[[nodiscard]] ConstIteratorType find(unsigned hash, TUnaryPredicate predicate) const
|
2019-08-24 20:29:05 +00:00
|
|
|
{
|
2021-07-12 21:27:34 +00:00
|
|
|
return m_table.find(hash, predicate);
|
2019-08-24 20:29:05 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-11-07 13:52:20 +00:00
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2022-01-29 18:01:35 +00:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] IteratorType find(Key const& key)
|
2021-11-07 13:52:20 +00:00
|
|
|
{
|
2024-03-16 06:17:09 +00: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 14:38:11 +00:00
|
|
|
return m_table.find(Traits<Key>::hash(key), [&](auto& entry) { return Traits<K>::equals(entry.key, key); });
|
2021-11-07 13:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2022-01-29 18:01:35 +00:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] ConstIteratorType find(Key const& key) const
|
2021-11-07 13:52:20 +00:00
|
|
|
{
|
2024-03-16 06:17:09 +00: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 14:38:11 +00:00
|
|
|
return m_table.find(Traits<Key>::hash(key), [&](auto& entry) { return Traits<K>::equals(entry.key, key); });
|
2021-11-07 13:52:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 22:00:21 +00:00
|
|
|
ErrorOr<void> try_ensure_capacity(size_t capacity) { return m_table.try_ensure_capacity(capacity); }
|
2019-05-27 11:07:20 +00:00
|
|
|
|
2024-03-06 01:07:48 +00:00
|
|
|
void ensure_capacity(size_t capacity) { return m_table.ensure_capacity(capacity); }
|
|
|
|
|
2022-12-09 16:39:56 +00:00
|
|
|
Optional<typename ValueTraits::ConstPeekType> get(K const& key) const
|
|
|
|
requires(!IsPointer<typename ValueTraits::PeekType>)
|
2021-05-08 09:12:32 +00:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:39:56 +00:00
|
|
|
Optional<typename ValueTraits::ConstPeekType> get(K const& key) const
|
|
|
|
requires(IsPointer<typename ValueTraits::PeekType>)
|
2021-05-08 09:12:32 +00:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:39:56 +00:00
|
|
|
Optional<typename ValueTraits::PeekType> get(K const& key)
|
|
|
|
requires(!IsConst<typename ValueTraits::PeekType>)
|
2018-12-31 14:10:12 +00:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
2019-07-24 08:25:43 +00:00
|
|
|
return {};
|
2018-12-31 14:10:12 +00:00
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
2021-11-07 13:52:20 +00:00
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2023-02-02 16:02:39 +00:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) Optional<typename ValueTraits::ConstPeekType> get(Key const& key) const
|
2022-12-09 16:39:56 +00:00
|
|
|
requires(!IsPointer<typename ValueTraits::PeekType>)
|
2021-11-07 13:52:20 +00:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2023-02-02 16:02:39 +00:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) Optional<typename ValueTraits::ConstPeekType> get(Key const& key) const
|
2022-12-09 16:39:56 +00:00
|
|
|
requires(IsPointer<typename ValueTraits::PeekType>)
|
2021-11-07 13:52:20 +00:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2022-12-09 16:39:56 +00:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) Optional<typename ValueTraits::PeekType> get(Key const& key)
|
|
|
|
requires(!IsConst<typename ValueTraits::PeekType>)
|
2021-11-07 13:52:20 +00:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
[[nodiscard]] bool contains(K const& key) const
|
2018-12-31 14:10:12 +00:00
|
|
|
{
|
|
|
|
return find(key) != end();
|
|
|
|
}
|
|
|
|
|
2021-11-07 13:52:20 +00:00
|
|
|
template<Concepts::HashCompatible<K> Key>
|
2023-03-03 09:27:50 +00:00
|
|
|
requires(IsSame<KeyTraits, Traits<K>>) [[nodiscard]] bool contains(Key const& value) const
|
2021-11-07 13:52:20 +00:00
|
|
|
{
|
|
|
|
return find(value) != end();
|
|
|
|
}
|
|
|
|
|
2022-03-06 18:14:29 +00:00
|
|
|
void remove(IteratorType it)
|
2019-02-01 02:50:06 +00:00
|
|
|
{
|
2022-03-06 18:14:29 +00:00
|
|
|
m_table.remove(it);
|
2019-02-01 02:50:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 15:55:50 +00: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 {};
|
|
|
|
}
|
|
|
|
|
2024-10-16 13:42:23 +00:00
|
|
|
V take_first()
|
|
|
|
requires(IsOrdered)
|
|
|
|
{
|
|
|
|
return take(begin()->key).release_value();
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
V& ensure(K const& key)
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
auto it = find(key);
|
2021-09-09 22:41:40 +00:00
|
|
|
if (it != end())
|
|
|
|
return it->value;
|
|
|
|
auto result = set(key, V());
|
|
|
|
VERIFY(result == HashSetResult::InsertedNewEntry);
|
2019-04-15 00:22:08 +00:00
|
|
|
return find(key)->value;
|
|
|
|
}
|
|
|
|
|
2021-09-04 15:27:59 +00:00
|
|
|
template<typename Callback>
|
|
|
|
V& ensure(K const& key, Callback initialization_callback)
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
2021-09-09 22:41:40 +00:00
|
|
|
if (it != end())
|
|
|
|
return it->value;
|
|
|
|
auto result = set(key, initialization_callback());
|
|
|
|
VERIFY(result == HashSetResult::InsertedNewEntry);
|
2021-09-04 15:27:59 +00:00
|
|
|
return find(key)->value;
|
|
|
|
}
|
|
|
|
|
2022-12-11 02:03:02 +00: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-24 00:22:31 +00: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-11 02:03:02 +00:00
|
|
|
return find(key)->value;
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:18:29 +00:00
|
|
|
[[nodiscard]] Vector<K> keys() const
|
2019-04-15 00:22:08 +00:00
|
|
|
{
|
|
|
|
Vector<K> list;
|
|
|
|
list.ensure_capacity(size());
|
2024-03-01 13:23:03 +00:00
|
|
|
for (auto const& [key, _] : *this)
|
|
|
|
list.unchecked_append(key);
|
2019-04-15 00:22:08 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-09-12 03:17:40 +00:00
|
|
|
[[nodiscard]] u32 hash() const
|
|
|
|
{
|
|
|
|
u32 hash = 0;
|
2024-03-01 13:23:03 +00:00
|
|
|
for (auto const& [key, value] : *this) {
|
|
|
|
auto entry_hash = pair_int_hash(key.hash(), value.hash());
|
2021-09-12 03:17:40 +00:00
|
|
|
hash = pair_int_hash(hash, entry_hash);
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2023-05-13 18:25:22 +00:00
|
|
|
template<typename NewKeyTraits = KeyTraits, typename NewValueTraits = ValueTraits, bool NewIsOrdered = IsOrdered>
|
|
|
|
ErrorOr<HashMap<K, V, NewKeyTraits, NewValueTraits, NewIsOrdered>> clone() const
|
2023-02-11 20:07:58 +00:00
|
|
|
{
|
2023-05-13 18:25:22 +00:00
|
|
|
HashMap<K, V, NewKeyTraits, NewValueTraits, NewIsOrdered> hash_map_clone;
|
2024-03-01 13:23:03 +00:00
|
|
|
TRY(hash_map_clone.try_ensure_capacity(size()));
|
|
|
|
for (auto const& [key, value] : *this)
|
|
|
|
hash_map_clone.set(key, value);
|
2023-02-11 20:07:58 +00:00
|
|
|
return hash_map_clone;
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
2019-06-27 12:19:50 +00:00
|
|
|
HashTableType m_table;
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2018-10-10 09:53:07 +00:00
|
|
|
using AK::HashMap;
|
2021-06-15 20:59:39 +00:00
|
|
|
using AK::OrderedHashMap;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|