2007-04-24 15:04:35 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2007, Hugo Santos. All Rights Reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Hugo Santos, hugosantos@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _OPEN_HASH_TABLE_H_
|
|
|
|
#define _OPEN_HASH_TABLE_H_
|
|
|
|
|
2007-04-25 20:14:14 +04:00
|
|
|
#include <KernelExport.h>
|
2007-04-26 10:05:08 +04:00
|
|
|
#include <util/kernel_cpp.h>
|
2007-04-24 15:04:35 +04:00
|
|
|
|
2007-04-25 23:21:06 +04:00
|
|
|
// the Definition template must have four methods: `HashKey', `Hash',
|
2007-04-25 22:55:05 +04:00
|
|
|
// `Compare' and `GetLink;. It must also define several types as shown in the
|
|
|
|
// following example:
|
2007-04-24 15:04:35 +04:00
|
|
|
//
|
2007-04-25 22:55:05 +04:00
|
|
|
// struct Foo : HashTableLink<Foo> {
|
2007-04-24 15:04:35 +04:00
|
|
|
// int bar;
|
2007-04-25 22:55:05 +04:00
|
|
|
//
|
|
|
|
// HashTableLink<Foo> otherLink;
|
2007-04-24 15:04:35 +04:00
|
|
|
// };
|
|
|
|
//
|
|
|
|
// struct HashTableDefinition {
|
2007-04-25 23:21:06 +04:00
|
|
|
// typedef void ParentType;
|
2007-04-24 15:04:35 +04:00
|
|
|
// typedef int KeyType;
|
|
|
|
// typedef Foo ValueType;
|
|
|
|
//
|
2007-04-25 23:21:06 +04:00
|
|
|
// HashTableDefinition(void *parent) {}
|
|
|
|
//
|
2007-04-30 16:31:31 +04:00
|
|
|
// size_t HashKey(int key) const { return key >> 1; }
|
|
|
|
// size_t Hash(Foo *value) const { return HashKey(value->bar); }
|
|
|
|
// bool Compare(int key, Foo *value) const { return value->bar == key; }
|
|
|
|
// HashTableLink<Foo> *GetLink(Foo *value) const { return value; }
|
2007-04-24 15:04:35 +04:00
|
|
|
// };
|
|
|
|
|
2007-04-25 22:55:05 +04:00
|
|
|
template<typename Type>
|
|
|
|
struct HashTableLink {
|
|
|
|
Type *fNext;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Definition, bool AutoExpand = true,
|
|
|
|
bool CheckDuplicates = false>
|
2007-04-24 15:04:35 +04:00
|
|
|
class OpenHashTable {
|
|
|
|
public:
|
2007-04-27 12:07:38 +04:00
|
|
|
typedef OpenHashTable<Definition, AutoExpand, CheckDuplicates> HashTable;
|
2007-04-24 15:04:35 +04:00
|
|
|
typedef typename Definition::KeyType KeyType;
|
|
|
|
typedef typename Definition::ValueType ValueType;
|
|
|
|
|
2007-04-30 16:31:31 +04:00
|
|
|
static const size_t kMinimumSize = 8;
|
2007-04-24 15:04:35 +04:00
|
|
|
|
|
|
|
// we use new [] / delete [] for allocation. If in the future this
|
|
|
|
// is revealed to be insufficient we can switch to a template based
|
|
|
|
// allocator. All allocations are of power of 2 lengths.
|
|
|
|
|
|
|
|
// regrowth factor: 200 / 256 = 78.125%
|
|
|
|
// 50 / 256 = 19.53125%
|
|
|
|
|
2007-04-25 23:21:06 +04:00
|
|
|
OpenHashTable(size_t initialSize = kMinimumSize)
|
2007-04-30 16:31:31 +04:00
|
|
|
: fTableSize(0), fItemCount(0), fTable(NULL)
|
2007-04-25 23:21:06 +04:00
|
|
|
{
|
2007-04-30 16:31:31 +04:00
|
|
|
if (initialSize > 0)
|
|
|
|
_Resize(initialSize);
|
2007-04-25 23:21:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
OpenHashTable(typename Definition::ParentType *parent,
|
|
|
|
size_t initialSize = kMinimumSize)
|
2007-04-30 16:31:31 +04:00
|
|
|
: fDefinition(parent), fTableSize(0), fItemCount(0), fTable(NULL)
|
2007-04-24 15:04:35 +04:00
|
|
|
{
|
2007-04-30 16:31:31 +04:00
|
|
|
if (initialSize > 0)
|
|
|
|
_Resize(initialSize);
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~OpenHashTable()
|
|
|
|
{
|
|
|
|
delete [] fTable;
|
|
|
|
}
|
|
|
|
|
2007-04-30 16:31:31 +04:00
|
|
|
status_t InitCheck() const
|
|
|
|
{
|
|
|
|
return (fTableSize == 0 || fTable) ? B_OK : B_NO_MEMORY;
|
|
|
|
}
|
2007-04-24 15:04:35 +04:00
|
|
|
|
|
|
|
ValueType *Lookup(const KeyType &key) const
|
|
|
|
{
|
2007-04-30 16:31:31 +04:00
|
|
|
if (fTableSize == 0)
|
|
|
|
return NULL;
|
|
|
|
|
2007-04-25 23:21:06 +04:00
|
|
|
size_t index = fDefinition.HashKey(key) & (fTableSize - 1);
|
2007-04-25 22:55:05 +04:00
|
|
|
ValueType *slot = fTable[index];
|
2007-04-24 15:04:35 +04:00
|
|
|
|
2007-04-25 22:55:05 +04:00
|
|
|
while (slot) {
|
2007-04-25 23:21:06 +04:00
|
|
|
if (fDefinition.Compare(key, slot))
|
2007-04-25 22:55:05 +04:00
|
|
|
break;
|
|
|
|
slot = _Link(slot)->fNext;
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
2007-04-25 22:55:05 +04:00
|
|
|
|
|
|
|
return slot;
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
|
|
|
|
2007-04-30 16:31:31 +04:00
|
|
|
status_t Insert(ValueType *value)
|
2007-04-24 15:04:35 +04:00
|
|
|
{
|
2007-04-30 16:31:31 +04:00
|
|
|
if (fTableSize == 0) {
|
|
|
|
if (!_Resize(kMinimumSize))
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
} else if (AutoExpand && fItemCount >= (fTableSize * 200 / 256))
|
2007-04-25 22:55:05 +04:00
|
|
|
_Resize(fTableSize * 2);
|
2007-04-24 15:04:35 +04:00
|
|
|
|
|
|
|
InsertUnchecked(value);
|
2007-04-30 16:31:31 +04:00
|
|
|
return B_OK;
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void InsertUnchecked(ValueType *value)
|
|
|
|
{
|
2007-04-25 20:14:14 +04:00
|
|
|
if (CheckDuplicates) {
|
|
|
|
for (size_t i = 0; i < fTableSize; i++) {
|
2007-04-25 22:55:05 +04:00
|
|
|
ValueType *bucket = fTable[i];
|
|
|
|
while (bucket) {
|
|
|
|
if (bucket == value)
|
|
|
|
panic("Hash Table: value already in table.");
|
|
|
|
bucket = _Link(bucket)->fNext;
|
|
|
|
}
|
2007-04-25 20:14:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-25 22:55:05 +04:00
|
|
|
_Insert(fTable, fTableSize, value);
|
2007-04-24 15:04:35 +04:00
|
|
|
fItemCount++;
|
|
|
|
}
|
|
|
|
|
2007-04-27 19:06:30 +04:00
|
|
|
bool Remove(ValueType *value)
|
2007-04-24 15:04:35 +04:00
|
|
|
{
|
2007-04-27 19:06:30 +04:00
|
|
|
if (!RemoveUnchecked(value))
|
|
|
|
return false;
|
2007-04-24 15:04:35 +04:00
|
|
|
|
2007-04-25 22:55:05 +04:00
|
|
|
if (AutoExpand && fTableSize > kMinimumSize
|
|
|
|
&& fItemCount < (fTableSize * 50 / 256))
|
2007-04-24 15:04:35 +04:00
|
|
|
_Resize(fTableSize / 2);
|
2007-04-27 19:06:30 +04:00
|
|
|
|
|
|
|
return true;
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
|
|
|
|
2007-04-27 19:06:30 +04:00
|
|
|
bool RemoveUnchecked(ValueType *value)
|
2007-04-24 15:04:35 +04:00
|
|
|
{
|
2007-04-25 23:21:06 +04:00
|
|
|
size_t index = fDefinition.Hash(value) & (fTableSize - 1);
|
2007-04-25 22:55:05 +04:00
|
|
|
ValueType *previous = NULL, *slot = fTable[index];
|
2007-04-24 15:04:35 +04:00
|
|
|
|
2007-04-25 22:55:05 +04:00
|
|
|
while (slot) {
|
|
|
|
ValueType *next = _Link(slot)->fNext;
|
|
|
|
|
|
|
|
if (value == slot) {
|
|
|
|
if (previous)
|
|
|
|
_Link(previous)->fNext = next;
|
|
|
|
else
|
|
|
|
fTable[index] = next;
|
2007-04-24 15:04:35 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-04-25 22:55:05 +04:00
|
|
|
previous = slot;
|
|
|
|
slot = next;
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
|
|
|
|
2007-04-27 19:06:30 +04:00
|
|
|
if (slot == NULL)
|
|
|
|
return false;
|
|
|
|
|
2007-04-25 20:14:14 +04:00
|
|
|
if (CheckDuplicates) {
|
|
|
|
for (size_t i = 0; i < fTableSize; i++) {
|
2007-04-25 22:55:05 +04:00
|
|
|
ValueType *bucket = fTable[i];
|
|
|
|
while (bucket) {
|
|
|
|
if (bucket == value)
|
|
|
|
panic("Hash Table: duplicate detected.");
|
|
|
|
bucket = _Link(bucket)->fNext;
|
|
|
|
}
|
2007-04-25 20:14:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-24 15:04:35 +04:00
|
|
|
fItemCount--;
|
2007-04-27 19:06:30 +04:00
|
|
|
return true;
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
|
|
|
|
2007-04-27 12:07:38 +04:00
|
|
|
class Iterator {
|
|
|
|
public:
|
|
|
|
Iterator(const HashTable *table)
|
2007-04-27 18:47:34 +04:00
|
|
|
: fTable(table)
|
2007-04-27 12:07:38 +04:00
|
|
|
{
|
|
|
|
Rewind();
|
|
|
|
}
|
|
|
|
|
2007-04-27 19:06:30 +04:00
|
|
|
Iterator(const HashTable *table, size_t index, ValueType *value)
|
|
|
|
: fTable(table), fIndex(index), fNext(value) {}
|
|
|
|
|
2007-04-27 12:07:38 +04:00
|
|
|
bool HasNext() const { return fNext != NULL; }
|
|
|
|
|
|
|
|
ValueType *Next()
|
|
|
|
{
|
2007-04-27 18:47:34 +04:00
|
|
|
ValueType *current = fNext;
|
2007-04-27 12:07:38 +04:00
|
|
|
_GetNext();
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rewind()
|
|
|
|
{
|
|
|
|
// get the first one
|
|
|
|
fIndex = 0;
|
2007-04-27 18:47:34 +04:00
|
|
|
fNext = NULL;
|
2007-04-27 12:07:38 +04:00
|
|
|
_GetNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void _GetNext()
|
|
|
|
{
|
2007-04-27 18:47:34 +04:00
|
|
|
if (fNext)
|
|
|
|
fNext = fTable->_Link(fNext)->fNext;
|
2007-04-27 12:07:38 +04:00
|
|
|
|
|
|
|
while (fNext == NULL && fIndex < fTable->fTableSize)
|
|
|
|
fNext = fTable->fTable[fIndex++];
|
|
|
|
}
|
|
|
|
|
|
|
|
const HashTable *fTable;
|
|
|
|
size_t fIndex;
|
2007-04-27 18:47:34 +04:00
|
|
|
ValueType *fNext;
|
2007-04-27 12:07:38 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
Iterator GetIterator() const { return Iterator(this); }
|
|
|
|
|
2007-04-27 19:06:30 +04:00
|
|
|
protected:
|
2007-04-27 12:07:38 +04:00
|
|
|
// for g++ 2.95
|
|
|
|
friend class Iterator;
|
|
|
|
|
2007-04-25 22:55:05 +04:00
|
|
|
void _Insert(ValueType **table, size_t tableSize, ValueType *value)
|
2007-04-24 15:04:35 +04:00
|
|
|
{
|
2007-04-25 23:21:06 +04:00
|
|
|
size_t index = fDefinition.Hash(value) & (tableSize - 1);
|
2007-04-24 15:04:35 +04:00
|
|
|
|
2007-04-25 22:55:05 +04:00
|
|
|
_Link(value)->fNext = table[index];
|
|
|
|
table[index] = value;
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool _Resize(size_t newSize)
|
|
|
|
{
|
|
|
|
ValueType **newTable = new ValueType *[newSize];
|
|
|
|
if (newTable == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < newSize; i++)
|
|
|
|
newTable[i] = NULL;
|
|
|
|
|
|
|
|
if (fTable) {
|
|
|
|
for (size_t i = 0; i < fTableSize; i++) {
|
2007-04-25 22:55:05 +04:00
|
|
|
ValueType *bucket = fTable[i];
|
|
|
|
while (bucket) {
|
|
|
|
ValueType *next = _Link(bucket)->fNext;
|
|
|
|
_Insert(newTable, newSize, bucket);
|
|
|
|
bucket = next;
|
|
|
|
}
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
delete [] fTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
fTableSize = newSize;
|
|
|
|
fTable = newTable;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-25 22:55:05 +04:00
|
|
|
HashTableLink<ValueType> *_Link(ValueType *bucket) const
|
|
|
|
{
|
2007-04-25 23:21:06 +04:00
|
|
|
return fDefinition.GetLink(bucket);
|
2007-04-25 22:55:05 +04:00
|
|
|
}
|
|
|
|
|
2007-04-25 23:21:06 +04:00
|
|
|
Definition fDefinition;
|
2007-04-25 22:55:05 +04:00
|
|
|
size_t fTableSize, fItemCount;
|
2007-04-24 15:04:35 +04:00
|
|
|
ValueType **fTable;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|