#include "HashTable.h" #include #include CcuDictionnary dict (16); main () { char line [256]; char *s; int i = 0; printf ("? to get help\n"); for (;;) { s = gets (line); if (! s) break; if (strcmp (s, ".") == 0) break; /* help */ if (strcmp (s, "?") == 0) { printf ("name add name\n"); printf ("-name delete name\n"); printf ("# dump table\n"); #ifdef TUNE printf ("%% statistics\n"); #endif printf ("> rehash into smaller\n"); printf ("< rehash into larger\n"); printf (". quit\n"); continue; } #ifdef TUNE /* stats */ if (strcmp (s, "%") == 0) { dict.CollStats (); continue; } #endif if (strcmp (s, "#") == 0) { CcuDictionnary dictbis (dict); CcuHashCellIter hi (dictbis); while (++hi) printf ("%s\n", (*hi)->GetKey()); continue; } /* resizing */ if (strcmp (s, ">") == 0) { int s; s = dict.GetSize() / 2; if (s < 1) { printf ("too small\n"); continue; } printf ("rehashing into smaller table : size %d\n", s); dict.Resize (s); continue; } if (strcmp (s, "<") == 0) { int s; s = dict.GetSize() * 2; printf ("rehashing into larger table : size %d\n", s); dict.Resize (s); continue; } /* delete */ if (*s == '-') { int found; void* p = dict.Remove (++s, &found); if (! found) printf ("%s not found\n", s); else printf ("%s had info %d\n", s, (int) p); continue; } /* add */ // BasicHashCell* h = dict.Add (s, &found); // int found; // if (found) // printf ("%s duplicate\n", s); // else // h->SetInfo ((void*) i++); dict[s] = (void*) i++; } } #if 0 static char strout[200]; static char *ptout; static h_strlen (char *str) { int i = 0; char *pt = str; while (*pt++) ; return (pt - str); } static decimal(int i) { int tmp = 0; char buff[15]; char *pbuff = buff; do { tmp = i % 10; *pbuff ++ = tmp + '0'; i /= 10; } while (i) ; while (pbuff - buff) *ptout++ = *--pbuff; } static hexa(int i) { int tmp = 0; char buff[15]; char *pbuff = buff; do { tmp = i % 16; *pbuff ++ = "0123456789abcdef"[tmp]; i /= 16; } while (i) ; while (pbuff - buff) *ptout++ = *--pbuff; } static h_write(int fd, char *buff, int n) { write(fd, buff, n); } print(char *fmt, int a1, int a2, int a3, int a4, int a5, int a6) { char *pt = fmt; char c; int arg[6]; int i = 0; ptout = strout; arg[0] = a1; arg[1] = a2; arg[2] = a3; arg[3] = a4; arg[4] = a5; arg[5] = a6; for (c = *pt; c = *pt; pt++) { if(c != '%'){ *ptout++ = c; continue; } switch (c = *++pt) { default: *ptout++ = c; break; case 'd': decimal((int)arg[i++]); break; case 'x': hexa((int)arg[i++]); break; case 'c': *ptout++ = arg[i++]; break; case 's':{ char *p; for (p = (char *)arg[i++]; *p; p++) *ptout++ = *p; } } } *ptout = 0; h_write(1, strout, h_strlen(strout)); } #endif