2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Assertions.h"
|
2018-10-13 14:50:44 +03:00
|
|
|
#include "DoublyLinkedList.h"
|
2018-10-10 12:53:07 +03:00
|
|
|
#include "Traits.h"
|
2018-12-04 02:27:16 +03:00
|
|
|
#include "StdLibExtras.h"
|
2018-10-17 11:55:43 +03:00
|
|
|
#include "kstdio.h"
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
//#define HASHTABLE_DEBUG
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T, typename = Traits<T>> class HashTable;
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
class HashTable {
|
|
|
|
private:
|
|
|
|
struct Bucket {
|
2018-10-13 14:50:44 +03:00
|
|
|
DoublyLinkedList<T> chain;
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
HashTable() { }
|
|
|
|
explicit HashTable(HashTable&& other)
|
|
|
|
: 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) {
|
|
|
|
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(); }
|
|
|
|
bool isEmpty() const { return !m_size; }
|
|
|
|
unsigned size() const { return m_size; }
|
|
|
|
unsigned capacity() const { return m_capacity; }
|
|
|
|
|
2018-11-07 03:38:51 +03:00
|
|
|
void set(const T&);
|
2018-10-10 12:53:07 +03:00
|
|
|
void set(T&&);
|
|
|
|
bool contains(const T&) const;
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
void dump() const;
|
|
|
|
|
|
|
|
class Iterator {
|
|
|
|
public:
|
2018-10-26 18:42:12 +03:00
|
|
|
bool operator!=(const Iterator& other) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
if (m_isEnd && other.m_isEnd)
|
|
|
|
return false;
|
|
|
|
return &m_table != &other.m_table
|
|
|
|
|| m_isEnd != other.m_isEnd
|
|
|
|
|| m_bucketIndex != other.m_bucketIndex
|
|
|
|
|| m_bucketIterator != other.m_bucketIterator;
|
|
|
|
}
|
2018-10-26 18:42:12 +03:00
|
|
|
bool operator==(const Iterator& other) const { return !(*this != other); }
|
2018-10-10 12:53:07 +03:00
|
|
|
T& operator*()
|
|
|
|
{
|
|
|
|
#ifdef HASHTABLE_DEBUG
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("retrieve { bucketIndex: %u, isEnd: %u }\n", m_bucketIndex, m_isEnd);
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
|
|
|
return *m_bucketIterator;
|
|
|
|
}
|
|
|
|
Iterator& operator++()
|
|
|
|
{
|
|
|
|
skipToNext();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void skipToNext()
|
|
|
|
{
|
|
|
|
#ifdef HASHTABLE_DEBUG
|
|
|
|
unsigned pass = 0;
|
|
|
|
#endif
|
|
|
|
while (!m_isEnd) {
|
|
|
|
#ifdef HASHTABLE_DEBUG
|
|
|
|
++pass;
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("skipToNext pass %u, m_bucketIndex=%u\n", pass, m_bucketIndex);
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
|
|
|
if (m_bucketIterator.isEnd()) {
|
|
|
|
++m_bucketIndex;
|
|
|
|
if (m_bucketIndex >= m_table.capacity()) {
|
|
|
|
m_isEnd = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_bucketIterator = m_table.m_buckets[m_bucketIndex].chain.begin();
|
|
|
|
} else {
|
|
|
|
++m_bucketIterator;
|
|
|
|
}
|
|
|
|
if (!m_bucketIterator.isEnd())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
friend class HashTable;
|
2018-10-14 23:08:36 +03:00
|
|
|
explicit Iterator(HashTable& table, bool isEnd, typename DoublyLinkedList<T>::Iterator bucketIterator = DoublyLinkedList<T>::Iterator::universalEnd(), unsigned bucketIndex = 0)
|
2018-10-10 12:53:07 +03:00
|
|
|
: m_table(table)
|
2018-10-14 23:08:36 +03:00
|
|
|
, m_bucketIndex(bucketIndex)
|
2018-10-10 12:53:07 +03:00
|
|
|
, m_isEnd(isEnd)
|
|
|
|
, m_bucketIterator(bucketIterator)
|
|
|
|
{
|
2018-10-13 14:50:44 +03:00
|
|
|
if (!isEnd && !m_table.isEmpty() && !(m_bucketIterator != DoublyLinkedList<T>::Iterator::universalEnd())) {
|
2018-10-10 12:53:07 +03:00
|
|
|
#ifdef HASHTABLE_DEBUG
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("bucket iterator init!\n");
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
|
|
|
m_bucketIterator = m_table.m_buckets[0].chain.begin();
|
|
|
|
if (m_bucketIterator.isEnd())
|
|
|
|
skipToNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable& m_table;
|
|
|
|
unsigned m_bucketIndex { 0 };
|
|
|
|
bool m_isEnd { false };
|
2018-10-13 14:50:44 +03:00
|
|
|
typename DoublyLinkedList<T>::Iterator m_bucketIterator;
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
2018-10-26 18:42:12 +03:00
|
|
|
Iterator begin() { return Iterator(*this, isEmpty()); }
|
2018-10-10 12:53:07 +03:00
|
|
|
Iterator end() { return Iterator(*this, true); }
|
|
|
|
|
|
|
|
class ConstIterator {
|
|
|
|
public:
|
2018-10-26 18:42:12 +03:00
|
|
|
bool operator!=(const ConstIterator& other) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
if (m_isEnd && other.m_isEnd)
|
|
|
|
return false;
|
|
|
|
return &m_table != &other.m_table
|
|
|
|
|| m_isEnd != other.m_isEnd
|
|
|
|
|| m_bucketIndex != other.m_bucketIndex
|
|
|
|
|| m_bucketIterator != other.m_bucketIterator;
|
|
|
|
}
|
2018-10-26 18:42:12 +03:00
|
|
|
bool operator==(const ConstIterator& other) const { return !(*this != other); }
|
2018-10-10 12:53:07 +03:00
|
|
|
const T& operator*() const
|
|
|
|
{
|
|
|
|
#ifdef HASHTABLE_DEBUG
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("retrieve { bucketIndex: %u, isEnd: %u }\n", m_bucketIndex, m_isEnd);
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
|
|
|
return *m_bucketIterator;
|
|
|
|
}
|
|
|
|
ConstIterator& operator++()
|
|
|
|
{
|
|
|
|
skipToNext();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void skipToNext()
|
|
|
|
{
|
|
|
|
#ifdef HASHTABLE_DEBUG
|
|
|
|
unsigned pass = 0;
|
|
|
|
#endif
|
|
|
|
while (!m_isEnd) {
|
|
|
|
#ifdef HASHTABLE_DEBUG
|
|
|
|
++pass;
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("skipToNext pass %u, m_bucketIndex=%u\n", pass, m_bucketIndex);
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
|
|
|
if (m_bucketIterator.isEnd()) {
|
|
|
|
++m_bucketIndex;
|
|
|
|
if (m_bucketIndex >= m_table.capacity()) {
|
|
|
|
m_isEnd = true;
|
|
|
|
return;
|
|
|
|
}
|
2018-10-13 14:50:44 +03:00
|
|
|
const DoublyLinkedList<T>& chain = m_table.m_buckets[m_bucketIndex].chain;
|
2018-10-10 12:53:07 +03:00
|
|
|
m_bucketIterator = chain.begin();
|
|
|
|
} else {
|
|
|
|
++m_bucketIterator;
|
|
|
|
}
|
|
|
|
if (!m_bucketIterator.isEnd())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
friend class HashTable;
|
2018-10-14 23:08:36 +03:00
|
|
|
ConstIterator(const HashTable& table, bool isEnd, typename DoublyLinkedList<T>::ConstIterator bucketIterator = DoublyLinkedList<T>::ConstIterator::universalEnd(), unsigned bucketIndex = 0)
|
2018-10-10 12:53:07 +03:00
|
|
|
: m_table(table)
|
2018-10-14 23:08:36 +03:00
|
|
|
, m_bucketIndex(bucketIndex)
|
2018-10-10 12:53:07 +03:00
|
|
|
, m_isEnd(isEnd)
|
|
|
|
, m_bucketIterator(bucketIterator)
|
|
|
|
{
|
2018-10-13 14:50:44 +03:00
|
|
|
if (!isEnd && !m_table.isEmpty() && !(m_bucketIterator != DoublyLinkedList<T>::ConstIterator::universalEnd())) {
|
2018-10-10 12:53:07 +03:00
|
|
|
#ifdef HASHTABLE_DEBUG
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("const bucket iterator init!\n");
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
2018-10-13 14:50:44 +03:00
|
|
|
const DoublyLinkedList<T>& chain = m_table.m_buckets[0].chain;
|
2018-10-10 12:53:07 +03:00
|
|
|
m_bucketIterator = chain.begin();
|
2018-10-13 15:22:09 +03:00
|
|
|
if (m_bucketIterator.isEnd())
|
|
|
|
skipToNext();
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const HashTable& m_table;
|
|
|
|
unsigned m_bucketIndex { 0 };
|
|
|
|
bool m_isEnd { false };
|
2018-10-13 14:50:44 +03:00
|
|
|
typename DoublyLinkedList<T>::ConstIterator m_bucketIterator;
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
2018-10-26 18:42:12 +03:00
|
|
|
ConstIterator begin() const { return ConstIterator(*this, isEmpty()); }
|
2018-10-10 12:53:07 +03:00
|
|
|
ConstIterator end() const { return ConstIterator(*this, true); }
|
|
|
|
|
|
|
|
Iterator find(const T&);
|
|
|
|
ConstIterator find(const T&) const;
|
|
|
|
|
2018-10-13 15:22:09 +03:00
|
|
|
void remove(const T& value)
|
|
|
|
{
|
|
|
|
auto it = find(value);
|
|
|
|
if (it != end())
|
|
|
|
remove(it);
|
|
|
|
}
|
|
|
|
|
2018-10-25 13:35:49 +03:00
|
|
|
void remove(Iterator);
|
2018-10-13 15:22:09 +03:00
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
private:
|
2018-10-14 23:08:36 +03:00
|
|
|
Bucket& lookup(const T&, unsigned* bucketIndex = nullptr);
|
|
|
|
const Bucket& lookup(const T&, unsigned* bucketIndex = nullptr) const;
|
2018-10-10 12:53:07 +03:00
|
|
|
void rehash(unsigned capacity);
|
2018-11-07 03:38:51 +03:00
|
|
|
void insert(const T&);
|
2018-10-10 12:53:07 +03:00
|
|
|
void insert(T&&);
|
|
|
|
|
|
|
|
Bucket* m_buckets { nullptr };
|
|
|
|
|
|
|
|
unsigned m_size { 0 };
|
|
|
|
unsigned m_capacity { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::set(T&& value)
|
|
|
|
{
|
|
|
|
if (!m_capacity)
|
|
|
|
rehash(1);
|
|
|
|
auto& bucket = lookup(value);
|
|
|
|
for (auto& e : bucket.chain) {
|
|
|
|
if (e == value)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (size() >= capacity()) {
|
|
|
|
rehash(size() + 1);
|
2018-10-17 11:55:43 +03:00
|
|
|
insert(move(value));
|
2018-10-10 12:53:07 +03:00
|
|
|
} else {
|
2018-10-17 11:55:43 +03:00
|
|
|
bucket.chain.append(move(value));
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
m_size++;
|
|
|
|
}
|
|
|
|
|
2018-11-07 03:38:51 +03:00
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::set(const T& value)
|
|
|
|
{
|
|
|
|
if (!m_capacity)
|
|
|
|
rehash(1);
|
|
|
|
auto& bucket = lookup(value);
|
|
|
|
for (auto& e : bucket.chain) {
|
|
|
|
if (e == value)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (size() >= capacity()) {
|
|
|
|
rehash(size() + 1);
|
|
|
|
insert(value);
|
|
|
|
} else {
|
|
|
|
bucket.chain.append(value);
|
|
|
|
}
|
|
|
|
m_size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::rehash(unsigned newCapacity)
|
|
|
|
{
|
|
|
|
newCapacity *= 2;
|
|
|
|
#ifdef HASHTABLE_DEBUG
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("rehash to %u buckets\n", newCapacity);
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
|
|
|
auto* newBuckets = new Bucket[newCapacity];
|
|
|
|
auto* oldBuckets = m_buckets;
|
|
|
|
unsigned oldCapacity = m_capacity;
|
|
|
|
m_buckets = newBuckets;
|
|
|
|
m_capacity = newCapacity;
|
|
|
|
|
|
|
|
#ifdef HASHTABLE_DEBUG
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("reinsert %u buckets\n", oldCapacity);
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
|
|
|
for (unsigned i = 0; i < oldCapacity; ++i) {
|
|
|
|
for (auto& value : oldBuckets[i].chain) {
|
2018-10-17 11:55:43 +03:00
|
|
|
insert(move(value));
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] oldBuckets;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::clear()
|
|
|
|
{
|
|
|
|
delete [] m_buckets;
|
|
|
|
m_capacity = 0;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::insert(T&& value)
|
|
|
|
{
|
|
|
|
auto& bucket = lookup(value);
|
2018-10-17 11:55:43 +03:00
|
|
|
bucket.chain.append(move(value));
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-07 03:38:51 +03:00
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::insert(const T& value)
|
|
|
|
{
|
|
|
|
auto& bucket = lookup(value);
|
|
|
|
bucket.chain.append(value);
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
bool HashTable<T, TraitsForT>::contains(const T& value) const
|
|
|
|
{
|
|
|
|
if (isEmpty())
|
|
|
|
return false;
|
|
|
|
auto& bucket = lookup(value);
|
|
|
|
for (auto& e : bucket.chain) {
|
|
|
|
if (e == value)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
auto HashTable<T, TraitsForT>::find(const T& value) -> Iterator
|
|
|
|
{
|
|
|
|
if (isEmpty())
|
|
|
|
return end();
|
2018-10-14 23:08:36 +03:00
|
|
|
unsigned bucketIndex;
|
|
|
|
auto& bucket = lookup(value, &bucketIndex);
|
2018-10-10 12:53:07 +03:00
|
|
|
auto bucketIterator = bucket.chain.find(value);
|
|
|
|
if (bucketIterator != bucket.chain.end())
|
2018-10-14 23:08:36 +03:00
|
|
|
return Iterator(*this, false, bucketIterator, bucketIndex);
|
2018-10-10 12:53:07 +03:00
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
auto HashTable<T, TraitsForT>::find(const T& value) const -> ConstIterator
|
|
|
|
{
|
|
|
|
if (isEmpty())
|
|
|
|
return end();
|
2018-10-14 23:08:36 +03:00
|
|
|
unsigned bucketIndex;
|
|
|
|
auto& bucket = lookup(value, &bucketIndex);
|
2018-10-10 12:53:07 +03:00
|
|
|
auto bucketIterator = bucket.chain.find(value);
|
|
|
|
if (bucketIterator != bucket.chain.end())
|
2018-10-14 23:08:36 +03:00
|
|
|
return ConstIterator(*this, false, bucketIterator, bucketIndex);
|
2018-10-10 12:53:07 +03:00
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
2018-10-13 15:22:09 +03:00
|
|
|
template<typename T, typename TraitsForT>
|
2018-10-25 13:35:49 +03:00
|
|
|
void HashTable<T, TraitsForT>::remove(Iterator it)
|
2018-10-13 15:22:09 +03:00
|
|
|
{
|
|
|
|
ASSERT(!isEmpty());
|
|
|
|
m_buckets[it.m_bucketIndex].chain.remove(it.m_bucketIterator);
|
|
|
|
--m_size;
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
template<typename T, typename TraitsForT>
|
2018-10-14 23:08:36 +03:00
|
|
|
typename HashTable<T, TraitsForT>::Bucket& HashTable<T, TraitsForT>::lookup(const T& value, unsigned* bucketIndex)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
unsigned hash = TraitsForT::hash(value);
|
|
|
|
#ifdef HASHTABLE_DEBUG
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("hash for ");
|
2018-10-10 12:53:07 +03:00
|
|
|
TraitsForT::dump(value);
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf(" is %u\n", hash);
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
2018-10-14 23:08:36 +03:00
|
|
|
if (bucketIndex)
|
|
|
|
*bucketIndex = hash % m_capacity;
|
2018-10-10 12:53:07 +03:00
|
|
|
return m_buckets[hash % m_capacity];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
2018-10-14 23:08:36 +03:00
|
|
|
const typename HashTable<T, TraitsForT>::Bucket& HashTable<T, TraitsForT>::lookup(const T& value, unsigned* bucketIndex) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
unsigned hash = TraitsForT::hash(value);
|
|
|
|
#ifdef HASHTABLE_DEBUG
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("hash for ");
|
2018-10-10 12:53:07 +03:00
|
|
|
TraitsForT::dump(value);
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf(" is %u\n", hash);
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
2018-10-14 23:08:36 +03:00
|
|
|
if (bucketIndex)
|
|
|
|
*bucketIndex = hash % m_capacity;
|
2018-10-10 12:53:07 +03:00
|
|
|
return m_buckets[hash % m_capacity];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename TraitsForT>
|
|
|
|
void HashTable<T, TraitsForT>::dump() const
|
|
|
|
{
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("HashTable{%p} m_size=%u, m_capacity=%u, m_buckets=%p\n", this, m_size, m_capacity, m_buckets);
|
2018-10-10 12:53:07 +03:00
|
|
|
for (unsigned i = 0; i < m_capacity; ++i) {
|
|
|
|
auto& bucket = m_buckets[i];
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("Bucket %u\n", i);
|
2018-10-10 12:53:07 +03:00
|
|
|
for (auto& e : bucket.chain) {
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf(" > ");
|
2018-10-10 12:53:07 +03:00
|
|
|
TraitsForT::dump(e);
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("\n");
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::HashTable;
|
|
|
|
|