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 285ed136e8SAlfred Perlstein * $FreeBSD$ 2944099b7bSPaul Traina * 3044099b7bSPaul Traina * Provides multiple, dynamically-allocated, variable-sized hash tables on 3144099b7bSPaul Traina * various data and keys. 3244099b7bSPaul Traina * 3344099b7bSPaul Traina * This package attempts to follow some of the coding conventions suggested 3444099b7bSPaul Traina * by Bob Sidebotham and the AFS Clean Code Committee. 3544099b7bSPaul Traina */ 3644099b7bSPaul Traina 3744099b7bSPaul Traina 3844099b7bSPaul Traina /* 3944099b7bSPaul Traina * The user must supply the following: 4044099b7bSPaul Traina * 4144099b7bSPaul Traina * 1. A comparison function which is declared as: 4244099b7bSPaul Traina * 4344099b7bSPaul Traina * int compare(data1, data2) 4444099b7bSPaul Traina * hash_datum *data1, *data2; 4544099b7bSPaul Traina * 4644099b7bSPaul Traina * This function must compare the desired fields of data1 and 4744099b7bSPaul Traina * data2 and return TRUE (1) if the data should be considered 4844099b7bSPaul Traina * equivalent (i.e. have the same key value) or FALSE (0) 4944099b7bSPaul Traina * otherwise. This function is called through a pointer passed to 5044099b7bSPaul Traina * the various hashtable functions (thus pointers to different 5144099b7bSPaul Traina * functions may be passed to effect different tests on different 5244099b7bSPaul Traina * hash tables). 5344099b7bSPaul Traina * 5444099b7bSPaul Traina * Internally, all the functions of this package always call the 5544099b7bSPaul Traina * compare function with the "key" parameter as the first parameter, 5644099b7bSPaul Traina * and a full data element as the second parameter. Thus, the key 5744099b7bSPaul Traina * and element arguments to functions such as hash_Lookup() may 5844099b7bSPaul Traina * actually be of different types and the programmer may provide a 5944099b7bSPaul Traina * compare function which compares the two different object types 6044099b7bSPaul Traina * as desired. 6144099b7bSPaul Traina * 6244099b7bSPaul Traina * Example: 6344099b7bSPaul Traina * 6444099b7bSPaul Traina * int compare(key, element) 6544099b7bSPaul Traina * char *key; 6644099b7bSPaul Traina * struct some_complex_structure *element; 6744099b7bSPaul Traina * { 6844099b7bSPaul Traina * return !strcmp(key, element->name); 6944099b7bSPaul Traina * } 7044099b7bSPaul Traina * 7144099b7bSPaul Traina * key = "John C. Doe" 7244099b7bSPaul Traina * element = &some_complex_structure 7344099b7bSPaul Traina * hash_Lookup(table, hashcode, compare, key); 7444099b7bSPaul Traina * 7544099b7bSPaul Traina * 2. A hash function yielding an unsigned integer value to be used 7644099b7bSPaul Traina * as the hashcode (index into the hashtable). Thus, the user 7744099b7bSPaul Traina * may hash on whatever data is desired and may use several 7844099b7bSPaul Traina * different hash functions for various different hash tables. 7944099b7bSPaul Traina * The actual hash table index will be the passed hashcode modulo 8044099b7bSPaul Traina * the hash table size. 8144099b7bSPaul Traina * 8244099b7bSPaul Traina * A generalized hash function, hash_HashFunction(), is included 8344099b7bSPaul Traina * with this package to make things a little easier. It is not 8444099b7bSPaul Traina * guarenteed to use the best hash algorithm in existence. . . . 8544099b7bSPaul Traina */ 8644099b7bSPaul Traina 8744099b7bSPaul Traina 8844099b7bSPaul Traina 8944099b7bSPaul Traina /* 9044099b7bSPaul Traina * Various hash table definitions 9144099b7bSPaul Traina */ 9244099b7bSPaul Traina 9344099b7bSPaul Traina 9444099b7bSPaul Traina /* 9544099b7bSPaul Traina * Define "hash_datum" as a universal data type 9644099b7bSPaul Traina */ 9744099b7bSPaul Traina typedef void hash_datum; 9844099b7bSPaul Traina 9944099b7bSPaul Traina typedef struct hash_memberstruct hash_member; 10044099b7bSPaul Traina typedef struct hash_tblstruct hash_tbl; 10144099b7bSPaul Traina typedef struct hash_tblstruct_hdr hash_tblhdr; 10244099b7bSPaul Traina 10344099b7bSPaul Traina struct hash_memberstruct { 10444099b7bSPaul Traina hash_member *next; 10544099b7bSPaul Traina hash_datum *data; 10644099b7bSPaul Traina }; 10744099b7bSPaul Traina 10844099b7bSPaul Traina struct hash_tblstruct_hdr { 10944099b7bSPaul Traina unsigned size, bucketnum; 11044099b7bSPaul Traina hash_member *member; 11144099b7bSPaul Traina }; 11244099b7bSPaul Traina 11344099b7bSPaul Traina struct hash_tblstruct { 11444099b7bSPaul Traina unsigned size, bucketnum; 11544099b7bSPaul Traina hash_member *member; /* Used for linear dump */ 11644099b7bSPaul Traina hash_member *table[1]; /* Dynamically extended */ 11744099b7bSPaul Traina }; 11844099b7bSPaul Traina 11944099b7bSPaul Traina /* ANSI function prototypes or empty arg list? */ 12044099b7bSPaul Traina 121f19d047aSAlfred Perlstein typedef int (*hash_cmpfp)(hash_datum *, hash_datum *); 122f19d047aSAlfred Perlstein typedef void (*hash_freefp)(hash_datum *); 12344099b7bSPaul Traina 124f19d047aSAlfred Perlstein extern hash_tbl *hash_Init(u_int tablesize); 12544099b7bSPaul Traina 126f19d047aSAlfred Perlstein extern void hash_Reset(hash_tbl *tbl, hash_freefp); 12744099b7bSPaul Traina 128f19d047aSAlfred Perlstein extern unsigned hash_HashFunction(u_char *str, u_int len); 12944099b7bSPaul Traina 130f19d047aSAlfred Perlstein extern int hash_Exists(hash_tbl *, u_int code, 131f19d047aSAlfred Perlstein hash_cmpfp, hash_datum *key); 13244099b7bSPaul Traina 133f19d047aSAlfred Perlstein extern int hash_Insert(hash_tbl *, u_int code, 13444099b7bSPaul Traina hash_cmpfp, hash_datum *key, 135f19d047aSAlfred Perlstein hash_datum *element); 13644099b7bSPaul Traina 137f19d047aSAlfred Perlstein extern int hash_Delete(hash_tbl *, u_int code, 13844099b7bSPaul Traina hash_cmpfp, hash_datum *key, 139f19d047aSAlfred Perlstein hash_freefp); 14044099b7bSPaul Traina 141f19d047aSAlfred Perlstein extern hash_datum *hash_Lookup(hash_tbl *, u_int code, 142f19d047aSAlfred Perlstein hash_cmpfp, hash_datum *key); 14344099b7bSPaul Traina 144f19d047aSAlfred Perlstein extern hash_datum *hash_FirstEntry(hash_tbl *); 14544099b7bSPaul Traina 146f19d047aSAlfred Perlstein extern hash_datum *hash_NextEntry(hash_tbl *); 14744099b7bSPaul Traina 14844099b7bSPaul Traina #endif /* HASH_H */ 149