From c417be0f32e0f66e5f0e4a6c3f3224da19f52e4f Mon Sep 17 00:00:00 2001 From: Ralph Ronnquist Date: Wed, 15 Jun 2022 10:06:26 +1000 Subject: [PATCH] corrections --- htable/Makefile | 2 +- htable/htable.c | 22 +++++++++++----------- htable/htable.h | 2 ++ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/htable/Makefile b/htable/Makefile index 236afb8..4d0ed95 100644 --- a/htable/Makefile +++ b/htable/Makefile @@ -1,7 +1,7 @@ default: libhtable.a .INTERMEDIATE: htable.o -htable.o: CFLAGS = -Wall +htable.o: CFLAGS = -Wall -g htable.o: htable.c libhtable.a: htable.o diff --git a/htable/htable.c b/htable/htable.c index c35355d..ce8c838 100644 --- a/htable/htable.c +++ b/htable/htable.c @@ -2,8 +2,6 @@ #include #include "htable.h" -#define HOLE ((unsigned char *)1) - //// Generic hash table implementation // Determine the index for a key. On match, it returns the index into @@ -14,21 +12,23 @@ static int htindex(htable *table,unsigned char *key) { table->data = calloc( 256, sizeof( unsigned char* ) ); table->size = 256; } - unsigned int hash = (*table->hashcode)( table, key ) % table->size; + unsigned int hash = + ( (unsigned int) (*table->hashcode)( table, key ) ) % table->size; unsigned int i = hash; int hole = -1; for ( ;; ) { - unsigned char *x = table->data[ i++ ]; + unsigned char *x = table->data[ i ]; if ( x == 0 ) { - return ( hole >= 0 )? -hole : - (int) i; + return ( hole >= 0 )? (- hole - 1 ): ( - (int) i - 1 ); } - if ( x == HOLE ) { + if ( x == HTHOLE ) { if ( hole < 0 ) { hole = i; } } else if ( memcmp( x + table->offset, key, table->esize ) == 0 ) { - return i-1; + return i; } + i++; if ( i >= table->size ) { i = 0; } @@ -36,7 +36,7 @@ static int htindex(htable *table,unsigned char *key) { break; } } - return -1; + return - hole - 1; } // Find the keyed element, and assign the x pointer, or assign 0. @@ -89,7 +89,7 @@ static unsigned char *htnext(htable *table,int *i) { unsigned char **e = table->data + table->size; for ( ; p < e; p++ ) { (*i) += 1; - if ( *p != 0 && *p != HOLE ) { + if ( *p != 0 && *p != HTHOLE ) { return *p; } } @@ -127,7 +127,7 @@ void htdelete(htable *table,unsigned char *p) { pthread_mutex_lock( &table->lock ); int i = htindex( table, p + table->offset ); if ( i >= 0 ) { - table->data[ i ] = HOLE; + table->data[ i ] = HTHOLE; table->holes += 1; if ( table->holes > table->fill / 2 ) { htrehash( table, table->size ); @@ -139,7 +139,7 @@ void htdelete(htable *table,unsigned char *p) { void htdump(htable *table,void (*dumpitem)(int i,unsigned char *p)) { int i; for ( i = 0 ; i < table->size; i++ ) { - if ( table->data[ i ] && table->data[ i ] != HOLE ) { + if ( table->data[ i ] && table->data[ i ] != HTHOLE ) { dumpitem( i, table->data[ i ] ); } } diff --git a/htable/htable.h b/htable/htable.h index e0233b2..c002dc6 100644 --- a/htable/htable.h +++ b/htable/htable.h @@ -71,4 +71,6 @@ void htdump(htable *table,void (*dumpitem)(int i,unsigned char *p)); .esize = sizeof( ((type *)0)->member ), .hashcode = hashcodefn, \ .lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP } +#define HTHOLE ((unsigned char *)1) + #endif // HTABLE_H -- 2.39.2