1321964Ssjg/*	$NetBSD: hash.h,v 1.12 2017/05/31 21:07:03 maya Exp $	*/
2236769Sobrien
3236769Sobrien/*
4236769Sobrien * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5236769Sobrien *
6236769Sobrien * This code is derived from software contributed to Berkeley by
7236769Sobrien * Adam de Boor.
8236769Sobrien *
9236769Sobrien * Redistribution and use in source and binary forms, with or without
10236769Sobrien * modification, are permitted provided that the following conditions
11236769Sobrien * are met:
12236769Sobrien * 1. Redistributions of source code must retain the above copyright
13236769Sobrien *    notice, this list of conditions and the following disclaimer.
14236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright
15236769Sobrien *    notice, this list of conditions and the following disclaimer in the
16236769Sobrien *    documentation and/or other materials provided with the distribution.
17236769Sobrien * 3. Neither the name of the University nor the names of its contributors
18236769Sobrien *    may be used to endorse or promote products derived from this software
19236769Sobrien *    without specific prior written permission.
20236769Sobrien *
21236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24236769Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31236769Sobrien * SUCH DAMAGE.
32236769Sobrien *
33236769Sobrien *	from: @(#)hash.h	8.1 (Berkeley) 6/6/93
34236769Sobrien */
35236769Sobrien
36236769Sobrien/*
37236769Sobrien * Copyright (c) 1988, 1989 by Adam de Boor
38236769Sobrien * Copyright (c) 1989 by Berkeley Softworks
39236769Sobrien * All rights reserved.
40236769Sobrien *
41236769Sobrien * This code is derived from software contributed to Berkeley by
42236769Sobrien * Adam de Boor.
43236769Sobrien *
44236769Sobrien * Redistribution and use in source and binary forms, with or without
45236769Sobrien * modification, are permitted provided that the following conditions
46236769Sobrien * are met:
47236769Sobrien * 1. Redistributions of source code must retain the above copyright
48236769Sobrien *    notice, this list of conditions and the following disclaimer.
49236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright
50236769Sobrien *    notice, this list of conditions and the following disclaimer in the
51236769Sobrien *    documentation and/or other materials provided with the distribution.
52236769Sobrien * 3. All advertising materials mentioning features or use of this software
53236769Sobrien *    must display the following acknowledgement:
54236769Sobrien *	This product includes software developed by the University of
55236769Sobrien *	California, Berkeley and its contributors.
56236769Sobrien * 4. Neither the name of the University nor the names of its contributors
57236769Sobrien *    may be used to endorse or promote products derived from this software
58236769Sobrien *    without specific prior written permission.
59236769Sobrien *
60236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63236769Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70236769Sobrien * SUCH DAMAGE.
71236769Sobrien *
72236769Sobrien *	from: @(#)hash.h	8.1 (Berkeley) 6/6/93
73236769Sobrien */
74236769Sobrien
75236769Sobrien/* hash.h --
76236769Sobrien *
77236769Sobrien * 	This file contains definitions used by the hash module,
78236769Sobrien * 	which maintains hash tables.
79236769Sobrien */
80236769Sobrien
81321964Ssjg#ifndef	_HASH_H
82321964Ssjg#define	_HASH_H
83236769Sobrien
84236769Sobrien/*
85236769Sobrien * The following defines one entry in the hash table.
86236769Sobrien */
87236769Sobrien
88236769Sobrientypedef struct Hash_Entry {
89236769Sobrien    struct Hash_Entry *next;		/* Used to link together all the
90236769Sobrien    					 * entries associated with the same
91236769Sobrien					 * bucket. */
92321964Ssjg    void	      *clientPtr;	/* Arbitrary pointer */
93236769Sobrien    unsigned	      namehash;		/* hash value of key */
94236769Sobrien    char	      name[1];		/* key string */
95236769Sobrien} Hash_Entry;
96236769Sobrien
97236769Sobrientypedef struct Hash_Table {
98236769Sobrien    struct Hash_Entry **bucketPtr;/* Pointers to Hash_Entry, one
99236769Sobrien    				 * for each bucket in the table. */
100236769Sobrien    int 	size;		/* Actual size of array. */
101236769Sobrien    int 	numEntries;	/* Number of entries in the table. */
102236769Sobrien    int 	mask;		/* Used to select bits for hashing. */
103236769Sobrien} Hash_Table;
104236769Sobrien
105236769Sobrien/*
106236769Sobrien * The following structure is used by the searching routines
107236769Sobrien * to record where we are in the search.
108236769Sobrien */
109236769Sobrien
110236769Sobrientypedef struct Hash_Search {
111236769Sobrien    Hash_Table  *tablePtr;	/* Table being searched. */
112236769Sobrien    int 	nextIndex;	/* Next bucket to check (after current). */
113236769Sobrien    Hash_Entry 	*hashEntryPtr;	/* Next entry to check in current bucket. */
114236769Sobrien} Hash_Search;
115236769Sobrien
116236769Sobrien/*
117236769Sobrien * Macros.
118236769Sobrien */
119236769Sobrien
120236769Sobrien/*
121236769Sobrien * void * Hash_GetValue(h)
122236769Sobrien *     Hash_Entry *h;
123236769Sobrien */
124236769Sobrien
125321964Ssjg#define Hash_GetValue(h) ((h)->clientPtr)
126236769Sobrien
127236769Sobrien/*
128236769Sobrien * Hash_SetValue(h, val);
129236769Sobrien *     Hash_Entry *h;
130236769Sobrien *     char *val;
131236769Sobrien */
132236769Sobrien
133321964Ssjg#define Hash_SetValue(h, val) ((h)->clientPtr = (val))
134236769Sobrien
135236769Sobrien/*
136236769Sobrien * Hash_Size(n) returns the number of words in an object of n bytes
137236769Sobrien */
138236769Sobrien
139236769Sobrien#define	Hash_Size(n)	(((n) + sizeof (int) - 1) / sizeof (int))
140236769Sobrien
141236769Sobrienvoid Hash_InitTable(Hash_Table *, int);
142236769Sobrienvoid Hash_DeleteTable(Hash_Table *);
143236769SobrienHash_Entry *Hash_FindEntry(Hash_Table *, const char *);
144236769SobrienHash_Entry *Hash_CreateEntry(Hash_Table *, const char *, Boolean *);
145236769Sobrienvoid Hash_DeleteEntry(Hash_Table *, Hash_Entry *);
146236769SobrienHash_Entry *Hash_EnumFirst(Hash_Table *, Hash_Search *);
147236769SobrienHash_Entry *Hash_EnumNext(Hash_Search *);
148236769Sobrien
149321964Ssjg#endif /* _HASH_H */
150