/src/nspr/lib/ds/plhash.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | /* |
7 | | * PL hash table package. |
8 | | */ |
9 | | #include "plhash.h" |
10 | | #include "prbit.h" |
11 | | #include "prlog.h" |
12 | | #include "prmem.h" |
13 | | #include "prtypes.h" |
14 | | #include <stdlib.h> |
15 | | #include <string.h> |
16 | | |
17 | | /* Compute the number of buckets in ht */ |
18 | 516 | #define NBUCKETS(ht) (1 << (PL_HASH_BITS - (ht)->shift)) |
19 | | |
20 | | /* The smallest table has 16 buckets */ |
21 | 61 | #define MINBUCKETSLOG2 4 |
22 | 51 | #define MINBUCKETS (1 << MINBUCKETSLOG2) |
23 | | |
24 | | /* Compute the maximum entries given n buckets that we will tolerate, ~90% */ |
25 | 460 | #define OVERLOADED(n) ((n) - ((n) >> 3)) |
26 | | |
27 | | /* Compute the number of entries below which we shrink the table by half */ |
28 | 34 | #define UNDERLOADED(n) (((n) > MINBUCKETS) ? ((n) >> 2) : 0) |
29 | | |
30 | | /* |
31 | | ** Stubs for default hash allocator ops. |
32 | | */ |
33 | | static void * PR_CALLBACK |
34 | | DefaultAllocTable(void *pool, PRSize size) |
35 | 30 | { |
36 | 30 | return PR_MALLOC(size); |
37 | 30 | } |
38 | | |
39 | | static void PR_CALLBACK |
40 | | DefaultFreeTable(void *pool, void *item) |
41 | 30 | { |
42 | 30 | PR_Free(item); |
43 | 30 | } |
44 | | |
45 | | static PLHashEntry * PR_CALLBACK |
46 | | DefaultAllocEntry(void *pool, const void *key) |
47 | 452 | { |
48 | 452 | return PR_NEW(PLHashEntry); |
49 | 452 | } |
50 | | |
51 | | static void PR_CALLBACK |
52 | | DefaultFreeEntry(void *pool, PLHashEntry *he, PRUintn flag) |
53 | 515 | { |
54 | 515 | if (flag == HT_FREE_ENTRY) { |
55 | 452 | PR_Free(he); |
56 | 452 | } |
57 | 515 | } |
58 | | |
59 | | static PLHashAllocOps defaultHashAllocOps = { |
60 | | DefaultAllocTable, DefaultFreeTable, |
61 | | DefaultAllocEntry, DefaultFreeEntry |
62 | | }; |
63 | | |
64 | | PR_IMPLEMENT(PLHashTable *) |
65 | | PL_NewHashTable(PRUint32 n, PLHashFunction keyHash, |
66 | | PLHashComparator keyCompare, PLHashComparator valueCompare, |
67 | | const PLHashAllocOps *allocOps, void *allocPriv) |
68 | 17 | { |
69 | 17 | PLHashTable *ht; |
70 | 17 | PRSize nb; |
71 | | |
72 | 17 | if (n <= MINBUCKETS) { |
73 | 10 | n = MINBUCKETSLOG2; |
74 | 10 | } else { |
75 | 7 | n = PR_CeilingLog2(n); |
76 | 7 | if ((PRInt32)n < 0) { |
77 | 0 | return 0; |
78 | 0 | } |
79 | 7 | } |
80 | | |
81 | 17 | if (!allocOps) { |
82 | 9 | allocOps = &defaultHashAllocOps; |
83 | 9 | } |
84 | | |
85 | 17 | ht = (PLHashTable*)((*allocOps->allocTable)(allocPriv, sizeof *ht)); |
86 | 17 | if (!ht) { |
87 | 0 | return 0; |
88 | 0 | } |
89 | 17 | memset(ht, 0, sizeof *ht); |
90 | 17 | ht->shift = PL_HASH_BITS - n; |
91 | 17 | n = 1 << n; |
92 | 17 | nb = n * sizeof(PLHashEntry *); |
93 | 17 | ht->buckets = (PLHashEntry**)((*allocOps->allocTable)(allocPriv, nb)); |
94 | 17 | if (!ht->buckets) { |
95 | 0 | (*allocOps->freeTable)(allocPriv, ht); |
96 | 0 | return 0; |
97 | 0 | } |
98 | 17 | memset(ht->buckets, 0, nb); |
99 | | |
100 | 17 | ht->keyHash = keyHash; |
101 | 17 | ht->keyCompare = keyCompare; |
102 | 17 | ht->valueCompare = valueCompare; |
103 | 17 | ht->allocOps = allocOps; |
104 | 17 | ht->allocPriv = allocPriv; |
105 | 17 | return ht; |
106 | 17 | } |
107 | | |
108 | | PR_IMPLEMENT(void) |
109 | | PL_HashTableDestroy(PLHashTable *ht) |
110 | 17 | { |
111 | 17 | PRUint32 i, n; |
112 | 17 | PLHashEntry *he, *next; |
113 | 17 | const PLHashAllocOps *allocOps = ht->allocOps; |
114 | 17 | void *allocPriv = ht->allocPriv; |
115 | | |
116 | 17 | n = NBUCKETS(ht); |
117 | 1.05k | for (i = 0; i < n; i++) { |
118 | 1.46k | for (he = ht->buckets[i]; he; he = next) { |
119 | 426 | next = he->next; |
120 | 426 | (*allocOps->freeEntry)(allocPriv, he, HT_FREE_ENTRY); |
121 | 426 | } |
122 | 1.04k | } |
123 | 17 | #ifdef DEBUG |
124 | 17 | memset(ht->buckets, 0xDB, n * sizeof ht->buckets[0]); |
125 | 17 | #endif |
126 | 17 | (*allocOps->freeTable)(allocPriv, ht->buckets); |
127 | 17 | #ifdef DEBUG |
128 | 17 | memset(ht, 0xDB, sizeof *ht); |
129 | 17 | #endif |
130 | 17 | (*allocOps->freeTable)(allocPriv, ht); |
131 | 17 | } |
132 | | |
133 | | /* |
134 | | ** Multiplicative hash, from Knuth 6.4. |
135 | | */ |
136 | 251k | #define GOLDEN_RATIO 0x9E3779B9U /* 2/(1+sqrt(5))*(2^32) */ |
137 | | |
138 | | PR_IMPLEMENT(PLHashEntry **) |
139 | | PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key) |
140 | 1.20k | { |
141 | 1.20k | PLHashEntry *he, **hep, **hep0; |
142 | 1.20k | PLHashNumber h; |
143 | | |
144 | | #ifdef HASHMETER |
145 | | ht->nlookups++; |
146 | | #endif |
147 | 1.20k | h = keyHash * GOLDEN_RATIO; |
148 | 1.20k | h >>= ht->shift; |
149 | 1.20k | hep = hep0 = &ht->buckets[h]; |
150 | 1.64k | while ((he = *hep) != 0) { |
151 | 577 | if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) { |
152 | | /* Move to front of chain if not already there */ |
153 | 134 | if (hep != hep0) { |
154 | 17 | *hep = he->next; |
155 | 17 | he->next = *hep0; |
156 | 17 | *hep0 = he; |
157 | 17 | } |
158 | 134 | return hep0; |
159 | 134 | } |
160 | 443 | hep = &he->next; |
161 | | #ifdef HASHMETER |
162 | | ht->nsteps++; |
163 | | #endif |
164 | 443 | } |
165 | 1.06k | return hep; |
166 | 1.20k | } |
167 | | |
168 | | /* |
169 | | ** Same as PL_HashTableRawLookup but doesn't reorder the hash entries. |
170 | | */ |
171 | | PR_IMPLEMENT(PLHashEntry **) |
172 | | PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash, |
173 | | const void *key) |
174 | 249k | { |
175 | 249k | PLHashEntry *he, **hep; |
176 | 249k | PLHashNumber h; |
177 | | |
178 | | #ifdef HASHMETER |
179 | | ht->nlookups++; |
180 | | #endif |
181 | 249k | h = keyHash * GOLDEN_RATIO; |
182 | 249k | h >>= ht->shift; |
183 | 249k | hep = &ht->buckets[h]; |
184 | 250k | while ((he = *hep) != 0) { |
185 | 250k | if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) { |
186 | 249k | break; |
187 | 249k | } |
188 | 720 | hep = &he->next; |
189 | | #ifdef HASHMETER |
190 | | ht->nsteps++; |
191 | | #endif |
192 | 720 | } |
193 | 249k | return hep; |
194 | 249k | } |
195 | | |
196 | | PR_IMPLEMENT(PLHashEntry *) |
197 | | PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep, |
198 | | PLHashNumber keyHash, const void *key, void *value) |
199 | 460 | { |
200 | 460 | PRUint32 i, n; |
201 | 460 | PLHashEntry *he, *next, **oldbuckets; |
202 | 460 | PRSize nb; |
203 | | |
204 | | /* Grow the table if it is overloaded */ |
205 | 460 | n = NBUCKETS(ht); |
206 | 460 | if (ht->nentries >= OVERLOADED(n)) { |
207 | 9 | oldbuckets = ht->buckets; |
208 | 9 | nb = 2 * n * sizeof(PLHashEntry *); |
209 | 9 | ht->buckets = (PLHashEntry**) |
210 | 9 | ((*ht->allocOps->allocTable)(ht->allocPriv, nb)); |
211 | 9 | if (!ht->buckets) { |
212 | 0 | ht->buckets = oldbuckets; |
213 | 0 | return 0; |
214 | 0 | } |
215 | 9 | memset(ht->buckets, 0, nb); |
216 | | #ifdef HASHMETER |
217 | | ht->ngrows++; |
218 | | #endif |
219 | 9 | ht->shift--; |
220 | | |
221 | 633 | for (i = 0; i < n; i++) { |
222 | 1.17k | for (he = oldbuckets[i]; he; he = next) { |
223 | 546 | next = he->next; |
224 | 546 | hep = PL_HashTableRawLookup(ht, he->keyHash, he->key); |
225 | 546 | PR_ASSERT(*hep == 0); |
226 | 546 | he->next = 0; |
227 | 546 | *hep = he; |
228 | 546 | } |
229 | 624 | } |
230 | 9 | #ifdef DEBUG |
231 | 9 | memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]); |
232 | 9 | #endif |
233 | 9 | (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets); |
234 | 9 | hep = PL_HashTableRawLookup(ht, keyHash, key); |
235 | 9 | } |
236 | | |
237 | | /* Make a new key value entry */ |
238 | 460 | he = (*ht->allocOps->allocEntry)(ht->allocPriv, key); |
239 | 460 | if (!he) { |
240 | 0 | return 0; |
241 | 0 | } |
242 | 460 | he->keyHash = keyHash; |
243 | 460 | he->key = key; |
244 | 460 | he->value = value; |
245 | 460 | he->next = *hep; |
246 | 460 | *hep = he; |
247 | 460 | ht->nentries++; |
248 | 460 | return he; |
249 | 460 | } |
250 | | |
251 | | PR_IMPLEMENT(PLHashEntry *) |
252 | | PL_HashTableAdd(PLHashTable *ht, const void *key, void *value) |
253 | 523 | { |
254 | 523 | PLHashNumber keyHash; |
255 | 523 | PLHashEntry *he, **hep; |
256 | | |
257 | 523 | keyHash = (*ht->keyHash)(key); |
258 | 523 | hep = PL_HashTableRawLookup(ht, keyHash, key); |
259 | 523 | if ((he = *hep) != 0) { |
260 | | /* Hit; see if values match */ |
261 | 63 | if ((*ht->valueCompare)(he->value, value)) { |
262 | | /* key,value pair is already present in table */ |
263 | 0 | return he; |
264 | 0 | } |
265 | 63 | if (he->value) { |
266 | 63 | (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_VALUE); |
267 | 63 | } |
268 | 63 | he->value = value; |
269 | 63 | return he; |
270 | 63 | } |
271 | 460 | return PL_HashTableRawAdd(ht, hep, keyHash, key, value); |
272 | 523 | } |
273 | | |
274 | | PR_IMPLEMENT(void) |
275 | | PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he) |
276 | 34 | { |
277 | 34 | PRUint32 i, n; |
278 | 34 | PLHashEntry *next, **oldbuckets; |
279 | 34 | PRSize nb; |
280 | | |
281 | 34 | *hep = he->next; |
282 | 34 | (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_ENTRY); |
283 | | |
284 | | /* Shrink table if it's underloaded */ |
285 | 34 | n = NBUCKETS(ht); |
286 | 34 | if (--ht->nentries < UNDERLOADED(n)) { |
287 | 3 | oldbuckets = ht->buckets; |
288 | 3 | nb = n * sizeof(PLHashEntry*) / 2; |
289 | 3 | ht->buckets = (PLHashEntry**)( |
290 | 3 | (*ht->allocOps->allocTable)(ht->allocPriv, nb)); |
291 | 3 | if (!ht->buckets) { |
292 | 0 | ht->buckets = oldbuckets; |
293 | 0 | return; |
294 | 0 | } |
295 | 3 | memset(ht->buckets, 0, nb); |
296 | | #ifdef HASHMETER |
297 | | ht->nshrinks++; |
298 | | #endif |
299 | 3 | ht->shift++; |
300 | | |
301 | 131 | for (i = 0; i < n; i++) { |
302 | 136 | for (he = oldbuckets[i]; he; he = next) { |
303 | 8 | next = he->next; |
304 | 8 | hep = PL_HashTableRawLookup(ht, he->keyHash, he->key); |
305 | 8 | PR_ASSERT(*hep == 0); |
306 | 8 | he->next = 0; |
307 | 8 | *hep = he; |
308 | 8 | } |
309 | 128 | } |
310 | 3 | #ifdef DEBUG |
311 | 3 | memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]); |
312 | 3 | #endif |
313 | 3 | (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets); |
314 | 3 | } |
315 | 34 | } |
316 | | |
317 | | PR_IMPLEMENT(PRBool) |
318 | | PL_HashTableRemove(PLHashTable *ht, const void *key) |
319 | 34 | { |
320 | 34 | PLHashNumber keyHash; |
321 | 34 | PLHashEntry *he, **hep; |
322 | | |
323 | 34 | keyHash = (*ht->keyHash)(key); |
324 | 34 | hep = PL_HashTableRawLookup(ht, keyHash, key); |
325 | 34 | if ((he = *hep) == 0) { |
326 | 0 | return PR_FALSE; |
327 | 0 | } |
328 | | |
329 | | /* Hit; remove element */ |
330 | 34 | PL_HashTableRawRemove(ht, hep, he); |
331 | 34 | return PR_TRUE; |
332 | 34 | } |
333 | | |
334 | | PR_IMPLEMENT(void *) |
335 | | PL_HashTableLookup(PLHashTable *ht, const void *key) |
336 | 81 | { |
337 | 81 | PLHashNumber keyHash; |
338 | 81 | PLHashEntry *he, **hep; |
339 | | |
340 | 81 | keyHash = (*ht->keyHash)(key); |
341 | 81 | hep = PL_HashTableRawLookup(ht, keyHash, key); |
342 | 81 | if ((he = *hep) != 0) { |
343 | 37 | return he->value; |
344 | 37 | } |
345 | 44 | return 0; |
346 | 81 | } |
347 | | |
348 | | /* |
349 | | ** Same as PL_HashTableLookup but doesn't reorder the hash entries. |
350 | | */ |
351 | | PR_IMPLEMENT(void *) |
352 | | PL_HashTableLookupConst(PLHashTable *ht, const void *key) |
353 | 249k | { |
354 | 249k | PLHashNumber keyHash; |
355 | 249k | PLHashEntry *he, **hep; |
356 | | |
357 | 249k | keyHash = (*ht->keyHash)(key); |
358 | 249k | hep = PL_HashTableRawLookupConst(ht, keyHash, key); |
359 | 249k | if ((he = *hep) != 0) { |
360 | 249k | return he->value; |
361 | 249k | } |
362 | 4 | return 0; |
363 | 249k | } |
364 | | |
365 | | /* |
366 | | ** Iterate over the entries in the hash table calling func for each |
367 | | ** entry found. Stop if "f" says to (return value & PR_ENUMERATE_STOP). |
368 | | ** Return a count of the number of elements scanned. |
369 | | */ |
370 | | PR_IMPLEMENT(int) |
371 | | PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg) |
372 | 5 | { |
373 | 5 | PLHashEntry *he, **hep; |
374 | 5 | PRUint32 i, nbuckets; |
375 | 5 | int rv, n = 0; |
376 | 5 | PLHashEntry *todo = 0; |
377 | | |
378 | 5 | nbuckets = NBUCKETS(ht); |
379 | 197 | for (i = 0; i < nbuckets; i++) { |
380 | 192 | hep = &ht->buckets[i]; |
381 | 192 | while ((he = *hep) != 0) { |
382 | 0 | rv = (*f)(he, n, arg); |
383 | 0 | n++; |
384 | 0 | if (rv & (HT_ENUMERATE_REMOVE | HT_ENUMERATE_UNHASH)) { |
385 | 0 | *hep = he->next; |
386 | 0 | if (rv & HT_ENUMERATE_REMOVE) { |
387 | 0 | he->next = todo; |
388 | 0 | todo = he; |
389 | 0 | } |
390 | 0 | } else { |
391 | 0 | hep = &he->next; |
392 | 0 | } |
393 | 0 | if (rv & HT_ENUMERATE_STOP) { |
394 | 0 | goto out; |
395 | 0 | } |
396 | 0 | } |
397 | 192 | } |
398 | | |
399 | 5 | out: |
400 | 5 | hep = &todo; |
401 | 5 | while ((he = *hep) != 0) { |
402 | 0 | PL_HashTableRawRemove(ht, hep, he); |
403 | 0 | } |
404 | 5 | return n; |
405 | 5 | } |
406 | | |
407 | | #ifdef HASHMETER |
408 | | #include <math.h> |
409 | | #include <stdio.h> |
410 | | |
411 | | PR_IMPLEMENT(void) |
412 | | PL_HashTableDumpMeter(PLHashTable *ht, PLHashEnumerator dump, FILE *fp) |
413 | | { |
414 | | double mean, variance; |
415 | | PRUint32 nchains, nbuckets; |
416 | | PRUint32 i, n, maxChain, maxChainLen; |
417 | | PLHashEntry *he; |
418 | | |
419 | | variance = 0; |
420 | | nchains = 0; |
421 | | maxChainLen = 0; |
422 | | nbuckets = NBUCKETS(ht); |
423 | | for (i = 0; i < nbuckets; i++) { |
424 | | he = ht->buckets[i]; |
425 | | if (!he) { |
426 | | continue; |
427 | | } |
428 | | nchains++; |
429 | | for (n = 0; he; he = he->next) { |
430 | | n++; |
431 | | } |
432 | | variance += n * n; |
433 | | if (n > maxChainLen) { |
434 | | maxChainLen = n; |
435 | | maxChain = i; |
436 | | } |
437 | | } |
438 | | mean = (double)ht->nentries / nchains; |
439 | | variance = fabs(variance / nchains - mean * mean); |
440 | | |
441 | | fprintf(fp, "\nHash table statistics:\n"); |
442 | | fprintf(fp, " number of lookups: %u\n", ht->nlookups); |
443 | | fprintf(fp, " number of entries: %u\n", ht->nentries); |
444 | | fprintf(fp, " number of grows: %u\n", ht->ngrows); |
445 | | fprintf(fp, " number of shrinks: %u\n", ht->nshrinks); |
446 | | fprintf(fp, " mean steps per hash: %g\n", (double)ht->nsteps |
447 | | / ht->nlookups); |
448 | | fprintf(fp, "mean hash chain length: %g\n", mean); |
449 | | fprintf(fp, " standard deviation: %g\n", sqrt(variance)); |
450 | | fprintf(fp, " max hash chain length: %u\n", maxChainLen); |
451 | | fprintf(fp, " max hash chain: [%u]\n", maxChain); |
452 | | |
453 | | for (he = ht->buckets[maxChain], i = 0; he; he = he->next, i++) |
454 | | if ((*dump)(he, i, fp) != HT_ENUMERATE_NEXT) { |
455 | | break; |
456 | | } |
457 | | } |
458 | | #endif /* HASHMETER */ |
459 | | |
460 | | PR_IMPLEMENT(int) |
461 | | PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp) |
462 | 0 | { |
463 | 0 | int count; |
464 | |
|
465 | 0 | count = PL_HashTableEnumerateEntries(ht, dump, fp); |
466 | | #ifdef HASHMETER |
467 | | PL_HashTableDumpMeter(ht, dump, fp); |
468 | | #endif |
469 | 0 | return count; |
470 | 0 | } |
471 | | |
472 | | PR_IMPLEMENT(PLHashNumber) |
473 | | PL_HashString(const void *key) |
474 | 0 | { |
475 | 0 | PLHashNumber h; |
476 | 0 | const PRUint8 *s; |
477 | |
|
478 | 0 | h = 0; |
479 | 0 | for (s = (const PRUint8*)key; *s; s++) { |
480 | 0 | h = PR_ROTATE_LEFT32(h, 4) ^ *s; |
481 | 0 | } |
482 | 0 | return h; |
483 | 0 | } |
484 | | |
485 | | PR_IMPLEMENT(int) |
486 | | PL_CompareStrings(const void *v1, const void *v2) |
487 | 0 | { |
488 | 0 | return strcmp((const char*)v1, (const char*)v2) == 0; |
489 | 0 | } |
490 | | |
491 | | PR_IMPLEMENT(int) |
492 | | PL_CompareValues(const void *v1, const void *v2) |
493 | 233k | { |
494 | 233k | return v1 == v2; |
495 | 233k | } |