2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * Compile time switches:
2N/A *
2N/A * MULT - use a multiplicative hashing function.
2N/A * DIV - use the remainder mod table size as a hashing function.
2N/A * CHAINED - use a linked list to resolve collisions.
2N/A * OPEN - use open addressing to resolve collisions.
2N/A * BRENT - use Brent's modification to improve the OPEN algorithm.
2N/A * SORTUP - CHAINED list is sorted in increasing order.
2N/A * SORTDOWN - CHAINED list is sorted in decreasing order.
2N/A * START - CHAINED list with entries appended at front.
2N/A * DRIVER - compile in a main program to drive the tests.
2N/A * DEBUG - compile some debugging printout statements.
2N/A * USCR - user supplied comparison routine.
2N/A */
2N/A
2N/A#pragma weak _hcreate = hcreate
2N/A#pragma weak _hdestroy = hdestroy
2N/A#pragma weak _hsearch = hsearch
2N/A
2N/A#include "lint.h"
2N/A#include <mtlib.h>
2N/A#include <limits.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <thread.h>
2N/A#include <synch.h>
2N/A#include <search.h>
2N/A
2N/Atypedef char *POINTER;
2N/A
2N/A#define SUCCEED 0
2N/A#define FAIL 1
2N/A#define TRUE 1
2N/A#define FALSE 0
2N/A#define repeat for (;;)
2N/A#define until(A) if (A) break;
2N/A
2N/A#ifdef OPEN
2N/A#undef CHAINED
2N/A#else
2N/A#ifndef CHAINED
2N/A#define OPEN
2N/A#endif
2N/A#endif
2N/A
2N/A#ifdef MULT
2N/A#undef DIV
2N/A#else
2N/A#ifndef DIV
2N/A#define MULT
2N/A#endif
2N/A#endif
2N/A
2N/A#ifdef START
2N/A#undef SORTUP
2N/A#undef SORTDOWN
2N/A#else
2N/A#ifdef SORTUP
2N/A#undef SORTDOWN
2N/A#endif
2N/A#endif
2N/A
2N/A#ifdef USCR
2N/A#define COMPARE(A, B) (* hcompar)((A), (B))
2N/Aextern int (* hcompar)();
2N/A#else
2N/A#define COMPARE(A, B) strcmp((A), (B))
2N/A#endif
2N/A
2N/A#ifdef MULT
2N/A#define SHIFT ((CHAR_BIT * sizeof (int)) - m) /* Shift factor */
2N/A#define FACTOR 035761254233 /* Magic multiplication factor */
2N/A#define HASH hashm /* Multiplicative hash function */
2N/A#define HASH2 hash2m /* Secondary hash function */
2N/Astatic unsigned int hashm(POINTER);
2N/Astatic unsigned int hash2m(POINTER);
2N/A#else
2N/A#ifdef DIV
2N/A#define HASH hashd /* Division hashing routine */
2N/A#define HASH2(A) 1 /* Secondary hash function */
2N/Astatic unsigned int hashd();
2N/A#endif
2N/A#endif
2N/A
2N/A#ifdef CHAINED
2N/Atypedef struct node { /* Part of the linked list of entries */
2N/A ENTRY item;
2N/A struct node *next;
2N/A} NODE;
2N/Atypedef NODE *TABELEM;
2N/Astatic NODE **table; /* The address of the hash table */
2N/Astatic ENTRY *build();
2N/A#else
2N/A#ifdef OPEN
2N/Atypedef ENTRY TABELEM; /* What the table contains (TABle ELEMents) */
2N/Astatic TABELEM *table; /* The address of the hash table */
2N/Astatic unsigned int count = 0; /* Number of entries in hash table */
2N/A#endif
2N/A#endif
2N/A
2N/Astatic unsigned int length; /* Size of the hash table */
2N/Astatic unsigned int m; /* Log base 2 of length */
2N/Astatic unsigned int prcnt; /* Number of probes this item */
2N/Astatic mutex_t table_lock = DEFAULTMUTEX;
2N/A#define RETURN(n) { lmutex_unlock(&table_lock); return (n); }
2N/A
2N/A/*
2N/A * forward declarations
2N/A */
2N/A
2N/Astatic unsigned int crunch(POINTER);
2N/A
2N/A#ifdef DRIVER
2N/Astatic void hdump();
2N/A
2N/Amain()
2N/A{
2N/A char line[80]; /* Room for the input line */
2N/A int i = 0; /* Data generator */
2N/A ENTRY *res; /* Result of hsearch */
2N/A ENTRY *new; /* Test entry */
2N/A
2N/Astart:
2N/A if (hcreate(5))
2N/A printf("Length = %u, m = %u\n", length, m);
2N/A else {
2N/A fprintf(stderr, "Out of core\n");
2N/A exit(FAIL);
2N/A }
2N/A repeat {
2N/A hdump();
2N/A printf("Enter a probe: ");
2N/A until(EOF == scanf("%s", line) || strcmp(line, "quit") == 0);
2N/A#ifdef DEBUG
2N/A printf("%s, ", line);
2N/A printf("division: %d, ", hashd(line));
2N/A printf("multiplication: %d\n", hashm(line));
2N/A#endif
2N/A new = (ENTRY *) malloc(sizeof (ENTRY));
2N/A if (new == NULL) {
2N/A fprintf(stderr, "Out of core \n");
2N/A exit(FAIL);
2N/A } else {
2N/A new->key = malloc((unsigned)strlen(line) + 1);
2N/A if (new->key == NULL) {
2N/A fprintf(stderr, "Out of core \n");
2N/A exit(FAIL);
2N/A }
2N/A (void) strcpy(new->key, line);
2N/A new->data = malloc(sizeof (int));
2N/A if (new->data == NULL) {
2N/A fprintf(stderr, "Out of core \n");
2N/A exit(FAIL);
2N/A }
2N/A *new->data = i++;
2N/A }
2N/A res = hsearch(*new, ENTER);
2N/A printf("The number of probes required was %d\n", prcnt);
2N/A if (res == (ENTRY *) 0)
2N/A printf("Table is full\n");
2N/A else {
2N/A printf("Success: ");
2N/A printf("Key = %s, Value = %d\n", res->key, *res->data);
2N/A }
2N/A }
2N/A printf("Do you wish to start another hash table (yes/no?)");
2N/A if (EOF == scanf("%s", line) || strcmp(line, "no") == 0)
2N/A exit(SUCCEED);
2N/A hdestroy();
2N/A goto start;
2N/A}
2N/A#endif
2N/A
2N/Aint
2N/Ahcreate(size_t size) /* Create a hash table no smaller than size */
2N/A /* Minimum "size" for hash table */
2N/A{
2N/A size_t unsize; /* Holds the shifted size */
2N/A TABELEM *local_table;
2N/A TABELEM *old_table;
2N/A unsigned int local_length;
2N/A unsigned int local_m;
2N/A
2N/A if (size == 0)
2N/A return (FALSE);
2N/A
2N/A unsize = size; /* +1 for empty table slot; -1 for ceiling */
2N/A local_length = 1; /* Maximum entries in table */
2N/A local_m = 0; /* Log2 length */
2N/A while (unsize) {
2N/A unsize >>= 1;
2N/A local_length <<= 1;
2N/A local_m++;
2N/A }
2N/A
2N/A local_table = (TABELEM *) calloc(local_length, sizeof (TABELEM));
2N/A
2N/A lmutex_lock(&table_lock);
2N/A old_table = table;
2N/A table = local_table;
2N/A length = local_length;
2N/A m = local_m;
2N/A lmutex_unlock(&table_lock);
2N/A if (old_table != NULL)
2N/A free(old_table);
2N/A return (local_table != NULL);
2N/A}
2N/A
2N/Avoid
2N/Ahdestroy(void) /* Reset the module to its initial state */
2N/A{
2N/A POINTER local_table;
2N/A
2N/A lmutex_lock(&table_lock);
2N/A#ifdef CHAINED
2N/A int i;
2N/A NODE *p, *oldp;
2N/A for (i = 0; i < length; i++) {
2N/A if (table[i] != (NODE *)NULL) {
2N/A p = table[i];
2N/A while (p != (NODE *)NULL) {
2N/A oldp = p;
2N/A p = p -> next;
2N/A /*
2N/A * This is a locking vs malloc() violation.
2N/A * Fortunately, it is not actually compiled.
2N/A */
2N/A free(oldp);
2N/A }
2N/A }
2N/A }
2N/A#endif
2N/A local_table = (POINTER)table;
2N/A table = 0;
2N/A#ifdef OPEN
2N/A count = 0;
2N/A#endif
2N/A lmutex_unlock(&table_lock);
2N/A free(local_table);
2N/A}
2N/A
2N/A#ifdef OPEN
2N/A/*
2N/A * Hash search of a fixed-capacity table. Open addressing used to
2N/A * resolve collisions. Algorithm modified from Knuth, Volume 3,
2N/A * section 6.4, algorithm D. Labels flag corresponding actions.
2N/A */
2N/A
2N/A/* Find or insert the item into the table */
2N/AENTRY
2N/A*hsearch(ENTRY item, ACTION action)
2N/A /* "item" to be inserted or found */
2N/A /* action: FIND or ENTER */
2N/A{
2N/A unsigned int i; /* Insertion index */
2N/A unsigned int c; /* Secondary probe displacement */
2N/A
2N/A lmutex_lock(&table_lock);
2N/A prcnt = 1;
2N/A
2N/A/* D1: */
2N/A i = HASH(item.key); /* Primary hash on key */
2N/A#ifdef DEBUG
2N/A if (action == ENTER)
2N/A printf("hash = %o\n", i);
2N/A#endif
2N/A
2N/A/* D2: */
2N/A if (table[i].key == NULL) /* Empty slot? */
2N/A goto D6;
2N/A else if (COMPARE(table[i].key, item.key) == 0) /* Match? */
2N/A RETURN(&table[i]);
2N/A
2N/A/* D3: */
2N/A c = HASH2(item.key); /* No match => compute secondary hash */
2N/A#ifdef DEBUG
2N/A if (action == ENTER)
2N/A printf("hash2 = %o\n", c);
2N/A#endif
2N/A
2N/AD4:
2N/A i = (i + c) % length; /* Advance to next slot */
2N/A prcnt++;
2N/A
2N/A/* D5: */
2N/A if (table[i].key == NULL) /* Empty slot? */
2N/A goto D6;
2N/A else if (COMPARE(table[i].key, item.key) == 0) /* Match? */
2N/A RETURN(&table[i])
2N/A else
2N/A goto D4;
2N/A
2N/AD6: if (action == FIND) /* Insert if requested */
2N/A RETURN((ENTRY *) NULL);
2N/A if (count == (length - 1)) /* Table full? */
2N/A RETURN((ENTRY *) 0);
2N/A
2N/A#ifdef BRENT
2N/A/*
2N/A * Brent's variation of the open addressing algorithm. Do extra
2N/A * work during insertion to speed retrieval. May require switching
2N/A * of previously placed items. Adapted from Knuth, Volume 3, section
2N/A * 4.6 and Brent's article in CACM, volume 10, #2, February 1973.
2N/A */
2N/A
2N/A {
2N/A unsigned int p0 = HASH(item.key); /* First probe index */
2N/A unsigned int c0 = HASH2(item.key); /* Main branch increment */
2N/A unsigned int r = prcnt - 1; /* Current minimum distance */
2N/A unsigned int j; /* Counts along main branch */
2N/A unsigned int k; /* Counts along secondary branch */
2N/A unsigned int curj; /* Current best main branch site */
2N/A unsigned int curpos; /* Current best table index */
2N/A unsigned int pj; /* Main branch indices */
2N/A unsigned int cj; /* Secondary branch increment distance */
2N/A unsigned int pjk; /* Secondary branch probe indices */
2N/A
2N/A if (prcnt >= 3) {
2N/A for (j = 0; j < prcnt; j++) { /* Count along main branch */
2N/A pj = (p0 + j * c0) % length; /* New main branch index */
2N/A cj = HASH2(table[pj].key); /* Secondary branch incr. */
2N/A for (k = 1; j+k <= r; k++) {
2N/A /* Count on secondary branch */
2N/A pjk = (pj + k * cj) % length;
2N/A /* Secondary probe */
2N/A if (table[pjk].key == NULL) {
2N/A /* Improvement found */
2N/A r = j + k; /* Decrement upper bound */
2N/A curj = pj; /* Save main probe index */
2N/A curpos = pjk;
2N/A /* Save secondeary index */
2N/A }
2N/A }
2N/A }
2N/A if (r != prcnt - 1) { /* If an improvement occurred */
2N/A table[curpos] = table[curj]; /* Old key to new site */
2N/A#ifdef DEBUG
2N/A printf("Switch curpos = %o, curj = %o, oldi = %o\n",
2N/A curj, curpos, i);
2N/A#endif
2N/A i = curj;
2N/A }
2N/A }
2N/A }
2N/A#endif
2N/A count++; /* Increment table occupancy count */
2N/A table[i] = item; /* Save item */
2N/A
2N/A lmutex_unlock(&table_lock);
2N/A return (&table[i]); /* Address of item is returned */
2N/A}
2N/A#endif
2N/A
2N/A#ifdef USCR
2N/A#ifdef DRIVER
2N/Astatic int
2N/Acompare(a, b)
2N/APOINTER a;
2N/APOINTER b;
2N/A{
2N/A return (strcmp(a, b));
2N/A}
2N/A
2N/Aint (* hcompar)() = compare;
2N/A#endif
2N/A#endif
2N/A
2N/A#ifdef CHAINED
2N/A#ifdef SORTUP
2N/A#define STRCMP(A, B) (COMPARE((A), (B)) > 0)
2N/A#else
2N/A#ifdef SORTDOWN
2N/A#define STRCMP(A, B) (COMPARE((A), (B)) < 0)
2N/A#else
2N/A#define STRCMP(A, B) (COMPARE((A), (B)) != 0)
2N/A#endif
2N/A#endif
2N/A
2N/AENTRY
2N/A*hsearch(item, action) /* Chained search with sorted lists */
2N/AENTRY item; /* Item to be inserted or found */
2N/AACTION action; /* FIND or ENTER */
2N/A{
2N/A NODE *p; /* Searches through the linked list */
2N/A NODE **q; /* Where to store the pointer to a new NODE */
2N/A unsigned int i; /* Result of hash */
2N/A int res; /* Result of string comparison */
2N/A
2N/A lmutex_lock(&table_lock);
2N/A prcnt = 1;
2N/A
2N/A i = HASH(item.key); /* Table[i] contains list head */
2N/A
2N/A if (table[i] == (NODE*)NULL) { /* List has not yet been begun */
2N/A if (action == FIND)
2N/A RETURN((ENTRY *) NULL);
2N/A else
2N/A RETURN(build(&table[i], (NODE *) NULL, item));
2N/A } else { /* List is not empty */
2N/A q = &table[i];
2N/A p = table[i];
2N/A while (p != NULL && (res = STRCMP(item.key, p->item.key))) {
2N/A prcnt++;
2N/A q = &(p->next);
2N/A p = p->next;
2N/A }
2N/A
2N/A if (p != NULL && res == 0) /* Item has been found */
2N/A RETURN(&(p->item));
2N/A else { /* Item is not yet on list */
2N/A if (action == FIND)
2N/A RETURN((ENTRY *) NULL);
2N/A else
2N/A#ifdef START
2N/A RETURN(build(&table[i], table[i], item));
2N/A#else
2N/A RETURN(build(q, p, item));
2N/A#endif
2N/A }
2N/A }
2N/A}
2N/A
2N/Astatic ENTRY
2N/A*build(last, next, item)
2N/ANODE **last; /* Where to store in last list item */
2N/ANODE *next; /* Link to next list item */
2N/AENTRY item; /* Item to be kept in node */
2N/A{
2N/A /*
2N/A * This is a locking vs malloc() violation.
2N/A * Fortunately, it is not actually compiled.
2N/A */
2N/A NODE *p = (NODE *) malloc(sizeof (NODE));
2N/A
2N/A if (p != NULL) {
2N/A p->item = item;
2N/A *last = p;
2N/A p->next = next;
2N/A return (&(p->item));
2N/A } else
2N/A return (NULL);
2N/A}
2N/A#endif
2N/A
2N/A#ifdef DIV
2N/Astatic unsigned int
2N/Ahashd(key) /* Division hashing scheme */
2N/APOINTER key; /* Key to be hashed */
2N/A{
2N/A return (crunch(key) % length);
2N/A}
2N/A#else
2N/A#ifdef MULT
2N/A/*
2N/A * NOTE: The following algorithm only works on machines where
2N/A * the results of multiplying two integers is the least
2N/A * significant part of the double word integer required to hold
2N/A * the result. It is adapted from Knuth, Volume 3, section 6.4.
2N/A */
2N/A
2N/Astatic unsigned int
2N/Ahashm(POINTER key) /* Multiplication hashing scheme */
2N/A /* "key" to be hashed */
2N/A{
2N/A return ((int)(((unsigned)(crunch(key) * FACTOR)) >> SHIFT));
2N/A}
2N/A
2N/A/*
2N/A * Secondary hashing, for use with multiplicitive hashing scheme.
2N/A * Adapted from Knuth, Volume 3, section 6.4.
2N/A */
2N/A
2N/Astatic unsigned int
2N/Ahash2m(POINTER key) /* Secondary hashing routine */
2N/A /* "key" is the string to be hashed */
2N/A{
2N/A return (((unsigned int)((crunch(key) * FACTOR) << m) >> SHIFT) | 1);
2N/A}
2N/A#endif
2N/A#endif
2N/A
2N/A/* PJ Weinberger's hash function */
2N/Astatic unsigned int
2N/Acrunch(POINTER key) /* Convert multicharacter key to unsigned int */
2N/A{
2N/A unsigned int h = 0;
2N/A unsigned int g;
2N/A unsigned char *p = (unsigned char *)key;
2N/A
2N/A for (; *p; p++) {
2N/A h = (h << 4) + *p;
2N/A g = h & 0xf0000000;
2N/A if (g != 0) {
2N/A h ^= (g >> 24);
2N/A h ^= g;
2N/A }
2N/A }
2N/A return (h);
2N/A}
2N/A
2N/A#ifdef DRIVER
2N/Astatic void
2N/Ahdump() /* Dumps loc, data, probe count, key */
2N/A{
2N/A unsigned int i; /* Counts table slots */
2N/A#ifdef OPEN
2N/A unsigned int sum = 0; /* Counts probes */
2N/A#else
2N/A#ifdef CHAINED
2N/A NODE *a; /* Current Node on list */
2N/A#endif
2N/A#endif
2N/A
2N/A for (i = 0; i < length; i++)
2N/A#ifdef OPEN
2N/A if (table[i].key == NULL)
2N/A printf("%o.\t-,\t-,\t(NULL)\n", i);
2N/A else {
2N/A unsigned int oldpr = prcnt;
2N/A /* Save current probe count */
2N/A
2N/A hsearch(table[i], FIND);
2N/A sum += prcnt;
2N/A printf("%o.\t%d,\t%d,\t%s\n", i,
2N/A *table[i].data, prcnt, table[i].key);
2N/A prcnt = oldpr;
2N/A }
2N/A printf("Total probes = %d\n", sum);
2N/A#else
2N/A#ifdef CHAINED
2N/A if (table[i] == NULL)
2N/A printf("%o.\t-,\t-,\t(NULL)\n", i);
2N/A else {
2N/A printf("%o.", i);
2N/A for (a = table[i]; a != NULL; a = a->next)
2N/A printf("\t%d,\t%#0.4x,\t%s\n",
2N/A *a->item.data, a, a->item.key);
2N/A }
2N/A#endif
2N/A#endif
2N/A}
2N/A#endif