1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * PL hash table package.
49 /* Compute the number of buckets in ht */
50 #define NBUCKETS(ht) (1 << (PL_HASH_BITS - (ht)->shift))
52 /* The smallest table has 16 buckets */
53 #define MINBUCKETSLOG2 4
54 #define MINBUCKETS (1 << MINBUCKETSLOG2)
56 /* Compute the maximum entries given n buckets that we will tolerate, ~90% */
57 #define OVERLOADED(n) ((n) - ((n) >> 3))
59 /* Compute the number of entries below which we shrink the table by half */
60 #define UNDERLOADED(n) (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
63 ** Stubs for default hash allocator ops.
65 static void * PR_CALLBACK
66 DefaultAllocTable(void *pool
, PRSize size
)
72 return PR_MALLOC(size
);
75 static void PR_CALLBACK
76 DefaultFreeTable(void *pool
, void *item
)
85 static PLHashEntry
* PR_CALLBACK
86 DefaultAllocEntry(void *pool
, const void *key
)
89 #pragma unused (pool,key)
92 return PR_NEW(PLHashEntry
);
95 static void PR_CALLBACK
96 DefaultFreeEntry(void *pool
, PLHashEntry
*he
, PRUintn flag
)
102 if (flag
== HT_FREE_ENTRY
)
106 static PLHashAllocOps defaultHashAllocOps
= {
107 DefaultAllocTable
, DefaultFreeTable
,
108 DefaultAllocEntry
, DefaultFreeEntry
111 PR_IMPLEMENT(PLHashTable
*)
112 PL_NewHashTable(PRUint32 n
, PLHashFunction keyHash
,
113 PLHashComparator keyCompare
, PLHashComparator valueCompare
,
114 const PLHashAllocOps
*allocOps
, void *allocPriv
)
119 if (n
<= MINBUCKETS
) {
122 n
= PR_CeilingLog2(n
);
127 if (!allocOps
) allocOps
= &defaultHashAllocOps
;
129 ht
= (PLHashTable
*)((*allocOps
->allocTable
)(allocPriv
, sizeof *ht
));
132 memset(ht
, 0, sizeof *ht
);
133 ht
->shift
= PL_HASH_BITS
- n
;
137 (*allocOps
->freeTable
)(allocPriv
, ht
);
141 nb
= n
* sizeof(PLHashEntry
*);
142 ht
->buckets
= (PLHashEntry
**)((*allocOps
->allocTable
)(allocPriv
, nb
));
144 (*allocOps
->freeTable
)(allocPriv
, ht
);
147 memset(ht
->buckets
, 0, nb
);
149 ht
->keyHash
= keyHash
;
150 ht
->keyCompare
= keyCompare
;
151 ht
->valueCompare
= valueCompare
;
152 ht
->allocOps
= allocOps
;
153 ht
->allocPriv
= allocPriv
;
158 PL_HashTableDestroy(PLHashTable
*ht
)
161 PLHashEntry
*he
, *next
;
162 const PLHashAllocOps
*allocOps
= ht
->allocOps
;
163 void *allocPriv
= ht
->allocPriv
;
166 for (i
= 0; i
< n
; i
++) {
167 for (he
= ht
->buckets
[i
]; he
; he
= next
) {
169 (*allocOps
->freeEntry
)(allocPriv
, he
, HT_FREE_ENTRY
);
173 memset(ht
->buckets
, 0xDB, n
* sizeof ht
->buckets
[0]);
175 (*allocOps
->freeTable
)(allocPriv
, ht
->buckets
);
177 memset(ht
, 0xDB, sizeof *ht
);
179 (*allocOps
->freeTable
)(allocPriv
, ht
);
183 ** Multiplicative hash, from Knuth 6.4.
185 #define GOLDEN_RATIO 0x9E3779B9U /* 2/(1+sqrt(5))*(2^32) */
187 PR_IMPLEMENT(PLHashEntry
**)
188 PL_HashTableRawLookup(PLHashTable
*ht
, PLHashNumber keyHash
, const void *key
)
190 PLHashEntry
*he
, **hep
, **hep0
;
196 h
= keyHash
* GOLDEN_RATIO
;
198 hep
= hep0
= &ht
->buckets
[h
];
199 while ((he
= *hep
) != 0) {
200 if (he
->keyHash
== keyHash
&& (*ht
->keyCompare
)(key
, he
->key
)) {
201 /* Move to front of chain if not already there */
218 ** Same as PL_HashTableRawLookup but doesn't reorder the hash entries.
220 PR_IMPLEMENT(PLHashEntry
**)
221 PL_HashTableRawLookupConst(PLHashTable
*ht
, PLHashNumber keyHash
,
224 PLHashEntry
*he
, **hep
;
230 h
= keyHash
* GOLDEN_RATIO
;
232 hep
= &ht
->buckets
[h
];
233 while ((he
= *hep
) != 0) {
234 if (he
->keyHash
== keyHash
&& (*ht
->keyCompare
)(key
, he
->key
)) {
245 PR_IMPLEMENT(PLHashEntry
*)
246 PL_HashTableRawAdd(PLHashTable
*ht
, PLHashEntry
**hep
,
247 PLHashNumber keyHash
, const void *key
, void *value
)
250 PLHashEntry
*he
, *next
, **oldbuckets
;
253 /* Grow the table if it is overloaded */
255 if (ht
->nentries
>= OVERLOADED(n
)) {
256 oldbuckets
= ht
->buckets
;
261 nb
= 2 * n
* sizeof(PLHashEntry
*);
262 ht
->buckets
= (PLHashEntry
**)
263 ((*ht
->allocOps
->allocTable
)(ht
->allocPriv
, nb
));
265 ht
->buckets
= oldbuckets
;
268 memset(ht
->buckets
, 0, nb
);
274 for (i
= 0; i
< n
; i
++) {
275 for (he
= oldbuckets
[i
]; he
; he
= next
) {
277 hep
= PL_HashTableRawLookup(ht
, he
->keyHash
, he
->key
);
278 PR_ASSERT(*hep
== 0);
284 memset(oldbuckets
, 0xDB, n
* sizeof oldbuckets
[0]);
286 (*ht
->allocOps
->freeTable
)(ht
->allocPriv
, oldbuckets
);
287 hep
= PL_HashTableRawLookup(ht
, keyHash
, key
);
290 /* Make a new key value entry */
291 he
= (*ht
->allocOps
->allocEntry
)(ht
->allocPriv
, key
);
294 he
->keyHash
= keyHash
;
303 PR_IMPLEMENT(PLHashEntry
*)
304 PL_HashTableAdd(PLHashTable
*ht
, const void *key
, void *value
)
306 PLHashNumber keyHash
;
307 PLHashEntry
*he
, **hep
;
309 keyHash
= (*ht
->keyHash
)(key
);
310 hep
= PL_HashTableRawLookup(ht
, keyHash
, key
);
311 if ((he
= *hep
) != 0) {
312 /* Hit; see if values match */
313 if ((*ht
->valueCompare
)(he
->value
, value
)) {
314 /* key,value pair is already present in table */
318 (*ht
->allocOps
->freeEntry
)(ht
->allocPriv
, he
, HT_FREE_VALUE
);
322 return PL_HashTableRawAdd(ht
, hep
, keyHash
, key
, value
);
326 PL_HashTableRawRemove(PLHashTable
*ht
, PLHashEntry
**hep
, PLHashEntry
*he
)
329 PLHashEntry
*next
, **oldbuckets
;
333 (*ht
->allocOps
->freeEntry
)(ht
->allocPriv
, he
, HT_FREE_ENTRY
);
335 /* Shrink table if it's underloaded */
337 if (--ht
->nentries
< UNDERLOADED(n
)) {
338 oldbuckets
= ht
->buckets
;
339 nb
= n
* sizeof(PLHashEntry
*) / 2;
340 ht
->buckets
= (PLHashEntry
**)(
341 (*ht
->allocOps
->allocTable
)(ht
->allocPriv
, nb
));
343 ht
->buckets
= oldbuckets
;
346 memset(ht
->buckets
, 0, nb
);
352 for (i
= 0; i
< n
; i
++) {
353 for (he
= oldbuckets
[i
]; he
; he
= next
) {
355 hep
= PL_HashTableRawLookup(ht
, he
->keyHash
, he
->key
);
356 PR_ASSERT(*hep
== 0);
362 memset(oldbuckets
, 0xDB, n
* sizeof oldbuckets
[0]);
364 (*ht
->allocOps
->freeTable
)(ht
->allocPriv
, oldbuckets
);
369 PL_HashTableRemove(PLHashTable
*ht
, const void *key
)
371 PLHashNumber keyHash
;
372 PLHashEntry
*he
, **hep
;
374 keyHash
= (*ht
->keyHash
)(key
);
375 hep
= PL_HashTableRawLookup(ht
, keyHash
, key
);
376 if ((he
= *hep
) == 0)
379 /* Hit; remove element */
380 PL_HashTableRawRemove(ht
, hep
, he
);
385 PL_HashTableLookup(PLHashTable
*ht
, const void *key
)
387 PLHashNumber keyHash
;
388 PLHashEntry
*he
, **hep
;
390 keyHash
= (*ht
->keyHash
)(key
);
391 hep
= PL_HashTableRawLookup(ht
, keyHash
, key
);
392 if ((he
= *hep
) != 0) {
399 ** Same as PL_HashTableLookup but doesn't reorder the hash entries.
402 PL_HashTableLookupConst(PLHashTable
*ht
, const void *key
)
404 PLHashNumber keyHash
;
405 PLHashEntry
*he
, **hep
;
407 keyHash
= (*ht
->keyHash
)(key
);
408 hep
= PL_HashTableRawLookupConst(ht
, keyHash
, key
);
409 if ((he
= *hep
) != 0) {
416 ** Iterate over the entries in the hash table calling func for each
417 ** entry found. Stop if "f" says to (return value & PR_ENUMERATE_STOP).
418 ** Return a count of the number of elements scanned.
421 PL_HashTableEnumerateEntries(PLHashTable
*ht
, PLHashEnumerator f
, void *arg
)
423 PLHashEntry
*he
, **hep
;
424 PRUint32 i
, nbuckets
;
426 PLHashEntry
*todo
= 0;
428 nbuckets
= NBUCKETS(ht
);
429 for (i
= 0; i
< nbuckets
; i
++) {
430 hep
= &ht
->buckets
[i
];
431 while ((he
= *hep
) != 0) {
432 rv
= (*f
)(he
, n
, arg
);
434 if (rv
& (HT_ENUMERATE_REMOVE
| HT_ENUMERATE_UNHASH
)) {
436 if (rv
& HT_ENUMERATE_REMOVE
) {
443 if (rv
& HT_ENUMERATE_STOP
) {
451 while ((he
= *hep
) != 0) {
452 PL_HashTableRawRemove(ht
, hep
, he
);
462 PL_HashTableDumpMeter(PLHashTable
*ht
, PLHashEnumerator dump
, FILE *fp
)
464 double mean
, variance
;
465 PRUint32 nchains
, nbuckets
;
466 PRUint32 i
, n
, maxChain
, maxChainLen
;
472 nbuckets
= NBUCKETS(ht
);
473 for (i
= 0; i
< nbuckets
; i
++) {
478 for (n
= 0; he
; he
= he
->next
)
481 if (n
> maxChainLen
) {
486 mean
= (double)ht
->nentries
/ nchains
;
487 variance
= fabs(variance
/ nchains
- mean
* mean
);
489 fprintf(fp
, "\nHash table statistics:\n");
490 fprintf(fp
, " number of lookups: %u\n", ht
->nlookups
);
491 fprintf(fp
, " number of entries: %u\n", ht
->nentries
);
492 fprintf(fp
, " number of grows: %u\n", ht
->ngrows
);
493 fprintf(fp
, " number of shrinks: %u\n", ht
->nshrinks
);
494 fprintf(fp
, " mean steps per hash: %g\n", (double)ht
->nsteps
496 fprintf(fp
, "mean hash chain length: %g\n", mean
);
497 fprintf(fp
, " standard deviation: %g\n", sqrt(variance
));
498 fprintf(fp
, " max hash chain length: %u\n", maxChainLen
);
499 fprintf(fp
, " max hash chain: [%u]\n", maxChain
);
501 for (he
= ht
->buckets
[maxChain
], i
= 0; he
; he
= he
->next
, i
++)
502 if ((*dump
)(he
, i
, fp
) != HT_ENUMERATE_NEXT
)
505 #endif /* HASHMETER */
508 PL_HashTableDump(PLHashTable
*ht
, PLHashEnumerator dump
, FILE *fp
)
512 count
= PL_HashTableEnumerateEntries(ht
, dump
, fp
);
514 PL_HashTableDumpMeter(ht
, dump
, fp
);
519 PR_IMPLEMENT(PLHashNumber
)
520 PL_HashString(const void *key
)
526 for (s
= (const PRUint8
*)key
; *s
; s
++)
527 h
= PR_ROTATE_LEFT32(h
, 4) ^ *s
;
532 PL_CompareStrings(const void *v1
, const void *v2
)
534 return strcmp((const char*)v1
, (const char*)v2
) == 0;
538 PL_CompareValues(const void *v1
, const void *v2
)