#pragma weak _hcreate = hcreate
#pragma weak _hdestroy = hdestroy
#pragma weak _hsearch = hsearch
#include "lint.h"
#include <mtlib.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread.h>
#include <synch.h>
#include <search.h>
typedef char *POINTER;
#define SUCCEED 0
#define FAIL 1
#define TRUE 1
#define FALSE 0
#define repeat for (;;)
#define until(A) if (A) break;
#ifdef OPEN
#undef CHAINED
#else
#ifndef CHAINED
#define OPEN
#endif
#endif
#ifdef MULT
#undef DIV
#else
#ifndef DIV
#define MULT
#endif
#endif
#ifdef START
#undef SORTUP
#undef SORTDOWN
#else
#ifdef SORTUP
#undef SORTDOWN
#endif
#endif
#ifdef USCR
#define COMPARE(A, B) (* hcompar)((A), (B))
extern int (* hcompar)();
#else
#define COMPARE(A, B) strcmp((A), (B))
#endif
#ifdef MULT
#define SHIFT ((CHAR_BIT * sizeof (int)) - m)
#define FACTOR 035761254233
#define HASH hashm
#define HASH2 hash2m
static unsigned int hashm(POINTER);
static unsigned int hash2m(POINTER);
#else
#ifdef DIV
#define HASH hashd
#define HASH2(A) 1
static unsigned int hashd();
#endif
#endif
#ifdef CHAINED
typedef struct node {
ENTRY item;
struct node *next;
} NODE;
typedef NODE *TABELEM;
static NODE **table;
static ENTRY *build();
#else
#ifdef OPEN
typedef ENTRY TABELEM;
static TABELEM *table;
static unsigned int count = 0;
#endif
#endif
static unsigned int length;
static unsigned int m;
static unsigned int prcnt;
static mutex_t table_lock = DEFAULTMUTEX;
#define RETURN(n) { lmutex_unlock(&table_lock); return (n); }
static unsigned int crunch(POINTER);
#ifdef DRIVER
static void hdump();
main()
{
char line[80];
int i = 0;
ENTRY *res;
ENTRY *new;
start:
if (hcreate(5))
printf("Length = %u, m = %u\n", length, m);
else {
fprintf(stderr, "Out of core\n");
exit(FAIL);
}
repeat {
hdump();
printf("Enter a probe: ");
until(EOF == scanf("%s", line) || strcmp(line, "quit") == 0);
#ifdef HSEARCH_DEBUG
printf("%s, ", line);
printf("division: %d, ", hashd(line));
printf("multiplication: %d\n", hashm(line));
#endif
new = (ENTRY *) malloc(sizeof (ENTRY));
if (new == NULL) {
fprintf(stderr, "Out of core \n");
exit(FAIL);
} else {
new->key = malloc((unsigned)strlen(line) + 1);
if (new->key == NULL) {
fprintf(stderr, "Out of core \n");
exit(FAIL);
}
(void) strcpy(new->key, line);
new->data = malloc(sizeof (int));
if (new->data == NULL) {
fprintf(stderr, "Out of core \n");
exit(FAIL);
}
*new->data = i++;
}
res = hsearch(*new, ENTER);
printf("The number of probes required was %d\n", prcnt);
if (res == (ENTRY *) 0)
printf("Table is full\n");
else {
printf("Success: ");
printf("Key = %s, Value = %d\n", res->key, *res->data);
}
}
printf("Do you wish to start another hash table (yes/no?)");
if (EOF == scanf("%s", line) || strcmp(line, "no") == 0)
exit(SUCCEED);
hdestroy();
goto start;
}
#endif
int
hcreate(size_t size)
{
size_t unsize;
TABELEM *local_table;
TABELEM *old_table;
unsigned int local_length;
unsigned int local_m;
if (size == 0)
return (FALSE);
unsize = size;
local_length = 1;
local_m = 0;
while (unsize) {
unsize >>= 1;
local_length <<= 1;
local_m++;
}
local_table = (TABELEM *) calloc(local_length, sizeof (TABELEM));
lmutex_lock(&table_lock);
old_table = table;
table = local_table;
length = local_length;
m = local_m;
lmutex_unlock(&table_lock);
if (old_table != NULL)
free(old_table);
return (local_table != NULL);
}
void
hdestroy(void)
{
POINTER local_table;
lmutex_lock(&table_lock);
#ifdef CHAINED
int i;
NODE *p, *oldp;
for (i = 0; i < length; i++) {
if (table[i] != (NODE *)NULL) {
p = table[i];
while (p != (NODE *)NULL) {
oldp = p;
p = p -> next;
free(oldp);
}
}
}
#endif
local_table = (POINTER)table;
table = 0;
#ifdef OPEN
count = 0;
#endif
lmutex_unlock(&table_lock);
free(local_table);
}
#ifdef OPEN
ENTRY
*hsearch(ENTRY item, ACTION action)
{
unsigned int i;
unsigned int c;
lmutex_lock(&table_lock);
prcnt = 1;
i = HASH(item.key);
#ifdef HSEARCH_DEBUG
if (action == ENTER)
printf("hash = %o\n", i);
#endif
if (table[i].key == NULL)
goto D6;
else if (COMPARE(table[i].key, item.key) == 0)
RETURN(&table[i]);
c = HASH2(item.key);
#ifdef HSEARCH_DEBUG
if (action == ENTER)
printf("hash2 = %o\n", c);
#endif
D4:
i = (i + c) % length;
prcnt++;
if (table[i].key == NULL)
goto D6;
else if (COMPARE(table[i].key, item.key) == 0)
RETURN(&table[i])
else
goto D4;
D6: if (action == FIND)
RETURN((ENTRY *) NULL);
if (count == (length - 1))
RETURN((ENTRY *) 0);
#ifdef BRENT
{
unsigned int p0 = HASH(item.key);
unsigned int c0 = HASH2(item.key);
unsigned int r = prcnt - 1;
unsigned int j;
unsigned int k;
unsigned int curj;
unsigned int curpos;
unsigned int pj;
unsigned int cj;
unsigned int pjk;
if (prcnt >= 3) {
for (j = 0; j < prcnt; j++) {
pj = (p0 + j * c0) % length;
cj = HASH2(table[pj].key);
for (k = 1; j+k <= r; k++) {
pjk = (pj + k * cj) % length;
if (table[pjk].key == NULL) {
r = j + k;
curj = pj;
curpos = pjk;
}
}
}
if (r != prcnt - 1) {
table[curpos] = table[curj];
#ifdef HSEARCH_DEBUG
printf("Switch curpos = %o, curj = %o, oldi = %o\n",
curj, curpos, i);
#endif
i = curj;
}
}
}
#endif
count++;
table[i] = item;
lmutex_unlock(&table_lock);
return (&table[i]);
}
#endif
#ifdef USCR
#ifdef DRIVER
static int
compare(a, b)
POINTER a;
POINTER b;
{
return (strcmp(a, b));
}
int (* hcompar)() = compare;
#endif
#endif
#ifdef CHAINED
#ifdef SORTUP
#define STRCMP(A, B) (COMPARE((A), (B)) > 0)
#else
#ifdef SORTDOWN
#define STRCMP(A, B) (COMPARE((A), (B)) < 0)
#else
#define STRCMP(A, B) (COMPARE((A), (B)) != 0)
#endif
#endif
ENTRY
*hsearch(item, action)
ENTRY item;
ACTION action;
{
NODE *p;
NODE **q;
unsigned int i;
int res;
lmutex_lock(&table_lock);
prcnt = 1;
i = HASH(item.key);
if (table[i] == (NODE*)NULL) {
if (action == FIND)
RETURN((ENTRY *) NULL);
else
RETURN(build(&table[i], (NODE *) NULL, item));
} else {
q = &table[i];
p = table[i];
while (p != NULL && (res = STRCMP(item.key, p->item.key))) {
prcnt++;
q = &(p->next);
p = p->next;
}
if (p != NULL && res == 0)
RETURN(&(p->item));
else {
if (action == FIND)
RETURN((ENTRY *) NULL);
else
#ifdef START
RETURN(build(&table[i], table[i], item));
#else
RETURN(build(q, p, item));
#endif
}
}
}
static ENTRY
*build(last, next, item)
NODE **last;
NODE *next;
ENTRY item;
{
NODE *p = (NODE *) malloc(sizeof (NODE));
if (p != NULL) {
p->item = item;
*last = p;
p->next = next;
return (&(p->item));
} else
return (NULL);
}
#endif
#ifdef DIV
static unsigned int
hashd(key)
POINTER key;
{
return (crunch(key) % length);
}
#else
#ifdef MULT
static unsigned int
hashm(POINTER key)
{
return ((int)(((unsigned)(crunch(key) * FACTOR)) >> SHIFT));
}
static unsigned int
hash2m(POINTER key)
{
return (((unsigned int)((crunch(key) * FACTOR) << m) >> SHIFT) | 1);
}
#endif
#endif
static unsigned int
crunch(POINTER key)
{
unsigned int h = 0;
unsigned int g;
unsigned char *p = (unsigned char *)key;
for (; *p; p++) {
h = (h << 4) + *p;
g = h & 0xf0000000;
if (g != 0) {
h ^= (g >> 24);
h ^= g;
}
}
return (h);
}
#ifdef DRIVER
static void
hdump()
{
unsigned int i;
#ifdef OPEN
unsigned int sum = 0;
#else
#ifdef CHAINED
NODE *a;
#endif
#endif
for (i = 0; i < length; i++)
#ifdef OPEN
if (table[i].key == NULL)
printf("%o.\t-,\t-,\t(NULL)\n", i);
else {
unsigned int oldpr = prcnt;
hsearch(table[i], FIND);
sum += prcnt;
printf("%o.\t%d,\t%d,\t%s\n", i,
*table[i].data, prcnt, table[i].key);
prcnt = oldpr;
}
printf("Total probes = %d\n", sum);
#else
#ifdef CHAINED
if (table[i] == NULL)
printf("%o.\t-,\t-,\t(NULL)\n", i);
else {
printf("%o.", i);
for (a = table[i]; a != NULL; a = a->next)
printf("\t%d,\t%#0.4x,\t%s\n",
*a->item.data, a, a->item.key);
}
#endif
#endif
}
#endif