2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
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>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-12-28 14:49:16 +03: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 12:53:07 +03:00
|
|
|
namespace AK {
|
|
|
|
|
2020-02-16 04:01:18 +03:00
|
|
|
template<typename K, typename V, typename KeyTraits>
|
2018-10-10 12:53:07 +03:00
|
|
|
class HashMap {
|
|
|
|
private:
|
|
|
|
struct Entry {
|
|
|
|
K key;
|
|
|
|
V value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EntryTraits {
|
2019-07-13 12:00:29 +03: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 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
|
|
|
#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 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(); }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-07-07 00:44:33 +03: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 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
|
|
|
}
|
2019-01-31 19:31:23 +03:00
|
|
|
void remove_one_randomly() { m_table.remove(m_table.begin()); }
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-11-12 01:21:01 +03:00
|
|
|
using HashTableType = HashTable<Entry, EntryTraits>;
|
|
|
|
using IteratorType = typename HashTableType::Iterator;
|
|
|
|
using ConstIteratorType = typename HashTableType::ConstIterator;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
IteratorType begin() { return m_table.begin(); }
|
|
|
|
IteratorType end() { return m_table.end(); }
|
2019-07-24 11:25:43 +03:00
|
|
|
IteratorType find(const K& key)
|
|
|
|
{
|
|
|
|
return m_table.find(KeyTraits::hash(key), [&](auto& entry) { return KeyTraits::equals(key, entry.key); });
|
|
|
|
}
|
2019-08-24 23:29:05 +03:00
|
|
|
template<typename Finder>
|
|
|
|
IteratorType find(unsigned hash, Finder finder)
|
|
|
|
{
|
|
|
|
return m_table.find(hash, finder);
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
ConstIteratorType begin() const { return m_table.begin(); }
|
|
|
|
ConstIteratorType end() const { return m_table.end(); }
|
2019-07-24 11:25:43 +03: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 23:29:05 +03:00
|
|
|
template<typename Finder>
|
|
|
|
ConstIteratorType find(unsigned hash, Finder finder) const
|
|
|
|
{
|
|
|
|
return m_table.find(hash, finder);
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-02-24 11:42:52 +03:00
|
|
|
void ensure_capacity(size_t capacity) { m_table.ensure_capacity(capacity); }
|
2019-05-27 14:07:20 +03:00
|
|
|
|
2021-05-08 12:12:32 +03: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 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(const K& key) const
|
|
|
|
{
|
|
|
|
return find(key) != end();
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:50:06 +03:00
|
|
|
void remove(IteratorType it)
|
|
|
|
{
|
|
|
|
m_table.remove(it);
|
|
|
|
}
|
|
|
|
|
2019-04-15 03:22:08 +03: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 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
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::HashMap;
|