2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
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/StdLibExtras.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <AK/kstdio.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-07-13 09:00:29 +00:00
|
|
|
template<typename K, typename V, typename KeyTraits = Traits<K>>
|
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
|
|
|
static void dump(const Entry& entry)
|
|
|
|
{
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("key=");
|
2019-07-13 09:00:29 +00:00
|
|
|
KeyTraits::dump(entry.key);
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf(" value=");
|
2018-10-10 09:53:07 +00:00
|
|
|
Traits<V>::dump(entry.value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2019-05-28 09:53:16 +00:00
|
|
|
HashMap() {}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
bool is_empty() const { return m_table.is_empty(); }
|
2019-05-06 11:28:52 +00:00
|
|
|
int size() const { return m_table.size(); }
|
|
|
|
int 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
|
|
|
|
2019-06-27 12:19:50 +00:00
|
|
|
void set(const K& key, const V& value) { m_table.set({ key, value }); }
|
|
|
|
void set(const K& key, V&& value) { m_table.set({ key, move(value) }); }
|
2019-06-29 19:09:40 +00:00
|
|
|
void remove(const K& key)
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it != end())
|
|
|
|
m_table.remove(it);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
typedef HashTable<Entry, EntryTraits> HashTableType;
|
|
|
|
typedef typename HashTableType::Iterator IteratorType;
|
|
|
|
typedef typename HashTableType::ConstIterator ConstIteratorType;
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-05-27 11:07:20 +00:00
|
|
|
void ensure_capacity(int capacity) { m_table.ensure_capacity(capacity); }
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
void dump() const { m_table.dump(); }
|
|
|
|
|
2019-08-14 09:47:38 +00:00
|
|
|
Optional<typename Traits<V>::PeekType> get(const K& key) const
|
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;
|