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