- fprintf (f, "\t%lu lookups\n", table->lookups);
- fprintf (f, "\t%lu hash comparisons\n", table->hash_compares);
- fprintf (f, "\t%lu string comparisons\n", table->string_compares);
- fprintf (f, "\t%lu insertions\n", table->insertions);
- fprintf (f, "\t%lu replacements\n", table->replacements);
- fprintf (f, "\t%lu deletions\n", table->deletions);
-
- total = 0;
- empty = 0;
- for (i = 0; i < table->size; ++i)
- {
- struct hash_entry *p;
-
- if (table->table[i] == NULL)
- ++empty;
- else
- {
- for (p = table->table[i]; p != NULL; p = p->next)
- ++total;
- }
- }
-
- fprintf (f, "\t%g average chain length\n", (double) total / table->size);
- fprintf (f, "\t%lu empty slots\n", empty);
-#endif
-}
-\f
-#ifdef TEST
-
-/* This test program is left over from the old hash table code. */
-
-/* Number of hash tables to maintain (at once) in any testing. */
-#define TABLES (6)
-
-/* We can have 12 statistics. */
-#define STATBUFSIZE (12)
-
-/* Display statistics here. */
-int statbuf[STATBUFSIZE];
-
-/* Human farts here. */
-char answer[100];
-
-/* We test many hash tables at once. */
-char *hashtable[TABLES];
-
-/* Points to current hash_control. */
-char *h;
-char **pp;
-char *p;
-char *name;
-char *value;
-int size;
-int used;
-char command;
-
-/* Number 0:TABLES-1 of current hashed symbol table. */
-int number;
-
-int
-main ()
-{
- void applicatee ();
- void destroy ();
- char *what ();
- int *ip;
-
- number = 0;
- h = 0;
- printf ("type h <RETURN> for help\n");
- for (;;)
- {
- printf ("hash_test command: ");
- gets (answer);
- command = answer[0];
- command = TOLOWER (command); /* Ecch! */
- switch (command)
- {
- case '#':
- printf ("old hash table #=%d.\n", number);
- whattable ();
- break;
- case '?':
- for (pp = hashtable; pp < hashtable + TABLES; pp++)
- {
- printf ("address of hash table #%d control block is %xx\n",
- pp - hashtable, *pp);
- }
- break;
- case 'a':
- hash_traverse (h, applicatee);
- break;
- case 'd':
- hash_traverse (h, destroy);
- hash_die (h);
- break;
- case 'f':
- p = hash_find (h, name = what ("symbol"));
- printf ("value of \"%s\" is \"%s\"\n", name, p ? p : "NOT-PRESENT");
- break;
- case 'h':
- printf ("# show old, select new default hash table number\n");
- printf ("? display all hashtable control block addresses\n");
- printf ("a apply a simple display-er to each symbol in table\n");
- printf ("d die: destroy hashtable\n");
- printf ("f find value of nominated symbol\n");
- printf ("h this help\n");
- printf ("i insert value into symbol\n");
- printf ("j jam value into symbol\n");
- printf ("n new hashtable\n");
- printf ("r replace a value with another\n");
- printf ("s say what %% of table is used\n");
- printf ("q exit this program\n");
- printf ("x delete a symbol from table, report its value\n");
- break;
- case 'i':
- p = hash_insert (h, name = what ("symbol"), value = what ("value"));
- if (p)
- {
- printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value,
- p);
- }
- break;
- case 'j':
- p = hash_jam (h, name = what ("symbol"), value = what ("value"));
- if (p)
- {
- printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value, p);
- }
- break;
- case 'n':
- h = hashtable[number] = (char *) hash_new ();
- break;
- case 'q':
- exit (EXIT_SUCCESS);
- case 'r':
- p = hash_replace (h, name = what ("symbol"), value = what ("value"));
- printf ("old value was \"%s\"\n", p ? p : "{}");
- break;
- case 's':
- hash_say (h, statbuf, STATBUFSIZE);
- for (ip = statbuf; ip < statbuf + STATBUFSIZE; ip++)
- {
- printf ("%d ", *ip);
- }
- printf ("\n");
- break;
- case 'x':
- p = hash_delete (h, name = what ("symbol"));
- printf ("old value was \"%s\"\n", p ? p : "{}");
- break;
- default:
- printf ("I can't understand command \"%c\"\n", command);
- break;
- }
- }