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-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) {}
|
|
|
|
//
|
|
|
|
// size_t HashKey(int key) { return key >> 1; }
|
|
|
|
// size_t Hash(Foo *value) { return HashKey(value->bar); }
|
|
|
|
// bool Compare(int key, Foo *value) { return value->bar == key; }
|
|
|
|
// HashTableLink<Foo> *GetLink(Foo *value) { 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:
|
|
|
|
typedef typename Definition::KeyType KeyType;
|
|
|
|
typedef typename Definition::ValueType ValueType;
|
|
|
|
|
|
|
|
static const size_t kMinimumSize = 32;
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
: fItemCount(0), fTable(NULL)
|
|
|
|
{
|
|
|
|
if (initialSize < kMinimumSize)
|
|
|
|
initialSize = kMinimumSize;
|
|
|
|
|
|
|
|
_Resize(initialSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenHashTable(typename Definition::ParentType *parent,
|
|
|
|
size_t initialSize = kMinimumSize)
|
|
|
|
: fDefinition(parent), fItemCount(0), fTable(NULL)
|
2007-04-24 15:04:35 +04:00
|
|
|
{
|
|
|
|
if (initialSize < kMinimumSize)
|
|
|
|
initialSize = kMinimumSize;
|
|
|
|
|
|
|
|
_Resize(initialSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
~OpenHashTable()
|
|
|
|
{
|
|
|
|
delete [] fTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t InitCheck() const { return fTable ? B_OK : B_NO_MEMORY; }
|
|
|
|
|
|
|
|
ValueType *Lookup(const KeyType &key) const
|
|
|
|
{
|
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-25 22:55:05 +04:00
|
|
|
void Insert(ValueType *value)
|
2007-04-24 15:04:35 +04:00
|
|
|
{
|
2007-04-25 22:55:05 +04:00
|
|
|
if (AutoExpand && fItemCount >= (fTableSize * 200 / 256))
|
|
|
|
_Resize(fTableSize * 2);
|
2007-04-24 15:04:35 +04:00
|
|
|
|
|
|
|
InsertUnchecked(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Remove(ValueType *value)
|
|
|
|
{
|
|
|
|
RemoveUnchecked(value);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveUnchecked(ValueType *value)
|
|
|
|
{
|
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-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--;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
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
|