2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
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>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2020-12-28 11:49:16 +00:00
|
|
|
// NOTE: We can't include <initializer_list> during the toolchain bootstrap,
|
|
|
|
// since it's part of libstdc++, and libstdc++ depends on LibC.
|
|
|
|
// For this reason, we don't support HashMap(initializer_list) in LibC.
|
|
|
|
#ifndef SERENITY_LIBC_BUILD
|
|
|
|
# include <initializer_list>
|
|
|
|
#endif
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2021-06-13 14:26:08 +00:00
|
|
|
template<typename K, typename V, typename KeyTraits, bool IsOrdered>
|
2018-10-10 09:53:07 +00:00
|
|
|
class HashMap {
|
|
|
|
private:
|
|
|
|
struct Entry {
|
|
|
|
K key;
|
|
|
|
V value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EntryTraits {
|
2019-07-13 09:00:29 +00:00
|
|
|
static unsigned hash(const Entry& entry) { return KeyTraits::hash(entry.key); }
|
|
|
|
static bool equals(const Entry& a, const Entry& 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
|
|
|
#ifndef SERENITY_LIBC_BUILD
|
|
|
|
HashMap(std::initializer_list<Entry> list)
|
|
|
|
{
|
|
|
|
ensure_capacity(list.size());
|
|
|
|
for (auto& item : list)
|
|
|
|
set(item.key, item.value);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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(); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2020-07-06 21:44:33 +00:00
|
|
|
HashSetResult set(const K& key, const V& value) { return m_table.set({ key, value }); }
|
|
|
|
HashSetResult set(const K& key, V&& value) { return m_table.set({ key, move(value) }); }
|
|
|
|
bool remove(const K& 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
|
|
|
}
|
2019-01-31 16:31:23 +00:00
|
|
|
void remove_one_randomly() { m_table.remove(m_table.begin()); }
|
2018-10-10 09:53:07 +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
|
|
|
|
|
|
|
IteratorType begin() { return m_table.begin(); }
|
|
|
|
IteratorType end() { return m_table.end(); }
|
2019-07-24 08:25:43 +00:00
|
|
|
IteratorType find(const K& key)
|
|
|
|
{
|
|
|
|
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); });
|
|
|
|
}
|
2019-08-24 20:29:05 +00:00
|
|
|
template<typename Finder>
|
|
|
|
IteratorType find(unsigned hash, Finder finder)
|
|
|
|
{
|
|
|
|
return m_table.find(hash, finder);
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
ConstIteratorType begin() const { return m_table.begin(); }
|
|
|
|
ConstIteratorType end() const { return m_table.end(); }
|
2019-07-24 08:25:43 +00:00
|
|
|
ConstIteratorType find(const K& key) const
|
|
|
|
{
|
|
|
|
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); });
|
|
|
|
}
|
2019-08-24 20:29:05 +00:00
|
|
|
template<typename Finder>
|
|
|
|
ConstIteratorType find(unsigned hash, Finder finder) const
|
|
|
|
{
|
|
|
|
return m_table.find(hash, finder);
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2020-02-24 08:42:52 +00:00
|
|
|
void ensure_capacity(size_t capacity) { m_table.ensure_capacity(capacity); }
|
2019-05-27 11:07:20 +00:00
|
|
|
|
2021-05-08 09:12:32 +00:00
|
|
|
Optional<typename Traits<V>::PeekType> get(const K& key) const requires(!IsPointer<typename Traits<V>::PeekType>)
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<typename Traits<V>::ConstPeekType> get(const K& key) const requires(IsPointer<typename Traits<V>::PeekType>)
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
return {};
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<typename Traits<V>::PeekType> get(const K& key) requires(!IsConst<typename Traits<V>::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(const K& key) const
|
|
|
|
{
|
|
|
|
return find(key) != end();
|
|
|
|
}
|
|
|
|
|
2019-02-01 02:50:06 +00:00
|
|
|
void remove(IteratorType it)
|
|
|
|
{
|
|
|
|
m_table.remove(it);
|
|
|
|
}
|
|
|
|
|
2019-04-15 00:22:08 +00:00
|
|
|
V& ensure(const K& key)
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it == end())
|
|
|
|
set(key, V());
|
|
|
|
return find(key)->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<K> keys() const
|
|
|
|
{
|
|
|
|
Vector<K> list;
|
|
|
|
list.ensure_capacity(size());
|
|
|
|
for (auto& it : *this)
|
|
|
|
list.unchecked_append(it.key);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::HashMap;
|