2007-04-24 15:04:35 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2007, Hugo Santos. All Rights Reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2007-07-31 20:14:58 +04:00
|
|
|
#ifndef _KERNEL_UTIL_OPEN_HASH_TABLE_H
|
|
|
|
#define _KERNEL_UTIL_OPEN_HASH_TABLE_H
|
2007-04-24 15:04:35 +04:00
|
|
|
|
|
|
|
|
2009-02-27 19:11:57 +03:00
|
|
|
#include <OS.h>
|
2009-02-28 01:44:05 +03:00
|
|
|
#include <stdlib.h>
|
2009-04-25 22:34:24 +04:00
|
|
|
#include <string.h>
|
2009-02-27 19:11:57 +03:00
|
|
|
|
|
|
|
#ifdef _KERNEL_MODE
|
|
|
|
# include <KernelExport.h>
|
|
|
|
# include <util/kernel_cpp.h>
|
2014-04-20 02:26:05 +04:00
|
|
|
# include <util/TypeOperation.h>
|
|
|
|
#else
|
|
|
|
# include <TypeOperation.h>
|
2009-02-27 19:11:57 +03:00
|
|
|
#endif
|
2007-04-24 15:04:35 +04:00
|
|
|
|
2007-07-31 20:14:58 +04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
The Definition template must have four methods: `HashKey', `Hash',
|
|
|
|
`Compare' and `GetLink;. It must also define several types as shown in the
|
|
|
|
following example:
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
struct Foo {
|
2007-07-31 20:14:58 +04:00
|
|
|
int bar;
|
2008-07-10 16:19:22 +04:00
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
Foo* fNext;
|
2007-07-31 20:14:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct HashTableDefinition {
|
|
|
|
typedef int KeyType;
|
|
|
|
typedef Foo ValueType;
|
|
|
|
|
2015-01-09 19:28:40 +03:00
|
|
|
size_t HashKey(KeyType key) const
|
2010-01-25 15:18:20 +03:00
|
|
|
{
|
|
|
|
return key >> 1;
|
|
|
|
}
|
2007-07-31 20:14:58 +04:00
|
|
|
|
2015-01-09 19:28:40 +03:00
|
|
|
size_t Hash(ValueType* value) const
|
2010-01-25 15:18:20 +03:00
|
|
|
{
|
|
|
|
return HashKey(value->bar);
|
|
|
|
}
|
|
|
|
|
2015-01-09 19:28:40 +03:00
|
|
|
bool Compare(KeyType key, ValueType* value) const
|
2010-01-25 15:18:20 +03:00
|
|
|
{
|
|
|
|
return value->bar == key;
|
|
|
|
}
|
|
|
|
|
2015-01-09 19:28:40 +03:00
|
|
|
ValueType*& GetLink(ValueType* value) const
|
2010-01-25 15:18:20 +03:00
|
|
|
{
|
|
|
|
return value->fNext;
|
|
|
|
}
|
2007-07-31 20:14:58 +04:00
|
|
|
};
|
|
|
|
*/
|
2007-04-24 15:04:35 +04:00
|
|
|
|
2007-04-25 22:55:05 +04:00
|
|
|
|
2010-01-25 15:18:20 +03:00
|
|
|
struct MallocAllocator {
|
|
|
|
void* Allocate(size_t size) const
|
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Free(void* memory) const
|
|
|
|
{
|
|
|
|
free(memory);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-13 15:31:26 +03:00
|
|
|
/** Implements an hash table with open hashing, that is, colliding entries are
|
|
|
|
* stored in a linked list. The table may be made to adjust its number of slots
|
|
|
|
* depending on the load factor (this should be enabled unless the object is to
|
|
|
|
* be used at times where memory allocations aren't possible, such as code
|
|
|
|
* called byt he memory allocator).
|
|
|
|
*
|
|
|
|
* The link between entries is part of the ValueType stored items, which makes
|
|
|
|
* sure the table can always accept new items and will never fail because it is
|
|
|
|
* out of memory (except at Init time).
|
|
|
|
*/
|
2007-04-25 22:55:05 +04:00
|
|
|
template<typename Definition, bool AutoExpand = true,
|
2010-01-25 15:18:20 +03:00
|
|
|
bool CheckDuplicates = false, typename Allocator = MallocAllocator>
|
2009-07-27 04:39:12 +04:00
|
|
|
class BOpenHashTable {
|
2007-04-24 15:04:35 +04:00
|
|
|
public:
|
2009-07-27 04:39:12 +04:00
|
|
|
typedef BOpenHashTable<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
|
|
|
|
2010-01-25 15:18:20 +03:00
|
|
|
// All allocations are of power of 2 lengths.
|
2007-04-24 15:04:35 +04:00
|
|
|
|
|
|
|
// regrowth factor: 200 / 256 = 78.125%
|
|
|
|
// 50 / 256 = 19.53125%
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
BOpenHashTable()
|
2007-07-31 20:14:58 +04:00
|
|
|
:
|
|
|
|
fTableSize(0),
|
|
|
|
fItemCount(0),
|
|
|
|
fTable(NULL)
|
2007-04-25 23:21:06 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
BOpenHashTable(const Definition& definition)
|
2007-07-31 20:14:58 +04:00
|
|
|
:
|
|
|
|
fDefinition(definition),
|
|
|
|
fTableSize(0),
|
|
|
|
fItemCount(0),
|
|
|
|
fTable(NULL)
|
2007-04-24 15:04:35 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-01-25 15:18:20 +03:00
|
|
|
BOpenHashTable(const Definition& definition, const Allocator& allocator)
|
|
|
|
:
|
|
|
|
fDefinition(definition),
|
|
|
|
fAllocator(allocator),
|
|
|
|
fTableSize(0),
|
|
|
|
fItemCount(0),
|
|
|
|
fTable(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
~BOpenHashTable()
|
2007-04-24 15:04:35 +04:00
|
|
|
{
|
2010-01-25 15:18:20 +03:00
|
|
|
fAllocator.Free(fTable);
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
|
|
|
|
2008-06-25 02:23:57 +04:00
|
|
|
status_t Init(size_t initialSize = kMinimumSize)
|
2007-04-30 16:31:31 +04:00
|
|
|
{
|
2008-06-25 02:23:57 +04:00
|
|
|
if (initialSize > 0 && !_Resize(initialSize))
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
return B_OK;
|
2007-04-30 16:31:31 +04:00
|
|
|
}
|
2007-04-24 15:04:35 +04:00
|
|
|
|
2009-03-08 00:19:15 +03:00
|
|
|
size_t TableSize() const
|
|
|
|
{
|
|
|
|
return fTableSize;
|
|
|
|
}
|
|
|
|
|
2013-06-27 23:14:11 +04:00
|
|
|
bool IsEmpty() const
|
|
|
|
{
|
|
|
|
return fItemCount == 0;
|
|
|
|
}
|
|
|
|
|
2009-03-08 00:19:15 +03:00
|
|
|
size_t CountElements() const
|
|
|
|
{
|
|
|
|
return fItemCount;
|
|
|
|
}
|
|
|
|
|
2014-04-20 02:26:05 +04:00
|
|
|
ValueType* Lookup(typename TypeOperation<KeyType>::ConstRefT key) const
|
2007-04-24 15:04:35 +04:00
|
|
|
{
|
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);
|
2009-07-27 04:39:12 +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;
|
2009-07-27 04:39:12 +04:00
|
|
|
slot = _Link(slot);
|
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
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +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
|
|
|
}
|
|
|
|
|
2015-01-13 15:31:26 +03:00
|
|
|
/*! \brief Inserts a value without resizing the table.
|
|
|
|
|
|
|
|
Use this method if you need to insert a value into the table while
|
|
|
|
iterating it, as regular insertion can invalidate iterators.
|
|
|
|
*/
|
2009-07-27 04:39:12 +04:00
|
|
|
void InsertUnchecked(ValueType* value)
|
2007-04-24 15:04:35 +04:00
|
|
|
{
|
2009-02-27 19:11:57 +03:00
|
|
|
if (CheckDuplicates && _ExhaustiveSearch(value)) {
|
|
|
|
#ifdef _KERNEL_MODE
|
2007-05-02 18:08:55 +04:00
|
|
|
panic("Hash Table: value already in table.");
|
2009-02-27 19:11:57 +03:00
|
|
|
#else
|
|
|
|
debugger("Hash Table: value already in table.");
|
|
|
|
#endif
|
|
|
|
}
|
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++;
|
|
|
|
}
|
|
|
|
|
2008-07-10 16:19:22 +04:00
|
|
|
// TODO: a ValueType* Remove(const KeyType& key) method is missing
|
|
|
|
|
2009-07-27 04:39:12 +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
|
|
|
}
|
|
|
|
|
2015-01-13 15:31:26 +03:00
|
|
|
/*! \brief Removes a value without resizing the table.
|
|
|
|
|
|
|
|
Use this method if you need to remove a value from the table while
|
|
|
|
iterating it, as Remove can invalidate iterators.
|
|
|
|
|
|
|
|
Also use this method if you know you are going to reinsert the item soon
|
|
|
|
(possibly with a different hash) to avoid shrinking then growing the
|
|
|
|
table again.
|
|
|
|
*/
|
2009-07-27 04:39:12 +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);
|
2009-07-27 04:39:12 +04:00
|
|
|
ValueType* previous = NULL;
|
|
|
|
ValueType* slot = fTable[index];
|
2007-04-24 15:04:35 +04:00
|
|
|
|
2007-04-25 22:55:05 +04:00
|
|
|
while (slot) {
|
2009-07-27 04:39:12 +04:00
|
|
|
ValueType* next = _Link(slot);
|
2007-04-25 22:55:05 +04:00
|
|
|
|
|
|
|
if (value == slot) {
|
|
|
|
if (previous)
|
2009-07-27 04:39:12 +04:00
|
|
|
_Link(previous) = next;
|
2007-04-25 22:55:05 +04:00
|
|
|
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;
|
|
|
|
|
2009-02-27 19:11:57 +03:00
|
|
|
if (CheckDuplicates && _ExhaustiveSearch(value)) {
|
|
|
|
#ifdef _KERNEL_MODE
|
2007-05-02 18:08:55 +04:00
|
|
|
panic("Hash Table: duplicate detected.");
|
2009-02-27 19:11:57 +03:00
|
|
|
#else
|
|
|
|
debugger("Hash Table: duplicate detected.");
|
|
|
|
#endif
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-01-13 15:31:26 +03:00
|
|
|
/*! \brief Removes all elements from the hash table.
|
|
|
|
|
|
|
|
No resizing happens. The elements are not deleted. If \a returnElements
|
|
|
|
is \c true, the method returns all elements chained via their hash table
|
|
|
|
link.
|
2009-03-08 00:19:15 +03:00
|
|
|
*/
|
|
|
|
ValueType* Clear(bool returnElements = false)
|
|
|
|
{
|
2013-04-18 17:52:57 +04:00
|
|
|
if (fItemCount == 0)
|
2009-03-08 00:19:15 +03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ValueType* result = NULL;
|
|
|
|
|
|
|
|
if (returnElements) {
|
|
|
|
ValueType** nextPointer = &result;
|
|
|
|
|
|
|
|
// iterate through all buckets
|
|
|
|
for (size_t i = 0; i < fTableSize; i++) {
|
|
|
|
ValueType* element = fTable[i];
|
|
|
|
if (element != NULL) {
|
|
|
|
// add the bucket to the list
|
|
|
|
*nextPointer = element;
|
|
|
|
|
|
|
|
// update nextPointer to point to the fNext of the last
|
|
|
|
// element in the bucket
|
|
|
|
while (element != NULL) {
|
2009-07-27 04:39:12 +04:00
|
|
|
nextPointer = &_Link(element);
|
2009-03-08 00:19:15 +03:00
|
|
|
element = *nextPointer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(this->fTable, 0, sizeof(ValueType*) * this->fTableSize);
|
2013-04-18 17:52:57 +04:00
|
|
|
fItemCount = 0;
|
2009-03-08 00:19:15 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-08-22 05:10:24 +04:00
|
|
|
/*! If the table needs resizing, the number of bytes for the required
|
|
|
|
allocation is returned. If no resizing is needed, 0 is returned.
|
|
|
|
*/
|
|
|
|
size_t ResizeNeeded() const
|
|
|
|
{
|
|
|
|
size_t size = fTableSize;
|
|
|
|
if (size == 0 || fItemCount >= (size * 200 / 256)) {
|
|
|
|
// grow table
|
|
|
|
if (size == 0)
|
|
|
|
size = kMinimumSize;
|
|
|
|
while (fItemCount >= size * 200 / 256)
|
|
|
|
size <<= 1;
|
|
|
|
} else if (size > kMinimumSize && fItemCount < size * 50 / 256) {
|
|
|
|
// shrink table
|
|
|
|
while (fItemCount < size * 50 / 256)
|
|
|
|
size >>= 1;
|
|
|
|
if (size < kMinimumSize)
|
|
|
|
size = kMinimumSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == fTableSize)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return size * sizeof(ValueType*);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! Resizes the table using the given allocation. The allocation must not
|
2015-01-13 15:31:26 +03:00
|
|
|
be \c NULL. It must be of size \a size, which must be a value returned
|
2008-08-22 05:10:24 +04:00
|
|
|
earlier by ResizeNeeded(). If the size requirements have changed in the
|
2010-01-22 01:16:03 +03:00
|
|
|
meantime, the method free()s the given allocation and returns \c false,
|
|
|
|
unless \a force is \c true, in which case the supplied allocation is
|
|
|
|
used in any event.
|
2008-08-22 05:10:24 +04:00
|
|
|
Otherwise \c true is returned.
|
2010-01-25 15:18:20 +03:00
|
|
|
If \a oldTable is non-null and resizing is successful, the old table
|
|
|
|
will not be freed, but will be returned via this parameter instead.
|
2008-08-22 05:10:24 +04:00
|
|
|
*/
|
2010-01-25 15:18:20 +03:00
|
|
|
bool Resize(void* allocation, size_t size, bool force = false,
|
|
|
|
void** oldTable = NULL)
|
2008-08-22 05:10:24 +04:00
|
|
|
{
|
2010-01-22 01:16:03 +03:00
|
|
|
if (!force && size != ResizeNeeded()) {
|
2010-01-25 15:18:20 +03:00
|
|
|
fAllocator.Free(allocation);
|
2008-08-22 05:10:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-25 15:18:20 +03:00
|
|
|
_Resize((ValueType**)allocation, size / sizeof(ValueType*), oldTable);
|
2008-08-22 05:10:24 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-13 15:31:26 +03:00
|
|
|
/*! \brief Iterator for BOpenHashMap
|
|
|
|
|
|
|
|
The iterator is not invalidated when removing the current element from
|
|
|
|
the table, unless the removal triggers a resize.
|
|
|
|
*/
|
2007-04-27 12:07:38 +04:00
|
|
|
class Iterator {
|
|
|
|
public:
|
2009-07-27 04:39:12 +04:00
|
|
|
Iterator(const HashTable* table)
|
2007-04-27 18:47:34 +04:00
|
|
|
: fTable(table)
|
2007-04-27 12:07:38 +04:00
|
|
|
{
|
|
|
|
Rewind();
|
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
Iterator(const HashTable* table, size_t index, ValueType* value)
|
2007-04-27 19:06:30 +04:00
|
|
|
: fTable(table), fIndex(index), fNext(value) {}
|
|
|
|
|
2007-04-27 12:07:38 +04:00
|
|
|
bool HasNext() const { return fNext != NULL; }
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
ValueType* Next()
|
2007-04-27 12:07:38 +04:00
|
|
|
{
|
2009-07-27 04:39:12 +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();
|
|
|
|
}
|
|
|
|
|
2007-05-01 16:16:36 +04:00
|
|
|
protected:
|
|
|
|
Iterator() {}
|
|
|
|
|
2007-04-27 12:07:38 +04:00
|
|
|
void _GetNext()
|
|
|
|
{
|
2007-04-27 18:47:34 +04:00
|
|
|
if (fNext)
|
2009-07-27 04:39:12 +04:00
|
|
|
fNext = fTable->_Link(fNext);
|
2007-04-27 12:07:38 +04:00
|
|
|
|
|
|
|
while (fNext == NULL && fIndex < fTable->fTableSize)
|
|
|
|
fNext = fTable->fTable[fIndex++];
|
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
const HashTable* fTable;
|
2007-04-27 12:07:38 +04:00
|
|
|
size_t fIndex;
|
2009-07-27 04:39:12 +04:00
|
|
|
ValueType* fNext;
|
2007-04-27 12:07:38 +04:00
|
|
|
};
|
|
|
|
|
2010-01-09 23:19:29 +03:00
|
|
|
Iterator GetIterator() const
|
|
|
|
{
|
|
|
|
return Iterator(this);
|
|
|
|
}
|
|
|
|
|
2014-04-20 02:26:05 +04:00
|
|
|
Iterator GetIterator(typename TypeOperation<KeyType>::ConstRefT key) const
|
2010-01-09 23:19:29 +03:00
|
|
|
{
|
|
|
|
if (fTableSize == 0)
|
|
|
|
return Iterator(this, fTableSize, NULL);
|
|
|
|
|
|
|
|
size_t index = fDefinition.HashKey(key) & (fTableSize - 1);
|
|
|
|
ValueType* slot = fTable[index];
|
|
|
|
|
|
|
|
while (slot) {
|
|
|
|
if (fDefinition.Compare(key, slot))
|
|
|
|
break;
|
|
|
|
slot = _Link(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slot == NULL)
|
|
|
|
return Iterator(this, fTableSize, NULL);
|
|
|
|
|
|
|
|
return Iterator(this, index + 1, slot);
|
|
|
|
}
|
2007-04-27 12:07:38 +04:00
|
|
|
|
2007-04-27 19:06:30 +04:00
|
|
|
protected:
|
2007-04-27 12:07:38 +04:00
|
|
|
// for g++ 2.95
|
|
|
|
friend class Iterator;
|
|
|
|
|
2009-07-27 04:39:12 +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
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
_Link(value) = table[index];
|
2007-04-25 22:55:05 +04:00
|
|
|
table[index] = value;
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool _Resize(size_t newSize)
|
|
|
|
{
|
2008-08-22 05:10:24 +04:00
|
|
|
ValueType** newTable
|
2010-01-25 15:18:20 +03:00
|
|
|
= (ValueType**)fAllocator.Allocate(sizeof(ValueType*) * newSize);
|
2007-04-24 15:04:35 +04:00
|
|
|
if (newTable == NULL)
|
|
|
|
return false;
|
|
|
|
|
2008-08-22 05:10:24 +04:00
|
|
|
_Resize(newTable, newSize);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-11 00:54:38 +03:00
|
|
|
void _Resize(ValueType** newTable, size_t newSize, void** _oldTable = NULL)
|
2008-08-22 05:10:24 +04:00
|
|
|
{
|
2007-04-24 15:04:35 +04:00
|
|
|
for (size_t i = 0; i < newSize; i++)
|
|
|
|
newTable[i] = NULL;
|
|
|
|
|
|
|
|
if (fTable) {
|
|
|
|
for (size_t i = 0; i < fTableSize; i++) {
|
2009-07-27 04:39:12 +04:00
|
|
|
ValueType* bucket = fTable[i];
|
2007-04-25 22:55:05 +04:00
|
|
|
while (bucket) {
|
2009-07-27 04:39:12 +04:00
|
|
|
ValueType* next = _Link(bucket);
|
2007-04-25 22:55:05 +04:00
|
|
|
_Insert(newTable, newSize, bucket);
|
|
|
|
bucket = next;
|
|
|
|
}
|
2007-04-24 15:04:35 +04:00
|
|
|
}
|
|
|
|
|
2011-01-11 00:54:38 +03:00
|
|
|
if (_oldTable != NULL)
|
|
|
|
*_oldTable = fTable;
|
2010-01-25 15:18:20 +03:00
|
|
|
else
|
|
|
|
fAllocator.Free(fTable);
|
2011-01-11 00:54:38 +03:00
|
|
|
} else if (_oldTable != NULL)
|
|
|
|
*_oldTable = NULL;
|
2007-04-24 15:04:35 +04:00
|
|
|
|
|
|
|
fTableSize = newSize;
|
|
|
|
fTable = newTable;
|
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
ValueType*& _Link(ValueType* bucket) const
|
2007-04-25 22:55:05 +04:00
|
|
|
{
|
2007-04-25 23:21:06 +04:00
|
|
|
return fDefinition.GetLink(bucket);
|
2007-04-25 22:55:05 +04:00
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
bool _ExhaustiveSearch(ValueType* value) const
|
2007-05-02 18:08:55 +04:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < fTableSize; i++) {
|
2009-07-27 04:39:12 +04:00
|
|
|
ValueType* bucket = fTable[i];
|
2007-05-02 18:08:55 +04:00
|
|
|
while (bucket) {
|
|
|
|
if (bucket == value)
|
|
|
|
return true;
|
2009-07-27 04:39:12 +04:00
|
|
|
bucket = _Link(bucket);
|
2007-05-02 18:08:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-27 04:39:12 +04:00
|
|
|
Definition fDefinition;
|
2010-01-25 15:18:20 +03:00
|
|
|
Allocator fAllocator;
|
2009-07-27 04:39:12 +04:00
|
|
|
size_t fTableSize;
|
|
|
|
size_t fItemCount;
|
|
|
|
ValueType** fTable;
|
2007-04-24 15:04:35 +04:00
|
|
|
};
|
|
|
|
|
2007-07-31 20:14:58 +04:00
|
|
|
#endif // _KERNEL_UTIL_OPEN_HASH_TABLE_H
|