updated hash methods. just increasing the size of the hash helps a lot with loading larger networks. perhaps hash could be extended to dynamically size based on network size.
This commit is contained in:
186
src/hash.c
186
src/hash.c
@@ -1,22 +1,22 @@
|
||||
/*-----------------------------------------------------------------------------
|
||||
** hash.c
|
||||
**
|
||||
** Implementation of a simple Hash Table for string storage & retrieval
|
||||
**
|
||||
** Written by L. Rossman
|
||||
** Last Updated on 6/19/03
|
||||
**
|
||||
** The hash table data structure (HTable) is defined in "hash.h".
|
||||
** Interface Functions:
|
||||
** HTcreate() - creates a hash table
|
||||
** HTinsert() - inserts a string & its index value into a hash table
|
||||
** HTfind() - retrieves the index value of a string from a table
|
||||
** HTfree() - frees a hash table
|
||||
**
|
||||
*********************************************************************
|
||||
** NOTE: This is a modified version of the original HASH.C module.
|
||||
*********************************************************************
|
||||
*/
|
||||
** hash.c
|
||||
**
|
||||
** Implementation of a simple Hash Table for string storage & retrieval
|
||||
**
|
||||
** Written by L. Rossman
|
||||
** Last Updated on 6/19/03
|
||||
**
|
||||
** The hash table data structure (HTable) is defined in "hash.h".
|
||||
** Interface Functions:
|
||||
** HTcreate() - creates a hash table
|
||||
** HTinsert() - inserts a string & its index value into a hash table
|
||||
** HTfind() - retrieves the index value of a string from a table
|
||||
** HTfree() - frees a hash table
|
||||
**
|
||||
*********************************************************************
|
||||
** NOTE: This is a modified version of the original HASH.C module.
|
||||
*********************************************************************
|
||||
*/
|
||||
|
||||
#ifndef __APPLE__
|
||||
#include <malloc.h>
|
||||
@@ -26,89 +26,97 @@
|
||||
#include <string.h>
|
||||
#include "hash.h"
|
||||
|
||||
/* Use Fletcher's checksum to compute 2-byte hash of string */
|
||||
unsigned int hash(char *str)
|
||||
unsigned int _enHash(char *str);
|
||||
unsigned int _enHash(char *str)
|
||||
{
|
||||
unsigned int sum1= 0, check1;
|
||||
unsigned long sum2= 0L;
|
||||
while( '\0' != *str )
|
||||
{
|
||||
sum1 += (*str);
|
||||
str++;
|
||||
if ( 255 <= sum1 ) sum1 -= 255;
|
||||
sum2 += sum1;
|
||||
unsigned int hash = 5381;
|
||||
int c;
|
||||
while ((c = *str++)) {
|
||||
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
||||
}
|
||||
unsigned int retHash = hash % ENHASHTABLEMAXSIZE;
|
||||
return retHash;
|
||||
}
|
||||
|
||||
ENHashTable *ENHashTableCreate()
|
||||
{
|
||||
int i;
|
||||
ENHashTable *ht = (ENHashTable *) calloc(ENHASHTABLEMAXSIZE, sizeof(ENHashTable));
|
||||
if (ht != NULL) {
|
||||
for (i=0; i<ENHASHTABLEMAXSIZE; i++) {
|
||||
ht[i] = NULL;
|
||||
}
|
||||
check1= sum2;
|
||||
check1 %= 255;
|
||||
check1= 255 - (sum1+check1) % 255;
|
||||
sum1= 255 - (sum1+check1) % 255;
|
||||
return( ( ( check1 << 8 ) | sum1 ) % HTMAXSIZE);
|
||||
}
|
||||
return(ht);
|
||||
}
|
||||
|
||||
HTtable *HTcreate()
|
||||
int ENHashTableInsert(ENHashTable *ht, char *key, int data)
|
||||
{
|
||||
int i;
|
||||
HTtable *ht = (HTtable *) calloc(HTMAXSIZE, sizeof(HTtable));
|
||||
if (ht != NULL) for (i=0; i<HTMAXSIZE; i++) ht[i] = NULL;
|
||||
return(ht);
|
||||
unsigned int i = _enHash(key);
|
||||
ENHashEntry *entry;
|
||||
if ( i >= ENHASHTABLEMAXSIZE ) {
|
||||
return(0);
|
||||
}
|
||||
entry = (ENHashEntry *) malloc(sizeof(ENHashEntry));
|
||||
if (entry == NULL) {
|
||||
return(0);
|
||||
}
|
||||
entry->key = key;
|
||||
entry->data = data;
|
||||
entry->next = ht[i];
|
||||
ht[i] = entry;
|
||||
return(1);
|
||||
}
|
||||
|
||||
int HTinsert(HTtable *ht, char *key, int data)
|
||||
int ENHashTableFind(ENHashTable *ht, char *key)
|
||||
{
|
||||
unsigned int i = hash(key);
|
||||
struct HTentry *entry;
|
||||
if ( i >= HTMAXSIZE ) return(0);
|
||||
entry = (struct HTentry *) malloc(sizeof(struct HTentry));
|
||||
if (entry == NULL) return(0);
|
||||
entry->key = key;
|
||||
entry->data = data;
|
||||
entry->next = ht[i];
|
||||
ht[i] = entry;
|
||||
return(1);
|
||||
unsigned int i = _enHash(key);
|
||||
ENHashEntry *entry;
|
||||
if ( i >= ENHASHTABLEMAXSIZE ) {
|
||||
return(NOTFOUND);
|
||||
}
|
||||
entry = ht[i];
|
||||
while (entry != NULL)
|
||||
{
|
||||
if ( strcmp(entry->key,key) == 0 ) {
|
||||
return(entry->data);
|
||||
}
|
||||
entry = entry->next;
|
||||
}
|
||||
return(NOTFOUND);
|
||||
}
|
||||
|
||||
int HTfind(HTtable *ht, char *key)
|
||||
char *ENHashTableFindKey(ENHashTable *ht, char *key)
|
||||
{
|
||||
unsigned int i = hash(key);
|
||||
struct HTentry *entry;
|
||||
if ( i >= HTMAXSIZE ) return(NOTFOUND);
|
||||
entry = ht[i];
|
||||
while (entry != NULL)
|
||||
{
|
||||
if ( strcmp(entry->key,key) == 0 ) return(entry->data);
|
||||
entry = entry->next;
|
||||
}
|
||||
return(NOTFOUND);
|
||||
unsigned int i = _enHash(key);
|
||||
ENHashEntry *entry;
|
||||
if ( i >= ENHASHTABLEMAXSIZE ) {
|
||||
return(NULL);
|
||||
}
|
||||
entry = ht[i];
|
||||
while (entry != NULL)
|
||||
{
|
||||
if ( strcmp(entry->key,key) == 0 ) {
|
||||
return(entry->key);
|
||||
}
|
||||
entry = entry->next;
|
||||
}
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
char *HTfindKey(HTtable *ht, char *key)
|
||||
void ENHashTableFree(ENHashTable *ht)
|
||||
{
|
||||
unsigned int i = hash(key);
|
||||
struct HTentry *entry;
|
||||
if ( i >= HTMAXSIZE ) return(NULL);
|
||||
entry = ht[i];
|
||||
while (entry != NULL)
|
||||
{
|
||||
if ( strcmp(entry->key,key) == 0 ) return(entry->key);
|
||||
entry = entry->next;
|
||||
}
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
void HTfree(HTtable *ht)
|
||||
{
|
||||
struct HTentry *entry,
|
||||
*nextentry;
|
||||
int i;
|
||||
for (i=0; i<HTMAXSIZE; i++)
|
||||
{
|
||||
entry = ht[i];
|
||||
while (entry != NULL)
|
||||
{
|
||||
nextentry = entry->next;
|
||||
free(entry);
|
||||
entry = nextentry;
|
||||
}
|
||||
}
|
||||
free(ht);
|
||||
ENHashEntry *entry, *nextentry;
|
||||
int i;
|
||||
for (i=0; i<ENHASHTABLEMAXSIZE; i++)
|
||||
{
|
||||
entry = ht[i];
|
||||
while (entry != NULL)
|
||||
{
|
||||
nextentry = entry->next;
|
||||
free(entry);
|
||||
entry = nextentry;
|
||||
}
|
||||
}
|
||||
free(ht);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user