1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* ***** BEGIN LICENSE BLOCK *****
4
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
6
* The contents of this file are subject to the Mozilla Public License Version
7
* 1.1 (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
9
* http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the
16
* The Original Code is Mozilla Communicator client code, released
19
* The Initial Developer of the Original Code is
20
* Netscape Communications Corporation.
21
* Portions created by the Initial Developer are Copyright (C) 1998
22
* the Initial Developer. All Rights Reserved.
26
* Alternatively, the contents of this file may be used under the terms of
27
* either of the GNU General Public License Version 2 or later (the "GPL"),
28
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the MPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the MPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
41
* PR hash table package.
48
#include "jsutil.h" /* Added by JSIFY */
49
#include "jshash.h" /* Added by JSIFY */
51
/* Compute the number of buckets in ht */
52
#define NBUCKETS(ht) JS_BIT(JS_HASH_BITS - (ht)->shift)
54
/* The smallest table has 16 buckets */
55
#define MINBUCKETSLOG2 4
56
#define MINBUCKETS JS_BIT(MINBUCKETSLOG2)
58
/* Compute the maximum entries given n buckets that we will tolerate, ~90% */
59
#define OVERLOADED(n) ((n) - ((n) >> 3))
61
/* Compute the number of entries below which we shrink the table by half */
62
#define UNDERLOADED(n) (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
65
** Stubs for default hash allocator ops.
68
DefaultAllocTable(void *pool, size_t size)
74
DefaultFreeTable(void *pool, void *item)
80
DefaultAllocEntry(void *pool, const void *key)
82
return (JSHashEntry*) malloc(sizeof(JSHashEntry));
86
DefaultFreeEntry(void *pool, JSHashEntry *he, uintN flag)
88
if (flag == HT_FREE_ENTRY)
92
static JSHashAllocOps defaultHashAllocOps = {
93
DefaultAllocTable, DefaultFreeTable,
94
DefaultAllocEntry, DefaultFreeEntry
97
JS_PUBLIC_API(JSHashTable *)
98
JS_NewHashTable(uint32 n, JSHashFunction keyHash,
99
JSHashComparator keyCompare, JSHashComparator valueCompare,
100
JSHashAllocOps *allocOps, void *allocPriv)
105
if (n <= MINBUCKETS) {
108
n = JS_CeilingLog2(n);
113
if (!allocOps) allocOps = &defaultHashAllocOps;
115
ht = (JSHashTable*) (*allocOps->allocTable)(allocPriv, sizeof *ht);
118
memset(ht, 0, sizeof *ht);
119
ht->shift = JS_HASH_BITS - n;
121
#if defined _MSC_VER && _MSC_VER <= 800
123
(*allocOps->freeTable)(allocPriv, ht);
127
nb = n * sizeof(JSHashEntry *);
128
ht->buckets = (JSHashEntry**) (*allocOps->allocTable)(allocPriv, nb);
130
(*allocOps->freeTable)(allocPriv, ht);
133
memset(ht->buckets, 0, nb);
135
ht->keyHash = keyHash;
136
ht->keyCompare = keyCompare;
137
ht->valueCompare = valueCompare;
138
ht->allocOps = allocOps;
139
ht->allocPriv = allocPriv;
144
JS_HashTableDestroy(JSHashTable *ht)
147
JSHashEntry *he, **hep;
148
JSHashAllocOps *allocOps = ht->allocOps;
149
void *allocPriv = ht->allocPriv;
152
for (i = 0; i < n; i++) {
153
hep = &ht->buckets[i];
154
while ((he = *hep) != NULL) {
156
(*allocOps->freeEntry)(allocPriv, he, HT_FREE_ENTRY);
160
memset(ht->buckets, 0xDB, n * sizeof ht->buckets[0]);
162
(*allocOps->freeTable)(allocPriv, ht->buckets);
164
memset(ht, 0xDB, sizeof *ht);
166
(*allocOps->freeTable)(allocPriv, ht);
170
** Multiplicative hash, from Knuth 6.4.
172
JS_PUBLIC_API(JSHashEntry **)
173
JS_HashTableRawLookup(JSHashTable *ht, JSHashNumber keyHash, const void *key)
175
JSHashEntry *he, **hep, **hep0;
181
h = keyHash * JS_GOLDEN_RATIO;
183
hep = hep0 = &ht->buckets[h];
184
while ((he = *hep) != NULL) {
185
if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
186
/* Move to front of chain if not already there */
202
JS_PUBLIC_API(JSHashEntry *)
203
JS_HashTableRawAdd(JSHashTable *ht, JSHashEntry **hep,
204
JSHashNumber keyHash, const void *key, void *value)
207
JSHashEntry *he, *next, **oldbuckets;
210
/* Grow the table if it is overloaded */
212
if (ht->nentries >= OVERLOADED(n)) {
213
oldbuckets = ht->buckets;
214
#if defined _MSC_VER && _MSC_VER <= 800
218
nb = 2 * n * sizeof(JSHashEntry *);
219
ht->buckets = (JSHashEntry**) (*ht->allocOps->allocTable)(ht->allocPriv, nb);
221
ht->buckets = oldbuckets;
224
memset(ht->buckets, 0, nb);
230
for (i = 0; i < n; i++) {
231
for (he = oldbuckets[i]; he; he = next) {
233
hep = JS_HashTableRawLookup(ht, he->keyHash, he->key);
234
JS_ASSERT(*hep == NULL);
240
memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
242
(*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
243
hep = JS_HashTableRawLookup(ht, keyHash, key);
246
/* Make a new key value entry */
247
he = (*ht->allocOps->allocEntry)(ht->allocPriv, key);
250
he->keyHash = keyHash;
259
JS_PUBLIC_API(JSHashEntry *)
260
JS_HashTableAdd(JSHashTable *ht, const void *key, void *value)
262
JSHashNumber keyHash;
263
JSHashEntry *he, **hep;
265
keyHash = (*ht->keyHash)(key);
266
hep = JS_HashTableRawLookup(ht, keyHash, key);
267
if ((he = *hep) != NULL) {
268
/* Hit; see if values match */
269
if ((*ht->valueCompare)(he->value, value)) {
270
/* key,value pair is already present in table */
274
(*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_VALUE);
278
return JS_HashTableRawAdd(ht, hep, keyHash, key, value);
282
JS_HashTableRawRemove(JSHashTable *ht, JSHashEntry **hep, JSHashEntry *he)
285
JSHashEntry *next, **oldbuckets;
289
(*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_ENTRY);
291
/* Shrink table if it's underloaded */
293
if (--ht->nentries < UNDERLOADED(n)) {
294
oldbuckets = ht->buckets;
295
nb = n * sizeof(JSHashEntry*) / 2;
296
ht->buckets = (JSHashEntry**) (*ht->allocOps->allocTable)(ht->allocPriv, nb);
298
ht->buckets = oldbuckets;
301
memset(ht->buckets, 0, nb);
307
for (i = 0; i < n; i++) {
308
for (he = oldbuckets[i]; he; he = next) {
310
hep = JS_HashTableRawLookup(ht, he->keyHash, he->key);
311
JS_ASSERT(*hep == NULL);
317
memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
319
(*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
323
JS_PUBLIC_API(JSBool)
324
JS_HashTableRemove(JSHashTable *ht, const void *key)
326
JSHashNumber keyHash;
327
JSHashEntry *he, **hep;
329
keyHash = (*ht->keyHash)(key);
330
hep = JS_HashTableRawLookup(ht, keyHash, key);
331
if ((he = *hep) == NULL)
334
/* Hit; remove element */
335
JS_HashTableRawRemove(ht, hep, he);
339
JS_PUBLIC_API(void *)
340
JS_HashTableLookup(JSHashTable *ht, const void *key)
342
JSHashNumber keyHash;
343
JSHashEntry *he, **hep;
345
keyHash = (*ht->keyHash)(key);
346
hep = JS_HashTableRawLookup(ht, keyHash, key);
347
if ((he = *hep) != NULL) {
354
** Iterate over the entries in the hash table calling func for each
355
** entry found. Stop if "f" says to (return value & JS_ENUMERATE_STOP).
356
** Return a count of the number of elements scanned.
359
JS_HashTableEnumerateEntries(JSHashTable *ht, JSHashEnumerator f, void *arg)
361
JSHashEntry *he, **hep;
364
JSHashEntry *todo = NULL;
366
nbuckets = NBUCKETS(ht);
367
for (i = 0; i < nbuckets; i++) {
368
hep = &ht->buckets[i];
369
while ((he = *hep) != NULL) {
370
rv = (*f)(he, n, arg);
372
if (rv & (HT_ENUMERATE_REMOVE | HT_ENUMERATE_UNHASH)) {
374
if (rv & HT_ENUMERATE_REMOVE) {
381
if (rv & HT_ENUMERATE_STOP) {
389
while ((he = *hep) != NULL) {
390
JS_HashTableRawRemove(ht, hep, he);
400
JS_HashTableDumpMeter(JSHashTable *ht, JSHashEnumerator dump, FILE *fp)
402
double sqsum, mean, variance, sigma;
403
uint32 nchains, nbuckets, nentries;
404
uint32 i, n, maxChain, maxChainLen;
410
nbuckets = NBUCKETS(ht);
411
for (i = 0; i < nbuckets; i++) {
416
for (n = 0; he; he = he->next)
419
if (n > maxChainLen) {
424
nentries = ht->nentries;
425
mean = (double)nentries / nchains;
426
variance = nchains * sqsum - nentries * nentries;
427
if (variance < 0 || nchains == 1)
430
variance /= nchains * (nchains - 1);
431
sigma = sqrt(variance);
433
fprintf(fp, "\nHash table statistics:\n");
434
fprintf(fp, " number of lookups: %u\n", ht->nlookups);
435
fprintf(fp, " number of entries: %u\n", ht->nentries);
436
fprintf(fp, " number of grows: %u\n", ht->ngrows);
437
fprintf(fp, " number of shrinks: %u\n", ht->nshrinks);
438
fprintf(fp, " mean steps per hash: %g\n", (double)ht->nsteps
440
fprintf(fp, "mean hash chain length: %g\n", mean);
441
fprintf(fp, " standard deviation: %g\n", sigma);
442
fprintf(fp, " max hash chain length: %u\n", maxChainLen);
443
fprintf(fp, " max hash chain: [%u]\n", maxChain);
445
for (he = ht->buckets[maxChain], i = 0; he; he = he->next, i++)
446
if ((*dump)(he, i, fp) != HT_ENUMERATE_NEXT)
449
#endif /* HASHMETER */
452
JS_HashTableDump(JSHashTable *ht, JSHashEnumerator dump, FILE *fp)
456
count = JS_HashTableEnumerateEntries(ht, dump, fp);
458
JS_HashTableDumpMeter(ht, dump, fp);
463
JS_PUBLIC_API(JSHashNumber)
464
JS_HashString(const void *key)
467
const unsigned char *s;
470
for (s = (const unsigned char *)key; *s; s++)
471
h = (h >> (JS_HASH_BITS - 4)) ^ (h << 4) ^ *s;
476
JS_CompareValues(const void *v1, const void *v2)