sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
hashtab.h
Go to the documentation of this file.
1/*
2--------------------------------------------------------------------
3By Bob Jenkins, 1996. hash.h. Public Domain.
4
5This implements a hash table.
6* Keys are unique. Adding an item fails if the key is already there.
7* Keys and items are pointed at, not copied. If you change the value
8 of the key after it is inserted then hfind will not be able to find it.
9* The hash table maintains a position that can be set and queried.
10* The table length doubles dynamically and never shrinks. The insert
11 that causes table doubling may take a long time.
12* The table length splits when the table length equals the number of items
13 Comparisons usually take 7 instructions.
14 Computing a hash value takes 35+6n instructions for an n-byte key.
15
16 hcreate - create a hash table
17 hdestroy - destroy a hash table
18 hcount - The number of items in the hash table
19 hkey - key at the current position
20 hkeyl - key length at the current position
21 hstuff - stuff at the current position
22 hfind - find an item in the table
23 hadd - insert an item into the table
24 hdel - delete an item from the table
25 hstat - print statistics about the table
26 hfirst - position at the first item in the table
27 hnext - move the position to the next item in the table
28--------------------------------------------------------------------
29*/
30
35#ifndef HASHTAB_H
36#define HASHTAB_H
37
38#include "standard.h"
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/* PRIVATE TYPES AND DEFINITIONS */
45
46struct hitem
47{
48 const char *key; /* key that is hashed */
49 size_t keyl; /* length of key */
50 void *stuff; /* stuff stored in this hitem */
51 size_t hval; /* hash value */
52 struct hitem *next; /* next hitem in list */
53};
54typedef struct hitem hitem;
55
56
57struct htab
58{
59 struct hitem **table; /* hash table, array of size 2^logsize */
60 int logsize; /* log of size of table */
61 size_t mask; /* (hashval & mask) is position in table */
62 size_t count; /* how many items in this hash table so far? */
63 size_t apos; /* position in the array */
64 struct hitem *ipos; /* current item in the array */
65 struct reroot *space; /* space for the hitems */
66 size_t bcount; /* # hitems useable in current block */
67};
68typedef struct htab htab;
69
70
71
72
73
74/* PUBLIC FUNCTIONS */
75
76/* hcreate - create a hash table
77 ARGUMENTS:
78 logsize - 1<<logsize will be the initial table length
79 RETURNS:
80 the new table
81 */
82extern htab *hcreate(int logsize);
83
84
85/* hdestroy - destroy a hash table
86 ARGUMENTS:
87 t - the hash table to be destroyed. Note that the items and keys
88 will not be freed, the user created them and must destroy
89 them himself.
90 RETURNS:
91 nothing
92 */
93extern void hdestroy(htab *t);
94
95
96/* hcount, hkey, hkeyl, hstuff
97 ARGUMENTS:
98 t - the hash table
99 RETURNS:
100 hcount - (size_t) The number of items in the hash table
101 hkey - (const char *) key for the current item
102 hkeyl - (size_t) key length for the current item
103 hstuff - (void *) stuff for the current item
104 NOTE:
105 The current position always has an item as long as there
106 are items in the table, so hexist can be used to test if the
107 table is empty.
108 hkey, hkeyl, and hstuff will crash if hcount returns 0
109 */
110#define hcount(t) ((t)->count)
111#define hkey(t) ((t)->ipos->key)
112#define hkeyl(t) ((t)->ipos->keyl)
113#define hstuff(t) ((t)->ipos->stuff)
114
115
116
117/* hfind - move the current position to a given key
118 ARGUMENTS:
119 t - the hash table
120 key - the key to look for
121 keyl - length of the key
122 RETURNS:
123 TRUE if the item exists, FALSE if it does not.
124 If the item exists, moves the current position to that item.
125 */
126extern int hfind(htab *t, const char *key, size_t keyl);
127
128
129/* hadd - add a new item to the hash table
130 change the position to point at the item with the key
131 ARGUMENTS:
132 t - the hash table
133 key - the key to look for
134 keyl - length of the key
135 stuff - other stuff to be stored in this item
136 RETURNS:
137 FALSE if the operation fails (because that key is already there).
138 */
139extern int hadd(htab *t, const char *key, size_t keyl, void *stuff);
140
141
142/* hdel - delete the item at the current position
143 change the position to the following item
144 ARGUMENTS:
145 t - the hash table
146 RETURNS:
147 FALSE if there is no current item (meaning the table is empty)
148 NOTE:
149 This frees the item, but not the key or stuff stored in the item.
150 If you want these then deal with them first. For example:
151 if (hfind(tab, key, keyl))
152 {
153 free(hkey(tab));
154 free(hstuff(tab));
155 hdel(tab);
156 }
157 */
158extern int hdel(htab *t);
159
160
161/* hfirst - move position to the first item in the table
162 ARGUMENTS:
163 t - the hash table
164 RETURNS:
165 FALSE if there is no current item (meaning the table is empty)
166 NOTE:
167 */
168extern int hfirst(htab *t);
169
170
171/* hnext - move position to the next item in the table
172 ARGUMENTS:
173 t - the hash table
174 RETURNS:
175 FALSE if the position wraps around to the beginning of the table
176 NOTE:
177 To see every item in the table, do
178 if (hfirst(t)) do
179 {
180 key = hkey(t);
181 stuff = hstuff(t);
182 }
183 while (hnext(t));
184 */
185/* word hnext(/o_ htab *t _o/); */
186#define hnext(t) \
187 ((!(t)->ipos) ? FALSE : \
188 ((t)->ipos=(t)->ipos->next) ? TRUE : hnbucket(t))
189
190/* hnbucket - PRIVATE - move to first item in the next nonempty bucket
191 ARGUMENTS:
192 t - the hash table
193 RETURNS:
194 FALSE if the position wraps around to the beginning of the table
195 NOTE:
196 This is private to hashtab; do not use it externally.
197 */
198extern int hnbucket(htab *t);
199
200
201/* hstat - print statistics about the hash table
202 ARGUMENTS:
203 t - the hash table
204 NOTE:
205 items <0>: <#buckets with zero items> buckets
206 items <1>: <#buckets with 1 item> buckets
207 ...
208 buckets: #buckets items: #items existing: x
209 ( x is the average length of the list when you look for an
210 item that exists. When the item does not exists, the average
211 length is #items/#buckets. )
212
213 If you put n items into n buckets, expect 1/(n!)e buckets to
214 have n items. That is, .3678 0, .3678 1, .1839 2, ...
215 Also expect "existing" to be about 2.
216 */
217extern void hstat(/*_ htab *t _*/);
218
219#ifdef __cplusplus
220}
221#endif
222
223#endif /* HASHTAB_H */
Definition hashtab.h:58