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