1236769Sobrien/*	$NetBSD: hash.h,v 1.10 2009/01/24 10:59:09 dsl 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
81236769Sobrien#ifndef	_HASH
82236769Sobrien#define	_HASH
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. */
92236769Sobrien    union {
93236769Sobrien	void	      *clientPtr;	/* Arbitrary pointer */
94236769Sobrien	time_t	      clientTime;	/* Arbitrary Time */
95236769Sobrien    } clientInfo;
96236769Sobrien    unsigned	      namehash;		/* hash value of key */
97236769Sobrien    char	      name[1];		/* key string */
98236769Sobrien} Hash_Entry;
99236769Sobrien
100236769Sobrientypedef struct Hash_Table {
101236769Sobrien    struct Hash_Entry **bucketPtr;/* Pointers to Hash_Entry, one
102236769Sobrien    				 * for each bucket in the table. */
103236769Sobrien    int 	size;		/* Actual size of array. */
104236769Sobrien    int 	numEntries;	/* Number of entries in the table. */
105236769Sobrien    int 	mask;		/* Used to select bits for hashing. */
106236769Sobrien} Hash_Table;
107236769Sobrien
108236769Sobrien/*
109236769Sobrien * The following structure is used by the searching routines
110236769Sobrien * to record where we are in the search.
111236769Sobrien */
112236769Sobrien
113236769Sobrientypedef struct Hash_Search {
114236769Sobrien    Hash_Table  *tablePtr;	/* Table being searched. */
115236769Sobrien    int 	nextIndex;	/* Next bucket to check (after current). */
116236769Sobrien    Hash_Entry 	*hashEntryPtr;	/* Next entry to check in current bucket. */
117236769Sobrien} Hash_Search;
118236769Sobrien
119236769Sobrien/*
120236769Sobrien * Macros.
121236769Sobrien */
122236769Sobrien
123236769Sobrien/*
124236769Sobrien * void * Hash_GetValue(h)
125236769Sobrien *     Hash_Entry *h;
126236769Sobrien */
127236769Sobrien
128236769Sobrien#define Hash_GetValue(h) ((h)->clientInfo.clientPtr)
129236769Sobrien#define Hash_GetTimeValue(h) ((h)->clientInfo.clientTime)
130236769Sobrien
131236769Sobrien/*
132236769Sobrien * Hash_SetValue(h, val);
133236769Sobrien *     Hash_Entry *h;
134236769Sobrien *     char *val;
135236769Sobrien */
136236769Sobrien
137236769Sobrien#define Hash_SetValue(h, val) ((h)->clientInfo.clientPtr = (val))
138236769Sobrien#define Hash_SetTimeValue(h, val) ((h)->clientInfo.clientTime = (val))
139236769Sobrien
140236769Sobrien/*
141236769Sobrien * Hash_Size(n) returns the number of words in an object of n bytes
142236769Sobrien */
143236769Sobrien
144236769Sobrien#define	Hash_Size(n)	(((n) + sizeof (int) - 1) / sizeof (int))
145236769Sobrien
146236769Sobrienvoid Hash_InitTable(Hash_Table *, int);
147236769Sobrienvoid Hash_DeleteTable(Hash_Table *);
148236769SobrienHash_Entry *Hash_FindEntry(Hash_Table *, const char *);
149236769SobrienHash_Entry *Hash_CreateEntry(Hash_Table *, const char *, Boolean *);
150236769Sobrienvoid Hash_DeleteEntry(Hash_Table *, Hash_Entry *);
151236769SobrienHash_Entry *Hash_EnumFirst(Hash_Table *, Hash_Search *);
152236769SobrienHash_Entry *Hash_EnumNext(Hash_Search *);
153236769Sobrien
154236769Sobrien#endif /* _HASH */
155