From d2ec748984bb80e0137dbbbe33a850868463f3bb Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Sat, 4 Nov 2017 18:15:12 +0100 Subject: [PATCH] beautify the code with AStyle --- src/include/ngspice/hash.h | 38 +- src/misc/hash.c | 1001 ++++++++++++++++++------------------ 2 files changed, 520 insertions(+), 519 deletions(-) diff --git a/src/include/ngspice/hash.h b/src/include/ngspice/hash.h index 95aa5f8d1..f50ba77a9 100644 --- a/src/include/ngspice/hash.h +++ b/src/include/ngspice/hash.h @@ -1,8 +1,8 @@ -/* ----------------------------------------------------------------- -FILE: nghash.h +/* ----------------------------------------------------------------- +FILE: nghash.h DESCRIPTION:Insert file for threaded hash routines. This code was donated from TimberWolf. -CONTENTS: +CONTENTS: DATE: Jul 17, 1988 - original coding REVISIONS: Aug 21, 2009 - adapted for ngspice ----------------------------------------------------------------- */ @@ -69,20 +69,20 @@ typedef struct nghashbox NGHASHBOX, *NGHASHPTR; * on all architecture types include 64bits. * ----------------------------------------------------------------- */ typedef enum { - NGHASH_UNIQUE = 1 << 0, - NGHASH_POWER_OF_TWO = 1 << 1, - NGHASH_UNIQUE_TWO = NGHASH_UNIQUE | NGHASH_POWER_OF_TWO + NGHASH_UNIQUE = 1 << 0, + NGHASH_POWER_OF_TWO = 1 << 1, + NGHASH_UNIQUE_TWO = NGHASH_UNIQUE | NGHASH_POWER_OF_TWO } NGHASHFLAGS_T ; enum nghash_default_func_t { - NGHASH_FUNC_NUM = -2, - NGHASH_FUNC_PTR = -1, - NGHASH_FUNC_STR = 0 + NGHASH_FUNC_NUM = -2, + NGHASH_FUNC_PTR = -1, + NGHASH_FUNC_STR = 0 }; typedef struct nghash_iter_rec { - struct ngtable_rec *position ; + struct ngtable_rec *position ; } NGHASHITER, *NGHASHITERPTR ; /* ----------------------------------------------------------------- @@ -265,9 +265,9 @@ Function: is pointer comparison. */ -extern NGHASHPTR nghash_init_with_parms( nghash_compare_func_t *comp_func, - nghash_func_t *hash_func, int numentries, int max_density, - double growth, NGHASHFLAGS_T flags ) ; +extern NGHASHPTR nghash_init_with_parms( nghash_compare_func_t *comp_func, + nghash_func_t *hash_func, int numentries, int max_density, + double growth, NGHASHFLAGS_T flags ) ; /* Function: Returns a hash table with the given number of entries. @@ -282,7 +282,7 @@ Function: You may use your own hash and compare functions provided they look like * INT hash(void * key) and - * UNSIGNED_INT compare(void * key1, void * key2). + * UNSIGNED_INT compare(void * key1, void * key2). The hash function's return value should be in the interval [0, UINT_MAX]. The compare should return zero if the two keys are equal and a non-zero value otherwise. @@ -298,15 +298,15 @@ Function: extern int nghash_max_density(NGHASHPTR hashtable,int max_density) ; /* Function: - Changes the max_density limit in the hash table if max_density > 1. - This function returns the current value of max_density. + Changes the max_density limit in the hash table if max_density > 1. + This function returns the current value of max_density. */ extern int nghash_table_get( NGHASHPTR hashtable ) ; /* Function: - Returns the current size of hash table set by nghash_table_create + Returns the current size of hash table set by nghash_table_create */ extern int nghash_table_size( int num ) ; @@ -357,7 +357,7 @@ extern void nghash_free(NGHASHPTR htabl,void (*del_data)(void *),void (*del_key) /* Function: Frees the memory associated with a hash table. The user make supply a - function which deletes the memory associated with the data field. + function which deletes the memory associated with the data field. In addition, the user may free the memory stored at the key. This function must have the data pointer supplied by the hash add routines as an argument,ie. @@ -419,7 +419,7 @@ extern void *nghash_enumeratek(NGHASHPTR hashtable,void **key_ret,BOOL flag) ; /* Function: Since this is a threaded hash table, we can enumerate the elements of - the hash table in O(n) time where n is the number of valid entries. + the hash table in O(n) time where n is the number of valid entries. Returns the data and key associated with each entry. The flag is similar to the rbtree enumerate function. This eliminates the need for writing the following: diff --git a/src/misc/hash.c b/src/misc/hash.c index b212b3330..dd839a84b 100644 --- a/src/misc/hash.c +++ b/src/misc/hash.c @@ -1,6 +1,6 @@ -/* ----------------------------------------------------------------- -FILE: hash.c -DESCRIPTION:This file contains the routines for building and +/* ----------------------------------------------------------------- +FILE: hash.c +DESCRIPTION:This file contains the routines for building and maintaining a hash table. Abstract : Contains routines for managing hash tables. Tables may be given their own hash and compare functions. If your keys @@ -19,10 +19,10 @@ DESCRIPTION:This file contains the routines for building and The hash function's return value should be in the interval [0, Uint_MAX]. The compare should return zero if the two keys are equal and a non-zero value otherwise. -CONTENTS: +CONTENTS: DATE: Jul 7, 1988 - original coding. 1988 - 2009 many updates... -REVISIONS: +REVISIONS: ----------------------------------------------------------------- */ #include "ngspice/ngspice.h" #include "ngspice/hash.h" @@ -37,7 +37,7 @@ REVISIONS: static NGTABLEPTR _nghash_find_item(NGHASHPTR hhtable,void *user_key,void *data) ; -NGHASHPTR nghash_init_with_parms(nghash_compare_func_t *comp_func, nghash_func_t *hash_func, int num, +NGHASHPTR nghash_init_with_parms(nghash_compare_func_t *comp_func, nghash_func_t *hash_func, int num, int max, double growth, NGHASHFLAGS_T flags) { BOOL unique ; /* entries are to be unique */ @@ -48,11 +48,11 @@ NGHASHPTR nghash_init_with_parms(nghash_compare_func_t *comp_func, nghash_func_t power_of_two = flags & NGHASH_POWER_OF_TWO ; hashtable = NGMALLOC( 1, NGHASHBOX ) ; - if( power_of_two ){ - hashtable->size = nghash_table_size2( num ) ; + if( power_of_two ) { + hashtable->size = nghash_table_size2( num ) ; } else { - /* prime size */ - hashtable->size = nghash_table_size( num ) ; + /* prime size */ + hashtable->size = nghash_table_size( num ) ; } hashtable->compare_func = comp_func ; hashtable->hash_func = hash_func ; @@ -76,76 +76,76 @@ NGHASHPTR nghash_init_with_parms(nghash_compare_func_t *comp_func, nghash_func_t void nghash_resize(NGHASHPTR hashtable, int num) { - NGTABLEPTR *oldtable, hptr, zapptr ; - NGTABLEPTR new_hptr ; /* new hash table entry */ - int i, oldsize ; + NGTABLEPTR *oldtable, hptr, zapptr ; + NGTABLEPTR new_hptr ; /* new hash table entry */ + int i, oldsize ; - oldsize = hashtable->size ; - oldtable = hashtable->hash_table; + oldsize = hashtable->size ; + oldtable = hashtable->hash_table; - if( hashtable->power_of_two ){ - hashtable->size = nghash_table_size2( num - 1 ) ; - } else { - hashtable->size = nghash_table_size( num ) ; - } - hashtable->num_entries = 0 ; - hashtable->thread = NULL ; - hashtable->last_entry = NULL ; /* end of list */ - hashtable->need_resize = hashtable->size * hashtable->max_density ; - - hashtable->hash_table = NGMALLOC( hashtable->size, NGTABLEPTR); - for( i = 0 ; i < oldsize ; i++ ) { - for( hptr = oldtable[i]; hptr; ) { - zapptr = hptr ; - nghash_insert( hashtable, hptr->key, hptr->data ) ; - if( hashtable->searchPtr && hashtable->searchPtr == hptr ){ - new_hptr = _nghash_find_item(hashtable, hptr->key, hptr->data ) ; - hashtable->searchPtr = new_hptr ; - } - if( hashtable->enumeratePtr && hashtable->enumeratePtr == hptr ){ - new_hptr = _nghash_find_item(hashtable, hptr->key, hptr->data ) ; - hashtable->enumeratePtr = new_hptr ; - } - /* Now safe to free */ - if( hashtable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { - NGFREE( hptr->key); - } - hptr = hptr->next ; - NGFREE( zapptr ) ; + if( hashtable->power_of_two ) { + hashtable->size = nghash_table_size2( num - 1 ) ; + } else { + hashtable->size = nghash_table_size( num ) ; } - } + hashtable->num_entries = 0 ; + hashtable->thread = NULL ; + hashtable->last_entry = NULL ; /* end of list */ + hashtable->need_resize = hashtable->size * hashtable->max_density ; - NGFREE( oldtable ); + hashtable->hash_table = NGMALLOC( hashtable->size, NGTABLEPTR); + for( i = 0 ; i < oldsize ; i++ ) { + for( hptr = oldtable[i]; hptr; ) { + zapptr = hptr ; + nghash_insert( hashtable, hptr->key, hptr->data ) ; + if( hashtable->searchPtr && hashtable->searchPtr == hptr ) { + new_hptr = _nghash_find_item(hashtable, hptr->key, hptr->data ) ; + hashtable->searchPtr = new_hptr ; + } + if( hashtable->enumeratePtr && hashtable->enumeratePtr == hptr ) { + new_hptr = _nghash_find_item(hashtable, hptr->key, hptr->data ) ; + hashtable->enumeratePtr = new_hptr ; + } + /* Now safe to free */ + if( hashtable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { + NGFREE( hptr->key); + } + hptr = hptr->next ; + NGFREE( zapptr ) ; + } + } + + NGFREE( oldtable ); } /* end nghash_resize() */ void nghash_reset_stat(NGHASHPTR hashtable) { - hashtable->collision = 0 ; - hashtable->access = 0 ; + hashtable->collision = 0 ; + hashtable->access = 0 ; } NGHASHPTR nghash_init(int num_entries) { return( nghash_init_with_parms( NGHASH_DEF_CMP(NGHASH_FUNC_STR), NGHASH_DEF_HASH(NGHASH_FUNC_STR), - num_entries, NGHASH_DEF_MAX_DENSITY, - NGHASH_DEF_GROW_FACTOR, NGHASH_UNIQUE) ) ; + num_entries, NGHASH_DEF_MAX_DENSITY, + NGHASH_DEF_GROW_FACTOR, NGHASH_UNIQUE) ) ; } /* end nghash_init() */ NGHASHPTR nghash_init_pointer(int num_entries) { return( nghash_init_with_parms( NGHASH_DEF_CMP(NGHASH_FUNC_PTR), NGHASH_DEF_HASH(NGHASH_FUNC_PTR), - num_entries, NGHASH_DEF_MAX_DENSITY, - NGHASH_DEF_GROW_FACTOR, - NGHASH_UNIQUE_TWO) ) ; + num_entries, NGHASH_DEF_MAX_DENSITY, + NGHASH_DEF_GROW_FACTOR, + NGHASH_UNIQUE_TWO) ) ; } /* end nghash_init_pointer() */ NGHASHPTR nghash_init_integer(int num_entries) { return( nghash_init_with_parms( NGHASH_DEF_CMP(NGHASH_FUNC_NUM), NGHASH_DEF_HASH(NGHASH_FUNC_NUM), - num_entries, NGHASH_DEF_MAX_DENSITY, - NGHASH_DEF_GROW_FACTOR, - NGHASH_UNIQUE_TWO) ) ; + num_entries, NGHASH_DEF_MAX_DENSITY, + NGHASH_DEF_GROW_FACTOR, + NGHASH_UNIQUE_TWO) ) ; } /* end nghash_init_integer() */ int nghash_table_get(NGHASHPTR hashtable) @@ -155,39 +155,39 @@ int nghash_table_get(NGHASHPTR hashtable) int nghash_max_density(NGHASHPTR hashtable,int max_density) { - if( max_density > 0 ){ - hashtable->max_density = max_density ; - hashtable->need_resize = hashtable->size * hashtable->max_density ; + if( max_density > 0 ) { + hashtable->max_density = max_density ; + hashtable->need_resize = hashtable->size * hashtable->max_density ; } return(hashtable->max_density) ; } -void nghash_empty(NGHASHPTR hashtable, void (*delete_data) (void *), - void (*delete_key) (void *)) +void nghash_empty(NGHASHPTR hashtable, void (*delete_data) (void *), + void (*delete_key) (void *)) { NGTABLEPTR *table, hptr , zapptr ; nghash_reset_stat(hashtable); table = hashtable->hash_table ; - if( table ){ - for( hptr = hashtable->thread ; hptr ; ){ - zapptr = hptr ; - hptr = hptr->thread_next ; + if( table ) { + for( hptr = hashtable->thread ; hptr ; ) { + zapptr = hptr ; + hptr = hptr->thread_next ; - /* execute user define delete function if requested */ - if( delete_data ){ - delete_data (zapptr->data); - } - if( hashtable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { - /* we allocated this ourselves we can delete it */ - NGFREE( zapptr->key ) ; - } else if( delete_key ){ - delete_key (zapptr->key); - } - NGFREE( zapptr ) ; - } - memset(table, 0, (size_t) hashtable->size*sizeof(NGTABLEPTR)) ; + /* execute user define delete function if requested */ + if( delete_data ) { + delete_data (zapptr->data); + } + if( hashtable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { + /* we allocated this ourselves we can delete it */ + NGFREE( zapptr->key ) ; + } else if( delete_key ) { + delete_key (zapptr->key); + } + NGFREE( zapptr ) ; + } + memset(table, 0, (size_t) hashtable->size*sizeof(NGTABLEPTR)) ; } /* free decks associated with tree if they exist */ hashtable->thread = NULL ; /* initialize list */ @@ -196,8 +196,8 @@ void nghash_empty(NGHASHPTR hashtable, void (*delete_data) (void *), } /* end nghash_empty() */ -void nghash_free(NGHASHPTR hashtable, void (*delete_data) (void *), - void (*delete_key) (void *)) +void nghash_free(NGHASHPTR hashtable, void (*delete_data) (void *), + void (*delete_key) (void *)) { hashtable->call_from_free = TRUE; nghash_empty(hashtable, delete_data, delete_key ) ; @@ -239,48 +239,48 @@ void * _nghash_find(NGHASHPTR hashtable, void * user_key,BOOL *status) * Process the hash function. ----------------------------------------------------------------- */ switch( (intptr_t) hashtable->hash_func ) { - case NGHASH_FUNC_STR: - NGHASH_STR_TO_HASH( user_key, hsum, hashtable->size); - break ; - case NGHASH_FUNC_PTR: - NGHASH_PTR_TO_HASH( user_key, hsum, hashtable->size); - break ; - case NGHASH_FUNC_NUM: - NGHASH_NUM_TO_HASH( user_key, hsum, hashtable->size); - break ; - default: - hsum = hashtable->hash_func (hashtable, user_key); + case NGHASH_FUNC_STR: + NGHASH_STR_TO_HASH( user_key, hsum, hashtable->size); + break ; + case NGHASH_FUNC_PTR: + NGHASH_PTR_TO_HASH( user_key, hsum, hashtable->size); + break ; + case NGHASH_FUNC_NUM: + NGHASH_NUM_TO_HASH( user_key, hsum, hashtable->size); + break ; + default: + hsum = hashtable->hash_func (hashtable, user_key); } curPtr = table[hsum] ; - if( curPtr ){ - /* list started at this hash */ - for( ; curPtr ; curPtr=curPtr->next ) { - if( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { - ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; - } else if ( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || - hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { - ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key ); - } else { - ret_code = hashtable->compare_func (curPtr->key, user_key); - } - if( ret_code == STRINGEQ ){ - /* ---------------------------------------------------- - * Operation find or enter with unique items, we - * return item. On a nonunique enter, add item below. - ------------------------------------------------------- */ - hashtable->searchPtr = curPtr ; - if( status ){ - *status = TRUE ; - } - return( curPtr->data ) ; - } - } + if( curPtr ) { + /* list started at this hash */ + for( ; curPtr ; curPtr=curPtr->next ) { + if( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { + ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; + } else if ( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || + hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { + ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key ); + } else { + ret_code = hashtable->compare_func (curPtr->key, user_key); + } + if( ret_code == STRINGEQ ) { + /* ---------------------------------------------------- + * Operation find or enter with unique items, we + * return item. On a nonunique enter, add item below. + ------------------------------------------------------- */ + hashtable->searchPtr = curPtr ; + if( status ) { + *status = TRUE ; + } + return( curPtr->data ) ; + } + } } /* cant find anything on a find operation */ hashtable->searchPtr = NULL ; - if( status ){ - *status = FALSE ; + if( status ) { + *status = FALSE ; } return( NULL ) ; @@ -304,27 +304,27 @@ void * _nghash_find_again(NGHASHPTR hashtable, void * user_key,BOOL *status) /* initialization */ DS(hashtable->access++;) ; - if( hashtable->searchPtr ){ - for(curPtr=hashtable->searchPtr->next; curPtr ; curPtr=curPtr->next ) { - if( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { - ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; - } else if ( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || - hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { - ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key ); - } else { - ret_code = hashtable->compare_func (curPtr->key, user_key); - } - if( ret_code == STRINGEQ ){ - hashtable->searchPtr = curPtr ; - if( status ){ - *status = TRUE ; - } - return( curPtr->data ) ; - } - } + if( hashtable->searchPtr ) { + for(curPtr=hashtable->searchPtr->next; curPtr ; curPtr=curPtr->next ) { + if( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { + ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; + } else if ( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || + hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { + ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key ); + } else { + ret_code = hashtable->compare_func (curPtr->key, user_key); + } + if( ret_code == STRINGEQ ) { + hashtable->searchPtr = curPtr ; + if( status ) { + *status = TRUE ; + } + return( curPtr->data ) ; + } + } } - if( status ){ - *status = FALSE ; + if( status ) { + *status = FALSE ; } return(NULL) ; } /* end _nghash_find_again() */ @@ -351,55 +351,55 @@ void * nghash_delete(NGHASHPTR hashtable, void * user_key) * Process the hash function. ----------------------------------------------------------------- */ switch( (intptr_t) hashtable->hash_func ) { - case NGHASH_FUNC_STR: - NGHASH_STR_TO_HASH( user_key, hsum, hashtable->size); - break ; - case NGHASH_FUNC_PTR: - NGHASH_PTR_TO_HASH( user_key, hsum, hashtable->size); - break ; - case NGHASH_FUNC_NUM: - NGHASH_NUM_TO_HASH( user_key, hsum, hashtable->size); - break ; - default: - hsum = hashtable->hash_func (hashtable, user_key); + case NGHASH_FUNC_STR: + NGHASH_STR_TO_HASH( user_key, hsum, hashtable->size); + break ; + case NGHASH_FUNC_PTR: + NGHASH_PTR_TO_HASH( user_key, hsum, hashtable->size); + break ; + case NGHASH_FUNC_NUM: + NGHASH_NUM_TO_HASH( user_key, hsum, hashtable->size); + break ; + default: + hsum = hashtable->hash_func (hashtable, user_key); } /* insert into table only if distinct number */ curPtr = table[hsum] ; - if( curPtr ){ - /* list started at this hash */ - prevPtr = table + hsum ; - for( ; curPtr ; prevPtr = &(curPtr->next), curPtr=curPtr->next ) { - if( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { - ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; - } else if ( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || - hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { - ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key ); - } else { - ret_code = hashtable->compare_func (curPtr->key, user_key); - } - if( ret_code == STRINGEQ ){ - if( curPtr->thread_prev ){ /* no sentinel */ - curPtr->thread_prev->thread_next = curPtr->thread_next; - } else { - hashtable->thread = curPtr->thread_next ; - } - if( curPtr->thread_next ){ /* no sentinel */ - curPtr->thread_next->thread_prev = curPtr->thread_prev ; - } else { - hashtable->last_entry = curPtr->thread_prev ; - } - *prevPtr = curPtr->next ; - if( hashtable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { - /* we allocated this ourselves we can delete it */ - NGFREE( curPtr->key ) ; - } - user_data_p = curPtr->data ; - NGFREE(curPtr); - hashtable->num_entries-- ; - return( user_data_p ) ; - } - } + if( curPtr ) { + /* list started at this hash */ + prevPtr = table + hsum ; + for( ; curPtr ; prevPtr = &(curPtr->next), curPtr=curPtr->next ) { + if( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { + ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; + } else if ( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || + hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { + ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key ); + } else { + ret_code = hashtable->compare_func (curPtr->key, user_key); + } + if( ret_code == STRINGEQ ) { + if( curPtr->thread_prev ) { /* no sentinel */ + curPtr->thread_prev->thread_next = curPtr->thread_next; + } else { + hashtable->thread = curPtr->thread_next ; + } + if( curPtr->thread_next ) { /* no sentinel */ + curPtr->thread_next->thread_prev = curPtr->thread_prev ; + } else { + hashtable->last_entry = curPtr->thread_prev ; + } + *prevPtr = curPtr->next ; + if( hashtable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { + /* we allocated this ourselves we can delete it */ + NGFREE( curPtr->key ) ; + } + user_data_p = curPtr->data ; + NGFREE(curPtr); + hashtable->num_entries-- ; + return( user_data_p ) ; + } + } } return( NULL ) ; /* didn't find anything to delete */ @@ -420,46 +420,46 @@ void * nghash_insert(NGHASHPTR hashtable, void * user_key, void * data) * Process the hash function. ----------------------------------------------------------------- */ switch( (intptr_t) hashtable->hash_func ) { - case NGHASH_FUNC_STR: - NGHASH_STR_TO_HASH( user_key, hsum, hashtable->size); - break ; - case NGHASH_FUNC_PTR: - NGHASH_PTR_TO_HASH( user_key, hsum, hashtable->size); - break ; - case NGHASH_FUNC_NUM: - NGHASH_NUM_TO_HASH( user_key, hsum, hashtable->size); - break ; - default: - hsum = hashtable->hash_func (hashtable, user_key); + case NGHASH_FUNC_STR: + NGHASH_STR_TO_HASH( user_key, hsum, hashtable->size); + break ; + case NGHASH_FUNC_PTR: + NGHASH_PTR_TO_HASH( user_key, hsum, hashtable->size); + break ; + case NGHASH_FUNC_NUM: + NGHASH_NUM_TO_HASH( user_key, hsum, hashtable->size); + break ; + default: + hsum = hashtable->hash_func (hashtable, user_key); } /* insert into table only if distinct number */ temptr = table[hsum] ; - if( temptr ){ - /* list started at this hash */ - for( curPtr = temptr ; curPtr ; curPtr=curPtr->next ) { - DS(hashtable->collision++;) ; - if( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { - ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; - } else if ( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || - hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { - ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key); - } else { - ret_code = hashtable->compare_func (curPtr->key, user_key); - } - if( ret_code == STRINGEQ ){ - if( hashtable->unique ){ - /* ---------------------------------------------------- - * Operation enter with unique items, we - * return item. On a nonunique enter, add item below. - ------------------------------------------------------- */ - hashtable->searchPtr = curPtr ; - return( curPtr->data ) ; - } else { - break ; /* avoid some work for nonunique hash_enter */ - } - } - } + if( temptr ) { + /* list started at this hash */ + for( curPtr = temptr ; curPtr ; curPtr=curPtr->next ) { + DS(hashtable->collision++;) ; + if( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { + ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; + } else if ( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || + hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { + ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key); + } else { + ret_code = hashtable->compare_func (curPtr->key, user_key); + } + if( ret_code == STRINGEQ ) { + if( hashtable->unique ) { + /* ---------------------------------------------------- + * Operation enter with unique items, we + * return item. On a nonunique enter, add item below. + ------------------------------------------------------- */ + hashtable->searchPtr = curPtr ; + return( curPtr->data ) ; + } else { + break ; /* avoid some work for nonunique hash_enter */ + } + } + } } /* now save data */ @@ -467,26 +467,26 @@ void * nghash_insert(NGHASHPTR hashtable, void * user_key, void * data) table[hsum] = curTable = NGMALLOC(1,NGTABLEBOX); curTable->data = data ; if( hashtable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { - curTable->key = copy((char *) user_key); + curTable->key = copy((char *) user_key); } else { - curTable->key = user_key ; + curTable->key = user_key ; } curTable->next = temptr ; /* now fix thread which goes through hash table */ - if( hashtable->last_entry ){ - hashtable->last_entry->thread_next = curTable ; - curTable->thread_prev = hashtable->last_entry ; - hashtable->last_entry = curTable ; + if( hashtable->last_entry ) { + hashtable->last_entry->thread_next = curTable ; + curTable->thread_prev = hashtable->last_entry ; + hashtable->last_entry = curTable ; } else { - hashtable->thread = hashtable->last_entry = curTable ; - curTable->thread_prev = NULL ; + hashtable->thread = hashtable->last_entry = curTable ; + curTable->thread_prev = NULL ; } curTable->thread_next = NULL ; - if( hashtable->num_entries >= hashtable->need_resize ){ - int newsize ; /* new size of table */ - newsize = (int)(hashtable->size * hashtable->growth_factor); - nghash_resize(hashtable, newsize ) ; + if( hashtable->num_entries >= hashtable->need_resize ) { + int newsize ; /* new size of table */ + newsize = (int)(hashtable->size * hashtable->growth_factor); + nghash_resize(hashtable, newsize ) ; } return( NULL ) ; /* no conflict on a enter */ @@ -508,41 +508,41 @@ static NGTABLEPTR _nghash_find_item(NGHASHPTR htable,void * user_key,void * data * Process the hash function. ----------------------------------------------------------------- */ switch( (intptr_t) htable->hash_func ) { - case NGHASH_FUNC_STR: - NGHASH_STR_TO_HASH( user_key, hsum, htable->size); - break ; - case NGHASH_FUNC_PTR: - NGHASH_PTR_TO_HASH( user_key, hsum, htable->size); - break ; - case NGHASH_FUNC_NUM: - NGHASH_NUM_TO_HASH( user_key, hsum, htable->size); - break ; - default: - hsum = htable->hash_func (htable, user_key); + case NGHASH_FUNC_STR: + NGHASH_STR_TO_HASH( user_key, hsum, htable->size); + break ; + case NGHASH_FUNC_PTR: + NGHASH_PTR_TO_HASH( user_key, hsum, htable->size); + break ; + case NGHASH_FUNC_NUM: + NGHASH_NUM_TO_HASH( user_key, hsum, htable->size); + break ; + default: + hsum = htable->hash_func (htable, user_key); } /* insert into table only if distinct number */ - if( (temptr = table[hsum]) != NULL ){ - /* list started at this hash */ - for(curPtr=temptr ; curPtr ; curPtr=curPtr->next ) { - if( htable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { - ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; - } else if ( htable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || - htable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { - ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key); - } else { - ret_code = htable->compare_func (curPtr->key, user_key); - } - if( ret_code == STRINGEQ ){ - if( data ){ - if( curPtr->data == data ){ - return( curPtr ) ; - } - } else { - return( curPtr ) ; - } - } - } + if( (temptr = table[hsum]) != NULL ) { + /* list started at this hash */ + for(curPtr=temptr ; curPtr ; curPtr=curPtr->next ) { + if( htable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { + ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; + } else if ( htable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || + htable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { + ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key); + } else { + ret_code = htable->compare_func (curPtr->key, user_key); + } + if( ret_code == STRINGEQ ) { + if( data ) { + if( curPtr->data == data ) { + return( curPtr ) ; + } + } else { + return( curPtr ) ; + } + } + } } return( NULL ) ; /* no matching item */ @@ -554,43 +554,43 @@ void * nghash_enumeratek(NGHASHPTR htable, void * *key_return, BOOL start_flag) { NGTABLEPTR current_spot ; /* current place in threaded list */ - if( start_flag ){ - if( (htable->enumeratePtr = htable->thread) != NULL ) { - current_spot = htable->enumeratePtr ; - *key_return = current_spot->key ; - return( current_spot->data ) ; - } + if( start_flag ) { + if( (htable->enumeratePtr = htable->thread) != NULL ) { + current_spot = htable->enumeratePtr ; + *key_return = current_spot->key ; + return( current_spot->data ) ; + } } else { - if( htable->enumeratePtr && - (htable->enumeratePtr = htable->enumeratePtr->thread_next) != NULL ){ - current_spot = htable->enumeratePtr ; - *key_return = current_spot->key ; - return( current_spot->data ) ; - } + if( htable->enumeratePtr && + (htable->enumeratePtr = htable->enumeratePtr->thread_next) != NULL ) { + current_spot = htable->enumeratePtr ; + *key_return = current_spot->key ; + return( current_spot->data ) ; + } } *key_return = NULL ; return( NULL ) ; - + } /* end nghash_enumeratek() */ void * nghash_enumerate(NGHASHPTR htable,BOOL start_flag) { NGTABLEPTR current_spot ; /* current place in threaded list */ - if( start_flag ){ - if( (htable->enumeratePtr = htable->thread) != NULL ){ - current_spot = htable->enumeratePtr ; - return( current_spot->data ) ; - } + if( start_flag ) { + if( (htable->enumeratePtr = htable->thread) != NULL ) { + current_spot = htable->enumeratePtr ; + return( current_spot->data ) ; + } } else { - if( htable->enumeratePtr && - (htable->enumeratePtr = htable->enumeratePtr->thread_next) != NULL ){ - current_spot = htable->enumeratePtr ; - return( current_spot->data ) ; - } + if( htable->enumeratePtr && + (htable->enumeratePtr = htable->enumeratePtr->thread_next) != NULL ) { + current_spot = htable->enumeratePtr ; + return( current_spot->data ) ; + } } return( NULL ) ; - + } /* end nghash_enumerate() */ /* ----------------------------------------------------------------- @@ -601,27 +601,27 @@ void * nghash_enumeratekRE(NGHASHPTR htable, void * *key_return, NGHASHITERPTR i NGTABLEPTR current_spot ; /* current place in threaded list */ FUNC_NAME("nghash_enumeratekRE") ; - if(!(iter_p)){ - fprintf( stderr, "ERROR[%s]:Null iterator pointer.\n", routine ) ; - return(NULL) ; + if(!(iter_p)) { + fprintf( stderr, "ERROR[%s]:Null iterator pointer.\n", routine ) ; + return(NULL) ; } - if(!(iter_p->position)){ - if( (iter_p->position = htable->thread) != NULL ){ - current_spot = iter_p->position ; - *key_return = current_spot->key ; - return( current_spot->data ) ; - } + if(!(iter_p->position)) { + if( (iter_p->position = htable->thread) != NULL ) { + current_spot = iter_p->position ; + *key_return = current_spot->key ; + return( current_spot->data ) ; + } } else { - if( iter_p->position && - (iter_p->position = iter_p->position->thread_next) != NULL ){ - current_spot = iter_p->position ; - *key_return = current_spot->key ; - return( current_spot->data ) ; - } + if( iter_p->position && + (iter_p->position = iter_p->position->thread_next) != NULL ) { + current_spot = iter_p->position ; + *key_return = current_spot->key ; + return( current_spot->data ) ; + } } *key_return = NULL ; return( NULL ) ; - + } /* end nghash_enumeratekRE() */ /* ----------------------------------------------------------------- @@ -632,24 +632,24 @@ void * nghash_enumerateRE(NGHASHPTR htable, NGHASHITERPTR iter_p) NGTABLEPTR current_spot ; /* current place in threaded list */ FUNC_NAME("nghash_enumerateRE") ; - if(!(iter_p)){ - fprintf( stderr, "ERROR[%s]:Null iterator pointer.\n", routine ) ; - return(NULL) ; + if(!(iter_p)) { + fprintf( stderr, "ERROR[%s]:Null iterator pointer.\n", routine ) ; + return(NULL) ; } - if(!(iter_p->position)){ - if( (iter_p->position = htable->thread) != NULL ){ - current_spot = iter_p->position ; - return( current_spot->data ) ; - } + if(!(iter_p->position)) { + if( (iter_p->position = htable->thread) != NULL ) { + current_spot = iter_p->position ; + return( current_spot->data ) ; + } } else { - if( iter_p->position && - (iter_p->position = iter_p->position->thread_next) != NULL ){ - current_spot = iter_p->position ; - return( current_spot->data ) ; - } + if( iter_p->position && + (iter_p->position = iter_p->position->thread_next) != NULL ) { + current_spot = iter_p->position ; + return( current_spot->data ) ; + } } return( NULL ) ; - + } /* end nghash_enumerateRE() */ /* ----------------------------------------------------------------- @@ -660,20 +660,20 @@ NGHASHPTR nghash_merge( NGHASHPTR master_htable, NGHASHPTR merge_htable ) { NGTABLEPTR ptr ; /* traverse hash table */ - if(!(master_htable)){ - master_htable = NGMALLOC( 1, NGHASHBOX ) ; - *master_htable = *merge_htable ; - master_htable->hash_table = NGMALLOC( master_htable->size, NGTABLEPTR ) ; - master_htable->thread = NULL ; - master_htable->last_entry = NULL ; - master_htable->num_entries = 0 ; - master_htable->enumeratePtr = NULL ; - master_htable->searchPtr = NULL ; - master_htable->access = 0 ; - master_htable->collision = 0 ; + if(!(master_htable)) { + master_htable = NGMALLOC( 1, NGHASHBOX ) ; + *master_htable = *merge_htable ; + master_htable->hash_table = NGMALLOC( master_htable->size, NGTABLEPTR ) ; + master_htable->thread = NULL ; + master_htable->last_entry = NULL ; + master_htable->num_entries = 0 ; + master_htable->enumeratePtr = NULL ; + master_htable->searchPtr = NULL ; + master_htable->access = 0 ; + master_htable->collision = 0 ; } - for( ptr = merge_htable->thread ; ptr ; ptr = ptr->thread_next ){ - nghash_insert( master_htable, ptr->key, ptr->data ) ; + for( ptr = merge_htable->thread ; ptr ; ptr = ptr->thread_next ) { + nghash_insert( master_htable, ptr->key, ptr->data ) ; } return( master_htable ) ; } /* end nghash_merge() */ @@ -692,34 +692,34 @@ void nghash_dump(NGHASHPTR htable, void (*print_key) (void *)) NGTABLEPTR hptr ; /* hash table element */ table = htable->hash_table ; - fprintf( stderr, "Dump of hashtable containing %d entries...\n", - htable->num_entries ) ; - fprintf( stderr, "Table is %4.2f%% full\n", - 100.0 * (double) htable->num_entries / (double) htable->size ) ; + fprintf( stderr, "Dump of hashtable containing %d entries...\n", + htable->num_entries ) ; + fprintf( stderr, "Table is %4.2f%% full\n", + 100.0 * (double) htable->num_entries / (double) htable->size ) ; for( i = 0 ; i < htable->size ; i++ ) { - if( (hptr = table[i]) != NULL ){ - fprintf( stderr, " [%5d]:", i ) ; - count = 0 ; - for( ; hptr ; hptr=hptr->next ){ - if( ++count == 3 ){ - fprintf( stderr, "\n\t" ) ; - count = 0 ; - } - if( htable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { - fprintf( stderr, " key:%s ", (char *) hptr->key ) ; - } else { - fprintf( stderr, " key:%p ", hptr->key ) ; - } - if( print_key) { - print_key (hptr->data); - } else { - fprintf( stderr, " data:%p ", hptr->data ) ; - } - } - fprintf( stderr, "\n" ) ; - } + if( (hptr = table[i]) != NULL ) { + fprintf( stderr, " [%5d]:", i ) ; + count = 0 ; + for( ; hptr ; hptr=hptr->next ) { + if( ++count == 3 ) { + fprintf( stderr, "\n\t" ) ; + count = 0 ; + } + if( htable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { + fprintf( stderr, " key:%s ", (char *) hptr->key ) ; + } else { + fprintf( stderr, " key:%p ", hptr->key ) ; + } + if( print_key) { + print_key (hptr->data); + } else { + fprintf( stderr, " data:%p ", hptr->data ) ; + } + } + fprintf( stderr, "\n" ) ; + } } /* end for( i = 0... */ - + } /* end nghash_enumerate() */ /* ----------------------------------------------------------------- @@ -741,59 +741,59 @@ BOOL nghash_deleteItem(NGHASHPTR hashtable, void * user_key, void * data) * Process the hash function. ----------------------------------------------------------------- */ switch( (intptr_t) hashtable->hash_func ) { - case NGHASH_FUNC_STR: - NGHASH_STR_TO_HASH( user_key, hsum, hashtable->size); - break ; - case NGHASH_FUNC_PTR: - NGHASH_PTR_TO_HASH( user_key, hsum, hashtable->size); - break ; - case NGHASH_FUNC_NUM: - NGHASH_NUM_TO_HASH( user_key, hsum, hashtable->size); - break ; - default: - hsum = hashtable->hash_func (hashtable, user_key); + case NGHASH_FUNC_STR: + NGHASH_STR_TO_HASH( user_key, hsum, hashtable->size); + break ; + case NGHASH_FUNC_PTR: + NGHASH_PTR_TO_HASH( user_key, hsum, hashtable->size); + break ; + case NGHASH_FUNC_NUM: + NGHASH_NUM_TO_HASH( user_key, hsum, hashtable->size); + break ; + default: + hsum = hashtable->hash_func (hashtable, user_key); } /* insert into table only if distinct number */ temptr = table[hsum] ; - if( temptr ){ - /* list started at this hash */ - prevPtr = table + hsum ; - for(curPtr=temptr;curPtr;prevPtr = &(curPtr->next), curPtr=curPtr->next ) { - /* ----------------------------------------------------------------- - * Look for match. - ----------------------------------------------------------------- */ - if( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { - ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; - } else if ( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || - hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { - ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key ); - } else { - ret_code = hashtable->compare_func (curPtr->key, user_key); - } - if( ret_code == STRINGEQ ){ - if( curPtr->data == data ){ - if( curPtr->thread_prev ){ /* no sentinel */ - curPtr->thread_prev->thread_next = curPtr->thread_next; - } else { - hashtable->thread = curPtr->thread_next ; - } - if( curPtr->thread_next ){ /* no sentinel */ - curPtr->thread_next->thread_prev = curPtr->thread_prev ; - } else { - hashtable->last_entry = curPtr->thread_prev ; - } - *prevPtr = curPtr->next; - if( hashtable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { - /* we allocated this ourselves we can delete it */ - NGFREE( curPtr->key ) ; - } - NGFREE(curPtr); - hashtable->num_entries-- ; - return( TRUE ) ; - } - } - } /* end for(curPtr=temptr... */ + if( temptr ) { + /* list started at this hash */ + prevPtr = table + hsum ; + for(curPtr=temptr; curPtr; prevPtr = &(curPtr->next), curPtr=curPtr->next ) { + /* ----------------------------------------------------------------- + * Look for match. + ----------------------------------------------------------------- */ + if( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_STR) ) { + ret_code = strcmp((char *)curPtr->key, (char *) user_key ) ; + } else if ( hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_PTR) || + hashtable->compare_func == NGHASH_DEF_CMP(NGHASH_FUNC_NUM) ) { + ret_code = NGHASH_PTR_COMPARE_FUNC( curPtr->key, user_key ); + } else { + ret_code = hashtable->compare_func (curPtr->key, user_key); + } + if( ret_code == STRINGEQ ) { + if( curPtr->data == data ) { + if( curPtr->thread_prev ) { /* no sentinel */ + curPtr->thread_prev->thread_next = curPtr->thread_next; + } else { + hashtable->thread = curPtr->thread_next ; + } + if( curPtr->thread_next ) { /* no sentinel */ + curPtr->thread_next->thread_prev = curPtr->thread_prev ; + } else { + hashtable->last_entry = curPtr->thread_prev ; + } + *prevPtr = curPtr->next; + if( hashtable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { + /* we allocated this ourselves we can delete it */ + NGFREE( curPtr->key ) ; + } + NGFREE(curPtr); + hashtable->num_entries-- ; + return( TRUE ) ; + } + } + } /* end for(curPtr=temptr... */ } /* end if( temptr... */ return( FALSE ) ; /* could not find item */ @@ -803,56 +803,57 @@ BOOL nghash_deleteItem(NGHASHPTR hashtable, void * user_key, void * data) /*---------------------------- hash_table_size -------------------------*/ int nghash_table_size(int minEntries) { - int i; - BOOL isPrime; - int prime; - int testPrime; - static int primes[PRIMECOUNT] = - { 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, - 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, - 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, - 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, - 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, - 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, - 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, - 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, - 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, - 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, - 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, - 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, - 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, - 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, - 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, - 881, 883, 887, 907, 991, 919, 929, 937, 941, 947, - 953, 967, 971, 977, 983, 991, 997,1009,1013,1019, - 1021,1031,1033,1039,1049,1051,1061,1063,1069,1087, - 1091,1093,1097,1103,1109,1117,1123,1129,1151,1153, - 1163,1171,1181,1187,1193,1201,1213,1217,1223,1229 }; + int i; + BOOL isPrime; + int prime; + int testPrime; + static int primes[PRIMECOUNT] = { + 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, + 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, + 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, + 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, + 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, + 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, + 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, + 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, + 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, + 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, + 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, + 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, + 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, + 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, + 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, + 881, 883, 887, 907, 991, 919, 929, 937, 941, 947, + 953, 967, 971, 977, 983, 991, 997,1009,1013,1019, + 1021,1031,1033,1039,1049,1051,1061,1063,1069,1087, + 1091,1093,1097,1103,1109,1117,1123,1129,1151,1153, + 1163,1171,1181,1187,1193,1201,1213,1217,1223,1229 + }; - if (minEntries <= MINPRIMESIZE){ - return(MINPRIMESIZE); + if (minEntries <= MINPRIMESIZE) { + return(MINPRIMESIZE); } else { - testPrime = minEntries; - /* test to see if even */ - if ((testPrime % 2) == 0){ - testPrime = testPrime + 1; - } - do { - testPrime = testPrime + 2; - isPrime = TRUE; - for (i=0;i < PRIMECOUNT;i++){ - prime = primes[i]; - if (testPrime < prime*prime){ - break; - } - if ((testPrime % prime) == 0){ - isPrime = FALSE; - break; - } - } - } while (!(isPrime)); + testPrime = minEntries; + /* test to see if even */ + if ((testPrime % 2) == 0) { + testPrime = testPrime + 1; + } + do { + testPrime = testPrime + 2; + isPrime = TRUE; + for (i=0; i < PRIMECOUNT; i++) { + prime = primes[i]; + if (testPrime < prime*prime) { + break; + } + if ((testPrime % prime) == 0) { + isPrime = FALSE; + break; + } + } + } while (!(isPrime)); - return(testPrime); + return(testPrime); } } /* FUNCTION nghash_table_size */ @@ -862,9 +863,9 @@ int nghash_table_size2(int minEntries) int power ; int table_size ; power = 0 ; - while( minEntries > 0 ){ - minEntries = minEntries >> 1 ; - power++ ; + while( minEntries > 0 ) { + minEntries = minEntries >> 1 ; + power++ ; } power = MIN( power, 32 ) ; table_size = 1 << power ; @@ -876,53 +877,53 @@ int nghash_table_size2(int minEntries) void nghash_distribution(NGHASHPTR hashtable) { - int i ; /* counter */ - long min ; /* min count */ - long max ; /* max count */ - long nzero_cnt ; /* non zero count */ - long this_count ; /* count items at this list */ - long tablesize ; /* table size */ - double avg ; /* average */ - double sum2 ; /* squared sum */ - double diff ; /* difference */ - double diff2 ; /* difference squared */ - double nzavg ; /* non zero average */ - double variance ; /* variance of table */ - NGTABLEPTR *table ; /* hash table */ - NGTABLEPTR hptr ; /* hash table pointer */ - FUNC_NAME("nghash_distribution" ) ; + int i ; /* counter */ + long min ; /* min count */ + long max ; /* max count */ + long nzero_cnt ; /* non zero count */ + long this_count ; /* count items at this list */ + long tablesize ; /* table size */ + double avg ; /* average */ + double sum2 ; /* squared sum */ + double diff ; /* difference */ + double diff2 ; /* difference squared */ + double nzavg ; /* non zero average */ + double variance ; /* variance of table */ + NGTABLEPTR *table ; /* hash table */ + NGTABLEPTR hptr ; /* hash table pointer */ + FUNC_NAME("nghash_distribution" ) ; - tablesize = nghash_tablesize(hashtable) ; - table = hashtable->hash_table ; - avg = hashtable->num_entries / (double) tablesize ; - sum2 = 0.0 ; - min = max = 0 ; - nzero_cnt = 0 ; - for( i = 0 ; i < tablesize ; i++ ) { - this_count = 0 ; - for( hptr = table[i]; hptr; hptr = hptr->next ) { - this_count++ ; + tablesize = nghash_tablesize(hashtable) ; + table = hashtable->hash_table ; + avg = hashtable->num_entries / (double) tablesize ; + sum2 = 0.0 ; + min = max = 0 ; + nzero_cnt = 0 ; + for( i = 0 ; i < tablesize ; i++ ) { + this_count = 0 ; + for( hptr = table[i]; hptr; hptr = hptr->next ) { + this_count++ ; + } + if( i == 0 ) { + min = max = this_count ; + } else { + if( this_count < min ) { + min = this_count ; + } + if( this_count > max ) { + max = this_count ; + } + } + if( this_count > 0 ) { + nzero_cnt++ ; + } + diff = (double)this_count - avg ; + diff2 = diff * diff ; + sum2 += diff2 ; } - if( i == 0 ){ - min = max = this_count ; - } else { - if( this_count < min ){ - min = this_count ; - } - if( this_count > max ){ - max = this_count ; - } - } - if( this_count > 0 ){ - nzero_cnt++ ; - } - diff = (double)this_count - avg ; - diff2 = diff * diff ; - sum2 += diff2 ; - } - variance = sum2 / hashtable->num_entries ; - nzavg = hashtable->num_entries / (double) nzero_cnt ; - fprintf( stderr, "[%s]:min:%ld max:%ld nonzero avg:%f\n", routine, min, max, nzavg ) ; - fprintf( stderr, " variance:%f std dev:%f target:%f nonzero entries:%ld / %ld\n", - variance, sqrt(variance), avg, nzero_cnt, tablesize ) ; + variance = sum2 / hashtable->num_entries ; + nzavg = hashtable->num_entries / (double) nzero_cnt ; + fprintf( stderr, "[%s]:min:%ld max:%ld nonzero avg:%f\n", routine, min, max, nzavg ) ; + fprintf( stderr, " variance:%f std dev:%f target:%f nonzero entries:%ld / %ld\n", + variance, sqrt(variance), avg, nzero_cnt, tablesize ) ; } /* end nghash_distribution() */