diff options
author | Daniel Lange <DLange@git.local> | 2016-04-11 13:00:20 +0200 |
---|---|---|
committer | Daniel Lange <DLange@git.local> | 2016-04-11 13:00:20 +0200 |
commit | 85bb4ad9cb820ac3b8e935a930084a06cbfd2847 (patch) | |
tree | 681fd9b2d9fa80931b2a8bec4bb6667865b7c569 /Hashtable.c | |
parent | ea859f50d9438bc61ae96721a4d255b49de78653 (diff) | |
download | debian_htop-85bb4ad9cb820ac3b8e935a930084a06cbfd2847.tar.gz debian_htop-85bb4ad9cb820ac3b8e935a930084a06cbfd2847.tar.bz2 debian_htop-85bb4ad9cb820ac3b8e935a930084a06cbfd2847.zip |
Imported Upstream version 0.6.3upstream/0.6.3
Diffstat (limited to 'Hashtable.c')
-rw-r--r-- | Hashtable.c | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/Hashtable.c b/Hashtable.c index 759cbef..30218a5 100644 --- a/Hashtable.c +++ b/Hashtable.c @@ -16,7 +16,6 @@ in the source distribution for its full text. typedef struct Hashtable_ Hashtable; typedef void(*Hashtable_PairFunction)(int, void*, void*); -typedef int(*Hashtable_HashAlgorithm)(Hashtable*, int); typedef struct HashtableItem { int key; @@ -28,7 +27,6 @@ struct Hashtable_ { int size; HashtableItem** buckets; int items; - Hashtable_HashAlgorithm hashAlgorithm; bool owner; }; }*/ @@ -49,15 +47,10 @@ Hashtable* Hashtable_new(int size, bool owner) { this = (Hashtable*) malloc(sizeof(Hashtable)); this->size = size; this->buckets = (HashtableItem**) calloc(sizeof(HashtableItem*), size); - this->hashAlgorithm = Hashtable_hashAlgorithm; this->owner = owner; return this; } -int Hashtable_hashAlgorithm(Hashtable* this, int key) { - return (key % this->size); -} - void Hashtable_delete(Hashtable* this) { for (int i = 0; i < this->size; i++) { HashtableItem* walk = this->buckets[i]; @@ -78,7 +71,7 @@ inline int Hashtable_size(Hashtable* this) { } void Hashtable_put(Hashtable* this, int key, void* value) { - int index = this->hashAlgorithm(this, key); + int index = key % this->size; HashtableItem** bucketPtr = &(this->buckets[index]); while (true) if (*bucketPtr == NULL) { @@ -95,7 +88,7 @@ void Hashtable_put(Hashtable* this, int key, void* value) { } void* Hashtable_remove(Hashtable* this, int key) { - int index = this->hashAlgorithm(this, key); + int index = key % this->size; HashtableItem** bucketPtr = &(this->buckets[index]); while (true) if (*bucketPtr == NULL) { @@ -116,17 +109,20 @@ void* Hashtable_remove(Hashtable* this, int key) { } else bucketPtr = &((*bucketPtr)->next); } - +//#include <stdio.h> inline void* Hashtable_get(Hashtable* this, int key) { - int index = this->hashAlgorithm(this, key); + int index = key % this->size; HashtableItem* bucketPtr = this->buckets[index]; - while (true) + // fprintf(stderr, "%d -> %d\n", key, index); + while (true) { if (bucketPtr == NULL) { return NULL; } else if (bucketPtr->key == key) { return bucketPtr->value; } else bucketPtr = bucketPtr->next; + // fprintf(stderr, "*\n"); + } } void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData) { |