2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-06-27 14:36:31 +00:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/SinglyLinkedList.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
#include <AK/Traits.h>
|
|
|
|
#include <AK/kstdio.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename T, typename = Traits<T>>
|
|
|
|
class HashTable;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-06-27 13:57:49 +00:00
|
|
|
template<typename HashTableType, typename ElementType, typename BucketIteratorType>
|
|
|
|
class HashTableIterator {
|
|
|
|
public:
|
|
|
|
bool operator!=(const HashTableIterator& other) const
|
|
|
|
{
|
|
|
|
if (m_is_end && other.m_is_end)
|
|
|
|
return false;
|
|
|
|
return &m_table != &other.m_table
|
|
|
|
|| m_is_end != other.m_is_end
|
|
|
|
|| m_bucket_index != other.m_bucket_index
|
|
|
|
|| m_bucket_iterator != other.m_bucket_iterator;
|
|
|
|
}
|
|
|
|
bool operator==(const HashTableIterator& other) const { return !(*this != other); }
|
|
|
|
ElementType& operator*() { return *m_bucket_iterator; }
|
|
|
|
ElementType* operator->() { return m_bucket_iterator.operator->(); }
|
|
|
|
HashTableIterator& operator++()
|
|
|
|
{
|
|
|
|
skip_to_next();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void skip_to_next()
|
|
|
|
{
|
|
|
|
while (!m_is_end) {
|
|
|
|
if (m_bucket_iterator.is_end()) {
|
|
|
|
++m_bucket_index;
|
|
|
|
if (m_bucket_index >= m_table.capacity()) {
|
|
|
|
m_is_end = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_bucket_iterator = m_table.bucket(m_bucket_index).begin();
|
|
|
|
} else {
|
|
|
|
++m_bucket_iterator;
|
|
|
|
}
|
|
|
|
if (!m_bucket_iterator.is_end())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend HashTableType;
|
|
|
|
|
|
|
|
explicit HashTableIterator(HashTableType& table, bool is_end, BucketIteratorType bucket_iterator = BucketIteratorType::universal_end(), int bucket_index = 0)
|
|
|
|
: m_table(table)
|
|
|
|
, m_bucket_index(bucket_index)
|
|
|
|
, m_is_end(is_end)
|
|
|
|
, m_bucket_iterator(bucket_iterator)
|
|
|
|
{
|
|
|
|
if (!is_end && !m_table.is_empty() && !(m_bucket_iterator != BucketIteratorType::universal_end())) {
|
|
|
|
m_bucket_iterator = m_table.bucket(0).begin();
|
|
|
|
if (m_bucket_iterator.is_end())
|
|
|
|
skip_to_next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTableType& m_table;
|
|
|
|
int m_bucket_index { 0 };
|
|
|
|
bool m_is_end { false };
|
|
|
|
BucketIteratorType m_bucket_iterator;
|
|
|
|
};
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
class HashTable {
|
|
|
|
private:
|
2019-06-27 14:36:31 +00:00
|
|
|
using Bucket = SinglyLinkedList<T>;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
public:
|
2019-05-28 09:53:16 +00:00
|
|
|
HashTable() {}
|
2019-06-24 09:57:54 +00:00
|
|
|
HashTable(const HashTable& other)
|
|
|
|
{
|
|
|
|
ensure_capacity(other.size());
|
|
|
|
for (auto& it : other)
|
|
|
|
set(it);
|
|
|
|
}
|
|
|
|
HashTable& operator=(const HashTable& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
ensure_capacity(other.size());
|
|
|
|
for (auto& it : other)
|
|
|
|
set(it);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
HashTable(HashTable&& other)
|
2018-10-10 09:53:07 +00:00
|
|
|
: m_buckets(other.m_buckets)
|
|
|
|
, m_size(other.m_size)
|
|
|
|
, m_capacity(other.m_capacity)
|
|
|
|
{
|
|
|
|
other.m_size = 0;
|
|
|
|
other.m_capacity = 0;
|
|
|
|
other.m_buckets = nullptr;
|
|
|
|
}
|
|
|
|
HashTable& operator=(HashTable&& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
2019-02-04 07:53:12 +00:00
|
|
|
clear();
|
2018-10-10 09:53:07 +00:00
|
|
|
m_buckets = other.m_buckets;
|
|
|
|
m_size = other.m_size;
|
|
|
|
m_capacity = other.m_capacity;
|
|
|
|
other.m_size = 0;
|
|
|
|
other.m_capacity = 0;
|
|
|
|
other.m_buckets = nullptr;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~HashTable() { clear(); }
|
2018-12-21 01:10:45 +00:00
|
|
|
bool is_empty() const { return !m_size; }
|
2019-05-06 11:28:52 +00:00
|
|
|
int size() const { return m_size; }
|
|
|
|
int capacity() const { return m_capacity; }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-05-27 11:07:20 +00:00
|
|
|
void ensure_capacity(int capacity)
|
|
|
|
{
|
|
|
|
ASSERT(capacity >= size());
|
|
|
|
rehash(capacity);
|
|
|
|
}
|
|
|
|
|
2018-11-07 00:38:51 +00:00
|
|
|
void set(const T&);
|
2018-10-10 09:53:07 +00:00
|
|
|
void set(T&&);
|
|
|
|
bool contains(const T&) const;
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
void dump() const;
|
|
|
|
|
2019-06-27 14:36:31 +00:00
|
|
|
using Iterator = HashTableIterator<HashTable, T, typename SinglyLinkedList<T>::Iterator>;
|
2019-06-27 13:57:49 +00:00
|
|
|
friend Iterator;
|
2018-12-21 01:10:45 +00:00
|
|
|
Iterator begin() { return Iterator(*this, is_empty()); }
|
2018-10-10 09:53:07 +00:00
|
|
|
Iterator end() { return Iterator(*this, true); }
|
|
|
|
|
2019-06-27 14:36:31 +00:00
|
|
|
using ConstIterator = HashTableIterator<const HashTable, const T, typename SinglyLinkedList<T>::ConstIterator>;
|
2019-06-27 13:57:49 +00:00
|
|
|
friend ConstIterator;
|
2018-12-21 01:10:45 +00:00
|
|
|
ConstIterator begin() const { return ConstIterator(*this, is_empty()); }
|
2018-10-10 09:53:07 +00:00
|
|
|
ConstIterator end() const { return ConstIterator(*this, true); }
|
|
|
|
|
|
|
|
Iterator find(const T&);
|
|
|
|
ConstIterator find(const T&) const;
|
|
|
|
|
2018-10-13 12:22:09 +00:00
|
|
|
void remove(const T& value)
|
|
|
|
{
|
|
|
|
auto it = find(value);
|
|
|
|
if (it != end())
|
|
|
|
remove(it);
|
|
|
|
}
|
|
|
|
|
2018-10-25 10:35:49 +00:00
|
|
|
void remove(Iterator);
|
2018-10-13 12:22:09 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
2019-05-06 11:28:52 +00:00
|
|
|
Bucket& lookup(const T&, int* bucket_index = nullptr);
|
|
|
|
const Bucket& lookup(const T&, int* bucket_index = nullptr) const;
|
|
|
|
void rehash(int capacity);
|
2018-11-07 00:38:51 +00:00
|
|
|
void insert(const T&);
|
2018-10-10 09:53:07 +00:00
|
|
|
void insert(T&&);
|
|
|
|
|
2019-06-27 13:57:49 +00:00
|
|
|
Bucket& bucket(int index) { return m_buckets[index]; }
|
|
|
|
const Bucket& bucket(int index) const { return m_buckets[index]; }
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
Bucket* m_buckets { nullptr };
|
|
|
|
|
2019-05-06 11:28:52 +00:00
|
|
|
int m_size { 0 };
|
|
|
|
int m_capacity { 0 };
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::set(T&& value)
|
|
|
|
{
|
|
|
|
if (!m_capacity)
|
|
|
|
rehash(1);
|
|
|
|
auto& bucket = lookup(value);
|
2019-06-27 13:57:49 +00:00
|
|
|
for (auto& e : bucket) {
|
2019-06-29 17:14:03 +00:00
|
|
|
if (TraitsForT::equals(e, value)) {
|
2019-03-25 03:23:17 +00:00
|
|
|
e = move(value);
|
2018-10-10 09:53:07 +00:00
|
|
|
return;
|
2019-03-25 03:23:17 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
if (size() >= capacity()) {
|
|
|
|
rehash(size() + 1);
|
2018-10-17 08:55:43 +00:00
|
|
|
insert(move(value));
|
2018-10-10 09:53:07 +00:00
|
|
|
} else {
|
2019-06-27 13:57:49 +00:00
|
|
|
bucket.append(move(value));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
m_size++;
|
|
|
|
}
|
|
|
|
|
2018-11-07 00:38:51 +00:00
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::set(const T& value)
|
|
|
|
{
|
|
|
|
if (!m_capacity)
|
|
|
|
rehash(1);
|
|
|
|
auto& bucket = lookup(value);
|
2019-06-27 13:57:49 +00:00
|
|
|
for (auto& e : bucket) {
|
2019-06-29 17:14:03 +00:00
|
|
|
if (TraitsForT::equals(e, value)) {
|
2019-06-29 10:02:52 +00:00
|
|
|
e = value;
|
2018-11-07 00:38:51 +00:00
|
|
|
return;
|
2019-03-25 03:23:17 +00:00
|
|
|
}
|
2018-11-07 00:38:51 +00:00
|
|
|
}
|
|
|
|
if (size() >= capacity()) {
|
|
|
|
rehash(size() + 1);
|
|
|
|
insert(value);
|
|
|
|
} else {
|
2019-06-27 13:57:49 +00:00
|
|
|
bucket.append(value);
|
2018-11-07 00:38:51 +00:00
|
|
|
}
|
|
|
|
m_size++;
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
template<typename T, typename TraitsForT>
|
2019-05-06 11:28:52 +00:00
|
|
|
void HashTable<T, TraitsForT>::rehash(int new_capacity)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2019-01-19 21:53:05 +00:00
|
|
|
new_capacity *= 2;
|
|
|
|
auto* new_buckets = new Bucket[new_capacity];
|
|
|
|
auto* old_buckets = m_buckets;
|
2019-05-06 11:28:52 +00:00
|
|
|
int old_capacity = m_capacity;
|
2019-01-19 21:53:05 +00:00
|
|
|
m_buckets = new_buckets;
|
|
|
|
m_capacity = new_capacity;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-05-06 11:28:52 +00:00
|
|
|
for (int i = 0; i < old_capacity; ++i) {
|
2019-06-27 13:57:49 +00:00
|
|
|
for (auto& value : old_buckets[i]) {
|
2018-10-17 08:55:43 +00:00
|
|
|
insert(move(value));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
delete[] old_buckets;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::clear()
|
|
|
|
{
|
2019-01-30 18:32:54 +00:00
|
|
|
if (m_buckets) {
|
2019-05-28 09:53:16 +00:00
|
|
|
delete[] m_buckets;
|
2019-01-30 18:32:54 +00:00
|
|
|
m_buckets = nullptr;
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
m_capacity = 0;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::insert(T&& value)
|
|
|
|
{
|
|
|
|
auto& bucket = lookup(value);
|
2019-06-27 13:57:49 +00:00
|
|
|
bucket.append(move(value));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 00:38:51 +00:00
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::insert(const T& value)
|
|
|
|
{
|
|
|
|
auto& bucket = lookup(value);
|
2019-06-27 13:57:49 +00:00
|
|
|
bucket.append(value);
|
2018-11-07 00:38:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
bool HashTable<T, TraitsForT>::contains(const T& value) const
|
|
|
|
{
|
2018-12-21 01:10:45 +00:00
|
|
|
if (is_empty())
|
2018-10-10 09:53:07 +00:00
|
|
|
return false;
|
|
|
|
auto& bucket = lookup(value);
|
2019-06-27 13:57:49 +00:00
|
|
|
for (auto& e : bucket) {
|
2019-06-29 17:14:03 +00:00
|
|
|
if (TraitsForT::equals(e, value))
|
2018-10-10 09:53:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
auto HashTable<T, TraitsForT>::find(const T& value) -> Iterator
|
|
|
|
{
|
2018-12-21 01:10:45 +00:00
|
|
|
if (is_empty())
|
2018-10-10 09:53:07 +00:00
|
|
|
return end();
|
2019-05-06 11:28:52 +00:00
|
|
|
int bucket_index;
|
2019-01-19 21:53:05 +00:00
|
|
|
auto& bucket = lookup(value, &bucket_index);
|
2019-06-29 17:14:03 +00:00
|
|
|
auto bucket_iterator = bucket.template find<TraitsForT>(value);
|
2019-06-27 13:57:49 +00:00
|
|
|
if (bucket_iterator != bucket.end())
|
2019-01-19 21:53:05 +00:00
|
|
|
return Iterator(*this, false, bucket_iterator, bucket_index);
|
2018-10-10 09:53:07 +00:00
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
auto HashTable<T, TraitsForT>::find(const T& value) const -> ConstIterator
|
|
|
|
{
|
2018-12-21 01:10:45 +00:00
|
|
|
if (is_empty())
|
2018-10-10 09:53:07 +00:00
|
|
|
return end();
|
2019-05-06 11:28:52 +00:00
|
|
|
int bucket_index;
|
2019-06-27 13:57:49 +00:00
|
|
|
const auto& bucket = lookup(value, &bucket_index);
|
2019-06-29 17:14:03 +00:00
|
|
|
auto bucket_iterator = bucket.template find<TraitsForT>(value);
|
2019-06-27 13:57:49 +00:00
|
|
|
if (bucket_iterator != bucket.end())
|
2019-01-19 21:53:05 +00:00
|
|
|
return ConstIterator(*this, false, bucket_iterator, bucket_index);
|
2018-10-10 09:53:07 +00:00
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
2018-10-13 12:22:09 +00:00
|
|
|
template<typename T, typename TraitsForT>
|
2018-10-25 10:35:49 +00:00
|
|
|
void HashTable<T, TraitsForT>::remove(Iterator it)
|
2018-10-13 12:22:09 +00:00
|
|
|
{
|
2018-12-21 01:10:45 +00:00
|
|
|
ASSERT(!is_empty());
|
2019-06-27 13:57:49 +00:00
|
|
|
m_buckets[it.m_bucket_index].remove(it.m_bucket_iterator);
|
2018-10-13 12:22:09 +00:00
|
|
|
--m_size;
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
template<typename T, typename TraitsForT>
|
2019-06-27 13:57:49 +00:00
|
|
|
auto HashTable<T, TraitsForT>::lookup(const T& value, int* bucket_index) -> Bucket&
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
unsigned hash = TraitsForT::hash(value);
|
2019-01-19 21:53:05 +00:00
|
|
|
if (bucket_index)
|
|
|
|
*bucket_index = hash % m_capacity;
|
2018-10-10 09:53:07 +00:00
|
|
|
return m_buckets[hash % m_capacity];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
2019-06-27 13:57:49 +00:00
|
|
|
auto HashTable<T, TraitsForT>::lookup(const T& value, int* bucket_index) const -> const Bucket&
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
unsigned hash = TraitsForT::hash(value);
|
2019-01-19 21:53:05 +00:00
|
|
|
if (bucket_index)
|
|
|
|
*bucket_index = hash % m_capacity;
|
2018-10-10 09:53:07 +00:00
|
|
|
return m_buckets[hash % m_capacity];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::dump() const
|
|
|
|
{
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("HashTable{%p} m_size=%u, m_capacity=%u, m_buckets=%p\n", this, m_size, m_capacity, m_buckets);
|
2019-05-06 11:28:52 +00:00
|
|
|
for (int i = 0; i < m_capacity; ++i) {
|
2018-10-10 09:53:07 +00:00
|
|
|
auto& bucket = m_buckets[i];
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("Bucket %u\n", i);
|
2019-06-27 13:57:49 +00:00
|
|
|
for (auto& e : bucket) {
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf(" > ");
|
2018-10-10 09:53:07 +00:00
|
|
|
TraitsForT::dump(e);
|
2018-10-17 08:55:43 +00:00
|
|
|
kprintf("\n");
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::HashTable;
|