File: hash.c

package info (click to toggle)
yorick 1.4-14
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,948 kB
  • ctags: 6,609
  • sloc: ansic: 63,898; yacc: 889; makefile: 605; sh: 65; lisp: 60; fortran: 19
file content (522 lines) | stat: -rw-r--r-- 13,909 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
   HASH.C
   Define interface to hash routines

   $Id: hash.c,v 1.1 1993/08/27 18:32:09 munro Exp $
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

#include "hash.h"
#include "defmem.h"
#include "defstr.h"

long hashIndex;   /* set by HashFind, HashAdd, etc. */

int hashRealloc;  /* set by HashAdd if it returned 0 and if
		     the call forced table->maxItems to grow */

static long Hash(const char *name, long nSlots);
static long HashN(const char *name, long n, long nSlots);
static void ZapItems(HashItem **items, long nSlots);
static void HashReverse(HashItem **items, long nSlots);
static void Rehash(HashTable *table);
static HashItem *NewHashItem(HashItem *next, long index);
static void FreeHashItem(HashItem *item);
static long SlotsRequired(long nItems, long *nSlots);

static void XRehash(HashXTable *table, long n,
		    void *(*Index)(void *keyTable, long index),
		    void *keyTable);

/* ------------------------------------------------------------------------ */

int HashFind(const HashTable *table, const char *name, long n)
{
  long nSlots= table->nSlots;
  char **names= table->names;
  HashItem *item;

  if (nSlots<=0) return 0;

  if (n>0) {
    char *namei;

    hashIndex= HashN(name, n, nSlots);

    for (item=table->items[hashIndex] ; item ; item=item->next) {
      namei= names[item->index];
      if (strncmp(namei, name, n)==0 && !namei[n]) break;
    }

  } else {
    hashIndex= Hash(name, nSlots);

    for (item=table->items[hashIndex] ; item ; item=item->next)
      if (strcmp(names[item->index], name)==0) break;
  }

  if (!item) return 0;
  hashIndex= item->index;
  return 1;
}

int HashAdd(HashTable *table, const char *name, long n)
{
  if (HashFind(table, name, n)) {
    return 1;

  } else {
    long nItems= table->nItems;
    HashItem **item;

    if (table->maxItems <= nItems) {
      Rehash(table);
      hashIndex= n>0? HashN(name, n, table->nSlots) :
	              Hash(name, table->nSlots);
      hashRealloc= 1;
    } else {
      hashRealloc= 0;
    }

    item= &table->items[hashIndex];
    *item= NewHashItem(*item, nItems);
    table->names[nItems]= n>0? StrNCpy(name, n) : StrCpy(name);
    table->nItems= nItems+1;

    hashIndex= nItems;
    return 0;
  }
}

/* ------------------------------------------------------------------------ */

int HashXFind(const HashXTable *table, const void *key, long n,
	      int (*Compare)(void *keyTable, long index, const void *key),
	      void *keyTable)
{
  long nSlots= table->nSlots;
  HashItem *item;

  if (nSlots<=0) return 0;

  hashIndex= HashN(key, n, nSlots);

  for (item=table->items[hashIndex] ; item ; item=item->next)
    if (Compare(keyTable, item->index, key)==0) break;

  if (!item) return 0;
  hashIndex= item->index;
  return 1;
}

int HashXAdd(HashXTable *table, const char *key, long n,
	     int (*Compare)(void *keyTable, long index, const void *key),
	     void *(*Index)(void *keyTable, long index),
	     void *keyTable)
{
  if (HashXFind(table, key, n, Compare, keyTable)) {
    return 1;

  } else {
    long nItems= table->nItems;
    HashItem **item;

    if (table->maxItems <= nItems) {
      XRehash(table, n, Index, keyTable);
      hashIndex= HashN(key, n, table->nSlots);
      hashRealloc= 1;
    } else {
      hashRealloc= 0;
    }

    item= &table->items[hashIndex];
    *item= NewHashItem(*item, nItems);
    table->nItems= nItems+1;

    hashIndex= nItems;
    return 0;
  }
}

/* ------------------------------------------------------------------------ */

/* hashSlots  - HashTable->nSlots will always be one of these numbers,
                which are primes nearest powers of 2
   hashCutoff - when HashTable->maxItems reaches these values, the
                HashTable will be rehashed to the corresponding hashSlots.
		The numbers are chosen to keep the average number of items
		per slot between about 3/4 and 3/2.  This keeps the expected
		fraction of empty slots between about 1/2 and 1/4, or the
		average number of items per *occupied* slot between
		about 3/2 and 2.  The expected worst case (largest number
		of items in a slot) is less than about log(nSlots), which
		means that the worst case HashTable lookup time should
		be comparable to the average lookup time for a binary
		search. */
static long hashSlots[]=  {3,  7, 17, 31, 67, 127,
		           257, 521, 1031, 2053, 4099,  8191};
static long hashCutoff[]= {5, 12, 24, 49, 97, 192,
			   389, 776, 1542, 3076, 6145, 12287};
#define N_SIZES 12

/* ------------------------------------------------------------------------ */

