Added Hashtable::Size() method.

* Automatic whitespace cleanup.
This commit is contained in:
Axel Dörfler
2012-12-02 21:03:41 +01:00
parent 2ede175119
commit 772c9704b9
2 changed files with 18 additions and 10 deletions
+16 -9
View File
@@ -40,11 +40,11 @@ Hashtable::Hashtable(int capacity, float loadFactor)
if (!capacity) if (!capacity)
capacity = 1; capacity = 1;
if (!(fTable = (struct Entry **)malloc(capacity * sizeof(void *)))) if (!(fTable = (struct Entry **)malloc(capacity * sizeof(void *))))
return; return;
memset(fTable,0,capacity * sizeof(void *)); memset(fTable,0,capacity * sizeof(void *));
fThreshold = (int)(capacity * loadFactor); fThreshold = (int)(capacity * loadFactor);
fModCount = 0; fModCount = 0;
fLoadFactor = loadFactor; fLoadFactor = loadFactor;
@@ -58,7 +58,7 @@ Hashtable::Hashtable(int capacity, float loadFactor)
Hashtable::~Hashtable() Hashtable::~Hashtable()
{ {
struct Entry **table = fTable; struct Entry **table = fTable;
for(int32 index = fCapacity;--index >= 0;) for(int32 index = fCapacity;--index >= 0;)
{ {
struct Entry *entry,*next; struct Entry *entry,*next;
@@ -136,7 +136,7 @@ void *Hashtable::Remove(const void *key)
table = fTable; table = fTable;
hash = (func = fHashFunc)(key); hash = (func = fHashFunc)(key);
index = hash % fCapacity; index = hash % fCapacity;
for(entry = table[index],prev = NULL;entry;entry = entry->next) for(entry = table[index],prev = NULL;entry;entry = entry->next)
{ {
if ((func(entry->key) == hash) && fCompareFunc(entry->key,key)) if ((func(entry->key) == hash) && fCompareFunc(entry->key,key))
@@ -148,7 +148,7 @@ void *Hashtable::Remove(const void *key)
prev->next = entry->next; prev->next = entry->next;
else else
table[index] = entry->next; table[index] = entry->next;
fCount--; fCount--;
value = entry->value; value = entry->value;
delete entry; delete entry;
@@ -180,7 +180,7 @@ status_t Hashtable::GetNextEntry(void **value)
*value = fIteratorEntry->value; *value = fIteratorEntry->value;
return B_OK; return B_OK;
} }
return B_ENTRY_NOT_FOUND; return B_ENTRY_NOT_FOUND;
} }
@@ -227,10 +227,17 @@ Hashtable::MakeEmpty(int8 keyMode,int8 valueMode)
} }
size_t
Hashtable::Size() const
{
return fCount;
}
/** The hash table will be doubled in size, and rebuild. /** The hash table will be doubled in size, and rebuild.
* @return true on success * @return true on success
*/ */
bool Hashtable::Rehash() bool Hashtable::Rehash()
{ {
uint32 (*hashCode)(const void *) = fHashFunc; uint32 (*hashCode)(const void *) = fHashFunc;
@@ -270,10 +277,10 @@ Hashtable::Entry *Hashtable::GetHashEntry(const void *key)
{ {
Entry **table,*entry; Entry **table,*entry;
uint32 hash,(*func)(const void *); uint32 hash,(*func)(const void *);
table = fTable; table = fTable;
hash = (func = fHashFunc)(key); hash = (func = fHashFunc)(key);
for(entry = table[hash % fCapacity];entry;entry = entry->next) for(entry = table[hash % fCapacity];entry;entry = entry->next)
{ {
if ((func(entry->key) == hash) && fCompareFunc(entry->key,key)) if ((func(entry->key) == hash) && fCompareFunc(entry->key,key))
+2 -1
View File
@@ -33,7 +33,8 @@ class Hashtable
void Rewind(); void Rewind();
void MakeEmpty(int8 keyMode = HASH_EMPTY_NONE,int8 valueMode = HASH_EMPTY_NONE); void MakeEmpty(int8 keyMode = HASH_EMPTY_NONE,int8 valueMode = HASH_EMPTY_NONE);
size_t Size() const;
protected: protected:
class Entry class Entry
{ {