Hashtable update

- use consistent type for key by introducing a new typedef
- use unsigned types for sizes
- name parameters in foreach function typedef
This commit is contained in:
Christian Göttsche 2020-10-21 21:25:59 +02:00 committed by BenBE
parent 15eab2012d
commit 7914ec201e
5 changed files with 31 additions and 29 deletions

View File

@ -100,7 +100,7 @@ static bool changePriority(MainPanel* panel, int delta) {
return anyTagged; return anyTagged;
} }
static void addUserToVector(int key, void* userCast, void* panelCast) { static void addUserToVector(hkey_t key, void* userCast, void* panelCast) {
const char* user = userCast; const char* user = userCast;
Panel* panel = panelCast; Panel* panel = panelCast;
Panel_add(panel, (Object*) ListItem_new(user, key)); Panel_add(panel, (Object*) ListItem_new(user, key));

View File

@ -14,9 +14,9 @@ in the source distribution for its full text.
#ifndef NDEBUG #ifndef NDEBUG
static bool Hashtable_isConsistent(Hashtable* this) { static bool Hashtable_isConsistent(const Hashtable* this) {
int items = 0; unsigned int items = 0;
for (int i = 0; i < this->size; i++) { for (unsigned int i = 0; i < this->size; i++) {
HashtableItem* bucket = this->buckets[i]; HashtableItem* bucket = this->buckets[i];
while (bucket) { while (bucket) {
items++; items++;
@ -26,9 +26,9 @@ static bool Hashtable_isConsistent(Hashtable* this) {
return items == this->items; return items == this->items;
} }
int Hashtable_count(Hashtable* this) { unsigned int Hashtable_count(const Hashtable* this) {
int items = 0; unsigned int items = 0;
for (int i = 0; i < this->size; i++) { for (unsigned int i = 0; i < this->size; i++) {
HashtableItem* bucket = this->buckets[i]; HashtableItem* bucket = this->buckets[i];
while (bucket) { while (bucket) {
items++; items++;
@ -41,7 +41,7 @@ int Hashtable_count(Hashtable* this) {
#endif /* NDEBUG */ #endif /* NDEBUG */
static HashtableItem* HashtableItem_new(unsigned int key, void* value) { static HashtableItem* HashtableItem_new(hkey_t key, void* value) {
HashtableItem* this; HashtableItem* this;
this = xMalloc(sizeof(HashtableItem)); this = xMalloc(sizeof(HashtableItem));
@ -51,7 +51,7 @@ static HashtableItem* HashtableItem_new(unsigned int key, void* value) {
return this; return this;
} }
Hashtable* Hashtable_new(int size, bool owner) { Hashtable* Hashtable_new(unsigned int size, bool owner) {
Hashtable* this; Hashtable* this;
this = xMalloc(sizeof(Hashtable)); this = xMalloc(sizeof(Hashtable));
@ -65,7 +65,7 @@ Hashtable* Hashtable_new(int size, bool owner) {
void Hashtable_delete(Hashtable* this) { void Hashtable_delete(Hashtable* this) {
assert(Hashtable_isConsistent(this)); assert(Hashtable_isConsistent(this));
for (int i = 0; i < this->size; i++) { for (unsigned int i = 0; i < this->size; i++) {
HashtableItem* walk = this->buckets[i]; HashtableItem* walk = this->buckets[i];
while (walk != NULL) { while (walk != NULL) {
if (this->owner) if (this->owner)
@ -80,7 +80,7 @@ void Hashtable_delete(Hashtable* this) {
free(this); free(this);
} }
void Hashtable_put(Hashtable* this, unsigned int key, void* value) { void Hashtable_put(Hashtable* this, hkey_t key, void* value) {
unsigned int index = key % this->size; unsigned int index = key % this->size;
HashtableItem** bucketPtr = &(this->buckets[index]); HashtableItem** bucketPtr = &(this->buckets[index]);
while (true) while (true)
@ -101,7 +101,7 @@ void Hashtable_put(Hashtable* this, unsigned int key, void* value) {
assert(Hashtable_isConsistent(this)); assert(Hashtable_isConsistent(this));
} }
void* Hashtable_remove(Hashtable* this, unsigned int key) { void* Hashtable_remove(Hashtable* this, hkey_t key) {
unsigned int index = key % this->size; unsigned int index = key % this->size;
assert(Hashtable_isConsistent(this)); assert(Hashtable_isConsistent(this));
@ -128,7 +128,7 @@ void* Hashtable_remove(Hashtable* this, unsigned int key) {
return NULL; return NULL;
} }
void* Hashtable_get(Hashtable* this, unsigned int key) { void* Hashtable_get(Hashtable* this, hkey_t key) {
unsigned int index = key % this->size; unsigned int index = key % this->size;
HashtableItem* bucketPtr = this->buckets[index]; HashtableItem* bucketPtr = this->buckets[index];
while (true) { while (true) {
@ -146,7 +146,7 @@ void* Hashtable_get(Hashtable* this, unsigned int key) {
void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData) { void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData) {
assert(Hashtable_isConsistent(this)); assert(Hashtable_isConsistent(this));
for (int i = 0; i < this->size; i++) { for (unsigned int i = 0; i < this->size; i++) {
HashtableItem* walk = this->buckets[i]; HashtableItem* walk = this->buckets[i];
while (walk != NULL) { while (walk != NULL) {
f(walk->key, walk->value, userData); f(walk->key, walk->value, userData);

View File

@ -10,36 +10,38 @@ in the source distribution for its full text.
#include <stdbool.h> #include <stdbool.h>
typedef void(*Hashtable_PairFunction)(int, void*, void*); typedef unsigned int hkey_t;
typedef struct HashtableItem { typedef void(*Hashtable_PairFunction)(hkey_t key, void* value, void* userdata);
unsigned int key;
typedef struct HashtableItem_ {
hkey_t key;
void* value; void* value;
struct HashtableItem* next; struct HashtableItem_* next;
} HashtableItem; } HashtableItem;
typedef struct Hashtable_ { typedef struct Hashtable_ {
int size; unsigned int size;
HashtableItem** buckets; HashtableItem** buckets;
int items; unsigned int items;
bool owner; bool owner;
} Hashtable; } Hashtable;
#ifndef NDEBUG #ifndef NDEBUG
int Hashtable_count(Hashtable* this); unsigned int Hashtable_count(const Hashtable* this);
#endif /* NDEBUG */ #endif /* NDEBUG */
Hashtable* Hashtable_new(int size, bool owner); Hashtable* Hashtable_new(unsigned int size, bool owner);
void Hashtable_delete(Hashtable* this); void Hashtable_delete(Hashtable* this);
void Hashtable_put(Hashtable* this, unsigned int key, void* value); void Hashtable_put(Hashtable* this, hkey_t key, void* value);
void* Hashtable_remove(Hashtable* this, unsigned int key); void* Hashtable_remove(Hashtable* this, hkey_t key);
void* Hashtable_get(Hashtable* this, unsigned int key); void* Hashtable_get(Hashtable* this, hkey_t key);
void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData); void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData);

View File

@ -60,14 +60,14 @@ static bool Vector_isConsistent(const Vector* this) {
} }
} }
int Vector_count(const Vector* this) { unsigned int Vector_count(const Vector* this) {
int items = 0; unsigned int items = 0;
for (int i = 0; i < this->items; i++) { for (int i = 0; i < this->items; i++) {
if (this->array[i]) { if (this->array[i]) {
items++; items++;
} }
} }
assert(items == this->items); assert(items == (unsigned int)this->items);
return items; return items;
} }

View File

@ -54,7 +54,7 @@ void Vector_set(Vector* this, int idx, void* data_);
Object* Vector_get(Vector* this, int idx); Object* Vector_get(Vector* this, int idx);
int Vector_size(const Vector* this); int Vector_size(const Vector* this);
int Vector_count(const Vector* this); unsigned int Vector_count(const Vector* this);
#else /* NDEBUG */ #else /* NDEBUG */