File: hash.c

package info (click to toggle)
nickle 2.70-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,324 kB
  • ctags: 3,324
  • sloc: ansic: 31,410; yacc: 1,864; sh: 954; lex: 858; makefile: 238
file content (490 lines) | stat: -rw-r--r-- 9,964 bytes parent folder | download | duplicates (6)
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
/* $Header$ */

/*
 * Copyright © 2003 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

#include "nickle.h"

/*
 * From Knuth -- a good choice for hash/rehash values is p, p-2 where
 * p and p-2 are both prime.  These tables are sized to have an extra 10%
 * free to avoid exponential performance degradation as the hash table fills
 */

static HashSetRec hashSets[] = {
    { 2,		5,		3	  },
    { 4,		7,		5	  },
    { 8,		13,		11	  },
    { 16,		19,		17	  },
    { 32,		43,		41        },
    { 64,		73,		71        },
    { 128,		151,		149       },
    { 256,		283,		281       },
    { 512,		571,		569       },
    { 1024,		1153,		1151      },
    { 2048,		2269,		2267      },
    { 4096,		4519,		4517      },
    { 8192,		9013,		9011      },
    { 16384,		18043,		18041     },
    { 32768,		36109,		36107     },
    { 65536,		72091,		72089     },
    { 131072,		144409,		144407    },
    { 262144,		288361,		288359    },
    { 524288,		576883,		576881    },
    { 1048576,		1153459,	1153457   },
    { 2097152,		2307163,	2307161   },
    { 4194304,		4613893,	4613891   },
    { 8388608,		9227641,	9227639   },
    { 16777216,		18455029,	18455027  },
    { 33554432,		36911011,	36911009  },
    { 67108864,		73819861,	73819859  },
    { 134217728,	147639589,	147639587 },
    { 268435456,	295279081,	295279079 },
    { 536870912,	590559793,	590559791 },
    { 1073741824,	1181116273,	1181116271},
    { 2147483648ul,	2362232233ul,	2362232231ul}
};

#define NHASHSETS	(sizeof(hashSets)/sizeof(hashSets[0]))

static Value *
Find (HashTablePtr ht, Value hash, Value key)
{
    HashSetPtr	    hs = ht->hashSet;
    HashValue	    size = hs->size;
    HashValue	    h = ValueInt (hash);
    HashValue	    elt = h % size;
    HashValue	    step = 0;
    Value	    *elts = BoxElements (ht->elts);
    Value	    *er, *del = 0;

    for (;;)
    {
	er = &elts[elt * HashEltSize];
	if (!HashEltKey(er))
	{
	    /* check for a deleted entry */
	    if (HashEltValue (er))
	    {
		/* save first deleted entry found */
		if (!del)
		    del = er;
		else if (er == del)
		    break;
	    }
	    else
	    {
		/* pull reference as far forward on the chain as posible */
		if (del)
		    er = del;
		break;
	    }
	}
	else if (HashEltHash(er) == hash && 
		 Equal(HashEltKey(er), key) == TrueVal)
	{
	    break;
	}
	if (!step)
	{
	    step = h % hs->rehash;
	    if (!step)
		step = 1;
	}
	elt += step;
	if (elt >= size)
	    elt -= size;
    }
    return er;
}

static void
Rehash (BoxPtr old, HashTablePtr ht)
{
    Value	*o, *n;
    HashValue	h;
    
    o = BoxElements (old);
    ht->count = 0;
    for (h = old->nvalues / HashEltSize; h > 0; h--)
    {
	if (HashEltValid (o))
	{
	    /* XXX must rewrite references */
	    n = Find (ht, HashEltHash(o), HashEltKey(o));
	    HashEltCopy (n, o);
	    ht->count++;
	}
	HashEltStep (o);
    }
}

static void
Resize (HashTablePtr ht, const HashSetPtr hs)
{
    ENTER ();
    BoxPtr	old;
    
    if (ht->hashSet == hs)
	return;

    old = ht->elts;
    ht->elts = NewBox (False, False, hs->size * HashEltSize,
		       ht->type);
    ht->hashSet = hs;
    if (old)
	Rehash (old, ht);
    EXIT ();
}

#if HAVE_STDINT_H
typedef uint32_t	crc32_t;
#else
typedef unsigned int	crc32_t;
#endif

static crc32_t crc32_table[256];

