2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.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 {
|
|
|
|
|
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()));
|
2020-12-28 11:49:16 +00:00
|
|
|
for (auto& item : list)
|
|
|
|
set(item.key, item.value);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-03-06 18:11:17 +00:00
|
|
|
return m_table.template remove_all_matching([&](auto& entry) {
|
|
|
|
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
|
|
|
{
|
|
|
|
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); });
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); });
|
|
|
|
}
|
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
|
|
|
{
|
2022-01-29 18:01:35 +00:00
|
|
|
return m_table.find(Traits<Key>::hash(key), [&](auto& entry) { return Traits<K>::equals(key, entry.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
|
|
|
{
|
2022-01-29 18:01:35 +00:00
|
|
|
return m_table.find(Traits<Key>::hash(key), [&](auto& entry) { return Traits<K>::equals(key, entry.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
|
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
for (auto& it : *this)
|
|
|
|
list.unchecked_append(it.key);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-09-12 03:17:40 +00: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 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;
|
2023-02-11 20:07:58 +00:00
|
|
|
for (auto& it : *this)
|
|
|
|
TRY(hash_map_clone.try_set(it.key, it.value));
|
|
|
|
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
|