From 2d4fb82c0faf1bbe8c7a163227be83f347bb605f Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sat, 7 Mar 2009 21:19:15 +0000 Subject: [PATCH] Added TableSize(), CountElements(), and Clear() methods. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@29428 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/kernel/util/OpenHashTable.h | 46 +++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/headers/private/kernel/util/OpenHashTable.h b/headers/private/kernel/util/OpenHashTable.h index f233dc46ec..0594280767 100644 --- a/headers/private/kernel/util/OpenHashTable.h +++ b/headers/private/kernel/util/OpenHashTable.h @@ -90,6 +90,16 @@ public: return B_OK; } + size_t TableSize() const + { + return fTableSize; + } + + size_t CountElements() const + { + return fItemCount; + } + ValueType *Lookup(const KeyType &key) const { if (fTableSize == 0) @@ -182,6 +192,42 @@ public: return true; } + /*! 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. + */ + ValueType* Clear(bool returnElements = false) + { + if (this->fItemCount == 0) + 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) { + nextPointer = &_Link(element)->fNext; + element = *nextPointer; + } + } + } + } + + memset(this->fTable, 0, sizeof(ValueType*) * this->fTableSize); + + return result; + } + /*! If the table needs resizing, the number of bytes for the required allocation is returned. If no resizing is needed, 0 is returned. */