2020-01-18 11:38:21 +03: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 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/StdLibExtras.h>
|
|
|
|
#include <AK/Vector.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-07-13 12:00:29 +03:00
|
|
|
template<typename K, typename V, typename KeyTraits = Traits<K>>
|
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:
|
2019-05-28 12:53:16 +03:00
|
|
|
HashMap() {}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-21 04:10:45 +03:00
|
|
|
bool is_empty() const { return m_table.is_empty(); }
|
2019-05-06 14:28:52 +03:00
|
|
|
int size() const { return m_table.size(); }
|
|
|
|
int 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
|
|
|
|
2019-06-27 15:19:50 +03: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 22:09:40 +03:00
|
|
|
void remove(const K& key)
|
|
|
|
{
|
|
|
|
auto it = find(key);
|
|
|
|
if (it != end())
|
|
|
|
m_table.remove(it);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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 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
|
|
|
|
2019-05-27 14:07:20 +03:00
|
|
|
void ensure_capacity(int capacity) { m_table.ensure_capacity(capacity); }
|
|
|
|
|
2019-08-14 12:47:38 +03:00
|
|
|
Optional<typename Traits<V>::PeekType> get(const K& key) const
|
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;
|