ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
hashtab.c File Reference
#include <string.h>
#include <malloc.h>
#include "standard.h"
#include "lookupa.h"
#include "hashtab.h"
#include "recycle.h"
+ Include dependency graph for hashtab.c:

Go to the source code of this file.

Functions

static void hgrow (htab *t)
 
htabhcreate (word logsize)
 
void hdestroy (htab *t)
 
word hfind (htab *t, ub1 *key, ub4 keyl)
 
word hadd (htab *t, ub1 *key, ub4 keyl, void *stuff)
 
word hdel (htab *t)
 
word hfirst (htab *t)
 
word hnbucket (htab *t)
 

Function Documentation

◆ hadd()

word hadd ( htab t,
ub1 key,
ub4  keyl,
void *  stuff 
)

Definition at line 183 of file hashtab.c.

188 {
189  register hitem *h,**hp;
190  register ub4 y, x = lookup(key,keyl,0);
191 
192  /* make sure the key is not already there */
193  for (h = t->table[(y=(x&t->mask))]; h; h = h->next)
194  {
195  if ((x == h->hval) &&
196  (keyl == h->keyl) &&
197  !memcmp(key, h->key, keyl))
198  {
199  t->apos = y;
200  t->ipos = h;
201  return FALSE;
202  }
203  }
204 
205  /* find space for a new item */
206  h = (hitem *)renew(t->space);
207 
208  /* make the hash table bigger if it is getting full */
209  if (++t->count > (ub4)1<<(t->logsize))
210  {
211  hgrow(t);
212  y = (x&t->mask);
213  }
214 
215  /* add the new key to the table */
216  h->key = key;
217  h->keyl = keyl;
218  h->stuff = stuff;
219  h->hval = x;
220  hp = &t->table[y];
221  h->next = *hp;
222  *hp = h;
223  t->ipos = h;
224  t->apos = y;
225 
226 #ifdef HSANITY
227  hsanity(t);
228 #endif /* HSANITY */
229 
230  return TRUE;
231 }
#define TRUE
Definition: callstack.c:109
#define FALSE
Definition: callstack.c:112
static void hgrow(htab *t)
Definition: hashtab.c:86
s32 x
Definition: libmouse.c:34
s32 y
Definition: libmouse.c:34
ub4 lookup()
#define renew(r)
Definition: recycle.h:53
u32 ub4
Definition: standard.h:32
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
struct hitem * ipos
Definition: hashtab.h:62
struct reroot * space
Definition: hashtab.h:63
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

References FALSE, hgrow(), hitem::hval, hitem::key, hitem::keyl, lookup(), hitem::next, renew, hitem::stuff, TRUE, x, and y.

◆ hcreate()

htab* hcreate ( word  logsize)

Definition at line 123 of file hashtab.c.

125 {
126  ub4 i,len;
127  htab *t = (htab *)malloc(sizeof(htab));
128 
129  len = ((ub4)1<<logsize);
130  t->table = (hitem **)malloc(sizeof(hitem *)*(ub4)len);
131  for (i=0; i<len; ++i) t->table[i] = (hitem *)0;
132  t->logsize = logsize;
133  t->mask = len-1;
134  t->count = 0;
135  t->apos = (ub4)0;
136  t->ipos = (hitem *)0;
137  t->space = remkroot(sizeof(hitem));
138  t->bcount = 0;
139  return t;
140 }
reroot * remkroot()
Definition: hashtab.h:56
ub4 bcount
Definition: hashtab.h:64

References htab::apos, htab::bcount, htab::count, htab::ipos, htab::logsize, htab::mask, remkroot(), htab::space, and htab::table.

◆ hdel()

word hdel ( htab t)

Definition at line 234 of file hashtab.c.

