/*---------------------------------------------------------------------- File : hashtest.c Contents: test of static hash tables Author : Christian Borgelt History : 04.06.1998 file created 02.09.1998 adapted to changes of module hashtab ----------------------------------------------------------------------*/ #include #include #include #include "hashtab.h" /*---------------------------------------------------------------------- Preprocessor Definitions ----------------------------------------------------------------------*/ #define NAMELEN 256 /* maximal length of names */ #define TABSIZE 19 /* size of hash table */ /*---------------------------------------------------------------------- Auxiliary Functions ----------------------------------------------------------------------*/ static char* getname (char *buf, int maxlen) { /* --- read a name */ char *dst, *src; /* to traverse the name read */ fgets(buf, maxlen, stdin); /* read a string from stdin */ dst = src = buf; /* traverse the name read */ while ((*src == ' ') || (*src == '\t')) src++; /* skip leading blanks */ while (*src) *dst++ = *src++; /* copy name to front */ if ((dst > buf) && (*(dst-1) == '\n')) dst--; /* if there is a newline, remove it */ *dst = '\0'; /* terminate the string */ return buf; /* return buffer read into */ } /* getname() */ /*--------------------------------------------------------------------*/ static unsigned int hash (const char *s) { /* --- hash function */ unsigned int h = 0; /* hash value */ while (*s) h = (h << 3) ^ (unsigned int)*s++; return h; /* compute hash value */ } /* hash() */ /*---------------------------------------------------------------------- Main Function ----------------------------------------------------------------------*/ int main (int argc, char *argv[]) { /* --- main function */ int i, cnt; /* loop variable, number of elements */ HASHTAB *htab; /* hash table */ char name[NAMELEN]; /* name to insert/look up */ char *tmp; /* buffer for name to insert */ int cmd; /* command */ htab = ht_create(TABSIZE, (HASHFN*)hash, (HT_CMPFN*)strcmp, free); if (!htab) { printf("%s: not enough memory\n", argv[0]); return -1; } /* create a hash table */ printf("%s --- demonstration of static hashing\n", argv[0]); printf("type l for a list of commands\n"); while (1) { /* command loop */ printf("> "); fflush(stdout); do { cmd = getchar(); /* get next command */ } while (strchr(" \t\n", cmd) != NULL); switch (cmd) { /* evaluate command */ case 'i': /* -- insert into hash table */ tmp = (char*)malloc(strlen(getname(name, NAMELEN)) +1); if (!tmp) { printf("not enough memory\n"); break; } i = ht_insert(htab, strcpy(tmp, name)); if (i >= 0) break; /* insert name into vector */ if (i < -1) printf("%s already exists\n", name); else printf("not enough memory\n"); free(tmp); break; /* if an error occured, delete name */ case 'o': /* -- look up in hash table */ tmp = ht_lookup(htab, getname(name, NAMELEN)); printf("%s %sfound\n", name, (tmp) ? "" : "not "); break; /* look up name and print result */ case 'c': /* -- clear hash table */ ht_clear(htab); break; case 's': /* -- show hash table contents */ for (cnt = ht_size(htab), i = 0; i < cnt; i++) { printf("%2li:", i); /* traverse hash table */ tmp = ht_get(htab, i); if (tmp) printf(" \"%s\"", tmp); printf("\n"); /* print all hash table elements */ } break; case 'l': /* -- print help */ printf("commands:\n"); /* (i.e. a list of commands) */ printf("i n --- insert name n into hash table\n"); printf("o n --- look up name n in hash table\n"); printf("c --- clear hash table (remove all names)\n"); printf("s --- show contents of hash table\n"); printf("l --- print this list of commands\n"); printf("q --- quit program\n"); break; case 'q': /* -- quit program */ ht_delete(htab); /* delete hash table */ return 0; /* return `ok' */ default: /* -- unknown command */ printf("unknown command --- type l for a list\n"); break; } } /* while (1) */ } /* main() */