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:
@@ -45,6 +45,7 @@ template<typename Definition, bool AutoExpand = true,
|
|||||||
bool CheckDuplicates = false>
|
bool CheckDuplicates = false>
|
||||||
class OpenHashTable {
|
class OpenHashTable {
|
||||||
public:
|
public:
|
||||||
|
typedef OpenHashTable<Definition, AutoExpand, CheckDuplicates> HashTable;
|
||||||
typedef typename Definition::KeyType KeyType;
|
typedef typename Definition::KeyType KeyType;
|
||||||
typedef typename Definition::ValueType ValueType;
|
typedef typename Definition::ValueType ValueType;
|
||||||
|
|
||||||
@@ -165,7 +166,60 @@ public:
|
|||||||
fItemCount--;
|
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:
|
private:
|
||||||
|
// for g++ 2.95
|
||||||
|
friend class Iterator;
|
||||||
|
|
||||||
void _Insert(ValueType **table, size_t tableSize, ValueType *value)
|
void _Insert(ValueType **table, size_t tableSize, ValueType *value)
|
||||||
{
|
{
|
||||||
size_t index = fDefinition.Hash(value) & (tableSize - 1);
|
size_t index = fDefinition.Hash(value) & (tableSize - 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user