Skip to content

Commit

Permalink
Added Hashtable::Size() method.
Browse files Browse the repository at this point in the history
* Automatic whitespace cleanup.
  • Loading branch information
axeld committed Dec 2, 2012
1 parent 2ede175 commit 772c970
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
25 changes: 16 additions & 9 deletions src/bin/bfs_tools/lib/Hashtable.cpp
Expand Up @@ -40,11 +40,11 @@ Hashtable::Hashtable(int capacity, float loadFactor)

if (!capacity)
capacity = 1;

if (!(fTable = (struct Entry **)malloc(capacity * sizeof(void *))))
return;
memset(fTable,0,capacity * sizeof(void *));

fThreshold = (int)(capacity * loadFactor);
fModCount = 0;
fLoadFactor = loadFactor;
Expand All @@ -58,7 +58,7 @@ Hashtable::Hashtable(int capacity, float loadFactor)
Hashtable::~Hashtable()
{
struct Entry **table = fTable;

for(int32 index = fCapacity;--index >= 0;)
{
struct Entry *entry,*next;
Expand Down Expand Up @@ -136,7 +136,7 @@ void *Hashtable::Remove(const void *key)
table = fTable;
hash = (func = fHashFunc)(key);
index = hash % fCapacity;

for(entry = table[index],prev = NULL;entry;entry = entry->next)
{
if ((func(entry->key) == hash) && fCompareFunc(entry->key,key))
Expand All @@ -148,7 +148,7 @@ void *Hashtable::Remove(const void *key)
prev->next = entry->next;
else
table[index] = entry->next;

fCount--;
value = entry->value;
delete entry;
Expand Down Expand Up @@ -180,7 +180,7 @@ status_t Hashtable::GetNextEntry(void **value)
*value = fIteratorEntry->value;
return B_OK;
}

return B_ENTRY_NOT_FOUND;
}

Expand Down Expand Up @@ -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.
* @return true on success
*/

bool Hashtable::Rehash()
{
uint32 (*hashCode)(const void *) = fHashFunc;
Expand Down Expand Up @@ -270,10 +277,10 @@ Hashtable::Entry *Hashtable::GetHashEntry(const void *key)
{
Entry **table,*entry;
uint32 hash,(*func)(const void *);

table = fTable;
hash = (func = fHashFunc)(key);

for(entry = table[hash % fCapacity];entry;entry = entry->next)
{
if ((func(entry->key) == hash) && fCompareFunc(entry->key,key))
Expand Down
3 changes: 2 additions & 1 deletion src/bin/bfs_tools/lib/Hashtable.h
Expand Up @@ -33,7 +33,8 @@ class Hashtable
void Rewind();

void MakeEmpty(int8 keyMode = HASH_EMPTY_NONE,int8 valueMode = HASH_EMPTY_NONE);

size_t Size() const;

protected:
class Entry
{
Expand Down

0 comments on commit 772c970

Please sign in to comment.