2007-04-27 19:06:30 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2007, Hugo Santos. All Rights Reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Hugo Santos, hugosantos@gmail.com
|
|
|
|
*/
|
2007-07-31 20:14:58 +04:00
|
|
|
#ifndef _KERNEL_UTIL_MULTI_HASH_TABLE_H
|
|
|
|
#define _KERNEL_UTIL_MULTI_HASH_TABLE_H
|
2007-04-27 19:06:30 +04:00
|
|
|
|
|
|
|
|
|
|
|
#include <KernelExport.h>
|
|
|
|
#include <util/kernel_cpp.h>
|
|
|
|
#include <util/OpenHashTable.h>
|
|
|
|
|
2007-07-31 20:14:58 +04:00
|
|
|
|
2007-04-27 19:06:30 +04:00
|
|
|
// MultiHashTable is a container which acts a bit like multimap<>
|
|
|
|
// but with hash table semantics.
|
|
|
|
|
|
|
|
// refer to OpenHashTable.h for how to use this container.
|
|
|
|
|
|
|
|
template<typename Definition, bool AutoExpand = true,
|
|
|
|
bool CheckDuplicates = false>
|
2009-07-27 04:39:12 +04:00
|
|
|
class MultiHashTable : private BOpenHashTable<Definition,
|
2007-04-27 19:06:30 +04:00
|
|
|
AutoExpand, CheckDuplicates> {
|
|
|
|
public:
|
2009-07-27 04:39:12 +04:00
|
|
|
typedef BOpenHashTable<Definition, AutoExpand, CheckDuplicates> HashTable;
|
2007-04-27 19:06:30 +04:00
|
|
|
typedef MultiHashTable<Definition, AutoExpand, CheckDuplicates> MultiTable;
|
|
|
|
|
|
|
|
typedef typename HashTable::Iterator Iterator;
|
|
|
|
typedef typename Definition::KeyType KeyType;
|
|
|
|
typedef typename Definition::ValueType ValueType;
|
|
|
|
|
2008-06-25 02:23:57 +04:00
|
|
|
MultiHashTable()
|
|
|
|
: HashTable() {}
|
2007-04-27 19:06:30 +04:00
|
|
|
|
2008-06-25 02:23:57 +04:00
|
|
|
MultiHashTable(const Definition& definition)
|
|
|
|
: HashTable(definition) {}
|
2007-04-27 19:06:30 +04:00
|
|
|
|
2008-06-25 02:23:57 +04:00
|
|
|
status_t Init(size_t initialSize = HashTable::kMinimumSize)
|
|
|
|
{
|
|
|
|
return HashTable::Init(initialSize);
|
|
|
|
}
|
2007-04-27 19:06:30 +04:00
|
|
|
|
|
|
|
void Insert(ValueType *value)
|
|
|
|
{
|
|
|
|
if (AutoExpand
|
|
|
|
&& HashTable::fItemCount >= (HashTable::fTableSize * 200 / 256))
|
|
|
|
_Resize(HashTable::fTableSize * 2);
|
|
|
|
|
|
|
|
InsertUnchecked(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertUnchecked(ValueType *value)
|
|
|
|
{
|
|
|
|
_Insert(HashTable::fTable, HashTable::fTableSize, value);
|
|
|
|
HashTable::fItemCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Remove(ValueType *value)
|
|
|
|
{
|
|
|
|
if (!HashTable::RemoveUnchecked(value))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (AutoExpand && HashTable::fTableSize > HashTable::kMinimumSize
|
|
|
|
&& HashTable::fItemCount < (HashTable::fTableSize * 50 / 256))
|
|
|
|
_Resize(HashTable::fTableSize / 2);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-05-01 15:38:59 +04:00
|
|
|
Iterator GetIterator() const { return HashTable::GetIterator(); }
|
|
|
|
|
2007-05-01 16:16:36 +04:00
|
|
|
class ValueIterator : protected Iterator {
|
|
|
|
public:
|
|
|
|
ValueIterator(const HashTable *table, size_t index, ValueType *value)
|
|
|
|
: fOriginalIndex(index), fOriginalValue(value)
|
|
|
|
{
|
|
|
|
Iterator::fTable = table;
|
2007-05-02 18:08:55 +04:00
|
|
|
Rewind();
|
2007-05-01 16:16:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HasNext() const
|
|
|
|
{
|
|
|
|
if (Iterator::fNext == NULL)
|
|
|
|
return false;
|
|
|
|
if (Iterator::fNext == fOriginalValue)
|
|
|
|
return true;
|
|
|
|
return ((const MultiTable *)Iterator::fTable)->_Definition().CompareValues(
|
|
|
|
fOriginalValue, Iterator::fNext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rewind()
|
|
|
|
{
|
2007-05-02 18:08:55 +04:00
|
|
|
Iterator::fIndex = fOriginalIndex + 1;
|
2007-05-01 16:16:36 +04:00
|
|
|
Iterator::fNext = fOriginalValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueType *Next() { return Iterator::Next(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t fOriginalIndex;
|
|
|
|
ValueType *fOriginalValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
ValueIterator Lookup(const KeyType &key) const
|
|
|
|
{
|
2008-06-25 02:23:57 +04:00
|
|
|
size_t index = 0;
|
|
|
|
ValueType *slot = NULL;
|
|
|
|
if (HashTable::fTableSize > 0) {
|
|
|
|
index = HashTable::fDefinition.HashKey(key)
|
|
|
|
& (HashTable::fTableSize - 1);
|
|
|
|
slot = HashTable::fTable[index];
|
|
|
|
}
|
2007-05-01 16:16:36 +04:00
|
|
|
|
|
|
|
while (slot) {
|
|
|
|
if (HashTable::fDefinition.Compare(key, slot))
|
|
|
|
break;
|
2009-07-27 04:39:12 +04:00
|
|
|
slot = HashTable::_Link(slot);
|
2007-05-01 16:16:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (slot == NULL)
|
|
|
|
return ValueIterator(this, HashTable::fTableSize, NULL);
|
|
|
|
|
|
|
|
return ValueIterator(this, index, slot);
|
|
|
|
}
|
|
|
|
|
2007-04-27 19:06:30 +04:00
|
|
|
private:
|
2007-05-01 16:16:36 +04:00
|
|
|
// for g++ 2.95
|
|
|
|
friend class ValueIterator;
|
|
|
|
|
|
|
|
const Definition &_Definition() const { return HashTable::fDefinition; }
|
|
|
|
|
2007-04-27 19:06:30 +04:00
|
|
|
void _Insert(ValueType **table, size_t tableSize, ValueType *value)
|
|
|
|
{
|
|
|
|
size_t index = HashTable::fDefinition.Hash(value) & (tableSize - 1);
|
|
|
|
|
|
|
|
ValueType *previous;
|
|
|
|
|
|
|
|
// group values with the same key
|
|
|
|
for (previous = table[index]; previous
|
|
|
|
&& !HashTable::fDefinition.CompareValues(previous, value);
|
2009-07-27 04:39:12 +04:00
|
|
|
previous = HashTable::_Link(previous));
|
2007-04-27 19:06:30 +04:00
|
|
|
|
|
|
|
if (previous) {
|
2009-07-27 04:39:12 +04:00
|
|
|
_Link(value) = _Link(previous);
|
|
|
|
_Link(previous) = value;
|
2007-04-27 19:06:30 +04:00
|
|
|
} else {
|
2009-07-27 04:39:12 +04:00
|
|
|
_Link(value) = table[index];
|
2007-04-27 19:06:30 +04:00
|
|
|
table[index] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
// TODO use BOpenHashTable's _Resize
|
2007-04-27 19:06:30 +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 (HashTable::fTable) {
|
|
|
|
for (size_t i = 0; i < HashTable::fTableSize; i++) {
|
|
|
|
ValueType *bucket = HashTable::fTable[i];
|
|
|
|
while (bucket) {
|
2009-07-27 04:39:12 +04:00
|
|
|
ValueType *next = _Link(bucket);
|
2007-04-27 19:06:30 +04:00
|
|
|
_Insert(newTable, newSize, bucket);
|
|
|
|
bucket = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] HashTable::fTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashTable::fTableSize = newSize;
|
|
|
|
HashTable::fTable = newTable;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-07-31 20:14:58 +04:00
|
|
|
#endif // _KERNEL_UTIL_MULTI_HASH_TABLE_H
|