#ifndef HASH_H1#define HASH_H2/* hash.h */3/************************************************************************4Copyright 1988, 1991 by Carnegie Mellon University56All Rights Reserved78Permission to use, copy, modify, and distribute this software and its9documentation for any purpose and without fee is hereby granted, provided10that the above copyright notice appear in all copies and that both that11copyright notice and this permission notice appear in supporting12documentation, and that the name of Carnegie Mellon University not be used13in advertising or publicity pertaining to distribution of the software14without specific, written prior permission.1516CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS17SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.18IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL19DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR20PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS21ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS22SOFTWARE.23************************************************************************/2425/*26* Generalized hash table ADT27*28* Provides multiple, dynamically-allocated, variable-sized hash tables on29* various data and keys.30*31* This package attempts to follow some of the coding conventions suggested32* by Bob Sidebotham and the AFS Clean Code Committee.33*/343536/*37* The user must supply the following:38*39* 1. A comparison function which is declared as:40*41* int compare(data1, data2)42* hash_datum *data1, *data2;43*44* This function must compare the desired fields of data1 and45* data2 and return TRUE (1) if the data should be considered46* equivalent (i.e. have the same key value) or FALSE (0)47* otherwise. This function is called through a pointer passed to48* the various hashtable functions (thus pointers to different49* functions may be passed to effect different tests on different50* hash tables).51*52* Internally, all the functions of this package always call the53* compare function with the "key" parameter as the first parameter,54* and a full data element as the second parameter. Thus, the key55* and element arguments to functions such as hash_Lookup() may56* actually be of different types and the programmer may provide a57* compare function which compares the two different object types58* as desired.59*60* Example:61*62* int compare(key, element)63* char *key;64* struct some_complex_structure *element;65* {66* return !strcmp(key, element->name);67* }68*69* key = "John C. Doe"70* element = &some_complex_structure71* hash_Lookup(table, hashcode, compare, key);72*73* 2. A hash function yielding an unsigned integer value to be used74* as the hashcode (index into the hashtable). Thus, the user75* may hash on whatever data is desired and may use several76* different hash functions for various different hash tables.77* The actual hash table index will be the passed hashcode modulo78* the hash table size.79*80* A generalized hash function, hash_HashFunction(), is included81* with this package to make things a little easier. It is not82* guaranteed to use the best hash algorithm in existence. . . .83*/84858687/*88* Various hash table definitions89*/909192/*93* Define "hash_datum" as a universal data type94*/95typedef void hash_datum;9697typedef struct hash_memberstruct hash_member;98typedef struct hash_tblstruct hash_tbl;99typedef struct hash_tblstruct_hdr hash_tblhdr;100101struct hash_memberstruct {102hash_member *next;103hash_datum *data;104};105106struct hash_tblstruct_hdr {107unsigned size, bucketnum;108hash_member *member;109};110111struct hash_tblstruct {112unsigned size, bucketnum;113hash_member *member; /* Used for linear dump */114hash_member *table[1]; /* Dynamically extended */115};116117/* ANSI function prototypes or empty arg list? */118119typedef int (*hash_cmpfp)(hash_datum *, hash_datum *);120typedef void (*hash_freefp)(hash_datum *);121122extern hash_tbl *hash_Init(u_int tablesize);123124extern void hash_Reset(hash_tbl *tbl, hash_freefp);125126extern unsigned hash_HashFunction(u_char *str, u_int len);127128extern int hash_Exists(hash_tbl *, u_int code,129hash_cmpfp, hash_datum *key);130131extern int hash_Insert(hash_tbl *, u_int code,132hash_cmpfp, hash_datum *key,133hash_datum *element);134135extern int hash_Delete(hash_tbl *, u_int code,136hash_cmpfp, hash_datum *key,137hash_freefp);138139extern hash_datum *hash_Lookup(hash_tbl *, u_int code,140hash_cmpfp, hash_datum *key);141142extern hash_datum *hash_FirstEntry(hash_tbl *);143144extern hash_datum *hash_NextEntry(hash_tbl *);145146#endif /* HASH_H */147148149