143 static ULONG CalculateHashSize(
ULONG hashsize);
145 static void dumpHashTable(FILE *out,
HASHTABLE *hashtable);
172 hashtable->
size = CalculateHashSize(hashsize);
173 if((hashtable->
table =
177 for(i=0; i<hashtable->
size; i++)
207 for(i=0; keylist[i] !=
NULL; i++)
236 hashtab = hashtable->
table;
240 for(i=0; i<hashtable->
size; i++)
246 for(hashEntry=hashtab[i]; hashEntry!=
NULL;
NEXT(hashEntry))
252 if((keys = (
char **)malloc((nKeys+1)*
sizeof(
char *)))==
NULL)
259 for(i=0, j=0; i<hashtable->
size && j<nKeys; i++)
265 for(hashEntry=hashtab[i]; hashEntry!=
NULL;
NEXT(hashEntry))
299 if(hashtable ==
NULL)
302 hashtab = hashtable->
table;
305 for(i=0; i<hashtable->
size; i++)
312 hashEntry=hashtab[i];
315 while(hashEntry!=
NULL)
317 nextItem=hashEntry->
next;
318 if(hashEntry->
key !=
NULL) free(hashEntry->
key);
361 if(hashtable ==
NULL)
365 if((hashEntry=lookup(hashtable, key))==
NULL)
370 hashIndex = hash(key, hashtable->
size);
383 hashEntry->
next=hashtab[hashIndex];
384 hashtab[hashIndex]=hashEntry;
409 stringPtr = va_arg(ap,
char *);
414 intValue = va_arg(ap,
int);
418 charValue = va_arg(ap,
int);
422 doubleValue = va_arg(ap,
double);
426 ptrValue = va_arg(ap,
BPTR);
432 hashEntry->
type = type;
555 return(*(
int *)value);
579 return(*(
double *)value);
603 return(*(
char *)value);
672 if(hashtable ==
NULL)
675 if((hashEntry = lookup(hashtable, key))==
NULL)
682 *type = hashEntry->
type;
684 switch(hashEntry->
type)
722 char **keyList =
NULL;
727 for(i=0; keyList[i]!=
NULL; i++)
732 fprintf(out,
"%s => ", keyList[i]);
737 fprintf(out,
"%d\n", *(
int *)value);
740 fprintf(out,
"%c\n", *(
char *)value);
743 fprintf(out,
"%f\n", *(
double *)value);
746 fprintf(out,
"%s\n", (
char *)value);
750 fprintf(out,
"%lu\n", (
unsigned long)value);
779 if(lookup(hashtable, key)==
NULL)
806 if(hashtable ==
NULL)
809 hashtab = hashtable->
table;
812 if((hashEntry=lookup(hashtable, key))!=
NULL)
819 hashIndex = hash(key, hashtable->
size);
821 for(h=hashtab[hashIndex]; h!=
NULL;
NEXT(h))
827 hashtab[hashIndex] = h->
next;
838 free(hashEntry->
key);
865 static void dumpHashTable(FILE *out,
HASHTABLE *hashtable)
870 if(hashtable ==
NULL)
873 hashtab = hashtable->
table;
875 for(i=0; i<hashtable->
size; i++)
879 fprintf(out,
"()\n");
886 for(hashEntry=hashtab[i]; hashEntry!=
NULL;
NEXT(hashEntry))
888 switch(hashEntry->
type)
891 fprintf(out,
"(%s => %d) ",
895 fprintf(out,
"(%s => %s) ",
899 fprintf(out,
"(%s => %f) ",
903 fprintf(out,
"(%s => %c) ",
907 fprintf(out,
"(%s => %lu) ",
908 hashEntry->
key, (
unsigned long)hashEntry->
ptrValue);
938 for(h=0; *
string;
string++)
965 if(hashtable ==
NULL)
968 hashtab = hashtable->
table;
969 hashIndex = hash(key, hashtable->
size);
971 for(hashEntry=hashtab[hashIndex]; hashEntry!=
NULL;
NEXT(hashEntry))
973 if(!strcmp(hashEntry->
key,key))
998 static ULONG CalculateHashSize(
ULONG hashsize)
1006 return(1+(2*((
int)(hashsize/2))));
1020 int main(
int argc,
char **argv)
1023 char *keys[]={
"name",
"address",
"phone",
"room101",
NULL};
1024 char *values[]={
"Andrew",
"London",
"02071234567",
"Contents",
NULL};
1029 fprintf(stderr,
"No memory for hash table\n");
1033 for(i=0; keys[i]!=
NULL; i++)
1036 printf(
"\n\nInitial hash...\n");
1053 printf(
"\n\nAfter updating...\n");
1057 dumpHashTable(stdout, hashtable);
1060 printf(
"\n\nSome individual values:\n");
1061 printf(
"an int : %d\n",
1063 printf(
"double : %f\n",
1065 printf(
"pointer : %lu\n",
1067 printf(
"char : '%c'\n",
1069 printf(
"phone : \"%s\"\n",
1073 printf(
"\nChecking key existence:\n");
1074 printf(
"Key fubar: %s exist\n",
1076 printf(
"Key phone: %s exist\n",
1080 printf(
"\nDeleting entries: \"phone\" and \"double\"\n");
int main(int argc, char **argv)
BOOL blSetHashValuePointer(HASHTABLE *hashtable, char *key, BPTR ptr)
double blGetHashValueDouble(HASHTABLE *hashtable, char *key)
BOOL blSetHashValueDouble(HASHTABLE *hashtable, char *key, double value)
char ** blGetHashKeyList(HASHTABLE *hashtable)
void blDeleteHashKey(HASHTABLE *hashtable, char *key)
Defines for using hash functions.
BOOL blDumpHash(FILE *out, HASHTABLE *hashtable)
char * blGetHashValueString(HASHTABLE *hashtable, char *key)
char * blStrdup(char *instr)
BOOL blSetHashValueString(HASHTABLE *hashtable, char *key, char *value)
BOOL blSetHashValueInt(HASHTABLE *hashtable, char *key, int value)
BPTR blGetHashValuePointer(HASHTABLE *hashtable, char *key)
HASHTABLE * blInitializeHash(ULONG hashsize)
void blFreeHashKeyList(char **keylist)
void blFreeHash(HASHTABLE *hashtable)
int blGetHashValueInt(HASHTABLE *hashtable, char *key)
Prototypes, etc. for maths utility routines.
BOOL blHashKeyDefined(HASHTABLE *hashtable, char *key)
BOOL blSetHashValueChar(HASHTABLE *hashtable, char *key, char value)
char blGetHashValueChar(HASHTABLE *hashtable, char *key)
BPTR blGetHashValue(HASHTABLE *hashtable, char *key, int *type)
ULONG blFindNextPrime(ULONG input, BOOL above)
BOOL blSetHashValue(HASHTABLE *hashtable, char *key, int type,...)