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