static void
generate_crc32_table(void)
{
    crc32_t	c, p;
    int		n, m;

    p = 0xedb88320;
    for (n = 0; n < 256; n++)
    {
	c = n;
	for (m = 0; m < 8; m++)
	    c = (c >> 1) ^ ((c & 1) ? p : 0);
	crc32_table[n] = c;
    }
}

HashValue
HashCrc32 (unsigned char *bytes, int nbytes)
{
    crc32_t	crc32 = ~0;
    if (crc32_table[1] == 0) abort ();
    while (nbytes--)
	crc32 = (crc32 >> 8) ^ crc32_table[(crc32 ^ *bytes++) & 0xff];
    return (HashValue) ~crc32;
}

int
HashInit (void)
{
    generate_crc32_table ();
    return 1;
}

static Value
HashEqual (Value av, Value bv, int expandOk)
{
    HashTable	*at = &av->hash;
    HashTable	*bt = &bv->hash;
    HashValue	i;
    Value	*ae, *be;

    /* if they have different numbers of valid elements, they're not equal */
    if (at->count != bt->count)
	return FalseVal;
    ae = BoxElements (at->elts);
    for (i = 0; i < at->hashSet->size; i++)
    {
	if (HashEltValid (ae)) 
	{
	    be = Find (bt, HashEltHash(ae), HashEltKey (ae));
	    if (!be || !HashEltValid (be))
		return FalseVal;
	    if (Equal (HashEltValue (be), HashEltValue (ae)) != TrueVal)
		return FalseVal;
	}
	HashEltStep (ae);
    }
    return TrueVal;
}

static Bool
HashPrint (Value f, Value av, char format, int base, int width, int prec, int fill)
{
    HashTable	*ht = &av->hash;
    HashValue	h;
    Value	*e;
    Bool	first = True;
    Bool	pretty = format == 'v' || format == 'g';
    
    if (pretty)
    {
	FilePuts (f, "(");
	FilePutBaseType (f, ht->type, False);
	FilePuts (f, " ");
	FilePuts (f, "[");
	FilePutType (f, ht->keyType, False);
	FilePuts (f, "]");
	FilePutSubscriptType (f, ht->type, False);
	FilePuts (f, ") {");
    }
    e = BoxElements (ht->elts);
    for (h = ht->hashSet->size; h-- > 0; )
    {
	if (HashEltValid (e))
	{
	    if (pretty)
	    {
		if (!first)
		    FilePuts (f, ", ");
		else
		    FilePuts (f, " ");
		Print (f, HashEltKey (e), format, base, width, prec, fill);
		FilePuts (f, " => ");
	    }
	    else if (!first)
		FilePuts (f, " ");
	    Print (f, HashEltValue (e), format, base, width, prec, fill);
	    first = False;
	}
	HashEltStep (e);
    }
    if (pretty)
    {
	if (ht->def)
	{
	    if (!first)
		FilePuts (f, ",");
	    FilePuts (f, " => ");
	    Print (f, ht->def, format, base, width, prec, fill);
	    first = False;
	}
	if (!first)
	    FilePuts (f, " ");
	FilePuts (f, "}");
    }
    return True;
}

static HashValue
HashHash (Value av)
{
    HashTable	*ht = &av->hash;
    HashValue	h;
    Value	*e;
    HashValue	hash = 0;
    
    e = BoxElements (ht->elts);
    for (h = ht->hashSet->size; h-- > 0; )
    {
	if (HashEltValue (e))
	    hash ^= ValueInt (HashEltHash (e));
	HashEltStep (e);
    }
    return hash;
}

static void
HashMark (void *object)
{
    HashTable	*ht = (HashTable *) object;

    MemReference (ht->type);
    MemReference (ht->keyType);
    MemReference (ht->elts);
    MemReference (ht->def);
}

ValueRep    HashRep = { 
    { HashMark, 0, "HashRep" },
    rep_hash,
    {
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	HashEqual,
	0,
	0,
    },
    {
	0,
    },
    0,
    0,
    HashPrint,
    0,
    HashHash,
};

Value
NewHash (Bool constant, TypePtr keyType, TypePtr type)
{
    ENTER ();
    Value   ret = ALLOCATE (&HashRep.data, sizeof (HashTable));
    ret->hash.hashSet = 0;
    ret->hash.count = 0;
    ret->hash.type = type;
    ret->hash.keyType = keyType;
    ret->hash.elts = 0;
    ret->hash.def = 0;
    Resize (&ret->hash, &hashSets[0]);
    RETURN (ret);
}