void HashInit(HashTable *table, int estSize)
{
  int i;
  HashItem **items;
  long n;
  for (i=0 ; i<N_SIZES-1 ; i++) if (hashCutoff[i]>=estSize) break;
  table->maxItems= hashCutoff[i];
  table->nSlots= hashSlots[i];
  table->nItems= 0;
  table->items= items= Ymalloc(sizeof(HashItem *)*hashSlots[i]);
  table->names= Ymalloc(sizeof(char *)*hashCutoff[i]);
  n= hashSlots[i];
  while (n--) (*items++)= 0;
}

void HashClear(HashTable *table)
{
  long nItems= table->nItems;
  char **names= table->names;
  HashItem **items= table->items;
  long nSlots= table->nSlots;
  long i;
  char *name;

  table->maxItems= 0;
  table->nSlots= 0;
  table->items= 0;

  ZapItems(items, nSlots);

  for (i=0 ; i<nItems ; i++) {
    name= names[i];
    names[i]= 0;
    StrFree(name);
  }
  table->nItems= 0;
  table->names= 0;
  Yfree(names);
}

void HashXClear(HashXTable *table)
{
  HashItem **items= table->items;
  long nSlots= table->nSlots;

  table->maxItems= 0;
  table->nSlots= 0;
  table->items= 0;

  ZapItems(items, nSlots);
  table->nItems= 0;
}

void HashShrink(HashTable *table)
{
  long nItems= table->nItems;
  if (nItems<=0) {
    HashClear(table);
  } else {
    long maxItems= table->maxItems;
    if (maxItems!=nItems) {
      Rehash(table);
      if (table->maxItems!=nItems) {
	table->maxItems= nItems;
	table->names= Yrealloc(table->names, sizeof(char *)*nItems);
	hashRealloc= 1;
      } else {
	hashRealloc= 0;
      }
    }
  }
}

void HashRemove(HashTable *table, long index0)
{
  char **names= table->names;
  char *name0= names[index0];
  long nItems= table->nItems;
  long nSlots= table->nSlots;
  HashItem **items= table->items;
  long i= Hash(name0, nSlots);
  HashItem *item= items[i];
  HashItem *prev= 0;

  /* free the name */
  names[index0]= 0;
  StrFree(name0);

  /* unlink HashItem from the list in its slot and free it */
  while (item->index!=index0) { prev= item; item= item->next; }
  if (prev) prev->next= item->next;
  else items[i]= item->next;
  FreeHashItem(item);

  /* adjust nItems */
  table->nItems--;

  /* scan through all hash items to renumber those above index0 */
  if (index0<(nItems-1)/2) {
    /* if index0 is small, faster to go through the whole list */
    for (i=0 ; i<nSlots ; i++) {
      for (item=table->items[i] ; item ; item=item->next)
	if (item->index>index0) item->index--;
    }
    for (i=index0+1 ; i<nItems ; i++) names[i-1]= names[i];

  } else {
    /* if index0 is close to nItems, faster to use hash lookup */
    for (i=index0+1 ; i<nItems ; i++) {
      item= items[Hash(names[i], nSlots)];
      while (item->index!=i) item= item->next;
      item->index--;
      names[i-1]= names[i];
    }
  }
}

void HashSwap(HashTable *table, long index0, long index1)
{
  char **names= table->names;
  char *name0= names[index0];  /* both index0 and index1 had better... */
  char *name1= names[index1];  /* ...be between 0 and nItems-1 inclusive */
  long nSlots= table->nSlots;
  HashItem **items= table->items;

  HashItem *item= items[Hash(name0, nSlots)];
  while (item->index!=index0) item= item->next;
  item->index= index1;
  names[index0]= name1;

  item= items[Hash(name1, nSlots)];
  while (item->index!=index1) item= item->next;
  item->index= index0;
  names[index1]= name0;
}

/* ------------------------------------------------------------------------ */

static long Hash(const char *name, long nSlots)
{
  register unsigned char c;
  long hash= 0;
  while ((c= *name++))
    hash= ((hash<<1)&0x7fffffffL) ^ c;  /* long is at least 4 bytes */
  return hash%nSlots;
}

static long HashN(const char *name, long n, long nSlots)
{
  long hash= 0;
  while (n--)
    hash= ((hash<<1)&0x7fffffffL) ^ ((unsigned char)(*name++));
  return hash%nSlots;
}

/* ------------------------------------------------------------------------ */

static HashItem **itemList= 0;   /* failsafe list being zapped */
static long itemSlots= 0;        /* nSlots for itemList */

static void ZapItems(HashItem **items, long nSlots)
{
  HashItem *item;

  /* failsafe is really for Rehash, but must be done here */
  if (itemList && itemList!=items) ZapItems(itemList, itemSlots);
  if (!items) return;

  itemList= items;
  itemSlots= nSlots;

  while (nSlots--) {
    for (item=*items ; item ; item=*items) {
      *items= item->next;
      FreeHashItem(item);
    }
    items++;
  }

  items= itemList;
  itemList= 0;
  Yfree(items);
  itemSlots= 0;
}

