Home | History | Annotate | Line # | Download | only in common
hash.h revision 1.1
      1  1.1  gwr #ifndef	HASH_H
      2  1.1  gwr #define HASH_H
      3  1.1  gwr /* hash.h */
      4  1.1  gwr /************************************************************************
      5  1.1  gwr           Copyright 1988, 1991 by Carnegie Mellon University
      6  1.1  gwr 
      7  1.1  gwr                           All Rights Reserved
      8  1.1  gwr 
      9  1.1  gwr Permission to use, copy, modify, and distribute this software and its
     10  1.1  gwr documentation for any purpose and without fee is hereby granted, provided
     11  1.1  gwr that the above copyright notice appear in all copies and that both that
     12  1.1  gwr copyright notice and this permission notice appear in supporting
     13  1.1  gwr documentation, and that the name of Carnegie Mellon University not be used
     14  1.1  gwr in advertising or publicity pertaining to distribution of the software
     15  1.1  gwr without specific, written prior permission.
     16  1.1  gwr 
     17  1.1  gwr CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
     18  1.1  gwr SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
     19  1.1  gwr IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
     20  1.1  gwr DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
     21  1.1  gwr PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
     22  1.1  gwr ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
     23  1.1  gwr SOFTWARE.
     24  1.1  gwr ************************************************************************/
     25  1.1  gwr 
     26  1.1  gwr /*
     27  1.1  gwr  * Generalized hash table ADT
     28  1.1  gwr  *
     29  1.1  gwr  * Provides multiple, dynamically-allocated, variable-sized hash tables on
     30  1.1  gwr  * various data and keys.
     31  1.1  gwr  *
     32  1.1  gwr  * This package attempts to follow some of the coding conventions suggested
     33  1.1  gwr  * by Bob Sidebotham and the AFS Clean Code Committee.
     34  1.1  gwr  */
     35  1.1  gwr 
     36  1.1  gwr 
     37  1.1  gwr /*
     38  1.1  gwr  * The user must supply the following:
     39  1.1  gwr  *
     40  1.1  gwr  *	1.  A comparison function which is declared as:
     41  1.1  gwr  *
     42  1.1  gwr  *		int compare(data1, data2)
     43  1.1  gwr  *		hash_datum *data1, *data2;
     44  1.1  gwr  *
     45  1.1  gwr  *	    This function must compare the desired fields of data1 and
     46  1.1  gwr  *	    data2 and return TRUE (1) if the data should be considered
     47  1.1  gwr  *	    equivalent (i.e. have the same key value) or FALSE (0)
     48  1.1  gwr  *	    otherwise.  This function is called through a pointer passed to
     49  1.1  gwr  *	    the various hashtable functions (thus pointers to different
     50  1.1  gwr  *	    functions may be passed to effect different tests on different
     51  1.1  gwr  *	    hash tables).
     52  1.1  gwr  *
     53  1.1  gwr  *	    Internally, all the functions of this package always call the
     54  1.1  gwr  *	    compare function with the "key" parameter as the first parameter,
     55  1.1  gwr  *	    and a full data element as the second parameter.  Thus, the key
     56  1.1  gwr  *	    and element arguments to functions such as hash_Lookup() may
     57  1.1  gwr  *	    actually be of different types and the programmer may provide a
     58  1.1  gwr  *	    compare function which compares the two different object types
     59  1.1  gwr  *	    as desired.
     60  1.1  gwr  *
     61  1.1  gwr  *	    Example:
     62  1.1  gwr  *
     63  1.1  gwr  *		int compare(key, element)
     64  1.1  gwr  *		char *key;
     65  1.1  gwr  *		struct some_complex_structure *element;
     66  1.1  gwr  *		{
     67  1.1  gwr  *		    return !strcmp(key, element->name);
     68  1.1  gwr  *		}
     69  1.1  gwr  *
     70  1.1  gwr  *		key = "John C. Doe"
     71  1.1  gwr  *		element = &some_complex_structure
     72  1.1  gwr  *		hash_Lookup(table, hashcode, compare, key);
     73  1.1  gwr  *
     74  1.1  gwr  *	2.  A hash function yielding an unsigned integer value to be used
     75  1.1  gwr  *	    as the hashcode (index into the hashtable).  Thus, the user
     76  1.1  gwr  *	    may hash on whatever data is desired and may use several
     77  1.1  gwr  *	    different hash functions for various different hash tables.
     78  1.1  gwr  *	    The actual hash table index will be the passed hashcode modulo
     79  1.1  gwr  *	    the hash table size.
     80  1.1  gwr  *
     81  1.1  gwr  *	    A generalized hash function, hash_HashFunction(), is included
     82  1.1  gwr  *	    with this package to make things a little easier.  It is not
     83  1.1  gwr  *	    guarenteed to use the best hash algorithm in existence. . . .
     84  1.1  gwr  */
     85  1.1  gwr 
     86  1.1  gwr 
     87  1.1  gwr 
     89  1.1  gwr /*
     90  1.1  gwr  * Various hash table definitions
     91  1.1  gwr  */
     92  1.1  gwr 
     93  1.1  gwr 
     94  1.1  gwr /*
     95  1.1  gwr  * Define "hash_datum" as a universal data type
     96  1.1  gwr  */
     97  1.1  gwr #ifdef __STDC__
     98  1.1  gwr typedef void hash_datum;
     99  1.1  gwr #else
    100  1.1  gwr typedef char hash_datum;
    101  1.1  gwr #endif
    102  1.1  gwr 
    103  1.1  gwr typedef struct hash_memberstruct  hash_member;
    104  1.1  gwr typedef struct hash_tblstruct     hash_tbl;
    105  1.1  gwr typedef struct hash_tblstruct_hdr hash_tblhdr;
    106  1.1  gwr 
    107  1.1  gwr struct hash_memberstruct {
    108  1.1  gwr     hash_member *next;
    109  1.1  gwr     hash_datum  *data;
    110  1.1  gwr };
    111  1.1  gwr 
    112  1.1  gwr struct hash_tblstruct_hdr {
    113  1.1  gwr     unsigned	size, bucketnum;
    114  1.1  gwr     hash_member *member;
    115  1.1  gwr };
    116  1.1  gwr 
    117  1.1  gwr struct hash_tblstruct {
    118  1.1  gwr     unsigned	size, bucketnum;
    119  1.1  gwr     hash_member *member;		/* Used for linear dump */
    120  1.1  gwr     hash_member	*table[1];		/* Dynamically extended */
    121  1.1  gwr };
    122  1.1  gwr 
    123  1.1  gwr /* ANSI function prototypes or empty arg list? */
    124  1.1  gwr #ifdef	__STDC__
    125  1.1  gwr #define P(args) args
    126  1.1  gwr #else
    127  1.1  gwr #define P(args) ()
    128  1.1  gwr #endif
    129  1.1  gwr 
    130  1.1  gwr typedef int (*hash_cmpfp) P((hash_datum *, hash_datum *));
    131  1.1  gwr typedef void (*hash_freefp) P((hash_datum *));
    132  1.1  gwr 
    133  1.1  gwr extern hash_tbl	  *hash_Init P((u_int tablesize));
    134  1.1  gwr 
    135  1.1  gwr extern void	   hash_Reset P((hash_tbl *tbl, hash_freefp));
    136  1.1  gwr 
    137  1.1  gwr extern unsigned	   hash_HashFunction P((u_char *str, u_int len));
    138  1.1  gwr 
    139  1.1  gwr extern int	   hash_Exists P((hash_tbl *, u_int code,
    140  1.1  gwr 				  hash_cmpfp, hash_datum *key));
    141  1.1  gwr 
    142  1.1  gwr extern int	   hash_Insert P((hash_tbl *, u_int code,
    143  1.1  gwr 				  hash_cmpfp, hash_datum *key,
    144  1.1  gwr 				  hash_datum *element));
    145  1.1  gwr 
    146  1.1  gwr extern int	   hash_Delete P((hash_tbl *, u_int code,
    147  1.1  gwr 				  hash_cmpfp, hash_datum *key,
    148  1.1  gwr 				  hash_freefp));
    149  1.1  gwr 
    150  1.1  gwr extern hash_datum *hash_Lookup P((hash_tbl *, u_int code,
    151  1.1  gwr 				  hash_cmpfp, hash_datum *key));
    152  1.1  gwr 
    153  1.1  gwr extern hash_datum *hash_FirstEntry P((hash_tbl *));
    154  1.1  gwr 
    155  1.1  gwr extern hash_datum *hash_NextEntry P((hash_tbl *));
    156  1.1  gwr 
    157  1.1  gwr #undef P
    158  1.1  gwr 
    159           #endif	/* HASH_H */
    160