236 {
237  hitem *h; /* item being deleted */
238  hitem **ip; /* a counter */
239 
240  /* check for item not existing */
241  if (!(h = t->ipos)) return FALSE;
242 
243  /* remove item from its list */
244  for (ip = &t->table[t->apos]; *ip != h; ip = &(*ip)->next)
245  ;
246  *ip = (*ip)->next;
247  --(t->count);
248 
249  /* adjust position to something that exists */
250  if (!(t->ipos = h->next)) hnbucket(t);
251 
252  /* recycle the deleted hitem node */
253  redel(t->space, h);
254 
255 #ifdef HSANITY
256  hsanity(t);
257 #endif /* HSANITY */
258 
259  return TRUE;
260 }
word hnbucket(htab *t)
Definition: hashtab.c:277
#define redel(root, item)
Definition: recycle.h:60

References FALSE, hnbucket(), hitem::next, redel, and TRUE.

◆ hdestroy()

void hdestroy ( htab t)

Definition at line 143 of file hashtab.c.

145 {
146  refree(t->space);
147  free((char *)t->table);
148  free((char *)t);
149 }
void refree()

References refree().

◆ hfind()

word hfind ( htab t,
ub1 key,
ub4  keyl 
)

Definition at line 157 of file hashtab.c.

161 {
162  hitem *h;
163  ub4 x = lookup(key,keyl,0);
164  ub4 y;
165  for (h = t->table[y=(x&t->mask)]; h; h = h->next)
166  {
167  if ((x == h->hval) &&
168  (keyl == h->keyl) &&
169  !memcmp(key, h->key, keyl))
170  {
171  t->apos = y;
172  t->ipos = h;
173  return TRUE;
174  }
175  }
176  return FALSE;
177 }

References FALSE, hitem::hval, hitem::key, hitem::keyl, lookup(), hitem::next, TRUE, x, and y.

◆ hfirst()

word hfirst ( htab t)

Definition at line 263 of file hashtab.c.

265 {
266  t->apos = t->mask;
267  (void)hnbucket(t);
268  return (t->ipos != (hitem *)0);
269 }

References htab::apos, and hnbucket().

Referenced by hgrow().

◆ hgrow()

static void hgrow ( htab t)
static

Definition at line 86 of file hashtab.c.

88 {
89  register ub4 newsize = (ub4)1<<(++t->logsize);
90  register ub4 newmask = newsize-1;
91  register ub4 i;
92  register hitem **oldtab = t->table;
93  register hitem **newtab = (hitem **)malloc(newsize*sizeof(hitem *));
94 
95  /* make sure newtab is cleared */
96  for (i=0; i<newsize; ++i) newtab[i] = (hitem *)0;
97  t->table = newtab;
98  t->mask = newmask;
99 
100  /* Walk through old table putting entries in new table */
101  for (i=newsize>>1; i--;)
102  {
103  register hitem *this, *that, **newplace;
104  for (this = oldtab[i]; this;)
105  {
106  that = this;
107  this = this->next;
108  newplace = &newtab[(that->hval & newmask)];
109  that->next = *newplace;
110  *newplace = that;
111  }
112  }
113 
114  /* position the hash table on some existing item */
115  hfirst(t);
116 
117  /* free the old array */
118  free((char *)oldtab);
119 
120 }
word hfirst(htab *t)
Definition: hashtab.c:263

References hfirst(), hitem::hval, and hitem::next.

Referenced by hadd().

◆ hnbucket()

word hnbucket ( htab t)

Definition at line 277 of file hashtab.c.

279 {
280  ub4 oldapos = t->apos;
281  ub4 end = (ub4)1<<(t->logsize);
282  ub4 i;
283 
284  /* see if the element can be found without wrapping around */
285  for (i=oldapos+1; i<end; ++i)
286  {
287  if (t->table[i&t->mask])
288  {
289  t->apos = i;
290  t->ipos = t->table[i];
291  return TRUE;
292  }
293  }
294 
295  /* must have to wrap around to find the last element */
296  for (i=0; i<=oldapos; ++i)
297  {
298  if (t->table[i])
299  {
300  t->apos = i;
301  t->ipos = t->table[i];
302  return FALSE;
303  }
304  }
305 
306  return FALSE;
307 }

References htab::apos, FALSE, and TRUE.

Referenced by hdel(), and hfirst().