static void HashReverse(HashItem **items, long nSlots)
{
  HashItem *item, *prev, *next;
  while (nSlots--) {
    prev= 0;
    for (item=*items ; item ; item=next) {
      next= item->next;
      item->next= prev;
      prev= item;
    }
    *(items++)= prev;
  }
}

static void Rehash(HashTable *table)
{
  long nItems= table->nItems;
  char **names= table->names;
  long nSlots= table->nSlots;
  HashItem **items= table->items;
  long newSlots, maxItems= SlotsRequired(nItems, &newSlots);

  if (newSlots!=nSlots) {
    /* a change in number of slots means that the table must
       be rehashed -- otherwise, only the names list needs to be
       reallocated */
    long i, old, new;
    HashItem *item;

    table->maxItems= 0;  /* will force rehash if interrupted */
    table->nSlots= 0;
    table->items= 0;
    itemList= items;     /* failsafe storage */
    itemSlots= nSlots;
    items= Ymalloc(sizeof(HashItem *)*newSlots);
    for (i=0 ; i<newSlots ; i++) items[i]= 0;
    table->items= items;
    table->nSlots= newSlots;

    HashReverse(itemList, nSlots);  /* reverse order of old items */

    for (i=0 ; i<nItems ; i++) {
      old= Hash(names[i], nSlots);
      new= Hash(names[i], newSlots);
      item= itemList[old];  /* this must have item->index==i */
      itemList[old]= item->next;
      if (!item || item->index!=i) {
	/* attempt to repair partial table */
	item= NewHashItem(items[new], i);
      } else {
	/* just move correct item from old table into new */
	item->next= items[new];
      }
      items[new]= item;
    }

    items= itemList;
    itemList= 0;
    ZapItems(items, nSlots);
  }

  table->names= Yrealloc(names, sizeof(char *)*maxItems);
  table->maxItems= maxItems;
}

static void XRehash(HashXTable *table, long n,
		    void *(*Index)(void *keyTable, long index),
		    void *keyTable)
{
  long nItems= table->nItems;
  long nSlots= table->nSlots;
  HashItem **items= table->items;
  long newSlots, maxItems= SlotsRequired(nItems, &newSlots);

  if (newSlots!=nSlots) {
    /* a change in number of slots means that the table must
       be rehashed */
    long i, old, new;
    HashItem *item;
    void *key;

    table->maxItems= 0;  /* will force rehash if interrupted */
    table->nSlots= 0;
    table->items= 0;
    itemList= items;     /* failsafe storage */
    itemSlots= nSlots;
    items= Ymalloc(sizeof(HashItem *)*newSlots);
    for (i=0 ; i<newSlots ; i++) items[i]= 0;
    table->items= items;
    table->nSlots= newSlots;
    table->maxItems= maxItems;

    HashReverse(itemList, nSlots);  /* reverse order of old items */

    for (i=0 ; i<nItems ; i++) {
      key= Index(keyTable, i);
      old= HashN(key, n, nSlots);
      new= HashN(key, n, newSlots);
      item= itemList[old];  /* this must have item->index==i */
      itemList[old]= item->next;
      if (!item || item->index!=i) {
	/* attempt to repair partial table */
	item= NewHashItem(items[new], i);
      } else {
	/* just move correct item from old table into new */
	item->next= items[new];
      }
      items[new]= item;
    }

    items= itemList;
    itemList= 0;
    ZapItems(items, nSlots);
  }
}

/* ------------------------------------------------------------------------ */

/* Set up a block allocator for HashItems */
union HashBlock {
  HashItem item;
  void *dummy;  /* forces sizeof(HashBlock) to multiple of sizeof(void*) */
};
static MemoryBlock hashBlock= {0, 0, sizeof(union HashBlock),
				 256*sizeof(union HashBlock)};

static HashItem *NewHashItem(HashItem *next, long index)
{
  HashItem *item= NextUnit(&hashBlock);
  item->next= next;
  item->index= index;
  return item;
}

static void FreeHashItem(HashItem *item)
{
  if (!item) return;
  FreeUnit(&hashBlock, item);
}

/* ------------------------------------------------------------------------ */

/* Slots required returns the number of slots required for a HashTable
   to hold nItems.  The function returns maxItems>=nItems, and nSlots
   is set to the required number of slots.  When the table has grown
   to maxItems, it is time to expand, and SlotsRequired should be
   called again.  */
static long SlotsRequired(long nItems, long *nSlots)
{
  int i;
  long maxItems;

  for (i=0 ; i<N_SIZES ; i++) if (nItems<hashCutoff[i]) break;

  if (i<N_SIZES) {
    /* Tables with 12287 or fewer entries have a number of slots
       within a factor of about 1.5 of their number of items.  */
    maxItems= hashCutoff[i];
    *nSlots= hashSlots[i];

  } else {
    /* Tables larger than 12287 always use 8191 slots; each call to
       SlotsRequired returns a maxItems of twice the input nItems.
       Performance will suffer, but we're talking conspicuous
       consumption here anyway.  */
    maxItems= 2*nItems;
    *nSlots= hashCutoff[N_SIZES-1];
  }

  return maxItems;
}

/* ------------------------------------------------------------------------ */