ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
hashtab.h
Go to the documentation of this file.
1 /*
2 --------------------------------------------------------------------
3 By Bob Jenkins, 1996. hash.h. Public Domain.
4 
5 This 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 
31 #ifndef STANDARD
32 #include "standard.h"
33 #endif
34 
35 #ifndef HASHTAB
36 #define HASHTAB
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /* PRIVATE TYPES AND DEFINITIONS */
43 
44 struct hitem
45 {
46  ub1 *key; /* key that is hashed */
47  ub4 keyl; /* length of key */
48  void *stuff; /* stuff stored in this hitem */
49  ub4 hval; /* hash value */
50  struct hitem *next; /* next hitem in list */
51 };
52 typedef struct hitem hitem;
53 
54 
55 struct htab
56 {
57  struct hitem **table; /* hash table, array of size 2^logsize */
58  word logsize; /* log of size of table */
59  size_t mask; /* (hashval & mask) is position in table */
60  ub4 count; /* how many items in this hash table so far? */
61  ub4 apos; /* position in the array */
62  struct hitem *ipos; /* current item in the array */
63  struct reroot *space; /* space for the hitems */
64  ub4 bcount; /* # hitems useable in current block */
65 };
66 typedef struct htab htab;
67 
68 
69 
70 
71 
72 /* PUBLIC FUNCTIONS */
73 
74 /* hcreate - create a hash table
75  ARGUMENTS:
76  logsize - 1<<logsize will be the initial table length
77  RETURNS:
78  the new table
79  */
80 htab *hcreate(/*_ word logsize _*/);
81 
82 
83 /* hdestroy - destroy a hash table
84  ARGUMENTS:
85  t - the hash table to be destroyed. Note that the items and keys
86  will not be freed, the user created them and must destroy
87  them himself.
88  RETURNS:
89  nothing
90  */
91 void hdestroy(/*_ htab *t _*/);
92 
93 
94 /* hcount, hkey, hkeyl, hstuff
95  ARGUMENTS:
96  t - the hash table
97  RETURNS:
98  hcount - (ub4) The number of items in the hash table
99  hkey - (ub1 *) key for the current item
100  hkeyl - (ub4) key length for the current item
101  hstuff - (void *) stuff for the current item
102  NOTE:
103  The current position always has an item as long as there
104  are items in the table, so hexist can be used to test if the
105  table is empty.
106  hkey, hkeyl, and hstuff will crash if hcount returns 0
107  */
108 #define hcount(t) ((t)->count)
109 #define hkey(t) ((t)->ipos->key)
110 #define hkeyl(t) ((t)->ipos->keyl)
111 #define hstuff(t) ((t)->ipos->stuff)
112 
113 
114 
115 /* hfind - move the current position to a given key
116  ARGUMENTS:
117  t - the hash table
118  key - the key to look for
119  keyl - length of the key
120  RETURNS:
121  TRUE if the item exists, FALSE if it does not.
122  If the item exists, moves the current position to that item.
123  */
124 word hfind(/*_ htab *t, ub1 *key, ub4 keyl _*/);
125 
126 
127 /* hadd - add a new item to the hash table
128  change the position to point at the item with the key
129  ARGUMENTS:
130  t - the hash table
131  key - the key to look for
132  keyl - length of the key
133  stuff - other stuff to be stored in this item
134  RETURNS:
135  FALSE if the operation fails (because that key is already there).
136  */
137 word hadd(/*_ htab *t, ub1 *key, ub4 keyl, void *stuff _*/);
138 
139 
140 /* hdel - delete the item at the current position
141  change the position to the following item
142  ARGUMENTS:
143  t - the hash table
144  RETURNS:
145  FALSE if there is no current item (meaning the table is empty)
146  NOTE:
147  This frees the item, but not the key or stuff stored in the item.
148  If you want these then deal with them first. For example:
149  if (hfind(tab, key, keyl))
150  {
151  free(hkey(tab));
152  free(hstuff(tab));
153  hdel(tab);
154  }
155  */
156 word hdel(/* htab *t */);
157 
158 
159 /* hfirst - move position to the first item in the table
160  ARGUMENTS:
161  t - the hash table
162  RETURNS:
163  FALSE if there is no current item (meaning the table is empty)
164  NOTE:
165  */
166 word hfirst(/*_ htab *t _*/);
167 
168 
169 /* hnext - move position to the next item in the table
170  ARGUMENTS:
171  t - the hash table
172  RETURNS:
173  FALSE if the position wraps around to the beginning of the table
174  NOTE:
175  To see every item in the table, do
176  if (hfirst(t)) do
177  {
178  key = hkey(t);
179  stuff = hstuff(t);
180  }
181  while (hnext(t));
182  */
183 /* word hnext(/o_ htab *t _o/); */
184 #define hnext(t) \
185  ((!(t)->ipos) ? FALSE : \
186  ((t)->ipos=(t)->ipos->next) ? TRUE : hnbucket(t))
187 
188 /* hnbucket - PRIVATE - move to first item in the next nonempty bucket
189  ARGUMENTS:
190  t - the hash table
191  RETURNS:
192  FALSE if the position wraps around to the beginning of the table
193  NOTE:
194  This is private to hashtab; do not use it externally.
195  */
196 word hnbucket(/*_ htab *t _*/);
197 
198 
199 /* hstat - print statistics about the hash table
200  ARGUMENTS:
201  t - the hash table
202  NOTE:
203  items <0>: <#buckets with zero items> buckets
204  items <1>: <#buckets with 1 item> buckets
205  ...
206  buckets: #buckets items: #items existing: x
207  ( x is the average length of the list when you look for an
208  item that exists. When the item does not exists, the average
209  length is #items/#buckets. )
210 
211  If you put n items into n buckets, expect 1/(n!)e buckets to
212  have n items. That is, .3678 0, .3678 1, .1839 2, ...
213  Also expect "existing" to be about 2.
214  */
215 void hstat(/*_ htab *t _*/);
216 
217 #ifdef __cplusplus
218 }
219 #endif
220 
221 #endif /* HASHTAB */
word hfirst()
word hnbucket()
word hfind()
htab * hcreate()
void hstat()
word hdel()
void hdestroy()
word hadd()
u8 ub1
Definition: standard.h:36
u32 ub4
Definition: standard.h:32
int word
Definition: standard.h:48
Definition: hashtab.h:45
struct hitem * next
Definition: hashtab.h:50
void * stuff
Definition: hashtab.h:48
ub4 keyl
Definition: hashtab.h:47
ub4 hval
Definition: hashtab.h:49
ub1 * key
Definition: hashtab.h:46
Definition: hashtab.h:56
struct hitem * ipos
Definition: hashtab.h:62
struct reroot * space
Definition: hashtab.h:63
ub4 bcount
Definition: hashtab.h:64
size_t mask
Definition: hashtab.h:59
word logsize
Definition: hashtab.h:58
ub4 count
Definition: hashtab.h:60
ub4 apos
Definition: hashtab.h:61
struct hitem ** table
Definition: hashtab.h:57
Definition: recycle.h:37