xref: /freebsd/libexec/bootpd/hash.h (revision 42b388439bd3795e09258c57a74ce9eec3651c7b)
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
83*1acf0dbaSUlrich Spörlein  *	    guaranteed 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 typedef void hash_datum;
9744099b7bSPaul Traina 
9844099b7bSPaul Traina typedef struct hash_memberstruct  hash_member;
9944099b7bSPaul Traina typedef struct hash_tblstruct     hash_tbl;
10044099b7bSPaul Traina typedef struct hash_tblstruct_hdr hash_tblhdr;
10144099b7bSPaul Traina 
10244099b7bSPaul Traina struct hash_memberstruct {
10344099b7bSPaul Traina     hash_member *next;
10444099b7bSPaul Traina     hash_datum  *data;
10544099b7bSPaul Traina };
10644099b7bSPaul Traina 
10744099b7bSPaul Traina struct hash_tblstruct_hdr {
10844099b7bSPaul Traina     unsigned	size, bucketnum;
10944099b7bSPaul Traina     hash_member *member;
11044099b7bSPaul Traina };
11144099b7bSPaul Traina 
11244099b7bSPaul Traina struct hash_tblstruct {
11344099b7bSPaul Traina     unsigned	size, bucketnum;
11444099b7bSPaul Traina     hash_member *member;		/* Used for linear dump */
11544099b7bSPaul Traina     hash_member	*table[1];		/* Dynamically extended */
11644099b7bSPaul Traina };
11744099b7bSPaul Traina 
11844099b7bSPaul Traina /* ANSI function prototypes or empty arg list? */
11944099b7bSPaul Traina 
120f19d047aSAlfred Perlstein typedef int (*hash_cmpfp)(hash_datum *, hash_datum *);
121f19d047aSAlfred Perlstein typedef void (*hash_freefp)(hash_datum *);
12244099b7bSPaul Traina 
123f19d047aSAlfred Perlstein extern hash_tbl	  *hash_Init(u_int tablesize);
12444099b7bSPaul Traina 
125f19d047aSAlfred Perlstein extern void	   hash_Reset(hash_tbl *tbl, hash_freefp);
12644099b7bSPaul Traina 
127f19d047aSAlfred Perlstein extern unsigned	   hash_HashFunction(u_char *str, u_int len);
12844099b7bSPaul Traina 
129f19d047aSAlfred Perlstein extern int	   hash_Exists(hash_tbl *, u_int code,
130f19d047aSAlfred Perlstein 				  hash_cmpfp, hash_datum *key);
13144099b7bSPaul Traina 
132f19d047aSAlfred Perlstein extern int	   hash_Insert(hash_tbl *, u_int code,
13344099b7bSPaul Traina 				  hash_cmpfp, hash_datum *key,
134f19d047aSAlfred Perlstein 				  hash_datum *element);
13544099b7bSPaul Traina 
136f19d047aSAlfred Perlstein extern int	   hash_Delete(hash_tbl *, u_int code,
13744099b7bSPaul Traina 				  hash_cmpfp, hash_datum *key,
138f19d047aSAlfred Perlstein 				  hash_freefp);
13944099b7bSPaul Traina 
140f19d047aSAlfred Perlstein extern hash_datum *hash_Lookup(hash_tbl *, u_int code,
141f19d047aSAlfred Perlstein 				  hash_cmpfp, hash_datum *key);
14244099b7bSPaul Traina 
143f19d047aSAlfred Perlstein extern hash_datum *hash_FirstEntry(hash_tbl *);
14444099b7bSPaul Traina 
145f19d047aSAlfred Perlstein extern hash_datum *hash_NextEntry(hash_tbl *);
14644099b7bSPaul Traina 
14744099b7bSPaul Traina #endif	/* HASH_H */
148