Value
HashGet (Value hv, Value key)
{
    HashTablePtr    ht = &hv->hash;
    Value	    hash = ValueHash (key);
    Value	    *he;
    Value	    value;

    he = Find (ht, hash, key);
    if (!HashEltValid (he))
    {
	if (!ht->def)
	{
	    RaiseStandardException (exception_uninitialized_value, 0);
	    return (Void);
	}
	if (ht->count >= ht->hashSet->entries && 
	    ht->hashSet != &hashSets[NHASHSETS - 1])
	{
	    Resize (ht, ht->hashSet + 1);
	}
	ht->count++;
	HashEltHash(he) = hash;
	HashEltKey(he) = Copy (key);
	HashEltValue(he) = Copy(ht->def);
    }
    value = HashEltValue (he);
    if (!value)
    {
	RaiseStandardException (exception_uninitialized_value, 0);
	return (Void);
    }
    return value;
}

void
HashSet (Value hv, Value key, Value value)
{
    HashTablePtr    ht = &hv->hash;
    Value	    hash = ValueHash (key);
    Value	    *he;

    if (ht->count >= ht->hashSet->entries && 
	ht->hashSet != &hashSets[NHASHSETS - 1])
    {
	Resize (ht, ht->hashSet + 1);
    }
    he = Find (ht, hash, key);
    if (!HashEltValid (he))
	ht->count++;
    HashEltHash (he) = hash;
    HashEltKey (he) = Copy (key);
    HashEltValue (he) = Copy (value);
}

void
HashSetDef (Value hv, Value def)
{
    HashTablePtr    ht = &hv->hash;

    ht->def = Copy(def);
}

Value
HashRef (Value hv, Value key)
{
    ENTER ();
    HashTablePtr    ht = &hv->hash;
    Value	    *he;
    Value	    hash = ValueHash (key);

    if (ht->count == ht->hashSet->entries && 
	ht->hashSet != &hashSets[NHASHSETS - 1])
    {
	Resize (ht, ht->hashSet + 1);
    }
    he = Find (ht, hash, key);
    if (!HashEltValid (he))
    {
	ht->count++;
	HashEltHash (he) = hash;
	HashEltKey (he) = Copy (key);
	if (ht->def)
	    HashEltValue (he) = Copy(ht->def);
    }
    RETURN (NewRef (ht->elts, 
		    &HashEltValue(he) - BoxElements (ht->elts)));
}

Value
HashTest (Value hv, Value key)
{
    HashTablePtr    ht = &hv->hash;
    Value	    *he;
    Value	    hash = ValueHash (key);

    he = Find (ht, hash, key);
    return HashEltValid (he) ? TrueVal : FalseVal;
}

void
HashDelete (Value hv, Value key)
{
    HashTablePtr    ht = &hv->hash;
    Value	    *he;
    Value	    hash = ValueHash (key);

    he = Find (ht, hash, key);
    if (HashEltValid (he))
    {
	/* mark this entry as deleted -- value Void, key NULL */
	HashEltHash (he) = 0;
	HashEltKey (he) = 0;
	HashEltValue (he) = Void;
	--ht->count;
    }
}

Value
HashKeys (Value hv)
{
    ENTER ();
    HashTablePtr    ht = &hv->hash;
    int		    dim = ht->count;
    Value	    keys = NewArray (False, True, ht->keyType, 1, &dim);
    HashValue	    h;
    int		    i = 0;
    Value	    *e = BoxElements (ht->elts);

    for (h = ht->hashSet->size; h > 0; h--)
    {
	if (HashEltValid (e)) 
	{
	    ArrayValueSet (&keys->array, i, HashEltKey (e));
	    i++;
	}
	HashEltStep (e);
    }
    if (i != dim)
	ArrayResize (keys, 0, i);
    RETURN (keys);
}

Value
HashCopy (Value hv)
{
    ENTER ();
    Value   new = NewHash (False, hv->hash.keyType, hv->hash.type);
    new->hash.def = hv->hash.def;
    Resize (&new->hash, hv->hash.hashSet);
    Rehash (hv->hash.elts, &new->hash);
    RETURN (new);
}