144099b7bSPaul Traina #ifndef HASH_H 244099b7bSPaul Traina #define HASH_H 344099b7bSPaul Traina /* hash.h */ 444099b7bSPaul Traina /************************************************************************ 544099b7bSPaul Traina Copyright 1988, 1991 by Carnegie Mellon University 644099b7bSPaul Traina 744099b7bSPaul Traina All Rights Reserved 844099b7bSPaul Traina 944099b7bSPaul Traina Permission to use, copy, modify, and distribute this software and its 1044099b7bSPaul Traina documentation for any purpose and without fee is hereby granted, provided 1144099b7bSPaul Traina that the above copyright notice appear in all copies and that both that 1244099b7bSPaul Traina copyright notice and this permission notice appear in supporting 1344099b7bSPaul Traina documentation, and that the name of Carnegie Mellon University not be used 1444099b7bSPaul Traina in advertising or publicity pertaining to distribution of the software 1544099b7bSPaul Traina without specific, written prior permission. 1644099b7bSPaul Traina 1744099b7bSPaul Traina CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS 1844099b7bSPaul Traina SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 1944099b7bSPaul Traina IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 2044099b7bSPaul Traina DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 2144099b7bSPaul Traina PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 2244099b7bSPaul Traina ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 2344099b7bSPaul Traina SOFTWARE. 2444099b7bSPaul Traina ************************************************************************/ 2544099b7bSPaul Traina 2644099b7bSPaul Traina /* 2744099b7bSPaul Traina * Generalized hash table ADT 2844099b7bSPaul Traina * 2944099b7bSPaul Traina * Provides multiple, dynamically-allocated, variable-sized hash tables on 3044099b7bSPaul Traina * various data and keys. 3144099b7bSPaul Traina * 3244099b7bSPaul Traina * This package attempts to follow some of the coding conventions suggested 3344099b7bSPaul Traina * by Bob Sidebotham and the AFS Clean Code Committee. 3444099b7bSPaul Traina */ 3544099b7bSPaul Traina 3644099b7bSPaul Traina 3744099b7bSPaul Traina /* 3844099b7bSPaul Traina * The user must supply the following: 3944099b7bSPaul Traina * 4044099b7bSPaul Traina * 1. A comparison function which is declared as: 4144099b7bSPaul Traina * 4244099b7bSPaul Traina * int compare(data1, data2) 4344099b7bSPaul Traina * hash_datum *data1, *data2; 4444099b7bSPaul Traina * 4544099b7bSPaul Traina * This function must compare the desired fields of data1 and 4644099b7bSPaul Traina * data2 and return TRUE (1) if the data should be considered 4744099b7bSPaul Traina * equivalent (i.e. have the same key value) or FALSE (0) 4844099b7bSPaul Traina * otherwise. This function is called through a pointer passed to 4944099b7bSPaul Traina * the various hashtable functions (thus pointers to different 5044099b7bSPaul Traina * functions may be passed to effect different tests on different 5144099b7bSPaul Traina * hash tables). 5244099b7bSPaul Traina * 5344099b7bSPaul Traina * Internally, all the functions of this package always call the 5444099b7bSPaul Traina * compare function with the "key" parameter as the first parameter, 5544099b7bSPaul Traina * and a full data element as the second parameter. Thus, the key 5644099b7bSPaul Traina * and element arguments to functions such as hash_Lookup() may 5744099b7bSPaul Traina * actually be of different types and the programmer may provide a 5844099b7bSPaul Traina * compare function which compares the two different object types 5944099b7bSPaul Traina * as desired. 6044099b7bSPaul Traina * 6144099b7bSPaul Traina * Example: 6244099b7bSPaul Traina * 6344099b7bSPaul Traina * int compare(key, element) 6444099b7bSPaul Traina * char *key; 6544099b7bSPaul Traina * struct some_complex_structure *element; 6644099b7bSPaul Traina * { 6744099b7bSPaul Traina * return !strcmp(key, element->name); 6844099b7bSPaul Traina * } 6944099b7bSPaul Traina * 7044099b7bSPaul Traina * key = "John C. Doe" 7144099b7bSPaul Traina * element = &some_complex_structure 7244099b7bSPaul Traina * hash_Lookup(table, hashcode, compare, key); 7344099b7bSPaul Traina * 7444099b7bSPaul Traina * 2. A hash function yielding an unsigned integer value to be used 7544099b7bSPaul Traina * as the hashcode (index into the hashtable). Thus, the user 7644099b7bSPaul Traina * may hash on whatever data is desired and may use several 7744099b7bSPaul Traina * different hash functions for various different hash tables. 7844099b7bSPaul Traina * The actual hash table index will be the passed hashcode modulo 7944099b7bSPaul Traina * the hash table size. 8044099b7bSPaul Traina * 8144099b7bSPaul Traina * A generalized hash function, hash_HashFunction(), is included 8244099b7bSPaul Traina * with this package to make things a little easier. It is not 8344099b7bSPaul Traina * guarenteed to use the best hash algorithm in existence. . . . 8444099b7bSPaul Traina */ 8544099b7bSPaul Traina 8644099b7bSPaul Traina 8744099b7bSPaul Traina 8844099b7bSPaul Traina /* 8944099b7bSPaul Traina * Various hash table definitions 9044099b7bSPaul Traina */ 9144099b7bSPaul Traina 9244099b7bSPaul Traina 9344099b7bSPaul Traina /* 9444099b7bSPaul Traina * Define "hash_datum" as a universal data type 9544099b7bSPaul Traina */ 9644099b7bSPaul Traina #ifdef __STDC__ 9744099b7bSPaul Traina typedef void hash_datum; 9844099b7bSPaul Traina #else 9944099b7bSPaul Traina typedef char hash_datum; 10044099b7bSPaul Traina #endif 10144099b7bSPaul Traina 10244099b7bSPaul Traina typedef struct hash_memberstruct hash_member; 10344099b7bSPaul Traina typedef struct hash_tblstruct hash_tbl; 10444099b7bSPaul Traina typedef struct hash_tblstruct_hdr hash_tblhdr; 10544099b7bSPaul Traina 10644099b7bSPaul Traina struct hash_memberstruct { 10744099b7bSPaul Traina hash_member *next; 10844099b7bSPaul Traina hash_datum *data; 10944099b7bSPaul Traina }; 11044099b7bSPaul Traina 11144099b7bSPaul Traina struct hash_tblstruct_hdr { 11244099b7bSPaul Traina unsigned size, bucketnum; 11344099b7bSPaul Traina hash_member *member; 11444099b7bSPaul Traina }; 11544099b7bSPaul Traina 11644099b7bSPaul Traina struct hash_tblstruct { 11744099b7bSPaul Traina unsigned size, bucketnum; 11844099b7bSPaul Traina hash_member *member; /* Used for linear dump */ 11944099b7bSPaul Traina hash_member *table[1]; /* Dynamically extended */ 12044099b7bSPaul Traina }; 12144099b7bSPaul Traina 12244099b7bSPaul Traina /* ANSI function prototypes or empty arg list? */ 12344099b7bSPaul Traina #ifdef __STDC__ 12444099b7bSPaul Traina #define P(args) args 12544099b7bSPaul Traina #else 12644099b7bSPaul Traina #define P(args) () 12744099b7bSPaul Traina #endif 12844099b7bSPaul Traina 12944099b7bSPaul Traina typedef int (*hash_cmpfp) P((hash_datum *, hash_datum *)); 13044099b7bSPaul Traina typedef void (*hash_freefp) P((hash_datum *)); 13144099b7bSPaul Traina 13244099b7bSPaul Traina extern hash_tbl *hash_Init P((u_int tablesize)); 13344099b7bSPaul Traina 13444099b7bSPaul Traina extern void hash_Reset P((hash_tbl *tbl, hash_freefp)); 13544099b7bSPaul Traina 13644099b7bSPaul Traina extern unsigned hash_HashFunction P((u_char *str, u_int len)); 13744099b7bSPaul Traina 13844099b7bSPaul Traina extern int hash_Exists P((hash_tbl *, u_int code, 13944099b7bSPaul Traina hash_cmpfp, hash_datum *key)); 14044099b7bSPaul Traina 14144099b7bSPaul Traina extern int hash_Insert P((hash_tbl *, u_int code, 14244099b7bSPaul Traina hash_cmpfp, hash_datum *key, 14344099b7bSPaul Traina hash_datum *element)); 14444099b7bSPaul Traina 14544099b7bSPaul Traina extern int hash_Delete P((hash_tbl *, u_int code, 14644099b7bSPaul Traina hash_cmpfp, hash_datum *key, 14744099b7bSPaul Traina hash_freefp)); 14844099b7bSPaul Traina 14944099b7bSPaul Traina extern hash_datum *hash_Lookup P((hash_tbl *, u_int code, 15044099b7bSPaul Traina hash_cmpfp, hash_datum *key)); 15144099b7bSPaul Traina 15244099b7bSPaul Traina extern hash_datum *hash_FirstEntry P((hash_tbl *)); 15344099b7bSPaul Traina 15444099b7bSPaul Traina extern hash_datum *hash_NextEntry P((hash_tbl *)); 15544099b7bSPaul Traina 15644099b7bSPaul Traina #undef P 15744099b7bSPaul Traina 15844099b7bSPaul Traina #endif /* HASH_H */ 159