added iterator to OpenHashTable.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20853 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Hugo Santos 2007-04-27 08:07:38 +00:00
parent 0cd08b100c
commit 8ac2dba331
1 changed files with 54 additions and 0 deletions

View File

@ -45,6 +45,7 @@ template<typename Definition, bool AutoExpand = true,
bool CheckDuplicates = false>
class OpenHashTable {
public:
typedef OpenHashTable<Definition, AutoExpand, CheckDuplicates> HashTable;
typedef typename Definition::KeyType KeyType;
typedef typename Definition::ValueType ValueType;
@ -165,7 +166,60 @@ public:
fItemCount--;
}
class Iterator {
public:
Iterator(const HashTable *table)
: fTable(table), fIndex(0), fCurrent(NULL), fNext(NULL)
{
Rewind();
}
bool HasNext() const { return fNext != NULL; }
ValueType *Next()
{
ValueType *current = fCurrent;
fCurrent = fNext;
_GetNext();
return current;
}
void Rewind()
{
// get the first one
fIndex = 0;
fCurrent = NULL;
_GetNext();
fCurrent = fNext;
// get the proper next one
if (fCurrent)
_GetNext();
}
private:
void _GetNext()
{
if (fCurrent) {
fNext = fTable->_Link(fCurrent)->fNext;
} else {
fNext = NULL;
}
while (fNext == NULL && fIndex < fTable->fTableSize)
fNext = fTable->fTable[fIndex++];
}
const HashTable *fTable;
size_t fIndex;
ValueType *fCurrent, *fNext;
};
Iterator GetIterator() const { return Iterator(this); }
private:
// for g++ 2.95
friend class Iterator;
void _Insert(ValueType **table, size_t tableSize, ValueType *value)
{
size_t index = fDefinition.Hash(value) & (tableSize - 1);