xref: /freebsd/sbin/rcorder/hash.h (revision 51e16cb8fc536913f490ac6bc9c17e92ebd0411b)
19d62501fSDavid E. O'Brien /*	$NetBSD: hash.h,v 1.1.1.1 1999/11/19 04:30:56 mrg Exp $	*/
29d62501fSDavid E. O'Brien 
3*df57947fSPedro F. Giffuni /*-
4*df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
5*df57947fSPedro F. Giffuni  *
69d62501fSDavid E. O'Brien  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
79d62501fSDavid E. O'Brien  * Copyright (c) 1988, 1989 by Adam de Boor
89d62501fSDavid E. O'Brien  * Copyright (c) 1989 by Berkeley Softworks
99d62501fSDavid E. O'Brien  * All rights reserved.
109d62501fSDavid E. O'Brien  *
119d62501fSDavid E. O'Brien  * This code is derived from software contributed to Berkeley by
129d62501fSDavid E. O'Brien  * Adam de Boor.
139d62501fSDavid E. O'Brien  *
149d62501fSDavid E. O'Brien  * Redistribution and use in source and binary forms, with or without
159d62501fSDavid E. O'Brien  * modification, are permitted provided that the following conditions
169d62501fSDavid E. O'Brien  * are met:
179d62501fSDavid E. O'Brien  * 1. Redistributions of source code must retain the above copyright
189d62501fSDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer.
199d62501fSDavid E. O'Brien  * 2. Redistributions in binary form must reproduce the above copyright
209d62501fSDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer in the
219d62501fSDavid E. O'Brien  *    documentation and/or other materials provided with the distribution.
229d62501fSDavid E. O'Brien  * 3. All advertising materials mentioning features or use of this software
239d62501fSDavid E. O'Brien  *    must display the following acknowledgement:
249d62501fSDavid E. O'Brien  *	This product includes software developed by the University of
259d62501fSDavid E. O'Brien  *	California, Berkeley and its contributors.
269d62501fSDavid E. O'Brien  * 4. Neither the name of the University nor the names of its contributors
279d62501fSDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
289d62501fSDavid E. O'Brien  *    without specific prior written permission.
299d62501fSDavid E. O'Brien  *
309d62501fSDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
319d62501fSDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
329d62501fSDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
339d62501fSDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
349d62501fSDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
359d62501fSDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
369d62501fSDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
379d62501fSDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
389d62501fSDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
399d62501fSDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
409d62501fSDavid E. O'Brien  * SUCH DAMAGE.
419d62501fSDavid E. O'Brien  */
429d62501fSDavid E. O'Brien 
439d62501fSDavid E. O'Brien /* hash.h --
449d62501fSDavid E. O'Brien  *
459d62501fSDavid E. O'Brien  * 	This file contains definitions used by the hash module,
469d62501fSDavid E. O'Brien  * 	which maintains hash tables.
479d62501fSDavid E. O'Brien  */
489d62501fSDavid E. O'Brien 
499d62501fSDavid E. O'Brien #ifndef	_HASH
509d62501fSDavid E. O'Brien #define	_HASH
519d62501fSDavid E. O'Brien 
529d62501fSDavid E. O'Brien /*
539d62501fSDavid E. O'Brien  * The following defines one entry in the hash table.
549d62501fSDavid E. O'Brien  */
559d62501fSDavid E. O'Brien 
569d62501fSDavid E. O'Brien typedef struct Hash_Entry {
579d62501fSDavid E. O'Brien 	struct	Hash_Entry *next;	/* Used to link together all the
589d62501fSDavid E. O'Brien 					 * entries associated with the same
599d62501fSDavid E. O'Brien 					 * bucket. */
609d62501fSDavid E. O'Brien 	ClientData	clientData;	/* Arbitrary piece of data associated
619d62501fSDavid E. O'Brien 					 * with key. */
629d62501fSDavid E. O'Brien 	unsigned	namehash;	/* hash value of key */
639d62501fSDavid E. O'Brien 	char		name[1];	/* key string */
649d62501fSDavid E. O'Brien } Hash_Entry;
659d62501fSDavid E. O'Brien 
669d62501fSDavid E. O'Brien typedef struct Hash_Table {
679d62501fSDavid E. O'Brien 	struct	Hash_Entry **bucketPtr;
689d62501fSDavid E. O'Brien 				/* Pointers to Hash_Entry, one
699d62501fSDavid E. O'Brien 				 * for each bucket in the table. */
709d62501fSDavid E. O'Brien 	int 	size;		/* Actual size of array. */
719d62501fSDavid E. O'Brien 	int 	numEntries;	/* Number of entries in the table. */
729d62501fSDavid E. O'Brien 	int 	mask;		/* Used to select bits for hashing. */
739d62501fSDavid E. O'Brien } Hash_Table;
749d62501fSDavid E. O'Brien 
759d62501fSDavid E. O'Brien /*
769d62501fSDavid E. O'Brien  * The following structure is used by the searching routines
779d62501fSDavid E. O'Brien  * to record where we are in the search.
789d62501fSDavid E. O'Brien  */
799d62501fSDavid E. O'Brien 
809d62501fSDavid E. O'Brien typedef struct Hash_Search {
819d62501fSDavid E. O'Brien 	Hash_Table	*tablePtr;	/* Table being searched. */
829d62501fSDavid E. O'Brien 	int	 	nextIndex;	/* Next bucket to check (after
839d62501fSDavid E. O'Brien 					 * current). */
849d62501fSDavid E. O'Brien 	Hash_Entry 	*hashEntryPtr;	/* Next entry to check in current
859d62501fSDavid E. O'Brien 					 * bucket. */
869d62501fSDavid E. O'Brien } Hash_Search;
879d62501fSDavid E. O'Brien 
889d62501fSDavid E. O'Brien /*
899d62501fSDavid E. O'Brien  * Macros.
909d62501fSDavid E. O'Brien  */
919d62501fSDavid E. O'Brien 
929d62501fSDavid E. O'Brien /*
939d62501fSDavid E. O'Brien  * ClientData Hash_GetValue(h)
949d62501fSDavid E. O'Brien  *     Hash_Entry *h;
959d62501fSDavid E. O'Brien  */
969d62501fSDavid E. O'Brien 
979d62501fSDavid E. O'Brien #define Hash_GetValue(h) ((h)->clientData)
989d62501fSDavid E. O'Brien 
999d62501fSDavid E. O'Brien /*
1009d62501fSDavid E. O'Brien  * Hash_SetValue(h, val);
1019d62501fSDavid E. O'Brien  *     Hash_Entry *h;
1029d62501fSDavid E. O'Brien  *     char *val;
1039d62501fSDavid E. O'Brien  */
1049d62501fSDavid E. O'Brien 
1059d62501fSDavid E. O'Brien #define Hash_SetValue(h, val) ((h)->clientData = (ClientData) (val))
1069d62501fSDavid E. O'Brien 
1079d62501fSDavid E. O'Brien #ifdef ORDER
1089d62501fSDavid E. O'Brien /*
1099d62501fSDavid E. O'Brien  * Hash_GetKey(h);
1109d62501fSDavid E. O'Brien  *     Hash_Entry *h;
1119d62501fSDavid E. O'Brien  */
1129d62501fSDavid E. O'Brien 
1139d62501fSDavid E. O'Brien #define Hash_GetKey(h) ((h)->name)
1149d62501fSDavid E. O'Brien #endif /* ORDER */
1159d62501fSDavid E. O'Brien 
1169d62501fSDavid E. O'Brien /*
1179d62501fSDavid E. O'Brien  * Hash_Size(n) returns the number of words in an object of n bytes
1189d62501fSDavid E. O'Brien  */
1199d62501fSDavid E. O'Brien 
1209d62501fSDavid E. O'Brien #define	Hash_Size(n)	(((n) + sizeof (int) - 1) / sizeof (int))
1219d62501fSDavid E. O'Brien 
122784bddbcSKevin Lo void Hash_InitTable(Hash_Table *, int);
123784bddbcSKevin Lo void Hash_DeleteTable(Hash_Table *);
124784bddbcSKevin Lo Hash_Entry *Hash_FindEntry(Hash_Table *, char *);
125784bddbcSKevin Lo Hash_Entry *Hash_CreateEntry(Hash_Table *, char *, Boolean *);
126784bddbcSKevin Lo void Hash_DeleteEntry(Hash_Table *, Hash_Entry *);
127784bddbcSKevin Lo Hash_Entry *Hash_EnumFirst(Hash_Table *, Hash_Search *);
128784bddbcSKevin Lo Hash_Entry *Hash_EnumNext(Hash_Search *);
1299d62501fSDavid E. O'Brien 
1309d62501fSDavid E. O'Brien #endif /* _HASH */
131