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