#include <string.h>
#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
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;
}
break;
}
}
- return -1;
+ return - hole - 1;
}
// Find the keyed element, and assign the x pointer, or assign 0.
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;
}
}
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 );
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 ] );
}
}