From 7914ec201ef19fa0c0caed99dc150a953eb9bc19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Wed, 21 Oct 2020 21:25:59 +0200 Subject: Hashtable update - use consistent type for key by introducing a new typedef - use unsigned types for sizes - name parameters in foreach function typedef --- Action.c | 2 +- Hashtable.c | 26 +++++++++++++------------- Hashtable.h | 24 +++++++++++++----------- Vector.c | 6 +++--- Vector.h | 2 +- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/Action.c b/Action.c index 11e87009..822c9174 100644 --- a/Action.c +++ b/Action.c @@ -100,7 +100,7 @@ static bool changePriority(MainPanel* panel, int delta) { 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; Panel* panel = panelCast; Panel_add(panel, (Object*) ListItem_new(user, key)); diff --git a/Hashtable.c b/Hashtable.c index 92337822..606dfd60 100644 --- a/Hashtable.c +++ b/Hashtable.c @@ -14,9 +14,9 @@ in the source distribution for its full text. #ifndef NDEBUG -static bool Hashtable_isConsistent(Hashtable* this) { - int items = 0; - for (int i = 0; i < this->size; i++) { +static bool Hashtable_isConsistent(const Hashtable* this) { + unsigned int items = 0; + for (unsigned int i = 0; i < this->size; i++) { HashtableItem* bucket = this->buckets[i]; while (bucket) { items++; @@ -26,9 +26,9 @@ static bool Hashtable_isConsistent(Hashtable* this) { return items == this->items; } -int Hashtable_count(Hashtable* this) { - int items = 0; - for (int i = 0; i < this->size; i++) { +unsigned int Hashtable_count(const Hashtable* this) { + unsigned int items = 0; + for (unsigned int i = 0; i < this->size; i++) { HashtableItem* bucket = this->buckets[i]; while (bucket) { items++; @@ -41,7 +41,7 @@ int Hashtable_count(Hashtable* this) { #endif /* NDEBUG */ -static HashtableItem* HashtableItem_new(unsigned int key, void* value) { +static HashtableItem* HashtableItem_new(hkey_t key, void* value) { HashtableItem* this; this = xMalloc(sizeof(HashtableItem)); @@ -51,7 +51,7 @@ static HashtableItem* HashtableItem_new(unsigned int key, void* value) { return this; } -Hashtable* Hashtable_new(int size, bool owner) { +Hashtable* Hashtable_new(unsigned int size, bool owner) { Hashtable* this; this = xMalloc(sizeof(Hashtable)); @@ -65,7 +65,7 @@ Hashtable* Hashtable_new(int size, bool owner) { void Hashtable_delete(Hashtable* 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]; while (walk != NULL) { if (this->owner) @@ -80,7 +80,7 @@ void Hashtable_delete(Hashtable* 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; HashtableItem** bucketPtr = &(this->buckets[index]); while (true) @@ -101,7 +101,7 @@ void Hashtable_put(Hashtable* this, unsigned int key, void* value) { 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; assert(Hashtable_isConsistent(this)); @@ -128,7 +128,7 @@ void* Hashtable_remove(Hashtable* this, unsigned int key) { return NULL; } -void* Hashtable_get(Hashtable* this, unsigned int key) { +void* Hashtable_get(Hashtable* this, hkey_t key) { unsigned int index = key % this->size; HashtableItem* bucketPtr = this->buckets[index]; while (true) { @@ -146,7 +146,7 @@ void* Hashtable_get(Hashtable* this, unsigned int key) { void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData) { 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]; while (walk != NULL) { f(walk->key, walk->value, userData); diff --git a/Hashtable.h b/Hashtable.h index dcdc89fe..15af9e0d 100644 --- a/Hashtable.h +++ b/Hashtable.h @@ -10,36 +10,38 @@ in the source distribution for its full text. #include -typedef void(*Hashtable_PairFunction)(int, void*, void*); +typedef unsigned int hkey_t; -typedef struct HashtableItem { - unsigned int key; +typedef void(*Hashtable_PairFunction)(hkey_t key, void* value, void* userdata); + +typedef struct HashtableItem_ { + hkey_t key; void* value; - struct HashtableItem* next; + struct HashtableItem_* next; } HashtableItem; typedef struct Hashtable_ { - int size; + unsigned int size; HashtableItem** buckets; - int items; + unsigned int items; bool owner; } Hashtable; #ifndef NDEBUG -int Hashtable_count(Hashtable* this); +unsigned int Hashtable_count(const Hashtable* this); #endif /* NDEBUG */ -Hashtable* Hashtable_new(int size, bool owner); +Hashtable* Hashtable_new(unsigned int size, bool owner); 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); diff --git a/Vector.c b/Vector.c index 09d1612e..721ebdd3 100644 --- a/Vector.c +++ b/Vector.c @@ -60,14 +60,14 @@ static bool Vector_isConsistent(const Vector* this) { } } -int Vector_count(const Vector* this) { - int items = 0; +unsigned int Vector_count(const Vector* this) { + unsigned int items = 0; for (int i = 0; i < this->items; i++) { if (this->array[i]) { items++; } } - assert(items == this->items); + assert(items == (unsigned int)this->items); return items; } diff --git a/Vector.h b/Vector.h index 70688152..ec354b16 100644 --- a/Vector.h +++ b/Vector.h @@ -54,7 +54,7 @@ void Vector_set(Vector* this, int idx, void* data_); Object* Vector_get(Vector* this, int idx); int Vector_size(const Vector* this); -int Vector_count(const Vector* this); +unsigned int Vector_count(const Vector* this); #else /* NDEBUG */ -- cgit v1.2.3